Exercise 2_2: Basic Raster Operations

Introduction to Geospatial Techniques for Social Scientists in R

Author

Stefan Jünger, Anne-Kathrin Stroppe, Dennis Abel

Exercises

Note🏋️ Exercise 1

Create a matrix with 8 rows and 8 columns filled with random numbers between 1 and 1000.

You can create the numbers using the sample() function.

Note🏋 Exercise 2

Now, use this matrix to create a raster layer and plot it.

The terra::rast() function can be fed with matrices to create a raster layer.

The terra::rast() function can not only be used to create raster data on the fly, but it is also quite dull. Instead, we can use it to import already prepared data.

Note🏋 Exercise 3

Import one of the raster .tif files in the data folder of the workshop directory. Have a look at it!

Make sure your file paths are set correctly. You can check them with getwd(). Setting is done with setwd().

Note🏋️ Exercise 4

Import the raster data on inhabitants and citizens in Cologne. Create a new layer showing the proportion of immigrants (!) in each grid cell between 0 and 1.

You can handle raster layers as any simple data table using +, -, or / operators, e.g., to create a new layer that you might need. But make sure to define all missing values first.

Solutions

fancy_matrix <-
  sample(1:1000, 8*8, replace = TRUE) |> 
  matrix(nrow = 8, ncol = 8)

fancy_matrix
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]  272  237  798   73  378  566  565  549
[2,]  999   36   71  441  646  800  211  874
[3,]  248  852  339  852  491  256  424  339
[4,]  374  601   98  786  994   70  444  953
[5,]  795  614  212  696  277  572  612   66
[6,]  928  262  636  629  696  176  278  369
[7,]  485  370  961  271  983  285  771  523
[8,]   53  518  680  174  178  580  507  472
library(terra)
Warning: Paket 'terra' wurde unter R Version 4.4.3 erstellt
terra 1.9.11
fancy_raster_layer <- terra::rast(fancy_matrix)

terra::plot(fancy_raster_layer)

# Census 2022 on the number of families
families <- terra::rast("./data/census22_families.tif")

# inspect metadata
families

# plot the data
terra::plot(families)
class       : SpatRaster 
size        : 857, 641, 1  (nrow, ncol, nlyr)
resolution  : 1000, 1000  (x, y)
extent      : 4031000, 4672000, 2691000, 3548000  (xmin, xmax, ymin, ymax)
coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
source      : census22_families.tif 
name        : cat_0 
min value   :     3 
max value   :  4307 

# load all layers
citizens_cologne <- terra::rast("./data/citizens_cologne.tif")
inhabitants_cologne <- terra::rast("./data/inhabitants_cologne.tif")

# define missing values
citizens_cologne[citizens_cologne == -9] <- NA
inhabitants_cologne[inhabitants_cologne == -9] <- NA

# create immigrants layer
immigrants_cologne <- inhabitants_cologne - citizens_cologne

# inspect data
immigrants_cologne

# something's off - let's get rid of the wierd negative numbers
immigrants_cologne[immigrants_cologne < 0] <- NA

# create proportation layer
immigrants_proportion <- immigrants_cologne / inhabitants_cologne

# set a proper name for our created layer
names(immigrants_proportion) <- "immigrants_proportion"

# plot it
terra::plot(immigrants_proportion)
class       : SpatRaster 
size        : 290, 266, 1  (nrow, ncol, nlyr)
resolution  : 100, 100  (x, y)
extent      : 4094800, 4121400, 3083900, 3112900  (xmin, xmax, ymin, ymax)
coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
source(s)   : memory
varname     : inhabitants_cologne 
name        : cat_0 
min value   :    -5 
max value   :   516 
class       : SpatRaster 
size        : 290, 266, 1  (nrow, ncol, nlyr)
resolution  : 100, 100  (x, y)
extent      : 4094800, 4121400, 3083900, 3112900  (xmin, xmax, ymin, ymax)
coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035) 
source(s)   : memory
varname     : inhabitants_cologne 
name        : immigrants_proportion 
min value   :             0.0000000 
max value   :             0.9772727