7.8 Convert from and to Raster
\(^*\) or SpatRaster
objects
When you need to convert a stars
object to a Raster
\(^*\) or SpatRaster
object, you can use the as()
function as follows:
(# to Raster* object
<- as(prcp_tmax_PRISM_m8_y09, "Raster")
prcp_tmax_PRISM_m8_y09_rb )
class : RasterBrick
dimensions : 20, 20, 400, 10 (nrow, ncol, ncell, nlayers)
resolution : 0.04166667, 0.04166667 (x, y)
extent : -121.7292, -120.8958, 45.8125, 46.64583 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=NAD83 +no_defs
source : memory
names : layer.1, layer.2, layer.3, layer.4, layer.5, layer.6, layer.7, layer.8, layer.9, layer.10
min values : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
max values : 0.019, 18.888, 30.851, 4.287, 0.797, 0.003, 3.698, 1.801, 0.000, 0.000
time : 2009-08-11, 2009-08-12, 2009-08-13, 2009-08-14, 2009-08-15, 2009-08-16, 2009-08-17, 2009-08-18, 2009-08-19, 2009-08-20
(# to SpatRaster
<- as(prcp_tmax_PRISM_m8_y09, "SpatRaster")
prcp_tmax_PRISM_m8_y09_sr )
class : SpatRaster
dimensions : 20, 20, 10 (nrow, ncol, nlyr)
resolution : 0.04166667, 0.04166667 (x, y)
extent : -121.7292, -120.8958, 45.8125, 46.64583 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat NAD83 (EPSG:4269)
source : memory
names : date2~08-11, date2~08-12, date2~08-13, date2~08-14, date2~08-15, date2~08-16, ...
min values : 0, 0, 0, 0, 0, 0, ...
max values : 0.019, 18.888, 30.851, 4.287, 0.797, 0.003, ...
As you can see, date values in prcp_tmax_PRISM_m8_y09
appear in the time dimension of prcp_tmax_PRISM_m8_y09_rb
(RasterBrick
). However, in prcp_tmax_PRISM_m8_y09_sr
(SpatRaster
), date values appear in the names
dimension with date
added as prefix to the original date values.
Note also that the conversion was done for only the ppt
attribute. This is simply because the raster
and terra
package does not accommodate multiple attributes of 3-dimensional array. So, if you want a RasterBrick
or SpatRaster
of the tmax data, then you need to do the following:
(# to RasterBrick
<- as(prcp_tmax_PRISM_m8_y09["tmax",,,], "Raster")
tmax_PRISM_m8_y09_rb )
class : RasterBrick
dimensions : 20, 20, 400, 10 (nrow, ncol, ncell, nlayers)
resolution : 0.04166667, 0.04166667 (x, y)
extent : -121.7292, -120.8958, 45.8125, 46.64583 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=NAD83 +no_defs
source : memory
names : layer.1, layer.2, layer.3, layer.4, layer.5, layer.6, layer.7, layer.8, layer.9, layer.10
min values : 10.188, 8.811, 6.811, 3.640, 1.833, 6.780, 10.318, 14.270, 18.281, 19.818
max values : 29.842, 28.892, 27.123, 23.171, 21.858, 24.690, 27.740, 32.906, 35.568, 39.707
time : 2009-08-11, 2009-08-12, 2009-08-13, 2009-08-14, 2009-08-15, 2009-08-16, 2009-08-17, 2009-08-18, 2009-08-19, 2009-08-20
(# to SpatRaster
<- as(prcp_tmax_PRISM_m8_y09["tmax",,,], "SpatRaster")
tmax_PRISM_m8_y09_sr )
class : SpatRaster
dimensions : 20, 20, 10 (nrow, ncol, nlyr)
resolution : 0.04166667, 0.04166667 (x, y)
extent : -121.7292, -120.8958, 45.8125, 46.64583 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat NAD83 (EPSG:4269)
source : memory
names : date2~08-11, date2~08-12, date2~08-13, date2~08-14, date2~08-15, date2~08-16, ...
min values : 10.188, 8.811, 6.811, 3.640, 1.833, 6.780, ...
max values : 29.842, 28.892, 27.123, 23.171, 21.858, 24.690, ...
You can convert a Raster
\(^*\) object to a stars
object using st_as_stars()
(you will see a use case in Chapter 9.4.2).
(<- st_as_stars(tmax_PRISM_m8_y09_rb)
tmax_PRISM_m8_y09_back_to_stars )
stars object with 3 dimensions and 1 attribute
attribute(s):
Min. 1st Qu. Median Mean 3rd Qu. Max.
layer.1 1.833 17.55575 21.483 22.03543 26.54275 39.707
dimension(s):
from to offset delta refsys point values x/y
x 1 20 -121.729 0.0416667 NAD83 NA NULL [x]
y 1 20 46.6458 -0.0416667 NAD83 NA NULL [y]
band 1 10 2009-08-11 1 days Date NA NULL
Notice that the original date values (stored in the date
dimension) are recovered, but its dimension is now called just band
.
You can do the same with a SpatRaster
object.
(<- st_as_stars(tmax_PRISM_m8_y09_sr)
tmax_PRISM_m8_y09_back_to_stars )
stars object with 3 dimensions and 1 attribute
attribute(s):
Min. 1st Qu. Median Mean 3rd Qu. Max.
values 1.833 17.55575 21.483 22.03543 26.54275 39.707
dimension(s):
from to offset delta refsys point values
x 1 20 -121.729 0.0416667 NAD83 NA NULL
y 1 20 46.6458 -0.0416667 NAD83 NA NULL
band 1 10 NA NA NA NA date2009-08-11,...,date2009-08-20
x/y
x [x]
y [y]
band
Note that unlike the conversion from the RasterBrick
object, the band
dimension inherited values of the name
dimension in tmax_PRISM_m8_y09_sr
.