Reputation: 4940
In order to select/identify the border polygons of a shapefile, I would like to use a function able to select/identify polygon that share a line segment with a source polygon.
With figures:
I have this kind of shapefile:
Using gUnionCascaded
from rgeos
package, I have a second shapefile with the "contour polygon"
Now I am looking for a function that can select/identify border polygons (shaded on the fig) i.e. polygons of the first shapefile that share a line segment with the polygon of the second shapefile . :
Upvotes: 4
Views: 1621
Reputation: 4940
As proposed by Josh O'Brien, I have used the rgeos::gRelate()
function.
I get 3 DE-9IM cases:
x <- gRelate(shapefile.1, shapefile.2, byid = TRUE)
table(x)
2FF10F212 2FF11F212 2FF1FF212
63 2470 174495
The resulted DE-9IM string codes can be interpreted as follow:
1) 2FF1FF212: represent polygons from the first shapefile that don't intersect the border of the polygon of the second shapefile
2) 2FF11F212: represent polygons from the first shapefile that intersect the border of the polygon of the second shapefile with a line
3) 2FF10F212: represent polygons from the first shapefile that intersect the border of the polygon of the second shapefile with a point
The two last cases are my border polygons that I was looking for. I have got their ID with:
poly.border <- which(x %in% c("2FF10F212","2FF11F212"))
Upvotes: 4
Reputation: 237
I want to second the above solution, which really helped me a lot. To add a bit more to the DE-9IM solution (see wikipedia page on DE-9IM), see below for application - in this case, how I used it to subset counties which don't border any county that has at least one CAFO/IHO (concentrated animal feeding operations / industrial hog operations) in it. That is, the blue counties below were compared against a super-shape (like the original question asker using gUnionCascaded), and that vector was used to subset the noIHO counties.
IHOblob = gUnionCascaded(hasIHOcounties)
plot(IHOblob)
touch.v = gRelate(noIHO.counties, IHOblob, byid = T)
counties.not.touching<-(touch.v %in% c("FF2FF1212")) #DE-9IM is super cool.
notouchIHO.counties = noIHO.counties[counties.not.touching,]
plot(notouchIHO.counties, co="light blue", add=T)
invisible(text(getSpPPolygonsLabptSlots(notouchIHO.counties),
labels=as.character(notouchIHO.counties$NAME), cex=0.4))
#wish I had a better way to label polys than above...`
You can see it here and here:
Upvotes: 1