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, 2018 at 17:00

1 Answer 1


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. Jul 30, 2018 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, 2018 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" Jul 31, 2018 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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