Looking for a function allowing to select/identify polygon that share a line segment with a source polygon

时间秒杀一切 提交于 2019-12-01 06:58:29

问题


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 . :


回答1:


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"))



回答2:


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:



来源:https://stackoverflow.com/questions/19784793/looking-for-a-function-allowing-to-select-identify-polygon-that-share-a-line-seg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!