Search code examples
rrasterterra

cropping raster using polygon not selecting all cells in terra package


I have a raster and I am trying to crop it using a polygon

This is my polygon:

enter image description here

My raster file contains the entire globe at 0.25 degree resolution so I cropped the raster using the polygon:

 library(terra)
 crop_raster <- terra::crop(temp_rast, county_ref)  
 plot(crop_raster)
 plot(county_ref, add = T)

enter image description here

This cropping looks odd since only 2 raster cells are selected. I was expecting something like below which I get using the snap = "out" argument.

crop_raster_snap <- terra::crop(temp_rast, county_ref, snap = "out") 
plot(crop_raster_snap)
plot(county_ref, add = T)

enter image description here

Why didn't the crop argument worked in the first instance? And what is the purpose of the 'snap' argument?


Solution

  • The documentation of crop, with SpatRaster argument x, and SpatExtent (or the extent of another spatial object) argument y; that argument "snap" is

    One of "near", "in", or "out". Used to align y to the geometry of x

    y needs to be aligned to the geometry of x because you cannot crop partial rows or columns. You can only crop entire rows or columns. So you have three options, go inwards (smaller area), outwards (larger area) or to the border that is nearest.