[R-sig-Geo] Can voronoi polygons calculated in R be converted to ESRI shapefile?

Rick Reeves reeves at nceas.ucsb.edu
Thu Nov 15 22:48:47 CET 2007


Debs:

The script that I had written before used the older, deprecated  
SpatialRingsDataFrame
to create a shapefile from the tripack() voronoi polygons.
Here is an example, based on the answer to your message from Roger Bivand,
that demonstrates the technique  -
Regards,
Rick R
#
# example from the 'tripack' package documentation
#
voronoiShapefile <- function()
{
browser()
   library(tripack)
   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[[8]][,1],tritest.vp[[8]][,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))
   points(crds)
   library(maptools)
   writePolyShape(SPDF1, "hex")
}


Debarchaa Ghosh wrote:
> Hi Rick,
>
> Thanks for your help. I'll be looking forward to your script.
>
> Thanks,
> Debs.
>
> Rick Reeves wrote:
>> Hello Debs:
>>
>> Yes, this can be done by 'promoting' the polygons generated by the 
>> tripack package
>> into Spatial Polygon Data Frame objects, using the maptools and sp 
>> packages. I have
>> an example R script, written a few years ago, that does this with an 
>> older version of
>> the sp package. I suspect that it is easier to do now.....I could 
>> clean it up and send it to you..
>> Regards,
>> Rick Reeves
>>
>> Debarchana Ghosh wrote:
>>> Hi,
>>>
>>> I have a point data frame with X and Y coordinates, where n=1051. I am
>>> trying to create Voronoi polygons using both the Tripack and PBSMapping
>>> packages.
>>>
>>> library(tripack)
>>> testbird.vm<-voronoi.mosaic(testbird$X, testbird$Y)
>>> testbird.vp<-voronoi.polygons(testbird.vm)
>>> plot(testbird.vp)
>>>
>>> library(PBSMapping)
>>> testbirdvor<-calcVoronoi(testbird)
>>>
>>>  
>>>> >From here how can I create an ESRI shapefile of the voronoi 
>>>> polygons in R
>>>>     
>>> and export to ArcGIS for further use.
>>>
>>> Thanks for the help,
>>> Debs.
>>>
>>>   
>
-------------- 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/3c63bdb5/attachment.vcf>


More information about the R-sig-Geo mailing list