The terra
package provides bindings to the
Geospatial Data Abstraction Library (GDAL) and access to projection and transformation
operations from the PROJ
library. This software is the basis for spatial software applications
and geodata analysis within R and is the successor of the
raster
package. Its bindings allow fast I/O (input/output)
operations for GDAL-supported raster formats, OGR-supported vector formats and numerous utility
functions to convert geographic longitude and latitude coordinates into
Cartesian coordinates (and vice versa). Furthermore, the
terra
package simplifies the access to large raster data
sets and extends the analytic tools available for both raster and vector
data. Additionally, the rasterVis
package provides enhanced
visualization opportunities.
library(terra)
library(rasterVis)
## load Berlin spatial data
load(url("https://userpage.fu-berlin.de/soga/data/r-data/berlin_districts_sp_wgs84.RData"))
load(url("https://userpage.fu-berlin.de/soga/data/r-data/berlin_geocampus.RData"))
If you have not installed the required packages yet, you can do so by
install.packages("terra", "rasterVIS")
To showcase some of the features provided by the terra
and rasterVis
packages we download the SRTM digital elevation model (DEM) for Germany. The data
was retrieved from OPENDEM on June 25, 2017. Please note the zipped
file is about 86.4 MB in size.
## build a temporary folder on disk
temp <- tempfile()
download_url <- "https://userpage.fu-berlin.de/soga/data/raw-data/spatial/"
zipfile <- "srtm_germany_dtm.zip"
## download the file
download.file(paste0(download_url, zipfile), temp, mode = "wb")
## unzip the file
unzip(temp)
unlink(temp)
To load the raster data set we apply the rast()
function
out of the terra
package.
## read the data
srtm <- rast("srtm_germany_dtm.tif")
Printing the SpatRaster
object summarizes the raster
data set.
srtm
## class : SpatRaster
## dimensions : 10801, 13201, 1 (nrow, ncol, nlyr)
## resolution : 0.0008333333, 0.0008333333 (x, y)
## extent : 4.999583, 16.00042, 46.99958, 56.00042 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## source : srtm_germany_dtm.tif
## name : srtm_germany_dtm
When working with large data sets, checking the file size in advance is recommended. The processing time of large data may be quite long.
fs <- file.size("srtm_germany_dtm.tif")
print(paste("The file size is", as.numeric(fs) / 1e6, "MB"))
## [1] "The file size is 285.254776 MB"
Owing to the large file size, we downscaled the raster data set using
the aggregate()
function. The aggregate()
function performs aggregation by grouping adjacent pixels into new ones,
given an input raster and the aggregation factor fact
. For
example, using fact = 5
means that each set of 5 \(\times\) 5 cells in the original raster
becomes a single cell in the new raster. In addition, the
fun
argument specifies which function will be used to
calculate the new values based on existing ones (the default is
mean).
For demonstration purposes, we apply the aggregate()
function on the SRTM raster, aggregating 5 \(\times\) 5 sets of cells.
srtm_small <- aggregate(srtm, 5, fun = mean)
plot(srtm_small, main = "DEM-SRTM with default colormap")