1

I'm quite new to GIS so sorry if this is a daft question - is it possible to filter out non-overlapping parts of a shapefile? I have a boundary shapefile which I would like to cover with small areas from another shapefile. The small areas don't neatly fit inside the boundary, so filtering gives the wrong final shape.

In the picture, I'm trying to just get the areas which are entirely inside the red boundary, plus the parts of the 'overlapping' areas which are inside the red boundary. I'm using R and sf, but I'm happy to switch to QGIS if it's easier to do this sort of operation there. The code below makes a simpler version what I'm tring for - ideally I'd like to make a third shapefile which is the square & the part of the triangle contained inside the larger shape

enter image description here

p1 = st_polygon(list(rbind(c(1, 5), c(2, 2), c(4, 1), c(4, 4), c(1, 5))))
p2 = st_polygon(list(rbind(c(2, 3), c(2, 4), c(3, 4), c(3, 3), c(2, 3))))
p3 = st_polygon(list(rbind(c(2, 1.5), c(2.5, 2.5), c(2.5, 1), c(2, 1.5))))

big = st_as_sf(st_as_sfc(st_as_text(p1)))
small1 = st_as_sf(st_as_sfc(st_as_text(p2)))
small2 = st_as_sf(st_as_sfc(st_as_text(p3)))
small = bind_rows(small1, small2) %>% mutate(shape = c('square', 'triangle'))

ggplot() + geom_sf(data = big) + geom_sf(data = small)

Ideally I'd like to get just the highlighted shapes

enter image description here

3
  • This is called "intersection" ;)
    – MrXsquared
    Jun 26 at 10:54
  • @MrXsquared The first picture is the output of an intersection! In sf intersections will include anything which is inside the boundary, plus anything which crosses over or touches the boundary. I'd like to get just the parts of the overlapping areas which are inside the boundary if that makes sense Jun 26 at 11:50
  • 1
    In sf, the function st_intersects() will indeed return (the index of) whole polygons either inside or overlapping the boundaries, but st_intersection() will perform an actual clip, i.e. return the geometries highlighted in your second image.
    – Sandy AB
    Jun 26 at 12:52

1 Answer 1

1

Use st_intersection:

inter = st_intersection(big,small)
plot(st_geometry(inter))

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.