4

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

RasA

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 RasB

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). pprcontains 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)
  • 1
    Try projectRaster(RasA, RasB) – mdsumner Feb 15 '16 at 1:01
  • perhaps a different download to netCDF? but simply; projectRaster, disaggregate and crop – Sam Feb 15 '16 at 20:40
  • 1
    As you are supplying a RasterLayer, the res argument in projectRaster is ignored. Also, a resolution of 10000 makes no sense at all when using +proj=longlat. Otherwise, the approach would be to do something like x <- raster(rasB); res(x) <- 0.5; y <- projectRaster(rasA, x). – Robert Hijmans Feb 16 '16 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.