Introduction to GIS

Rasters

2025-06-26

What are rasters?

  1. Rasters are gridded areas where each grid pixel takes a value.
  2. Compress data into a manageable size (e.g., all buildings globally).
  3. Represent data obtained in a gridded format (e.g., satellite imagery).

Landcover in Leipzig (2015)

Load a raster

  1. Use rast(<filename>) to load a raster.
  2. Some files contains multiple layers; rast() can also load them.
landcover <- rast("data/landcover-2015.tif") # this has 1 layer
ndvi <- rast("data/ndvi-2024.tif") # this has 10 layers, one per month
nlyr(ndvi)
[1] 10
dim(ndvi)
[1] 469 695  10

Common raster file extensions are: .tif, .gif, .grib, .hd5, .nc (netCDF), and .png.

Raster metadata

class       : SpatRaster 
dimensions  : 469, 695, 10  (nrow, ncol, nlyr)
resolution  : 0.0004491576, 0.0004491576  (x, y)
extent      : 12.2338, 12.54596, 51.209, 51.41966  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
source      : ndvi-2024.tif 
names       :    january,      march,      april,        may,       june,       july, ... 
min values  : -0.8566782, -0.6070393, -1.0000000, -0.4106196, -1.0000000, -0.7984271, ... 
max values  :  0.8910936,  0.8981348,  0.9751282,  0.9380596,  0.9398647,  0.9476519, ... 
  1. dimensions: size of the raster.
  2. resolution: horizontal resolution.
  3. extent: area covered by the raster.
  4. coord. ref.: coordinate reference system (CRS).
  5. names: names of the layers.

Caution

Rasters without the same dimension, resolution, extent, or CRS cannot be combined.

Error: [rast] extents do not match

Getting metadata

terra has functions to get the metadata in a programmatic way:

  1. dim() for dimensions.
  2. res() for resolution.
  3. ext() for spatial extent.
  4. crs() for the CRS.
  5. names() for names.
crs(landcover) # WKT2
[1] "PROJCRS[\"ETRS89-extended / LAEA Europe\",\n    BASEGEOGCRS[\"ETRS89\",\n        ENSEMBLE[\"European Terrestrial Reference System 1989 ensemble\",\n            MEMBER[\"European Terrestrial Reference Frame 1989\"],\n            MEMBER[\"European Terrestrial Reference Frame 1990\"],\n            MEMBER[\"European Terrestrial Reference Frame 1991\"],\n            MEMBER[\"European Terrestrial Reference Frame 1992\"],\n            MEMBER[\"European Terrestrial Reference Frame 1993\"],\n            MEMBER[\"European Terrestrial Reference Frame 1994\"],\n            MEMBER[\"European Terrestrial Reference Frame 1996\"],\n            MEMBER[\"European Terrestrial Reference Frame 1997\"],\n            MEMBER[\"European Terrestrial Reference Frame 2000\"],\n            MEMBER[\"European Terrestrial Reference Frame 2005\"],\n            MEMBER[\"European Terrestrial Reference Frame 2014\"],\n            ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n                LENGTHUNIT[\"metre\",1]],\n            ENSEMBLEACCURACY[0.1]],\n        PRIMEM[\"Greenwich\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        ID[\"EPSG\",4258]],\n    CONVERSION[\"Europe Equal Area 2001\",\n        METHOD[\"Lambert Azimuthal Equal Area\",\n            ID[\"EPSG\",9820]],\n        PARAMETER[\"Latitude of natural origin\",52,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8801]],\n        PARAMETER[\"Longitude of natural origin\",10,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"False easting\",4321000,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8806]],\n        PARAMETER[\"False northing\",3210000,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8807]]],\n    CS[Cartesian,2],\n        AXIS[\"northing (Y)\",north,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1]],\n        AXIS[\"easting (X)\",east,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1]],\n    USAGE[\n        SCOPE[\"Statistical analysis.\"],\n        AREA[\"Europe - European Union (EU) countries and candidates. Europe - onshore and offshore: Albania; Andorra; Austria; Belgium; Bosnia and Herzegovina; Bulgaria; Croatia; Cyprus; Czechia; Denmark; Estonia; Faroe Islands; Finland; France; Germany; Gibraltar; Greece; Hungary; Iceland; Ireland; Italy; Kosovo; Latvia; Liechtenstein; Lithuania; Luxembourg; Malta; Monaco; Montenegro; Netherlands; North Macedonia; Norway including Svalbard and Jan Mayen; Poland; Portugal including Madeira and Azores; Romania; San Marino; Serbia; Slovakia; Slovenia; Spain including Canary Islands; Sweden; Switzerland; Turkey; United Kingdom (UK) including Channel Islands and Isle of Man; Vatican City State.\"],\n        BBOX[24.6,-35.58,84.73,44.83]],\n    ID[\"EPSG\",3035]]"
crs(landcover, proj = TRUE) # PROJ
[1] "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs"
crs(landcover, describe = TRUE) # CODE
                           name authority code
