smok
smok

Reputation: 43

Raster::extend does not extend rasters

I am trying to align .asc rasters with different extents but same resolution in R so that they can be used to create a Raster stack. I am working with the "raster" package (Hijmans et al. 2022, version 3.5-29)

The rasters have the following properties:

> r1;r2
class      : RasterLayer 
dimensions : 1420, 1207, 1713940  (nrow, ncol, ncell)
resolution : 50, 50  (x, y)
extent     : -30155.19, 30194.81, -35519.03, 35480.97  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : Naive_IS2018_UDScaled.asc 
names      : Naive_IS2018_UDScaled 

class      : RasterLayer 
dimensions : 1418, 939, 1331502  (nrow, ncol, ncell)
resolution : 50, 50  (x, y)
extent     : -23488.8, 23461.2, -35475.82, 35424.18  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : Naive_IS2019_UDScaled.asc 
names      : Naive_IS2019_UDScaled 

In order to bring them to the same extent, I am creating a raster list and extract the minimum and maximum values:

## align all rasters to same extent
rasterlist <- as.list(r1,r2)

raster_extents <- lapply(rasterlist, raster::extent)
do.call(raster::merge, raster_extents)

sharedextent <- c(-30155.19 , 30194.81 , -35519.03 , 35480.97)

Now I am using the enxtend() to extend the extents of the rasters so that they share the same extents.

r1 <- raster::extend(r1, sharedextent)
r2 <- raster::extend(r2, sharedextent)

However, this does not result in a change of extents. But, it changes the source to "memory" and add a "values" row in r2.

> r1;r2
class      : RasterLayer 
dimensions : 1420, 1207, 1713940  (nrow, ncol, ncell)
resolution : 50, 50  (x, y)
extent     : -30155.19, 30194.81, -35519.03, 35480.97  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : Naive_IS2018_UDScaled.asc 
names      : Naive_IS2018_UDScaled 

class      : RasterLayer 
dimensions : 1420, 1207, 1713940  (nrow, ncol, ncell)
resolution : 50, 50  (x, y)
extent     : -30138.8, 30211.2, -35525.82, 35474.18  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : Naive_IS2019_UDScaled 
values     : 0, 0.0006528347  (min, max)

Any help would be greatly appreciated.

Upvotes: 0

Views: 185

Answers (1)

Grzegorz Sapijaszko
Grzegorz Sapijaszko

Reputation: 3604

Is that you are looking for? (Updated with keepres = TRUE). Please keep in mind that changing extent will either change resolution, either number of rows/columns.

library(raster)
#> Loading required package: sp
r1 <- raster(nrows = 1420, ncols = 1207, resolution = 50, ext = extent(c(-30155.19, 30194.81, -35519.03, 35480.97)))
r2 <- raster(nrows = 1418, ncols = 939, resolution = 50, ext = extent(c(-23488.8, 23461.2, -35475.82, 35424.18)))
r2
#> class      : RasterLayer 
#> dimensions : 1418, 939, 1331502  (nrow, ncol, ncell)
#> resolution : 50, 50  (x, y)
#> extent     : -23488.8, 23461.2, -35475.82, 35424.18  (xmin, xmax, ymin, ymax)
#> crs        : NA
r2 <- setExtent(r2, r1, keepres=TRUE)
r2
#> class      : RasterLayer 
#> dimensions : 1420, 1207, 1713940  (nrow, ncol, ncell)
#> resolution : 50, 50  (x, y)
#> extent     : -30155.19, 30194.81, -35519.03, 35480.97  (xmin, xmax, ymin, ymax)
#> crs        : NA

Created on 2022-09-06 with reprex v2.0.2

Upvotes: 1

Related Questions