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.
Caution💡 Tip
You can create the numbers using the sample() function.
Note🏋 Exercise 2
Now, use this matrix to create a raster layer and plot it.
Caution💡 Tip
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!
Caution💡 Tip
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.
Caution💡 Tip
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.
# Census 2022 on the number of familiesfamilies <- terra::rast("./data/census22_families.tif")# inspect metadatafamilies# plot the dataterra::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
Tip✅ Solution 4
# load all layerscitizens_cologne <- terra::rast("./data/citizens_cologne.tif")inhabitants_cologne <- terra::rast("./data/inhabitants_cologne.tif")# define missing valuescitizens_cologne[citizens_cologne ==-9] <-NAinhabitants_cologne[inhabitants_cologne ==-9] <-NA# create immigrants layerimmigrants_cologne <- inhabitants_cologne - citizens_cologne# inspect dataimmigrants_cologne# something's off - let's get rid of the wierd negative numbersimmigrants_cologne[immigrants_cologne <0] <-NA# create proportation layerimmigrants_proportion <- immigrants_cologne / inhabitants_cologne# set a proper name for our created layernames(immigrants_proportion) <-"immigrants_proportion"# plot itterra::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