[R-sig-Geo] generating shapefile from scratch
Roger Bivand
Roger.Bivand at nhh.no
Thu Dec 30 16:49:58 CET 2010
On Wed, 29 Dec 2010, Jeff Hamann wrote:
> I've excavated some old code for generating line shapefiles and it no
> longer works (not atypical).
>
> I can't seem to get any traction searching to web on constructing an
> object that can pass through maptools::write.linelistShape()
>
> The code went something like this:
>
> stands <- deldir( pts, rw=c(0,x.dim,0,y.dim) )
>
> RoadShapes <- vector( "list", nrow( stands$delsgs ) )
>
> for( i in 1:nrow( stands$delsgs ) ) {
> RoadShapes[[i]] <- rbind( stands$delsgs[i,1:2], stands$delsgs[i,3:4])
> }
>
> write.linelistShape(RoadShapes, road.att.data, file="data/roads")
>
Please try to bring your code up to date:
library(deldir)
set.seed(1)
pts <- list(x=runif(50), y=runif(50))
x.dim <- y.dim <- 1
stands <- deldir( pts, rw=c(0,x.dim,0,y.dim) )
library(maptools)
RoadShapes <- vector( "list", nrow( stands$delsgs ) )
from <- integer(nrow( stands$delsgs ))
to <- integer(nrow( stands$delsgs ))
for( i in 1:nrow( stands$delsgs ) ) {
crds <- rbind(stands$delsgs[i,1:2], stands$delsgs[i,3:4])
# make coordinate matrix as before
RoadShapes[[i]] <- Lines(list(Line(crds)), ID=i)
# Make Lines object with single Line object member
from[i] <- stands$delsgs[i,5]
to[i] <- stands$delsgs[i,6]
# provide placeholder data from point indices
}
df <- data.frame(from, to)
SL <- SpatialLines(RoadShapes)
# create Spatial Lines object
SLDF <- SpatialLinesDataFrame(SL, df)
# create object matched with data.frame
writeSpatialShape(SLDF, "SLDF")
getinfo.shape("SLDF")
If your road.att.data has different row.names than 1:nrow(stands$delsgs),
you'll need to update them to match those of the Lines objects being
created in the loop - here I've just put from and to in the data.frame
object, which has 1:nrows(self) as its default row.names. Something
similar would apply to polygons.
Hope this helps,
Roger
>
>
>
> I can correctly generate polygon shapefiles, but for some reason, the errors I get when trying to write a line shapefile are:
>
>> write.linelistShape(RoadShapes, road.att.data, file="data/roads")
> Error in attr(x, "pstart")[[2]] : subscript out of bounds
> In addition: Warning messages:
> 1: use writeSpatialShape:
> objects other than Spatial objects defined in the sp package are deprecated
> 2: In write.linelistShape(RoadShapes, road.att.data, file = "data/roads") :
> nParts changed to integer
> 3: In write.linelistShape(RoadShapes, road.att.data, file = "data/roads") :
> pstart changed to integer
>
> Can anyone point me to an example where the geometry isn't already in a shapefile?
>
> I do (something) like this for generating polygon shapefiles:
>
> for( i in 1:stands$n.data ) {
>
> Shapes[[i]]$Pstart <- 0
> Shapes[[i]]$verts <- rbind( vert.list[[i]], vert.list[[i]][1,] )
> Shapes[[i]]$shp.type <- 5
> Shapes[[i]]$nVerts <- nrow( Shapes[[i]]$verts )
> Shapes[[i]]$nParts <- 1
> bb <- bbox( vert.list[[i]] )
> Shapes[[i]]$bbox <- c(bb[1,1],bb[2,1],bb[1,2],bb[2,2] )
> Shapes[[i]]$shpID <- i
>
> attr( Shapes[[i]], "nVerts" ) <- Shapes[[i]]$nVerts
> attr( Shapes[[i]], "nParts" ) <- Shapes[[i]]$nParts
> attr( Shapes[[i]], "shp.type" ) <- Shapes[[i]]$shp.type
> attr( Shapes[[i]], "bbox" ) <- Shapes[[i]]$bbox
>
> }
>
>
> Thanks,
> Jeff.
>
>
>
> Jeff Hamann, PhD
> PO Box 1421
> Corvallis, Oregon 97339-1421
> 541-754-2457
> jeff.hamann[at]forestinformatics[dot]com
> http://www.forestinformatics.com
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list