The raster package simplifies the access to large raster data sets and extends the analytic tools available for both raster and vector data. Used with rasterVis, it also provides enhanced visualization.

## load Berlin spatial data

To showcase some of the features provided by the raster 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 <- ""
zipfile <- ""
## download the file
download.file(paste0(download.url,zipfile),temp, mode="wb")
## unzip the file

To load the raster data set we apply the raster() function.

## read in the data 
srtm <- raster('srtm_germany_dtm.tif')

Printing the RasterLayer object summarizes the raster data set.

## class       : RasterLayer 
## dimensions  : 10801, 13201, 142584001  (nrow, ncol, ncell)
## resolution  : 0.0008333333, 0.0008333333  (x, y)
## extent      : 4.999583, 16.00042, 46.99958, 56.00042  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## data source : /Users/jokr/Documents/soga/srtm_germany_dtm.tif 
## names       : srtm_germany_dtm 
## values      : -32768, 32767  (min, max)

When working with large data sets it is recommendable to check the size of the file in advance. 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 downscale the raster data set using he 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)