1

I have a set of points in .shp (ESRI, available here: http://ulozto.cz/xGqRVFrR/voronoi-zip). I would like to calculate Voronoi tesselation from the points, thus create polygons. So far, so good. However, points attribute table is not transferred automatically in R to Voronoi tesselation polygons during calculation (as is the case in ArcGIS).

Thus, depending points' spatial location I want to add points' attributes to my new Voronoi's polygons. I tried to used function

over(diagram.poly, bod.sp) 

but it creates data.frame, not polygon.

Please,

  1. how to automatically add points' attributes to Voronoi polygon OR
  2. how to use resulting data.frame as my polygon's attribute table?

I'm sure it is pretty trivial task but I still can't find my answer!

my code:

# exemple with my data
library(spatstat)
library(sp)
library(deldir)
library(rgdal)
library(maptools)

# set directory
wd<- setwd("C:/Users/***/voronoi")

# read point shp, considered as SpatialPointsDataFrame
bod<-readOGR(dsn=setwd("C:/Users/***/voronoi"), layer="voronoi_tess")

# convert firstly to ppp (Spatial point pattern (object of class "ppp")
bod.ppp<-as.ppp(bod)

# calculate Voronoi tesselation
diagram<-dirichlet(bod.ppp)

# convert diagram to SpatialPolygon
diagram.poly=as(diagram, "SpatialPolygons")

# use CRS from "bod"
proj=CRS(proj4string(bod))

# define diagram CRS
proj4string(diagram.poly)=proj

# check if projections are identical
identical(proj4string(bod), proj4string(diagram.poly))

# plot the result
plot(diagram.poly, border = "darkgrey")
points(bod, col = "red", pch = 16)

# try to apply over{sp} function, producing data.frame
over(diagram.poly, bod)

Voronoi tesselation from red points

1 Answer 1

1

Here I post my answer:

I missed out how to convert SpatialPolygon to SpatialPolygonDataFrame and difference between them. SpatialPolygon doesn't contain attribute table, SpatialPolygonDataFrame does. So attribute table can be easily add from my points attribute table. No need to use over function :-D

# re-written code
library(sp)
library(deldir)
library(rgdal)
library(maptools)
library(spatstat)

# set directory
wd<- setwd("C:/Users/***/voronoi")

# read point shp, considered as SpatialPointsDataFrame
bod<-readOGR(dsn=setwd("C:/Users/***/voronoi"), layer="voronoi_tess")

# convert firstly to ppp (Spatial point pattern (object of class "ppp")
bod.ppp<-as.ppp(bod)

# calculate voronoi tesselation
diagram<-dirichlet(bod.ppp)

# convert diagram to SpatialPolygon - without attribute table
diagram.poly=as(diagram, "SpatialPolygons")

# create attribute table from points shp (bod), as data frame
att.bod<-as.data.frame(bod)

# convert it to SpatialPolygonDataFrame - include SpatialPolygon + attribute table (att.bod)
s_poly <- SpatialPolygonsDataFrame(diagram.poly, att.bod)

# export to ArcGIS to verify attributes
writeOGR(s_poly, wd, "s_poly", driver = "ESRI Shapefile")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.