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:
> 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:
> 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?
raster::res(x)
to check the resolution of each data. You can then resample to the desired resolution.