I have a list with 4 objects, each object is a data frame. The data frame is the coordinate matrix of each polygon. I would like calculate the total area of the 4 polygons and then draw the voronoi tessellation. Here is my workflow, first, I convert the data frame within the list to polygon:
Region_HE_srl <- lapply(Region_HE,FUN= function(x) Polygon(x))
Then, I create the SpatialPolygons object:
Region_HE_sp <- SpatialPolygons(list(Polygons(Region_HE_srl,length(Region_HE))))
Now that the SpatialPolygons object is created: enter image description here I used the 'gArea' function from the 'rgeos' package to calculate the area.
area <- gArea(Region_HE_sp, byid = TRUE)
Normally, when there is only 1 polygon within the SpatialPolygon object, this function works fine, but now as there are 4 polygons, an error occurs:
Error in createPolygonsComment(p) : rgeos_PolyCreateComment: orphaned hole, cannot find containing polygon for hole at index 1
Same thing when I applied the voronoi.polygons function from 'SDraw' package.
voronoi.tess <- SDraw::voronoi.polygons(pts_ppp, Region_HE_sp)
The error occurs again. As the package claimed that the bounding polygon parameter should be a spatialpolygons object, I cannot figure out why this error happened.
Any suggestions?
1 Answer 1
R thinks one or more of your polygons are holes, and a hole has to be inside a non-hole.
Hole detection is done in the Polygon
function:
Polygon(coords, hole=as.logical(NA))
coords: 2-column numeric matrix with coordinates; first point (row)
should equal last coordinates (row); if the hole argument is
not given, the status of the polygon as a hole or an island
will be taken from the ring direction, with clockwise meaning
island, and counter-clockwise meaning hole
So you either have to reverse the holey Polygon, or set hole=FALSE
in your Polygon
function call:
hole: logical value for setting polygon as hole or not; if the hole
argument is not given, the status of the polygon as a hole or
an island will be taken from the ring direction, with
clockwise meaning island, and counter-clockwise meaning hole