Skip to content
Prev 28244 / 29559 Next

Error trying to stack two raster bricks with different extension and resolution

Hi All,


I?m starting to work with the package raster and although I?ve been looking for solutions, I can?t find the way to solve my problem.


I?ve two ncdf files downloaded from Copernicus with near-bottom sea temperature data. They correspond to two different periods of time (1998-2018 & 2019) and they have both different extent and resolution. The files can be downloaded from https://www.dropbox.com/sh/orr567sjdvxjycn/AAB2zEbo5hMwRhm0YEvXY5dma?dl=0


I want to stack both files in one raster brick with a final resolution of 3km and a LAEA projection. I?ve tried several approaches but none has worked.


Any help will be very appreciated! Thanks in advance.


Juan Carlos



This is the code I have been working with
-----------------------------------------------------------------------


library(ncdf4)
library(raster)
library(proj4)

temp_9818 <-brick('temp_9818.nc')
temp_19 <-brick('temp_2019.nc')
LAEA <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"

temp_9818
#class      : RasterBrick
#dimensions : 61, 145, 8845, 252  (nrow, ncol, ncell, nlayers)
#resolution : 0.08333333, 0.08333333  (x, y)
#extent     : -11.04167, 1.041666, 40.95833, 46.04167  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

temp_19
#class      : RasterBrick
#dimensions : 181, 433, 78373, 12  (nrow, ncol, ncell, nlayers)
#resolution : 0.02777778, 0.02777778  (x, y)
#extent     : -11.01389, 1.013889, 40.98611, 46.01389  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

# Different extents
extent(temp_9818)
#xmin       : -11.04167
#xmax       : 1.041666
#ymin       : 40.95833
#ymax       : 46.04167

extent(temp_19)
#xmin       : -11.01389
#xmax       : 1.013889
#ymin       : 40.98611
#ymax       : 46.01389

##  Projecting 3km & LAEA ---
temp_9818_project <-projectRaster(temp_9818, crs=LAEA, res=3000, method="bilinear")
temp_19_project <-projectRaster(temp_19, crs=LAEA, res=3000, method="bilinear")

extent(temp_9818_project)
#xmin       : 2552554
#xmax       : 3644554
#ymin       : 2015246
#ymax       : 2792246

extent(temp_19_project)
#xmin       : 2555581
#xmax       : 3641581
#ymin       : 2017655
#ymax       : 2788655


##  Obviously, error!! ----
temp_9819_final <- stack(temp_9818_project,temp_19_project)
#Error in compareRaster(x) : different extent

## Defining new extension for cropping
newextend <- extent(c(2395000, 3400000, 2281000, 2600000))

temp_9818_project_crop <-crop(temp_9818_project, newextend)
temp_19_project_crop <-crop(temp_19_project, newextend)

temp_9819_final <- stack(temp_9818_project_crop, temp_19_project_crop) #Error again!!
#Error in compareRaster(x) : different extent

extent(temp_9818_project_crop)
#xmin       : 2552554
#xmax       : 3398554
#ymin       : 2282246
#ymax       : 2600246

extent(temp_19_project_crop)
#xmin       : 2555581
#xmax       : 3398581
#ymin       : 2281655
#ymax       : 2599655

[[elided Hotmail spam]]

-------------------------------------------------------------
Message-ID: <DB6PR0901MB1653115F64C0B76A00B6C5238B6A0@DB6PR0901MB1653.eurprd09.prod.outlook.com>
In-Reply-To: <CAMbH+HhD4MEEjucXa-dsYpO43R35G5BYkMvWKrROALe1fYS3MA@mail.gmail.com>