I have a raster file with the following attributes:
rstack_ET[[1]] # first layer from a stacked raster
class : RasterLayer
dimensions : 334, 733, 244822 (nrow, ncol, ncell)
resolution : 409, 498 (x, y)
extent : 386067.5, 685864.5, 3805999, 3972331 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=36 +datum=WGS84 +units=m +no_defs
source : memory
names : ET.1.1.1.1.1
values : 70, 674.7922 (min, max)
and I am using a polygon, obtained from a shapefile, with the following attributes:
> dom_shp_sr
Simple feature collection with 1 feature and 11 fields
Geometry type: POLYGON
Dimension: XYZ
Bounding box: xmin: 490187.9 ymin: 3837447 xmax: 499269.9 ymax: 3852987
z_range: zmin: 0 zmax: 0
Projected CRS: WGS 84 / UTM zone 36N
Name descriptio timestamp begin end altitudeMo tessellate extrude visibility drawOrder icon
1 recharge limestones-akrotiri <NA> <NA> <NA> <NA> <NA> 1 0 -1 NA <NA>
geometry
1 POLYGON Z ((490233.2 383762...
The polgon looks like this:
When I am trying to use the polygon to crop the raster file:
test = crop(rstack_ET[[1]], dom_shp_sr)
I get the following image:
So, it uses the extent of the polygon to crop the raster file, but not the actual shape of the polygon. Any help is appreciated.
Using terra
package
# ensure your raster is a terra raster if not already
ET_sr_r <- terra::rast(ET_sr_r)
# I think crop expects a SpatVector so cast sf object as vect
masked_ET <- terra::crop(ET_sr_r, vect(dom_shp_sr), mask=TRUE)
Using raster
package
masked_ET <- raster::mask(raster::crop(ET_sr_r, dom_shp_sr), dom_shp_sr)