Search code examples
rgisgeospatialshapefiler-sf

Find polygons with regions of non overlap in R using sf


I have two sets of polygons in shapefiles, imported using R's sf package. I'll call them A and B.

I would like to get all the polygons in B where there is either a) 0% overlap with any polygons in A or b) some non-overlap with polygons in A.

For example, for a given polygon b in B, I do not want regions where b is 100% contained within two (or three or four...) polygons in A.

However, let's say b is 50% contained within two (or three or four...) polygons in A, but 50% of it is not contained by any other polygons in A. In that case, I want to select b. Or if 100% of b is outside of all polygons in A, I want to select b.

Is there a possible way to do this using st_intersects? Please let me know if a diagram or something else would help clarify.

======

To put it more explicitly:

I have something like

## A is an sf object with many polygons, and so is B
## they both are part of the same geospatial area, 
## with some non-overlapping regions and many overlapping.
## I want all polygons in B that partially but not fully
## are covered by A. That is, I'm searching for regions of 
## the area not entirely covered by A, but if A covers it,
## I don't need B.

## So consider a loop approach

for (b in 1:nrow(B)) {
   
   if( !st_intersects(B[b,], A) {
       ....
   }
}

And this is where I get stuck... I'm not quite sure if I should be comparing polygons to A or to st_combine(A). And I'm not sure how to set up st_intersects to get me areas of non-overlap, or using st_intersection. I'm a little new to sf, and I have used both before, but only to test for some overlap, never to test for a case like this.


Solution

  • It just dawned on me I think this use case can be covered entirely using the st_crop() function. That's what I was trying to do. I'll close this as soon as I can since I think that gives me the answer I need. Thanks to all for the help.