Mark
Mark

Reputation: 137

Select polygons intersecting (or completely within) another polygon [R sf]

I would like to select polygons from one simple feature collection that intersect a polygon in another sf collection. Here is a reproducible example:

library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
nc <- st_transform(nc,32717);nc
poly <- nc[1,]
fishnet<-st_make_grid(st_as_sfc(st_bbox(poly)),cellsize=5000)

Now, having created poly and a fishnet around it, I would like to select those fishnet cells that intersect poly or that are completely contained by poly.

Thanks for any help!

Mark

Upvotes: 6

Views: 4837

Answers (1)

Eugene Chong
Eugene Chong

Reputation: 1741

fishnet2 <- fishnet[poly,]

And a plot

library(ggplot2)

ggplot() + 
  geom_sf(data = poly, fill = "blue") +
  geom_sf(data = fishnet2, color = "red", fill = NA)

enter image description here

Upvotes: 7

Related Questions