Geocomputation with R
Robin Lovelace, Jakub Nowosad,Jannes Muenchow
1 Introduction 1
1.1 What is geocomputation? . . . . . . . . . . . . . . . . . . . 2
1.2 Why use R for geocomputation? . . . . . . . . . . . . . . . 4
1.3 Software for geocomputation . . . . . . . . . . . . . . . . . 6
1.4 R’s spatial ecosystem . . . . . . . . . . . . . . . . . . . . . . 8
1.5 The history of R-spatial . . . . . . . . . . . . . . . . . . . . 10
1.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
I Foundations 15
2 Geographic data in R 17
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2 Vector data . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2.1 An introduction to simple features . . . . . . . . . . 20
2.2.2 Why simple features? . . . . . . . . . . . . . . . . . . 24
2.2.3 Basic map making . . . . . . . . . . . . . . . . . . . 25
2.2.4 Base plot arguments . . . . . . . . . . . . . . . . . . 26
2.2.5 Geometry types . . . . . . . . . . . . . . . . . . . . . 28
2.2.6 Simple feature geometries (sfg) . . . . . . . . . . . . 29
2.2.7 Simple feature columns (sfc) . . . . . . . . . . . . . . 32
2.2.8 The sf class . . . . . . . . . . . . . . . . . . . . . . . 34
2.3 Raster data . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.3.1 An introduction to raster . . . . . . . . . . . . . . . . 36
2.3.2 Basic map making . . . . . . . . . . . . . . . . . . . . 37
2.3.3 Raster classes . . . . . . . . . . . . . . . . . . . . . . 38
2.4 Coordinate Reference Systems . . . . . . . . . . . . . . . . 40
2.4.1 Geographic coordinate systems . . . . . . . . . . . . . 41
2.4.2 Projected coordinate reference systems . . . . . . . . . 41
2.4.3 CRSs in R . . . . . . . . . . . . . . . . . . . . . . . . 42
2.5 Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3 Attribute data operations 47
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.2 Vector attribute manipulation . . . . . . . . . . . . . . . . . 48
3.2.1 Vector attribute subsetting . . . . . . . . . . . . . . . 50
3.2.2 Vector attribute aggregation . . . . . . . . . . . . . . 54
3.2.3 Vector attribute joining . . . . . . . . . . . . . . . . . 55
3.2.4 Creating attributes and removing spatial information 59
3.3 Manipulating raster objects . . . . . . . . . . . . . . . . . . 60
3.3.1 Raster subsetting . . . . . . . . . . . . . . . . . . . . 62
3.3.2 Summarizing raster objects . . . . . . . . . . . . . . 64
3.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4 Spatial data operations 67
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.2 Spatial operations on vector data . . . . . . . . . . . . . . . 68
4.2.1 Spatial subsetting . . . . . . . . . . . . . . . . . . . . 68
4.2.2 Topological relations . . . . . . . . . . . . . . . . . . . 71
4.2.3 Spatial joining . . . . . . . . . . . . . . . . . . . . . . 73
4.2.4 Non-overlapping joins . . . . . . . . . . . . . . . . . . 75
4.2.5 Spatial data aggregation . . . . . . . . . . . . . . . . . 77
4.2.6 Distance relations . . . . . . . . . . . . . . . . . . . . 80
4.3 Spatial operations on raster data . . . . . . . . . . . . . . . . 81
4.3.1 Spatial subsetting . . . . . . . . . . . . . . . . . . . . . 81
4.3.2 Map algebra . . . . . . . . . . . . . . . . . . . . . . . 83
4.3.3 Local operations . . . . . . . . . . . . . . . . . . . . . 84
4.3.4 Focal operations . . . . . . . . . . . . . . . . . . . . . 85
4.3.5 Zonal operations . . . . . . . . . . . . . . . . . . . . 86
4.3.6 Global operations and distances . . . . . . . . . . . . . 87
4.3.7 Merging rasters . . . . . . . . . . . . . . . . . . . . . 88
4.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
5 Geometry operations 91
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.2 Geometric operations on vector data . . . . . . . . . . . . . 92
5.2.1 Simplification . . . . . . . . . . . . . . . . . . . . . . 92
5.2.2 Centroids . . . . . . . . . . . . . . . . . . . . . . . . 94
5.2.3 Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . 96
5.2.4 Affine transformations . . . . . . . . . . . . . . . . . . 97
5.2.5 Clipping . . . . . . . . . . . . . . . . . . . . . . . . . 99
5.2.6 Geometry unions . . . . . . . . . . . . . . . . . . . . . 101
5.2.7 Type transformations . . . . . . . . . . . . . . . . . . 102
5.3 Geometric operations on raster data . . . . . . . . . . . . . 106
5.3.1 Geometric intersections . . . . . . . . . . . . . . . . . . 107
5.3.2 Extent and origin . . . . . . . . . . . . . . . . . . . . . 107
5.3.3 Aggregation and disaggregation . . . . . . . . . . . . 109
5.4 Raster-vector interactions . . . . . . . . . . . . . . . . . . . . 111
5.4.1 Raster cropping . . . . . . . . . . . . . . . . . . . . . 112
5.4.2 Raster extraction . . . . . . . . . . . . . . . . . . . . 113
5.4.3 Rasterization . . . . . . . . . . . . . . . . . . . . . . . 117
5.4.4 Spatial vectorization . . . . . . . . . . . . . . . . . . 120
5.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
6 Reprojecting geographic data 127
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.2 When to reproject? . . . . . . . . . . . . . . . . . . . . . . . 130
6.3 Which CRS to use? . . . . . . . . . . . . . . . . . . . . . . . 131
6.4 Reprojecting vector geometries . . . . . . . . . . . . . . . . 134
6.5 Modifying map projections . . . . . . . . . . . . . . . . . . 135
6.6 Reprojecting raster geometries . . . . . . . . . . . . . . . . 138
6.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
7 Geographic data I/O 143
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
7.2 Retrieving open data . . . . . . . . . . . . . . . . . . . . . . 144
7.3 Geographic data packages . . . . . . . . . . . . . . . . . . . 145
7.4 Geographic web services . . . . . . . . . . . . . . . . . . . . . 147
7.5 File formats . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
7.6 Data input (I) . . . . . . . . . . . . . . . . . . . . . . . . . . 151
7.6.1 Vector data . . . . . . . . . . . . . . . . . . . . . . . . 151
7.6.2 Raster data . . . . . . . . . . . . . . . . . . . . . . . 154
7.7 Data output (O) . . . . . . . . . . . . . . . . . . . . . . . . 154
7.7.1 Vector data . . . . . . . . . . . . . . . . . . . . . . . 154
7.7.2 Raster data . . . . . . . . . . . . . . . . . . . . . . . 156
7.8 Visual outputs . . . . . . . . . . . . . . . . . . . . . . . . . . 157
7.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
II Extensions 159
8 Making maps with R 161
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
8.2 Static maps . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
8.2.1 tmap basics . . . . . . . . . . . . . . . . . . . . . . . 163
8.2.2 Map objects . . . . . . . . . . . . . . . . . . . . . . . 165
8.2.3 Aesthetics . . . . . . . . . . . . . . . . . . . . . . . . . 167
8.2.4 Color settings . . . . . . . . . . . . . . . . . . . . . . 168
8.2.5 Layouts . . . . . . . . . . . . . . . . . . . . . . . . . 172
8.2.6 Faceted maps . . . . . . . . . . . . . . . . . . . . . . 175
8.2.7 Inset maps . . . . . . . . . . . . . . . . . . . . . . . . . 177
8.3 Animated maps . . . . . . . . . . . . . . . . . . . . . . . . . 179
8.4 Interactive maps . . . . . . . . . . . . . . . . . . . . . . . . . 181
8.5 Mapping applications . . . . . . . . . . . . . . . . . . . . . 188
8.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
9 Bridges to GIS software 199
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
9.2 (R)QGIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
9.3 (R)SAGA . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
9.4 GRASS through rgrass7 . . . . . . . . . . . . . . . . . . . 209
9.5 When to use what? . . . . . . . . . . . . . . . . . . . . . . . 214
9.6 Other bridges . . . . . . . . . . . . . . . . . . . . . . . . . . 215
9.6.1 Bridges to GDAL . . . . . . . . . . . . . . . . . . . . 215
9.6.2 Bridges to spatial databases . . . . . . . . . . . . . . . 217
9.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
10 Scripts, algorithms and functions 221
10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
10.2 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
10.3 Geometric algorithms . . . . . . . . . . . . . . . . . . . . . 224
10.4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
10.5 Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 232
10.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
11 Statistical learning 235
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
11.2 Case study: Landslide susceptibility . . . . . . . . . . . . . . 237
11.3 Conventional modeling approach in R . . . . . . . . . . . . 239
11.4 Introduction to (spatial) cross-validation . . . . . . . . . . . 242
11.5 Spatial CV with mlr . . . . . . . . . . . . . . . . . . . . . . 243
11.5.1 Generalized linear model . . . . . . . . . . . . . . . . 244
11.5.2 Spatial tuning of machine-learning hyperparameters . . 247
11.6 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
11.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
III Applications 257
12 Transportation 259
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
12.2 A case study of Bristol . . . . . . . . . . . . . . . . . . . . . . 261
12.3 Transport zones . . . . . . . . . . . . . . . . . . . . . . . . . 263
12.4 Desire lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
12.5 Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
12.6 Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
12.7 Route networks . . . . . . . . . . . . . . . . . . . . . . . . . 274
12.8 Prioritizing new infrastructure . . . . . . . . . . . . . . . . 275
12.9 Future directions of travel . . . . . . . . . . . . . . . . . . . . 277
12.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
13 Geomarketing 281
13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
13.2 Case study: bike shops in Germany . . . . . . . . . . . . . . 282
13.3 Tidy the input data . . . . . . . . . . . . . . . . . . . . . . 283
13.4 Create census rasters . . . . . . . . . . . . . . . . . . . . . . 283
13.5 Define metropolitan areas . . . . . . . . . . . . . . . . . . . 286
13.6 Points of interest . . . . . . . . . . . . . . . . . . . . . . . . 289
13.7 Identifying suitable locations . . . . . . . . . . . . . . . . . . 291
13.8 Discussion and next steps . . . . . . . . . . . . . . . . . . . 293
13.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
14 Ecology 295
14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
14.2 Data and data preparation . . . . . . . . . . . . . . . . . . . 297
14.3 Reducing dimensionality . . . . . . . . . . . . . . . . . . . . 300
14.4 Modeling the floristic gradient . . . . . . . . . . . . . . . . 303
14.4.1 mlr building blocks . . . . . . . . . . . . . . . . . . . 305
14.4.2 Predictive mapping . . . . . . . . . . . . . . . . . . . . 307
14.5 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
14.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
15 Conclusion 313
15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
15.2 Package choice . . . . . . . . . . . . . . . . . . . . . . . . . 314
15.3 Gaps and overlaps . . . . . . . . . . . . . . . . . . . . . . . 316
15.4 Where to go next? . . . . . . . . . . . . . . . . . . . . . . . . 317
15.5 The open source approach . . . . . . . . . . . . . . . . . . . 319