Is it possible to perform a cookie cut in R?

Firstly, I have a polygon shapefile of many polygon some of which overlap. I then have a second polygon shapefile with contiguous non-overlapping polygons. I want to establish the parts of the first (potentially overlapping) polygons that fall within each of the second (non-overlapping) polygons. The first shapefile is a number of layers of cookie dough to cut and the second is the cutter to apply. Alternatively I can use QGIS if people think that's the best way to go.

  • 1
    Have you looked at the operations on polygons provided by the sf package? In particular st_difference might be the one. – Spacedman Jul 29 '18 at 17:00

The solution in R:

dough <- readOGR("poly1.shp")
cutter <- readOGR("poly2.shp")
cookies <- gIntersection(dough, cutter, byid=TRUE)
  • Thanks Micha, looks so elegant that it must be true. I'll try this later in the week and hopefully come back with a tick. – Stephen Clark Jul 30 '18 at 8:00
  • 1
    Note this uses sp classes for spatial data - a solution with sf classes should always be preferred, which would use st_intersection – Spacedman Jul 30 '18 at 13:30
  • I have the following class for my spatial data, so I think I'm good to go. > class(iso) [1] "SpatialPolygonsDataFrame" attr(,"package") [1] "sp" – Stephen Clark Jul 31 '18 at 14:59

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.