1

If I have to crop a raster for a irregular shapefile (for e.g. a country), I would do a crop followed by mask as follows:

library(raster)
r <- crop(my_raster, extent(polygon))
r <- mask(r, polygon)

Is there any equivalent of doing the same for polygons. I tried the below to clip world river basin shapefile for a given country but it only returned me the basins that were included in the extent and not the basin following the boundary of the shapefile

  library(sf)
  cropped <- st_crop(river_basins, country_shapefile)
89_Simple
  • 3,393
  • 3
  • 39
  • 94
  • 3
    consider using `sf::st_intersection()` - in addition you might find the sf cheatsheet (https://github.com/rstudio/cheatsheets/blob/main/sf.pdf) handy... – Jindra Lacko Aug 09 '22 at 07:50
  • https://gis.stackexchange.com/questions/377964/clip-function-in-the-sf-package-in-r – M-- Aug 11 '23 at 13:53
  • https://gis.stackexchange.com/questions/317804/is-there-a-way-to-clip-a-layer-with-a-shape-from-another-layer-in-sf – M-- Aug 11 '23 at 13:53
  • Including the link from the answer below, since as is, it's a link only answer which would get deleted: https://www.r-bloggers.com/2014/07/clipping-spatial-data-in-r/#google_vignette – M-- Aug 11 '23 at 13:56

0 Answers0