I'm trying to perform a union on a common field after merging two adjacent shapefiles. The shapefiles end up with at least one thin sliver of space between them. When I attempt a union I get the following orphaned hole error:
Error in createPolygonsComment(p) : rgeos_PolyCreateComment: orphaned hole, cannot find containing polygon for hole at index 17
I've uploaded a reproducible example to Dropbox at this link.
Here is the code to recreate the problem:
#loading required packages
require(sp)
require(rgdal)
require(maptools)
require(rgeos)
#load example data, set "dsn=" to your working directory or specify the path
example <- readOGR(dsn=".",layer="ReproducibleExample")
#Attempting a UnionSpatialPolygons based on the COUNTY field
example.df <- as(example, "data.frame")
countycol <- example.df$COUNTY
example.diss <- unionSpatialPolygons(example, countycol)
Returns:
Error in createPolygonsComment(p) : rgeos_PolyCreateComment: orphaned hole, cannot find containing polygon for hole at index 17
Trying the fix proposed here and here:
slot(example, "polygons") <- lapply(slot(example, "polygons"), checkPolygonsHoles)
This returns the same error that comes from the union attempt but with different index number:
rgeos_PolyCreateComment: orphaned hole, cannot find containing polygon for hole at index 30
Trying the fix proposed in Roger Bivand's helpful tutorial
fix <- slot(example, "polygons")
fixa <- lapply(fix, checkPolygonsHoles)
Returns same error at index 30 as above.
Others have raised this problem here and here, and while the solutions posited above appear to work for some cases, other cases are not resolved. One user used QGIS to address the problem, and the other had 2 of 3 items fixed, but no resolution for the final one.
It appears that people continue to have problems despite this code working from time to time. Has anybody found a solution within R?
I've performed "repair geometry" tool in ArcGIS, and it corrected the problem, but it seems like there should be a fix in R.