[R-sig-Geo] Correction to that example...

Rick Reeves reeves at nceas.ucsb.edu
Thu Nov 15 23:04:41 CET 2007


Debs (and list)

Please forgive the oversignt:  that post contained two errors:

     pcrds <- cbind(tritest.vp[[8]][,1],tritest.vp[[8]][,2] )
replaced by
    pcrds <- cbind(tritest.vp[[i]][,1],tritest.vp[[i]][,2] )

and this line should be removed:
    points(crds)

Thanks, Rick R

here is a corrected version:

voronoiShapefile <- function()
{
browser()
   data(tritest)
   tritest.vm <- voronoi.mosaic(tritest$x,tritest$y)
   tritest.vp <- voronoi.polygons(tritest.vm)

   polys <- vector(mode="list", length=length(tritest.vp))
   library(sp)
   for (i in seq(along=polys))
  {
      pcrds <- cbind(tritest.vp[[i]][,1],tritest.vp[[i]][,2] )
      pcrds <- rbind(pcrds, pcrds[1,])
      polys[[i]] <- Polygons(list(Polygon(pcrds)), ID=as.character(i))
   }
   SP <- SpatialPolygons(polys) # 8 spatial polygons generated using 
voronoi.polygons
#
# Generate the SpatialPolygonsDataFrame object. For the purposes of this 
example,
# we use the last 8 points in the input point dataset as the data 
component of the data frame.
# You will probably have 'actual' attribute data to assign to each 
polygon. See data.frame documentation
#
   SPDF <- SpatialPolygonsDataFrame(SP, 
data=data.frame(x=tritest$x[5:12],y=tritest$y[5:12],
                                                            
row.names=sapply(slot(SP, "polygons"),
                                                            function(x) 
slot(x, "ID"))))
#
   Areas <- sapply(slot(SP, "polygons"), function(x) slot(x, "area"))
 
   print(fivenum(Areas), digits=8)
   print(sort(Areas), digits=8)
   SPDF1 <- SPDF[Areas < 0.866027,]

   plot(SPDF1, axes=TRUE, xlim=c(1,10), ylim=c(1,10))
   library(maptools)
   writePolyShape(SPDF1, "hex")
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: reeves.vcf
Type: text/x-vcard
Size: 339 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20071115/4e8eb192/attachment.vcf>


More information about the R-sig-Geo mailing list