1 ETRS89-extended / LAEA Europe      EPSG 3035
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          area
1 Europe - European Union (EU) countries and candidates. Europe - onshore and offshore: Albania; Andorra; Austria; Belgium; Bosnia and Herzegovina; Bulgaria; Croatia; Cyprus; Czechia; Denmark; Estonia; Faroe Islands; Finland; France; Germany; Gibraltar; Greece; Hungary; Iceland; Ireland; Italy; Kosovo; Latvia; Liechtenstein; Lithuania; Luxembourg; Malta; Monaco; Montenegro; Netherlands; North Macedonia; Norway including Svalbard and Jan Mayen; Poland; Portugal including Madeira and Azores; Romania; San Marino; Serbia; Slovakia; Slovenia; Spain including Canary Islands; Sweden; Switzerland; Turkey; United Kingdom (UK) including Channel Islands and Isle of Man; Vatican City State
                       extent
1 -35.58, 44.83, 84.73, 24.60

Extract and combine layers

Extract one or more layers

This is done as if the layers were in a list.

  • <raster>[[i]] select the i-th layer.
  • <raster>[[c(3, 6, 12)]] select the third, sixth, and 12th layers.
ndvi[[c(3, 6)]]
class       : SpatRaster 
dimensions  : 469, 695, 2  (nrow, ncol, nlyr)
resolution  : 0.0004491576, 0.0004491576  (x, y)
extent      : 12.2338, 12.54596, 51.209, 51.41966  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
source      : ndvi-2024.tif 
names       :      april,       july 
min values  : -1.0000000, -0.7984271 
max values  :  0.9751282,  0.9476519 
ndvi[["march"]]
class       : SpatRaster 
dimensions  : 469, 695, 1  (nrow, ncol, nlyr)
resolution  : 0.0004491576, 0.0004491576  (x, y)
extent      : 12.2338, 12.54596, 51.209, 51.41966  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
source      : ndvi-2024.tif 
name        :      march 
min value   : -0.6070393 
max value   :  0.8981348 

Extract one or more cell values

This is done as if the layers were matrices.

  • <raster>[i, j] select the value in row i and column j
ndvi[50, 50]
    january     march     april       may     june     july    august september
1 0.7144094 0.8246858 0.5798226 0.8630141 0.236645 0.517687 0.7169055 0.1734121
    october  december
1 0.7473408 0.8695881

Note

We will see a better way of doing this later

Combine rasters

This is done using the default R operator c().

  • c(<raster-a>, <raster-b>) combines the layers of the two rasters.
c(ndvi[[1]], ndvi[[3]])
class       : SpatRaster 
dimensions  : 469, 695, 2  (nrow, ncol, nlyr)
resolution  : 0.0004491576, 0.0004491576  (x, y)
extent      : 12.2338, 12.54596, 51.209, 51.41966  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
sources     : ndvi-2024.tif  
              ndvi-2024.tif  
names       :    january,      april 
min values  : -0.8566782, -1.0000000 
max values  :  0.8910936,  0.9751282 

Caution

Rasters without the same dimension, resolution, extent, or CRS cannot be combined.

c(landcover, ndvi)

Error: [rast] extents do not match

Reading and writing rasters

  1. To read a raster, use rast(<filename>).
  2. To write a raster, use writeRaster(<raster>, <filename>)
landcover <- rast("data/landcover-2015.tif")
writeRaster(landcover, "data/landcover-2015.tif", overwrite = TRUE)

Note

By default, overwrite = FALSE and writeRaster() will stop if the file already exists.

All you can eat

  1. With geometries and rasters you can achieve almost anything you need.
  2. In addition to the data contained in these objects, the metadata is important.

I want to plot the geometry of the forest on top of landcover

forest <- vect("data/auwald.shp")
plot(landcover, axes = FALSE, frame = FALSE)
lines(forest)

Where is the forest?

Metadata is not right

Tip

When something does not work, check the metadata.

crs(landcover, proj = TRUE)
[1] "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs"
crs(forest, proj = TRUE)
[1] "+proj=longlat +datum=WGS84 +no_defs"

The CRS does not match.

Fix the metadata

forest <- forest |> project(crs(landcover))
plot(landcover, axes = FALSE, frame = FALSE)
lines(forest, lw = 2)

What is this project()?

Gui will explain after the break.