1

I have two raster layers that I would like to create a raster stack from. They are currently in different extents. One is a DEM layer and the other is a raster of an individual's home range. This is the DEM layer that I currently have:

enter image description here

> str(elev_med)
Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. ..@ name        : chr "C:\\Users\\jhnhn\\AppData\\Local\\Temp\\RtmpaOErZT\\file4f03b9d4e7b.tif"
  .. .. ..@ datanotation: chr "INT2S"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  .. .. ..@ NAchanged   : logi FALSE
  .. .. ..@ nbands      : int 1
  .. .. ..@ bandorder   : chr "BIL"
  .. .. ..@ offset      : int 0
  .. .. ..@ toptobottom : logi TRUE
  .. .. ..@ blockrows   : int 3
  .. .. ..@ blockcols   : int 1039
  .. .. ..@ driver      : chr "gdal"
  .. .. ..@ open        : logi FALSE
  ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. ..@ values    : logi(0) 
  .. .. ..@ offset    : num 0
  .. .. ..@ gain      : num 1
  .. .. ..@ inmemory  : logi FALSE
  .. .. ..@ fromdisk  : logi TRUE
  .. .. ..@ isfactor  : logi FALSE
  .. .. ..@ attributes: list()
  .. .. ..@ haveminmax: logi TRUE
  .. .. ..@ min       : num -32768
  .. .. ..@ max       : num 32767
  .. .. ..@ band      : int 1
  .. .. ..@ unit      : chr ""
  .. .. ..@ names     : chr "file4f03b9d4e7b"
  ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. ..@ type      : chr(0) 
  .. .. ..@ values    : logi(0) 
  .. .. ..@ color     : logi(0) 
  .. .. ..@ names     : logi(0) 
  .. .. ..@ colortable: logi(0) 
  ..@ title   : chr(0) 
  ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. ..@ xmin: num 368790
  .. .. ..@ xmax: num 491929
  .. .. ..@ ymin: num 4236172
  .. .. ..@ ymax: num 4358719
  ..@ rotated : logi FALSE
  ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. ..@ geotrans: num(0) 
  .. .. ..@ transfun:function ()  
  ..@ ncols   : int 1039
  ..@ nrows   : int 1034
  ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=utm +zone=12 +datum=WGS84 +units=m +no_defs"
  .. .. ..$ comment: chr "PROJCRS[\"WGS 84 / UTM zone 12N\",\n    BASEGEOGCRS[\"WGS 84\",\n        DATUM[\"World Geodetic System 1984\",\"| __truncated__
  ..@ history : list()
  ..@ z       : list()

and this is the home range raster that I have: enter image description here

> str(sum_raster[[1]])
Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. ..@ name        : chr ""
  .. .. ..@ datanotation: chr "FLT4S"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  .. .. ..@ NAchanged   : logi FALSE
  .. .. ..@ nbands      : int 1
  .. .. ..@ bandorder   : chr "BIL"
  .. .. ..@ offset      : int 0
  .. .. ..@ toptobottom : logi TRUE
  .. .. ..@ blockrows   : int 0
  .. .. ..@ blockcols   : int 0
  .. .. ..@ driver      : chr ""
  .. .. ..@ open        : logi FALSE
  ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. ..@ values    : num [1:30] NA NA 0 NA NA ...
  .. .. ..@ offset    : num 0
  .. .. ..@ gain      : num 1
  .. .. ..@ inmemory  : logi TRUE
  .. .. ..@ fromdisk  : logi FALSE
  .. .. ..@ isfactor  : logi FALSE
  .. .. ..@ attributes: list()
  .. .. ..@ haveminmax: logi TRUE
  .. .. ..@ min       : num 0
  .. .. ..@ max       : num 0.266
  .. .. ..@ band      : int 1
  .. .. ..@ unit      : chr ""
  .. .. ..@ names     : chr "layer"
  ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. ..@ type      : chr(0) 
  .. .. ..@ values    : logi(0) 
  .. .. ..@ color     : logi(0) 
  .. .. ..@ names     : logi(0) 
  .. .. ..@ colortable: logi(0) 
  ..@ title   : chr(0) 
  ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. ..@ xmin: num 400820
  .. .. ..@ xmax: num 401570
  .. .. ..@ ymin: num 4324369
  .. .. ..@ ymax: num 4324994
  ..@ rotated : logi FALSE
  ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. ..@ geotrans: num(0) 
  .. .. ..@ transfun:function ()  
  ..@ ncols   : int 6
  ..@ nrows   : int 5
  ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=utm +zone=12 +datum=WGS84 +units=m +no_defs"
  .. .. ..$ comment: chr "PROJCRS[\"WGS 84 / UTM zone 12N\",\n    BASEGEOGCRS[\"WGS 84\",\n        DATUM[\"World Geodetic System 1984\",\"| __truncated__
  ..@ history : list()
  ..@ z       : list()

I tried cropping the data:

# Load packages
library(elevatr)
library(sp)
library(raster)

# Download elevation data based on the home range isopleth
elev_med <- get_elev_raster(sum_iso[[1]], z = 9)

#Plot elevation raster
plot(elev_med)

#Crop elevation raster to home range raster
et <- crop(elev_med, sum_raster[[1]])

plot(sum_raster[[1]])
plot(et)

# Create raster stack
rs <- raster::stack(sum_raster[[1]], et)

I continue to get the error stating that they have different extents. What would be the best way to approach this?

2
  • 1
    I believe that you need to resolve the resolutions of the two data before the extents can match. Take a look at raster::res(x) to check the resolution of each data. You can then resample to the desired resolution. Commented Jun 30, 2021 at 16:35
  • 1
    You can't stack rasters with different pixel size, and extent probably isn't the same due to this. If you want to put together both layers you need to create a virtual raster with GDAL
    – aldo_tapia
    Commented Jun 30, 2021 at 16:36

1 Answer 1

2

You can first resample one raster with respect to the other to match the resolution, and then stack the two.

library(raster)

# Load rasters
et = raster("path/et.tiff)
sum_raster = raster("path/sum_raster.tiff)

# Get required extent 
extent(sum_raster) 

# Store extent values (coordinates) in a variable
Extent = c(-160, 10, 30, 60)

# Crop
et_crop = crop(et, Extent)

# Resample to match pixel size
et_resample = raster::resample(et_crop, sum_raster)

# Stack 
rst_stack = stack(et_resample, sum_raster) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.