library(raster)
How can one clip one raster based on another raster?
RasA:
class : RasterLayer
dimensions : 459, 533, 244647 (nrow, ncol, ncell)
resolution : 10000, 10000 (x, y)
extent : 3685000, 9015000, 655000, 5245000 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=lcc +lat_1=49 +lat_2=77 +lat_0=63.390675 +lon_0=-91.86666666666666 +x_0=6200000 +y_0=3000000 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0
data source : in memory
names : Total.Precipitation
values : 0, 11.85 (min, max)
The resolution of RasA
is 10km*10km
sample data for RasA an be found here. Shapefile included
RasB:
class : RasterBrick
dimensions : 416, 885, 368160, 2 (nrow, ncol, ncell, nlayers)
resolution : 0.1, 0.1 (x, y)
extent : -141.1, -52.6, 41.6, 83.2 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0
data source : in memory
names : layer.1, layer.2
min values : 0, 0
max values : 22.6, 27.8
The resolution of RasB
is 0.1deg*0.1deg
Question:
1) crop
RasA
to the extent of RasB
Using @mdsummer's suggestion I solved a previous projection issue using the following method:
prr=projectRaster(RasA,RasB,method = "ngb")
range(values(prr) , na.rm = T )
But trying to change the resolution of RasA
during (prr=projectRaster(RasA,RasB,method = "ngb",res=10000)
and after ( res(ppr)=10000)
reprojection
appears not to have any effect (see below). ppr
contains the same dimensions and values
as RasA
but the resolution is copied from RasB
.
class : RasterLayer
dimensions : 416, 885, 368160 (nrow, ncol, ncell)
resolution : 0.1, 0.1 (x, y)
extent : -141.1, -52.6, 41.6, 83.2 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0
data source : in memory
names : Total.Precipitation
values : 0, 11.85 (min, max)
Best Answer
Doing this through projection is quite weird, as you are trying to match extensions with rasters of different resolutions.
Here I suggest two options: (1) resample the clipping raster to the desired final resolution first and then projectRaster with that layer, or (2) just crop the raster to keep the original resolution (10000x10000 in your case).
Here is an example with downloadable layers:
Results: