class: left, bottom, title-slide .title[ # Spatial Data Wrangling with R ] .subtitle[ ## A Comprehensive Guide ] .author[ ### Long Nguyen ] .date[ ### Workshops for Ukraine · 2023-04-06 ] ---
<style type="text/css"> pre { overflow: auto; } </style> ### Link to slides - <https://lo-ng.netlify.app/slides/2023-04-06-ua-workshops-spatial.html> --- ## Today's objectives - Systematic, fundamental overview of spatial data manipulation techniques -- - Focus on operations on vector data geometries, implemented by the [{sf}](https://r-spatial.github.io/sf/) package -- | | Predicates<br>(return boolean) | Measures<br>(return number) | Transformers<br>(return geometry) | |---|:---:|:---:|:---:| | **Unary** | | x | x | | **Binary** | x | x | x | | **N-ary** | | | x | -- - Raster data operations to complement vector data attributes -- - Leans heavily on the philosophy of {dplyr} and {ggplot2} --- ### Non-goals - Different spatial data file formats - See workshop [Introduction to Spatial Analysis in R](https://sites.google.com/view/dariia-mykhailyshyna/main/r-workshops-for-ukraine#h.kyiiviouv9c3) by David Zuchowski -- - Modelling and inferential analysis - See workshop [Fundamentals of Exploratory and Inferential Spatial Data Analysis in R](https://sites.google.com/view/dariia-mykhailyshyna/main/r-workshops-for-ukraine#h.yzfjaoh7h9) by Denys Dukhovnov -- - How to make (beautiful) maps - We'll learn just enough to visualise the results of the operations performed on the data --- ### Topics I'll be hand-wavy about - Data import/export - Import: `sf::read_sf()` for vector data, `terra::rast()` for raster data - Export: `sf::write_sf()`, `terra::writeRaster()` -- - Coordinate reference systems (CRSs) - Important! - But default _just works_ most of the time (esp. since {sf} 1.0.0) - Further reading: [Chapter 7](https://r.geocompx.org/reproj-geo-data.html) of _Geocomputation with R_ --- class: inverse center middle ## Motivating Example: Non-Contiguous Cartogram of 2020 US Election --- count: false .panel1-ncont-vote-non_seq[ ```r library(sf) library(tidyverse) us_states <- read_sf("https://lo-ng.netlify.app/slides/data/us-states-albers.geojson") us_states ``` ] .panel2-ncont-vote-non_seq[ ``` Simple feature collection with 51 features and 2 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: -2036903 ymin: -2465746 xmax: 2521918 ymax: 731860.1 Projected CRS: NAD27 / US National Atlas Equal Area # A tibble: 51 × 3 fips_state name geometry <chr> <chr> <MULTIPOLYGON [m]> 1 04 Arizona (((-805018.1 -844577, -820986 -1004768, -847… 2 05 Arkansas (((558984.4 -1308375, 555289.4 -1249633, 542… 3 06 California (((-1857115 -997259, -1840861 -998344.4, -18… 4 08 Colorado (((-340987 -435780.7, -241136.9 -439894.2, -… 5 09 Connecticut (((2279630 65808.41, 2301408 2913.019, 23000… 6 11 District of Columbia (((1955464 -402054, 1960244 -393610.9, 19740… 7 13 Georgia (((1426586 -978019.1, 1484709 -967004.7, 153… 8 17 Illinois (((998533.7 -204278.7, 998810.4 -225676.6, 1… 9 18 Indiana (((1305615 -535053.6, 1300530 -540420.3, 131… 10 22 Louisiana (((558984.4 -1308375, 729349.1 -1295816, 828… # ℹ 41 more rows ``` ] --- count: false .panel1-ncont-vote-non_seq[ ```r library(sf) library(tidyverse) us_states <- read_sf("https://lo-ng.netlify.app/slides/data/us-states-albers.geojson") *us_votes <- read_rds("https://lo-ng.netlify.app/slides/data/us_votes.rds") us_states |> * left_join(us_votes) |> * select(called, pop18) ``` ] .panel2-ncont-vote-non_seq[ ``` Simple feature collection with 51 features and 2 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: -2036903 ymin: -2465746 xmax: 2521918 ymax: 731860.1 Projected CRS: NAD27 / US National Atlas Equal Area # A tibble: 51 × 3 called pop18 geometry <chr> <dbl> <MULTIPOLYGON [m]> 1 D 7171646 (((-805018.1 -844577, -820986 -1004768, -847281.8 -1277600, … 2 R 3013825 (((558984.4 -1308375, 555289.4 -1249633, 542141.8 -1246005, … 3 D 39557045 (((-1857115 -997259, -1840861 -998344.4, -1833809 -1009424, … 4 D 5695564 (((-340987 -435780.7, -241136.9 -439894.2, -172639.7 -441887… 5 D 3572665 (((2279630 65808.41, 2301408 2913.019, 2300040 -8879.752, 22… 6 D 702455 (((1955464 -402054, 1960244 -393610.9, 1974075 -401534.9, 19… 7 D 10519475 (((1426586 -978019.1, 1484709 -967004.7, 1534994 -955419.9, … 8 D 12741080 (((998533.7 -204278.7, 998810.4 -225676.6, 1014867 -248685, … 9 R 6691878 (((1305615 -535053.6, 1300530 -540420.3, 1313039 -560392.6, … 10 R 4659978 (((558984.4 -1308375, 729349.1 -1295816, 828091.2 -1286761, … # ℹ 41 more rows ``` ] --- count: false .panel1-ncont-vote-non_seq[ ```r library(sf) library(tidyverse) us_states <- read_sf("https://lo-ng.netlify.app/slides/data/us-states-albers.geojson") us_votes <- read_rds("https://lo-ng.netlify.app/slides/data/us_votes.rds") us_states |> left_join(us_votes) |> select(called, pop18) |> * ggplot(aes(fill = called)) + * geom_sf(show.legend = FALSE) + * scale_fill_manual(values = c("#25428f", "#c70000")) + * coord_sf(xlim = st_bbox(us_states)[c(1, 3)], ylim = st_bbox(us_states)[c(2, 4)]) + * theme_void() ``` ] .panel2-ncont-vote-non_seq[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/ncont-vote_non_seq_03_output-1.png" width="100%" style="display: block; margin: auto;" /> ] --- count: false .panel1-ncont-vote-non_seq[ ```r library(sf) library(tidyverse) us_states <- read_sf("https://lo-ng.netlify.app/slides/data/us-states-albers.geojson") us_votes <- read_rds("https://lo-ng.netlify.app/slides/data/us_votes.rds") us_states |> left_join(us_votes) |> select(called, pop18) |> * mutate( * area = as.numeric(st_area(geometry)), * scaled_area = pop18 / max(pop18) * * area[which.max(pop18)], * scale = sqrt(scaled_area / area), * cent = st_centroid(geometry), * geometry = (geometry - cent) * scale + cent * ) |> ggplot(aes(fill = called)) + geom_sf(show.legend = FALSE) + scale_fill_manual(values = c("#25428f", "#c70000")) + coord_sf(xlim = st_bbox(us_states)[c(1, 3)], ylim = st_bbox(us_states)[c(2, 4)]) + theme_void() ``` ] .panel2-ncont-vote-non_seq[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/ncont-vote_non_seq_04_output-1.png" width="100%" style="display: block; margin: auto;" /> ] <style> .panel1-ncont-vote-non_seq { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ncont-vote-non_seq { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ncont-vote-non_seq { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse center middle ## Spatial Vector Data in R --- ### Points, lines, and polygons <img src="2023-04-06-ua-workshops-spatial_files/figure-html/vector-types-1.png" width="64%" style="display: block; margin: auto;" /> --- ### Simple Features - Standard for representing spatial vector data -- - Simple Feature **geometries**: ``` POINT (2 2) ``` ``` MULTIPOINT ((0 1), (2 2), (4 1), (2 3), (1 4)) ``` ``` LINESTRING (1 1, 5 5, 5 6, 4 6, 3 4, 2 3) ``` ``` MULTILINESTRING ((1 1, 5 5, 5 6, 4 6, 3 4, 2 3), (3 0, 4 1, 2 1)) ``` ``` POLYGON ((2 1, 3 1, 5 2, 6 3, 5 3, 4 4, 3 4, 1 3, 2 1), (2 2, 3 3, 4 3, 4 2, 2 2)) ``` ``` MULTIPOLYGON (((2 1, 3 1, 5 2, 6 3, 5 3, 4 4, 3 4, 1 3, 2 1), (2 2, 3 3, 4 3, 4 2, 2 2)), ((3 5, 4 5, 5 6, 3 7, 2 6, 3 5))) ``` - _Simple_: linestrings and polygons are built from points connected by straight line segments -- - Features can have other non-geographic properties: **attributes** --- ### Package {sf} - Provides support for Simple Features in R - Represents Simple Features as records in a data frame: .pull-left.w52[ ```r us_states ``` .small-code[ ``` Simple feature collection with 51 features and 2 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: -2036903 ymin: -2465746 xmax: 2521918 ymax: 731860.1 Projected CRS: NAD27 / US National Atlas Equal Area # A tibble: 51 × 3 fips_state name geometry <chr> <chr> <MULTIPOLYGON [m]> 1 04 Arizona (((-805018.1 -844577, -8209… 2 05 Arkansas (((558984.4 -1308375, 55528… 3 06 California (((-1857115 -997259, -18408… 4 08 Colorado (((-340987 -435780.7, -2411… # ℹ 47 more rows ``` ] ] .pull-right.w44[ <br><br><br> - `sf` object: data frame with spatial geometries - Every row is a single Simple Feature consisting of attributes and geometry - `geometry`: list column that contains geometries (class `sfc`) ] --- ### Package {sf} - Implements operations on Simple Feature geometries - `st_*()` functions that work on `sf` and `sfc` objects, following [PostGIS](https://postgis.net) convention - **ST**: "spacetime" ([Pebesma 2018](https://journal.r-project.org/archive/2018/RJ-2018-009/RJ-2018-009.pdf))/"spatial type" ([PostGIS Manual](https://postgis.net/docs/manual-dev/reference.html)) --- class: inverse center middle ## Operations on Attributes --- ### Subsetting ```r (de_nuts3 <- read_sf("https://lo-ng.netlify.app/slides/data/de-nuts3.geojson")) ``` ``` Simple feature collection with 401 features and 2 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: 4031952 ymin: 2684075 xmax: 4671180 ymax: 3543086 Projected CRS: ETRS89-extended / LAEA Europe # A tibble: 401 × 3 nuts name geometry <chr> <chr> <MULTIPOLYGON [m]> 1 DE254 Nürnberg, Kreisfreie Stadt (((4395582 2936363, 4406401 2929710, 44059… 2 DE255 Schwabach, Kreisfreie Stadt (((4399488 2914611, 4399340 2912229, 43949… 3 DE256 Ansbach, Landkreis (((4384059 2916281, 4386305 2905631, 43820… 4 DE257 Erlangen-Höchstadt (((4387813 2962179, 4400140 2944483, 44135… 5 DE234 Amberg-Sulzbach (((4461918 2902744, 4434038 2924102, 44360… 6 DE235 Cham (((4552189 2900592, 4548518 2895155, 45230… 7 DE236 Neumarkt i. d. OPf. (((4461918 2902744, 4462116 2899857, 44516… 8 DE237 Neustadt a. d. Waldnaab (((4508712 2939911, 4474093 2939442, 44601… 9 DE238 Regensburg, Landkreis (((4502290 2882921, 4497804 2865770, 44780… 10 DE239 Schwandorf (((4508712 2939911, 4511976 2937497, 45118… # ℹ 391 more rows ``` --- ### Subsetting ```r de_nuts3 |> * filter(startsWith(name, "A")) ``` ``` Simple feature collection with 19 features and 2 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: 4091473 ymin: 2775241 xmax: 4539907 ymax: 3403586 Projected CRS: ETRS89-extended / LAEA Europe # A tibble: 19 × 3 nuts name geometry * <chr> <chr> <MULTIPOLYGON [m]> 1 DE256 Ansbach, Landkreis (((4384059 2916281, 4386305 2905631, 4… 2 DE234 Amberg-Sulzbach (((4461918 2902744, 4434038 2924102, 4… 3 DE251 Ansbach, Kreisfreie Stadt (((4356772 2910520, 4363699 2914647, 4… 4 DE261 Aschaffenburg, Kreisfreie Stadt (((4266274 2980543, 4256895 2980211, 4… 5 DE264 Aschaffenburg, Landkreis (((4278414 2997441, 4279506 2974455, 4… 6 DE271 Augsburg, Kreisfreie Stadt (((4388654 2794282, 4381950 2800775, 4… 7 DE145 Alb-Donau-Kreis (((4338058 2821924, 4330908 2815722, 4… 8 DE214 Altötting (((4539907 2792493, 4525936 2781512, 4… 9 DE231 Amberg, Kreisfreie Stadt (((4449020 2927007, 4457142 2934255, 4… 10 DEG0M Altenburger Land (((4504767 3099894, 4507488 3093634, 4… 11 DEB12 Ahrweiler (((4132529 3043937, 4106413 3030730, 4… 12 DEB13 Altenkirchen (Westerwald) (((4169961 3092710, 4182511 3066932, 4… 13 DEB3B Alzey-Worms (((4205818 2966931, 4209134 2959072, 4… 14 DEE04 Altmarkkreis Salzwedel (((4428877 3262216, 4420644 3255037, 4… 15 DEE05 Anhalt-Bitterfeld (((4483990 3217678, 4477747 3209147, 4… 16 DE275 Aichach-Friedberg (((4416814 2818744, 4418060 2816287, 4… 17 DE276 Augsburg, Landkreis (((4389187 2830822, 4386413 2816502, 4… 18 DE946 Ammerland (((4208210 3345003, 4201080 3344123, 4… 19 DE947 Aurich (((4141718 3363568, 4136206 3369633, 4… ``` --- ### Joining .pull-left[ ```r de_nuts3_pop <- de_nuts3 |> left_join( # Downloaded from inkar.de read_csv("https://lo-ng.netlify.app/slides/data/de_pop20.csv"), by = join_by(nuts == nuts) ) ``` .small-code[ ``` Simple feature collection with 401 features and 3 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: 4031952 ymin: 2684075 xmax: 4671180 ymax: 3543086 Projected CRS: ETRS89-extended / LAEA Europe # A tibble: 401 × 4 nuts name geometry pop20 <chr> <chr> <MULTIPOLYGON [m]> <dbl> 1 DE254 Nürnbe… (((4395582 2936363, 4406… 515543 2 DE255 Schwab… (((4399488 2914611, 4399… 41056 3 DE256 Ansbac… (((4384059 2916281, 4386… 185316 4 DE257 Erlang… (((4387813 2962179, 4400… 138105 5 DE234 Amberg… (((4461918 2902744, 4434… 102998 6 DE235 Cham (((4552189 2900592, 4548… 128094 # ℹ 395 more rows ``` ] ] -- .pull-right[ - New since {dplyr} 1.1.0: `by = join_by(...)` Old syntax: `by = c("nuts" = "nuts")` Shortcut: `by = join_by(nuts)` (old syntax: `by = "nuts"`) - Allows for other types of joins than equality joins (`==`) ] --- ### Aggregation ```r de_nuts1_pop <- de_nuts3_pop |> mutate(nuts = str_sub(nuts, 1, 3)) |> group_by(nuts) |> summarise(pop = sum(pop20)) ``` .small-code[ ``` Simple feature collection with 16 features and 2 fields Geometry type: GEOMETRY Dimension: XY Bounding box: xmin: 4031952 ymin: 2684075 xmax: 4671180 ymax: 3543086 Projected CRS: ETRS89-extended / LAEA Europe # A tibble: 16 × 3 nuts pop geometry <chr> <dbl> <GEOMETRY [m]> 1 DE1 11103043 POLYGON ((4162515 2721450, 4148793 2716636, 4142867 2719236, 4… 2 DE2 13140183 POLYGON ((4287757 2714352, 4297839 2721916, 4325984 2728191, 4… 3 DE3 3664088 MULTIPOLYGON (((4565855 3276636, 4570054 3269038, 4574088 3265… 4 DE4 2531071 POLYGON ((4556978 3153409, 4544296 3148678, 4541206 3166243, 4… # ℹ 12 more rows ``` ] -- - {sf} silently creates a union out of geometries in the same group --- class: inverse center middle ## Operations on Simple Feature Geometries | | Predicates | Measures | Transformers | |---|:---:|:---:|:---:| | **Unary** | | x | x | | **Binary** | x | x | x | | **N-ary** | | | x | --- class: inverse center middle ### Unary measures --- - `st_length()`: length of a LINESTRING or MULTILINESTRING geometry -- - `st_area()`: area of a geometry .pull-left.w40[ ```r us_states |> * mutate(area = st_area(geometry)) ``` ] .pull-right.w56.small-code[ ``` Simple feature collection with 51 features and 3 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: -2036903 ymin: -2465746 xmax: 2521918 ymax: 731860.1 Projected CRS: NAD27 / US National Atlas Equal Area # A tibble: 51 × 4 fips_state name geometry area * <chr> <chr> <MULTIPOLYGON [m]> [m^2] 1 04 Arizona (((-805018.1 -844577, -8… 2.95e11 2 05 Arkansas (((558984.4 -1308375, 55… 1.37e11 3 06 California (((-1857115 -997259, -18… 4.09e11 4 08 Colorado (((-340987 -435780.7, -2… 2.70e11 5 09 Connecticut (((2279630 65808.41, 230… 1.30e10 6 11 District of… (((1955464 -402054, 1960… 2.06e 8 7 13 Georgia (((1426586 -978019.1, 14… 1.53e11 8 17 Illinois (((998533.7 -204278.7, 9… 1.46e11 9 18 Indiana (((1305615 -535053.6, 13… 9.36e10 10 22 Louisiana (((558984.4 -1308375, 72… 1.22e11 # ℹ 41 more rows ``` ] --- class: inverse center middle ### Unary transformers --- count: false #### st_centroid() .panel1-us-centroids-non_seq[ ```r us_states |> ggplot() + geom_sf() + theme_void() ``` ] .panel2-us-centroids-non_seq[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-centroids_non_seq_01_output-1.png" width="100%" style="display: block; margin: auto;" /> ] --- count: false #### st_centroid() .panel1-us-centroids-non_seq[ ```r *us_centroids <- us_states |> * st_centroid() *us_centroids |> ggplot() + geom_sf() + theme_void() ``` ] .panel2-us-centroids-non_seq[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-centroids_non_seq_02_output-1.png" width="100%" style="display: block; margin: auto;" /> ] <style> .panel1-us-centroids-non_seq { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-us-centroids-non_seq { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-us-centroids-non_seq { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false #### st_point_on_surface() .panel1-bremen-centroid-user[ ```r *bremen <- de_nuts1_pop |> * filter(nuts == "DE5") *bremen |> * ggplot() + * geom_sf() + * coord_sf( * xlim = st_bbox(bremen)[c(1, 3)], * ylim = st_bbox(bremen)[c(2, 4)] * ) + * theme_void() ``` ] .panel2-bremen-centroid-user[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/bremen-centroid_user_01_output-1.png" width="100%" style="display: block; margin: auto;" /> ] <style> .panel1-bremen-centroid-user { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-bremen-centroid-user { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-bremen-centroid-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false #### st_point_on_surface() .panel1-bremen-centroid-rotate[ ```r bremen <- de_nuts1_pop |> filter(nuts == "DE5") bremen |> * st_centroid() |> ggplot() + geom_sf() + coord_sf( xlim = st_bbox(bremen)[c(1, 3)], ylim = st_bbox(bremen)[c(2, 4)] ) + theme_void() ``` ] .panel2-bremen-centroid-rotate[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/bremen-centroid_rotate_01_output-1.png" width="100%" style="display: block; margin: auto;" /> ] --- count: false #### st_point_on_surface() .panel1-bremen-centroid-rotate[ ```r bremen <- de_nuts1_pop |> filter(nuts == "DE5") bremen |> * st_point_on_surface() |> ggplot() + geom_sf() + coord_sf( xlim = st_bbox(bremen)[c(1, 3)], ylim = st_bbox(bremen)[c(2, 4)] ) + theme_void() ``` ] .panel2-bremen-centroid-rotate[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/bremen-centroid_rotate_02_output-1.png" width="100%" style="display: block; margin: auto;" /> ] <style> .panel1-bremen-centroid-rotate { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-bremen-centroid-rotate { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-bremen-centroid-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false #### st_buffer() .panel1-us-buffers-rotate[ ```r *us_centroids |> ggplot() + geom_sf() + theme_void() ``` ] .panel2-us-buffers-rotate[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-buffers_rotate_01_output-1.png" width="100%" style="display: block; margin: auto;" /> ] --- count: false #### st_buffer() .panel1-us-buffers-rotate[ ```r *st_buffer(us_centroids, 50000) |> ggplot() + geom_sf() + theme_void() ``` ] .panel2-us-buffers-rotate[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-buffers_rotate_02_output-1.png" width="100%" style="display: block; margin: auto;" /> ] --- count: false #### st_buffer() .panel1-us-buffers-rotate[ ```r *st_buffer(us_centroids, 100000) |> ggplot() + geom_sf() + theme_void() ``` ] .panel2-us-buffers-rotate[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-buffers_rotate_03_output-1.png" width="100%" style="display: block; margin: auto;" /> ] --- count: false #### st_buffer() .panel1-us-buffers-rotate[ ```r *st_buffer(us_centroids, 200000) |> ggplot() + geom_sf() + theme_void() ``` ] .panel2-us-buffers-rotate[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-buffers_rotate_04_output-1.png" width="100%" style="display: block; margin: auto;" /> ] <style> .panel1-us-buffers-rotate { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-us-buffers-rotate { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-us-buffers-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- #### Affine transformations Linear transformations that preserve lines and parallelism, but may change the orientation, scale, and position of objects ```r us_states |> # ggplot() + geom_sf() + coord_sf(xlim = st_bbox(us_states)[c(1, 3)], ylim = st_bbox(us_states)[c(2, 4)]) + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-affine0-out-1.png" width="40%" style="display: block; margin: auto;" /> --- #### Affine transformations Linear transformations that preserve lines and parallelism, but may change the orientation, scale, and position of objects ```r us_states |> * mutate(geometry = geometry + c(-100000, 100000)) |> ggplot() + geom_sf() + coord_sf(xlim = st_bbox(us_states)[c(1, 3)], ylim = st_bbox(us_states)[c(2, 4)]) + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-affine1-out-1.png" width="40%" style="display: block; margin: auto;" /> --- #### Affine transformations Linear transformations that preserve lines and parallelism, but may change the orientation, scale, and position of objects ```r us_states |> * mutate(geometry = geometry * 0.5) |> ggplot() + geom_sf() + coord_sf(xlim = st_bbox(us_states)[c(1, 3)], ylim = st_bbox(us_states)[c(2, 4)]) + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-affine2-out-1.png" width="40%" style="display: block; margin: auto;" /> --- #### Affine transformations Linear transformations that preserve lines and parallelism, but may change the orientation, scale, and position of objects ```r us_states |> * mutate(geometry = geometry - us_centroids$geometry) |> ggplot() + geom_sf() + coord_sf(xlim = st_bbox(us_states)[c(1, 3)], ylim = st_bbox(us_states)[c(2, 4)]) + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-affine3-out-1.png" width="40%" style="display: block; margin: auto;" /> --- #### Affine transformations Linear transformations that preserve lines and parallelism, but may change the orientation, scale, and position of objects ```r us_states |> * mutate(geometry = (geometry - us_centroids$geometry) * 0.5) |> ggplot() + geom_sf() + coord_sf(xlim = st_bbox(us_states)[c(1, 3)], ylim = st_bbox(us_states)[c(2, 4)]) + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-affine4-out-1.png" width="40%" style="display: block; margin: auto;" /> --- #### Affine transformations Linear transformations that preserve lines and parallelism, but may change the orientation, scale, and position of objects ```r us_states |> * mutate(geometry = (geometry - us_centroids$geometry) * 0.5 + us_centroids$geometry) |> ggplot() + geom_sf() + coord_sf(xlim = st_bbox(us_states)[c(1, 3)], ylim = st_bbox(us_states)[c(2, 4)]) + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/us-affine5-out-1.png" width="40%" style="display: block; margin: auto;" /> --- count: false #### st_cast(): Type transformations .panel1-us-cast-non_seq[ ```r us_states |> select(name) |> print(width = 48) ``` ] .panel2-us-cast-non_seq[ ``` Simple feature collection with 51 features and 1 field Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: -2036903 ymin: -2465746 xmax: 2521918 ymax: 731860.1 Projected CRS: NAD27 / US National Atlas Equal Area # A tibble: 51 × 2 name geometry <chr> <MULTIPOLYGON [m]> 1 Arizona (((-805018.1 -844577, -8… 2 Arkansas (((558984.4 -1308375, 55… 3 California (((-1857115 -997259, -18… 4 Colorado (((-340987 -435780.7, -2… 5 Connecticut (((2279630 65808.41, 230… 6 District of Columb… (((1955464 -402054, 1960… 7 Georgia (((1426586 -978019.1, 14… 8 Illinois (((998533.7 -204278.7, 9… 9 Indiana (((1305615 -535053.6, 13… 10 Louisiana (((558984.4 -1308375, 72… # ℹ 41 more rows ``` ] --- count: false #### st_cast(): Type transformations .panel1-us-cast-non_seq[ ```r us_states |> select(name) |> * st_cast("MULTILINESTRING") |> print(width = 48) ``` ] .panel2-us-cast-non_seq[ ``` Simple feature collection with 51 features and 1 field Geometry type: MULTILINESTRING Dimension: XY Bounding box: xmin: -2036903 ymin: -2465746 xmax: 2521918 ymax: 731860.1 Projected CRS: NAD27 / US National Atlas Equal Area # A tibble: 51 × 2 name geometry <chr> <MULTILINESTRING [m]> 1 Arizona ((-805018.1 -844577, -82… 2 Arkansas ((558984.4 -1308375, 555… 3 California ((-1857115 -997259, -184… 4 Colorado ((-340987 -435780.7, -24… 5 Connecticut ((2279630 65808.41, 2301… 6 District of Columb… ((1955464 -402054, 19602… 7 Georgia ((1426586 -978019.1, 148… 8 Illinois ((998533.7 -204278.7, 99… 9 Indiana ((1305615 -535053.6, 130… 10 Louisiana ((558984.4 -1308375, 729… # ℹ 41 more rows ``` ] --- count: false #### st_cast(): Type transformations .panel1-us-cast-non_seq[ ```r us_states |> select(name) |> st_cast("MULTILINESTRING") |> * mutate( * border_len = st_length(geometry) * ) |> * arrange(desc(border_len)) |> print(width = 48) ``` ] .panel2-us-cast-non_seq[ ``` Simple feature collection with 51 features and 2 fields Geometry type: MULTILINESTRING Dimension: XY Bounding box: xmin: -2036903 ymin: -2465746 xmax: 2521918 ymax: 731860.1 Projected CRS: NAD27 / US National Atlas Equal Area # A tibble: 51 × 3 name geometry border_len <chr> <MULTILINESTRING [m]> [m] 1 Alaska ((-1516285 -2108476, -15… 6138253. 2 Texas ((513575.4 -1242958, 526… 4735889. 3 Califor… ((-1857115 -997259, -184… 3606845. 4 Michigan ((1115493 184361.4, 1124… 3499197. 5 Montana ((-875690.5 1111.16, -88… 2825510. 6 Florida ((1876927 -2050256, 1894… 2776267. 7 Idaho ((-875690.5 1111.16, -91… 2566386. 8 Minneso… ((708640.3 -128513.3, 58… 2534621. 9 New Mex… ((-621546.9 -1440456, -7… 2374297. 10 Virginia ((2138105 -453486.6, 213… 2343343. # ℹ 41 more rows ``` ] <style> .panel1-us-cast-non_seq { color: black; width: 43.5555555555556%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-us-cast-non_seq { color: black; width: 54.4444444444444%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-us-cast-non_seq { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse center middle ### Binary predicates (and spatial joins) --- #### Topological relations: spatial relationships between two objects .center[st_*(<span style="font-weight:bold;color:#C58A71">x</span>, <span style="font-weight:bold;color:#8990CC">y</span>) → boolean] <img src="https://r.geocompx.org/04-spatial-operations_files/figure-html/relations-1.png" width="64%" style="display: block; margin: auto;" /> .center.smaller[([_Geocomputation with R_, Chapter 4](https://r.geocompx.org/spatial-operations.html#topological-relations))] --- #### Spatial joins .pull-left.w60[ ```r bavaria <- de_nuts1_pop |> filter(nuts == "DE2") st_join( de_nuts3, bavaria, join = st_intersects, ) |> ggplot() + geom_sf() + geom_sf(data = bavaria, linewidth = 1, fill = NA) + coord_sf(xlim = st_bbox(de_nuts3)[c(1, 3)], ylim = st_bbox(de_nuts3)[c(2, 4)]) + theme_void() ``` Nothing happened 🤔 ] .pull-right.w36[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/sp-joins-out-1.png" width="100%" style="display: block; margin: auto;" /> ] --- #### Spatial joins .pull-left.w60[ ```r bavaria <- de_nuts1_pop |> filter(nuts == "DE2") st_join( de_nuts3, bavaria, join = st_intersects, * left = FALSE # Performs inner join ) |> ggplot() + geom_sf() + geom_sf(data = bavaria, linewidth = 1, fill = NA) + coord_sf(xlim = st_bbox(de_nuts3)[c(1, 3)], ylim = st_bbox(de_nuts3)[c(2, 4)]) + theme_void() ``` ] .pull-right.w36[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/sp-joins0-out-1.png" width="100%" style="display: block; margin: auto;" /> ] --- #### Spatial joins .pull-left.w60[ ```r bavaria <- de_nuts1_pop |> filter(nuts == "DE2") st_join( de_nuts3, bavaria, * join = st_within, left = FALSE # Performs inner join ) |> ggplot() + geom_sf() + geom_sf(data = bavaria, linewidth = 1, fill = NA) + coord_sf(xlim = st_bbox(de_nuts3)[c(1, 3)], ylim = st_bbox(de_nuts3)[c(2, 4)]) + theme_void() ``` ] .pull-right.w36[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/sp-joins1-out-1.png" width="100%" style="display: block; margin: auto;" /> ] --- #### Spatial joins .pull-left.w60[ ```r bavaria <- de_nuts1_pop |> filter(nuts == "DE2") st_join( de_nuts3, bavaria, * join = st_touches, left = FALSE # Performs inner join ) |> ggplot() + geom_sf() + geom_sf(data = bavaria, linewidth = 1, fill = NA) + coord_sf(xlim = st_bbox(de_nuts3)[c(1, 3)], ylim = st_bbox(de_nuts3)[c(2, 4)]) + theme_void() ``` ] .pull-right.w36[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/sp-joins2-out-1.png" width="100%" style="display: block; margin: auto;" /> ] --- #### Spatial joins .pull-left.w60[ ```r bavaria <- de_nuts1_pop |> filter(nuts == "DE2") st_join( de_nuts3, bavaria, * join = st_disjoint, left = FALSE # Performs inner join ) |> ggplot() + geom_sf() + geom_sf(data = bavaria, linewidth = 1, fill = NA) + coord_sf(xlim = st_bbox(de_nuts3)[c(1, 3)], ylim = st_bbox(de_nuts3)[c(2, 4)]) + theme_void() ``` ] .pull-right.w36[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/sp-joins3-out-1.png" width="100%" style="display: block; margin: auto;" /> ] --- #### Spatial joins .pull-left.w60[ ```r bavaria <- de_nuts1_pop |> filter(nuts == "DE2") st_join( de_nuts3, bavaria, * join = st_is_within_distance, dist = 100000, left = FALSE # Performs inner join ) |> ggplot() + geom_sf() + geom_sf(data = bavaria, linewidth = 1, fill = NA) + coord_sf(xlim = st_bbox(de_nuts3)[c(1, 3)], ylim = st_bbox(de_nuts3)[c(2, 4)]) + theme_void() ``` ] .pull-right.w36[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/sp-joins4-out-1.png" width="100%" style="display: block; margin: auto;" /> ] --- #### Spatial joins .pull-left.w60[ ```r bavaria <- de_nuts1_pop |> filter(nuts == "DE2") st_join( * st_centroid(de_nuts3), bavaria, join = st_is_within_distance, dist = 100000, left = FALSE # Performs inner join ) |> ggplot() + geom_sf() + geom_sf(data = bavaria, linewidth = 1, fill = NA) + coord_sf(xlim = st_bbox(de_nuts3)[c(1, 3)], ylim = st_bbox(de_nuts3)[c(2, 4)]) + theme_void() ``` ] .pull-right.w36[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/sp-joins5-out-1.png" width="100%" style="display: block; margin: auto;" /> ] --- #### Spatial joins .pull-left.w60[ ```r bavaria <- de_nuts1_pop |> filter(nuts == "DE2") st_join( st_centroid(de_nuts3), bavaria, * join = negate(st_is_within_distance), dist = 100000, left = FALSE # Performs inner join ) |> ggplot() + geom_sf() + geom_sf(data = bavaria, linewidth = 1, fill = NA) + coord_sf(xlim = st_bbox(de_nuts3)[c(1, 3)], ylim = st_bbox(de_nuts3)[c(2, 4)]) + theme_void() ``` ] .pull-right.w36[ <img src="2023-04-06-ua-workshops-spatial_files/figure-html/sp-joins6-out-1.png" width="100%" style="display: block; margin: auto;" /> ] --- class: inverse center middle ### Binary measures --- #### st_distance() Returns a numeric matrix of `length(x)` rows and `length(y)` columns .pull-left.w40[ ```r bielefeld <- de_nuts3 |> filter(str_detect(name, "Bielefeld")) de_nuts3 |> * mutate(dist = st_distance( * x = geometry, * y = bielefeld * )[, 1]) |> arrange(dist) |> print(width = 60) ``` ] .pull-right.w56.small-code[ ``` Simple feature collection with 401 features and 3 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: 4031952 ymin: 2684075 xmax: 4671180 ymax: 3543086 Projected CRS: ETRS89-extended / LAEA Europe # A tibble: 401 × 4 nuts name geometry dist <chr> <chr> <MULTIPOLYGON [m]> [m] 1 DEA41 Bielefeld, Kreisf… (((4228708 3217545, 4225… 0 2 DEA42 Gütersloh (((4216273 3222309, 4214… 0 3 DEA43 Herford (((4247622 3231061, 4228… 0 4 DEA45 Lippe (((4263781 3220253, 4273… 0 5 DE94E Osnabrück, Landkr… (((4206125 3266872, 4205… 4471. 6 DEA47 Paderborn (((4249193 3187118, 4250… 9360. 7 DEA46 Minden-Lübbecke (((4261478 3256200, 4252… 17729. 8 DEA38 Warendorf (((4190451 3218063, 4195… 20998. 9 DEA5B Soest (((4210655 3179982, 4221… 26458. 10 DE928 Schaumburg (((4283343 3240666, 4265… 27191. # ℹ 391 more rows ``` ] --- #### st_distance() Returns a numeric matrix of `length(x)` rows and `length(y)` columns .pull-left.w40[ ```r bielefeld <- de_nuts3 |> filter(str_detect(name, "Bielefeld")) de_nuts3 |> mutate(dist = st_distance( x = geometry, * y = st_centroid(bielefeld) )[, 1]) |> arrange(dist) |> print(width = 60) ``` ] .pull-right.w56.small-code[ ``` Simple feature collection with 401 features and 3 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: 4031952 ymin: 2684075 xmax: 4671180 ymax: 3543086 Projected CRS: ETRS89-extended / LAEA Europe # A tibble: 401 × 4 nuts name geometry dist <chr> <chr> <MULTIPOLYGON [m]> [m] 1 DEA41 Bielefeld, Kreisf… (((4228708 3217545, 4225… 0 2 DEA42 Gütersloh (((4216273 3222309, 4214… 5866. 3 DEA45 Lippe (((4263781 3220253, 4273… 6058. 4 DEA43 Herford (((4247622 3231061, 4228… 7301. 5 DE94E Osnabrück, Landkr… (((4206125 3266872, 4205… 14625. 6 DEA47 Paderborn (((4249193 3187118, 4250… 19621. 7 DEA46 Minden-Lübbecke (((4261478 3256200, 4252… 25521. 8 DEA38 Warendorf (((4190451 3218063, 4195… 28652. 9 DEA5B Soest (((4210655 3179982, 4221… 34279. 10 DE928 Schaumburg (((4283343 3240666, 4265… 36136. # ℹ 391 more rows ``` ] --- class: inverse center middle ### Binary & n-ary transformers --- .panelset[ .panel[.panel-name[st_intersection()] ```r st_buffer(us_centroids, 100000) |> filter(name %in% c("Massachusetts", "Connecticut")) |> * st_intersection() |> ggplot() + * geom_sf(aes(fill = factor(n.overlaps)), show.legend = FALSE) + * geom_sf_text(aes(label = n.overlaps)) + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/st-intersections-1.png" width="40%" style="display: block; margin: auto;" /> ] .panel[.panel-name[st_union()] ```r # st_buffer(us_centroids, 100000) |> filter(name %in% c("Massachusetts", "Connecticut")) |> * st_union() |> ggplot() + geom_sf() + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/st-union-1.png" width="40%" style="display: block; margin: auto;" /> ] .panel[.panel-name[st_difference()] ```r st_buffer(us_centroids, 100000) |> filter(name %in% c("Massachusetts", "Connecticut")) |> * st_difference() |> * slice(2) |> ggplot() + geom_sf() + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/st-diff-1.png" width="40%" style="display: block; margin: auto;" /> ] .panel[.panel-name[st_sym_difference()] ```r st_buffer(us_centroids, 100000) |> filter(name %in% c("Massachusetts", "Connecticut")) |> * st_geometry() |> * do.call(what = st_sym_difference) |> ggplot() + geom_sf() + theme_void() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/st-sym-diff-1.png" width="40%" style="display: block; margin: auto;" /> ] ] --- class: inverse center middle ## Operations on Raster Data --- ### Raster .pull-left.w56[ ```r library(terra) (de_elevation <- rast( "https://lo-ng.netlify.app/slides/data/de_elevation.tif" )) ``` ``` class : SpatRaster dimensions : 119, 140, 1 (nrow, ncol, nlyr) resolution : 0.07145545, 0.07145545 (x, y) extent : 5.50207, 15.50583, 47.00592, 55.50912 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source : de_elevation.tif name : elev min value : -94 max value : 2759 ``` ] .pull-right.w40[ ```r plot(de_elevation) ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/de-elev-1.png" width="100%" style="display: block; margin: auto;" /> ] --- #### Reprojection of NUTS-3 polygons to work with elevation raster ```r (de_nuts3 <- de_nuts3 |> * st_transform(crs(de_elevation))) ``` ``` Simple feature collection with 401 features and 2 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: 5.877085 ymin: 47.27011 xmax: 15.02282 ymax: 54.98211 Geodetic CRS: WGS 84 # A tibble: 401 × 3 nuts name geometry * <chr> <chr> <MULTIPOLYGON [°]> 1 DE254 Nürnberg, Kreisfreie Stadt (((11.03021 49.53537, 11.17818 49.47409, 1… 2 DE255 Schwabach, Kreisfreie Stadt (((11.07981 49.33928, 11.07731 49.31788, 1… 3 DE256 Ansbach, Landkreis (((10.86784 49.35613, 10.89699 49.26011, 1… 4 DE257 Erlangen-Höchstadt (((10.92734 49.76843, 11.09481 49.6078, 11… 5 DE234 Amberg-Sulzbach (((11.93422 49.22107, 11.55776 49.41904, 1… 6 DE235 Cham (((13.17091 49.17358, 13.11751 49.12614, 1… 7 DE236 Neumarkt i. d. OPf. (((11.93422 49.22107, 11.9359 49.19507, 11… 8 DE237 Neustadt a. d. Waldnaab (((12.59378 49.54219, 12.11549 49.54795, 1… 9 DE238 Regensburg, Landkreis (((12.47903 49.03199, 12.4102 48.87916, 12… 10 DE239 Schwandorf (((12.59378 49.54219, 12.63766 49.51946, 1… # ℹ 391 more rows ``` --- ### Cropping & masking ```r bavaria <- st_transform(bavaria, crs(de_elevation)) de_elevation |> * crop(bavaria) |> # plot() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/by-crop-1.png" width="40%" style="display: block; margin: auto;" /> --- ### Cropping & masking ```r bavaria <- st_transform(bavaria, crs(de_elevation)) de_elevation |> crop(bavaria) |> * mask(bavaria) |> plot() ``` <img src="2023-04-06-ua-workshops-spatial_files/figure-html/by-crop1-1.png" width="40%" style="display: block; margin: auto;" /> --- count: false ### Extraction .panel1-by-elev-sum-auto[ ```r *de_elevation ``` ] .panel2-by-elev-sum-auto[ ``` class : SpatRaster dimensions : 119, 140, 1 (nrow, ncol, nlyr) resolution : 0.07145545, 0.07145545 (x, y) extent : 5.50207, 15.50583, 47.00592, 55.50912 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source : de_elevation.tif name : elev min value : -94 max value : 2759 ``` ] --- count: false ### Extraction .panel1-by-elev-sum-auto[ ```r de_elevation |> * extract(bavaria) ``` ] .panel2-by-elev-sum-auto[ ``` ID elev 1 1 626 2 1 474 3 1 375 4 1 366 5 1 624 6 1 616 7 1 658 8 1 630 9 1 488 10 1 385 11 1 325 12 1 301 13 1 335 14 1 346 15 1 568 16 1 602 17 1 609 18 1 635 19 1 555 20 1 558 21 1 539 22 1 554 23 1 464 24 1 526 25 1 596 26 1 469 27 1 391 28 1 325 29 1 297 30 1 310 31 1 314 32 1 317 33 1 314 34 1 325 35 1 357 36 1 372 37 1 381 38 1 383 39 1 415 40 1 460 41 1 514 42 1 516 43 1 593 44 1 611 45 1 600 46 1 582 47 1 565 48 1 523 49 1 534 50 1 564 51 1 388 52 1 414 53 1 431 54 1 449 55 1 360 56 1 312 57 1 309 58 1 316 59 1 313 60 1 315 61 1 360 62 1 355 63 1 348 64 1 334 65 1 335 66 1 337 67 1 355 68 1 342 69 1 345 70 1 370 71 1 408 72 1 457 73 1 522 74 1 573 75 1 618 76 1 619 77 1 588 78 1 571 79 1 533 80 1 562 81 1 595 82 1 619 83 1 393 84 1 329 85 1 338 86 1 349 87 1 335 88 1 299 89 1 298 90 1 301 91 1 323 92 1 341 93 1 343 94 1 343 95 1 351 96 1 339 97 1 343 98 1 356 99 1 338 100 1 337 101 1 324 102 1 322 103 1 325 104 1 322 105 1 318 106 1 325 107 1 350 108 1 390 109 1 432 110 1 454 111 1 505 112 1 564 113 1 585 114 1 578 115 1 596 116 1 603 117 1 605 118 1 593 119 1 588 120 1 348 121 1 416 122 1 420 123 1 313 124 1 291 125 1 289 126 1 281 127 1 268 128 1 302 129 1 313 130 1 298 131 1 301 132 1 337 133 1 354 134 1 340 135 1 294 136 1 312 137 1 369 138 1 345 139 1 328 140 1 312 141 1 302 142 1 321 143 1 377 144 1 415 145 1 389 146 1 382 147 1 375 148 1 363 149 1 397 150 1 406 151 1 474 152 1 551 153 1 627 154 1 715 155 1 646 156 1 597 157 1 572 158 1 556 159 1 536 160 1 162 161 1 256 162 1 228 163 1 320 164 1 376 165 1 377 166 1 356 167 1 372 168 1 298 169 1 268 170 1 266 171 1 276 172 1 289 173 1 294 174 1 283 175 1 251 176 1 231 177 1 247 178 1 288 179 1 301 180 1 275 181 1 293 182 1 346 183 1 350 184 1 326 185 1 297 186 1 286 187 1 334 188 1 449 189 1 504 190 1 468 191 1 462 192 1 437 193 1 376 194 1 369 195 1 382 196 1 429 197 1 552 198 1 707 199 1 782 200 1 661 201 1 612 202 1 581 203 1 557 204 1 545 205 1 551 206 1 145 207 1 164 208 1 230 209 1 317 210 1 389 211 1 422 212 1 401 213 1 324 214 1 292 215 1 271 216 1 254 217 1 261 218 1 303 219 1 292 220 1 269 221 1 247 222 1 229 223 1 223 224 1 242 225 1 296 226 1 332 227 1 345 228 1 335 229 1 308 230 1 301 231 1 278 232 1 283 233 1 336 234 1 433 235 1 500 236 1 464 237 1 456 238 1 465 239 1 459 240 1 398 241 1 383 242 1 421 243 1 524 244 1 640 245 1 677 246 1 614 247 1 623 248 1 628 249 1 601 250 1 547 251 1 526 252 1 557 253 1 605 254 1 172 255 1 237 256 1 333 257 1 403 258 1 430 259 1 397 260 1 289 261 1 261 262 1 279 263 1 267 264 1 260 265 1 306 266 1 300 267 1 283 268 1 263 269 1 244 270 1 231 271 1 238 272 1 333 273 1 387 274 1 389 275 1 356 276 1 324 277 1 299 278 1 280 279 1 272 280 1 322 281 1 448 282 1 492 283 1 459 284 1 434 285 1 434 286 1 456 287 1 475 288 1 450 289 1 439 290 1 462 291 1 501 292 1 513 293 1 528 294 1 607 295 1 615 296 1 560 297 1 504 298 1 513 299 1 536 300 1 613 301 1 672 302 1 659 303 1 229 304 1 214 305 1 304 306 1 346 307 1 344 308 1 306 309 1 238 310 1 244 311 1 281 312 1 296 313 1 283 314 1 257 315 1 261 316 1 283 317 1 256 318 1 226 319 1 230 320 1 258 321 1 363 322 1 389 323 1 372 324 1 343 325 1 323 326 1 306 327 1 296 328 1 282 329 1 297 330 1 365 331 1 438 332 1 435 333 1 437 334 1 442 335 1 456 336 1 515 337 1 504 338 1 501 339 1 484 340 1 460 341 1 459 342 1 486 343 1 555 344 1 546 345 1 500 346 1 493 347 1 529 348 1 580 349 1 660 350 1 689 351 1 230 352 1 287 353 1 307 354 1 301 355 1 323 356 1 322 357 1 281 358 1 254 359 1 272 360 1 241 361 1 223 362 1 262 363 1 319 364 1 380 365 1 387 366 1 366 367 1 341 368 1 322 369 1 302 370 1 293 371 1 298 372 1 301 373 1 294 374 1 358 375 1 434 376 1 474 377 1 484 378 1 481 379 1 482 380 1 471 381 1 509 382 1 505 383 1 462 384 1 450 385 1 443 386 1 477 387 1 485 388 1 472 389 1 472 390 1 532 391 1 645 392 1 705 393 1 276 394 1 338 395 1 351 396 1 353 397 1 320 398 1 299 399 1 272 400 1 270 401 1 254 402 1 260 403 1 306 404 1 353 405 1 347 406 1 359 407 1 357 408 1 334 409 1 311 410 1 308 411 1 300 412 1 304 413 1 306 414 1 298 415 1 358 416 1 435 417 1 487 418 1 515 419 1 517 420 1 469 421 1 448 422 1 477 423 1 478 424 1 449 425 1 443 426 1 434 427 1 431 428 1 429 429 1 437 430 1 464 431 1 529 432 1 599 433 1 619 434 1 419 435 1 378 436 1 390 437 1 309 438 1 295 439 1 302 440 1 302 441 1 312 442 1 339 443 1 354 444 1 338 445 1 331 446 1 333 447 1 340 448 1 344 449 1 346 450 1 332 451 1 309 452 1 298 453 1 315 454 1 365 455 1 386 456 1 422 457 1 470 458 1 504 459 1 469 460 1 475 461 1 495 462 1 474 463 1 437 464 1 461 465 1 460 466 1 455 467 1 433 468 1 427 469 1 468 470 1 523 471 1 566 472 1 562 473 1 543 474 1 319 475 1 328 476 1 345 477 1 361 478 1 367 479 1 347 480 1 330 481 1 348 482 1 370 483 1 379 484 1 374 485 1 363 486 1 345 487 1 318 488 1 302 489 1 316 490 1 351 491 1 360 492 1 369 493 1 395 494 1 436 495 1 457 496 1 487 497 1 497 498 1 468 499 1 430 500 1 443 501 1 476 502 1 499 503 1 474 504 1 444 505 1 460 506 1 516 507 1 574 508 1 594 509 1 618 510 1 655 511 1 408 512 1 408 513 1 381 514 1 387 515 1 408 516 1 413 517 1 396 518 1 376 519 1 362 520 1 353 521 1 334 522 1 314 523 1 311 524 1 340 525 1 361 526 1 383 527 1 418 528 1 468 529 1 514 530 1 518 531 1 523 532 1 494 533 1 443 534 1 414 535 1 443 536 1 471 537 1 460 538 1 435 539 1 433 540 1 478 541 1 513 542 1 548 543 1 590 544 1 620 545 1 583 546 1 423 547 1 449 548 1 441 549 1 458 550 1 455 551 1 438 552 1 415 553 1 393 554 1 386 555 1 382 556 1 371 557 1 349 558 1 325 559 1 347 560 1 373 561 1 406 562 1 442 563 1 485 564 1 548 565 1 549 566 1 533 567 1 507 568 1 480 569 1 460 570 1 428 571 1 426 572 1 406 573 1 388 574 1 409 575 1 456 576 1 458 577 1 493 578 1 521 579 1 516 580 1 503 581 1 621 582 1 433 583 1 471 584 1 454 585 1 454 586 1 457 587 1 454 588 1 442 589 1 428 590 1 426 591 1 415 592 1 401 593 1 379 594 1 351 595 1 361 596 1 387 597 1 421 598 1 443 599 1 455 600 1 500 601 1 542 602 1 532 603 1 506 604 1 484 605 1 469 606 1 441 607 1 443 608 1 403 609 1 372 610 1 408 611 1 436 612 1 441 613 1 469 614 1 490 615 1 491 616 1 501 617 1 545 618 1 544 619 1 512 620 1 485 621 1 470 622 1 478 623 1 476 624 1 453 625 1 451 626 1 452 627 1 448 628 1 442 629 1 438 630 1 423 631 1 410 632 1 393 633 1 371 634 1 368 635 1 400 636 1 424 637 1 433 638 1 443 639 1 495 640 1 526 641 1 534 642 1 529 643 1 508 644 1 463 645 1 432 646 1 428 647 1 404 648 1 393 649 1 425 650 1 399 651 1 422 652 1 432 653 1 430 654 1 423 655 1 419 656 1 432 657 1 449 658 1 506 659 1 576 660 1 603 661 1 709 662 1 487 663 1 472 664 1 483 665 1 473 666 1 461 667 1 451 668 1 441 669 1 429 670 1 449 671 1 447 672 1 428 673 1 406 674 1 396 675 1 404 676 1 424 677 1 430 678 1 433 679 1 449 680 1 494 681 1 518 682 1 523 683 1 518 684 1 509 685 1 496 686 1 437 687 1 404 688 1 389 689 1 407 690 1 456 691 1 437 692 1 471 693 1 467 694 1 459 695 1 460 696 1 430 697 1 456 698 1 468 699 1 512 700 1 594 701 1 696 702 1 821 703 1 908 704 1 476 705 1 473 706 1 467 707 1 471 708 1 469 709 1 459 710 1 453 711 1 445 712 1 452 713 1 438 714 1 432 715 1 470 716 1 492 717 1 482 718 1 475 719 1 479 720 1 490 721 1 488 722 1 491 723 1 504 724 1 511 725 1 507 726 1 490 727 1 452 728 1 425 729 1 389 730 1 387 731 1 448 732 1 476 733 1 527 734 1 546 735 1 569 736 1 581 737 1 483 738 1 529 739 1 561 740 1 556 741 1 553 742 1 630 743 1 813 744 1 920 745 1 880 746 1 885 747 1 473 748 1 475 749 1 474 750 1 462 751 1 461 752 1 513 753 1 520 754 1 469 755 1 451 756 1 488 757 1 560 758 1 560 759 1 534 760 1 510 761 1 492 762 1 480 763 1 472 764 1 465 765 1 476 766 1 503 767 1 520 768 1 492 769 1 458 770 1 422 771 1 383 772 1 362 773 1 374 774 1 406 775 1 423 776 1 470 777 1 536 778 1 560 779 1 432 780 1 503 781 1 664 782 1 703 783 1 618 784 1 560 785 1 627 786 1 698 787 1 690 788 1 748 789 1 976 790 1 460 791 1 439 792 1 443 793 1 499 794 1 563 795 1 511 796 1 489 797 1 515 798 1 552 799 1 549 800 1 528 801 1 502 802 1 483 803 1 483 804 1 508 805 1 489 806 1 466 807 1 470 808 1 489 809 1 479 810 1 444 811 1 407 812 1 390 813 1 374 814 1 341 815 1 334 816 1 329 817 1 340 818 1 361 819 1 375 820 1 356 821 1 386 822 1 508 823 1 639 824 1 701 825 1 649 826 1 683 827 1 641 828 1 651 829 1 732 830 1 860 831 1 996 832 1 1101 833 1 438 834 1 417 835 1 421 836 1 462 837 1 521 838 1 521 839 1 518 840 1 516 841 1 505 842 1 504 843 1 487 844 1 466 845 1 461 846 1 475 847 1 485 848 1 464 849 1 436 850 1 416 851 1 409 852 1 413 853 1 397 854 1 397 855 1 399 856 1 394 857 1 376 858 1 358 859 1 349 860 1 341 861 1 332 862 1 324 863 1 325 864 1 331 865 1 346 866 1 388 867 1 473 868 1 604 869 1 679 870 1 662 871 1 675 872 1 691 873 1 674 874 1 723 875 1 824 876 1 861 877 1 900 878 1 479 879 1 446 880 1 443 881 1 465 882 1 505 883 1 510 884 1 507 885 1 497 886 1 476 887 1 471 888 1 440 889 1 420 890 1 412 891 1 412 892 1 409 893 1 404 894 1 390 895 1 377 896 1 374 897 1 390 898 1 404 899 1 412 900 1 417 901 1 415 902 1 405 903 1 390 904 1 387 905 1 384 906 1 377 907 1 364 908 1 353 909 1 333 910 1 324 911 1 318 912 1 332 913 1 390 914 1 424 915 1 499 916 1 589 917 1 608 918 1 529 919 1 572 920 1 604 921 1 669 922 1 783 923 1 896 924 1 878 925 1 531 926 1 499 927 1 482 928 1 460 929 1 460 930 1 458 931 1 445 932 1 432 933 1 433 934 1 436 935 1 401 936 1 382 937 1 374 938 1 368 939 1 364 940 1 367 941 1 374 942 1 393 943 1 413 944 1 413 945 1 442 946 1 443 947 1 448 948 1 449 949 1 436 950 1 428 951 1 436 952 1 426 953 1 408 954 1 391 955 1 367 956 1 343 957 1 336 958 1 333 959 1 327 960 1 317 961 1 317 962 1 382 963 1 430 964 1 459 965 1 482 966 1 461 967 1 473 968 1 551 969 1 655 970 1 741 971 1 779 972 1 898 973 1 491 974 1 475 975 1 451 976 1 425 977 1 414 978 1 411 979 1 412 980 1 421 981 1 434 982 1 405 983 1 386 984 1 384 985 1 386 986 1 385 987 1 387 988 1 389 989 1 411 990 1 435 991 1 451 992 1 455 993 1 470 994 1 468 995 1 467 996 1 463 997 1 448 998 1 431 999 1 424 1000 1 411 1001 1 397 1002 1 388 1003 1 378 1004 1 375 1005 1 375 1006 1 369 1007 1 356 1008 1 338 1009 1 326 1010 1 368 1011 1 392 1012 1 404 1013 1 420 1014 1 419 1015 1 430 1016 1 506 1017 1 618 1018 1 713 1019 1 725 1020 1 463 1021 1 444 1022 1 434 1023 1 426 1024 1 421 1025 1 424 1026 1 441 1027 1 429 1028 1 440 1029 1 462 1030 1 460 1031 1 409 1032 1 408 1033 1 415 1034 1 423 1035 1 432 1036 1 438 1037 1 435 1038 1 456 1039 1 469 1040 1 471 1041 1 482 1042 1 484 1043 1 473 1044 1 458 1045 1 441 1046 1 424 1047 1 426 1048 1 433 1049 1 431 1050 1 426 1051 1 424 1052 1 427 1053 1 422 1054 1 413 1055 1 402 1056 1 389 1057 1 368 1058 1 354 1059 1 365 1060 1 380 1061 1 392 1062 1 395 1063 1 383 1064 1 403 1065 1 437 1066 1 514 1067 1 623 1068 1 692 1069 1 444 1070 1 446 1071 1 445 1072 1 445 1073 1 451 1074 1 466 1075 1 473 1076 1 449 1077 1 459 1078 1 482 1079 1 474 1080 1 441 1081 1 446 1082 1 456 1083 1 468 1084 1 478 1085 1 462 1086 1 474 1087 1 483 1088 1 485 1089 1 482 1090 1 477 1091 1 463 1092 1 447 1093 1 437 1094 1 438 1095 1 452 1096 1 472 1097 1 472 1098 1 468 1099 1 463 1100 1 459 1101 1 456 1102 1 445 1103 1 437 1104 1 430 1105 1 423 1106 1 410 1107 1 396 1108 1 401 1109 1 401 1110 1 395 1111 1 391 1112 1 377 1113 1 579 1114 1 464 1115 1 466 1116 1 469 1117 1 473 1118 1 489 1119 1 492 1120 1 488 1121 1 505 1122 1 495 1123 1 463 1124 1 467 1125 1 488 1126 1 489 1127 1 473 1128 1 485 1129 1 497 1130 1 501 1131 1 494 1132 1 476 1133 1 483 1134 1 480 1135 1 474 1136 1 466 1137 1 451 1138 1 426 1139 1 441 1140 1 463 1141 1 479 1142 1 485 1143 1 482 1144 1 473 1145 1 474 1146 1 474 1147 1 469 1148 1 465 1149 1 453 1150 1 446 1151 1 440 1152 1 432 1153 1 424 1154 1 416 1155 1 411 1156 1 398 1157 1 373 1158 1 342 1159 1 339 1160 1 485 1161 1 483 1162 1 488 1163 1 495 1164 1 501 1165 1 490 1166 1 508 1167 1 506 1168 1 488 1169 1 503 1170 1 506 1171 1 485 1172 1 481 1173 1 492 1174 1 500 1175 1 500 1176 1 509 1177 1 508 1178 1 498 1179 1 487 1180 1 486 1181 1 474 1182 1 470 1183 1 462 1184 1 448 1185 1 438 1186 1 428 1187 1 463 1188 1 490 1189 1 493 1190 1 478 1191 1 473 1192 1 483 1193 1 482 1194 1 475 1195 1 468 1196 1 465 1197 1 451 1198 1 452 1199 1 451 1200 1 448 1201 1 451 1202 1 465 1203 1 422 1204 1 379 1205 1 342 1206 1 315 1207 1 332 1208 1 505 1209 1 508 1210 1 514 1211 1 517 1212 1 515 1213 1 514 1214 1 521 1215 1 517 1216 1 507 1217 1 520 1218 1 529 1219 1 512 1220 1 502 1221 1 507 1222 1 520 1223 1 515 1224 1 511 1225 1 506 1226 1 500 1227 1 492 1228 1 480 1229 1 475 1230 1 470 1231 1 464 1232 1 459 1233 1 461 1234 1 461 1235 1 479 1236 1 487 1237 1 482 1238 1 470 1239 1 462 1240 1 466 1241 1 459 1242 1 448 1243 1 442 1244 1 449 1245 1 449 1246 1 452 1247 1 454 1248 1 458 1249 1 466 1250 1 447 1251 1 401 1252 1 361 1253 1 345 1254 1 528 1255 1 539 1256 1 544 1257 1 543 1258 1 541 1259 1 540 1260 1 548 1261 1 560 1262 1 567 1263 1 544 1264 1 531 1265 1 527 1266 1 532 1267 1 543 1268 1 532 1269 1 525 1270 1 515 1271 1 505 1272 1 496 1273 1 486 1274 1 488 1275 1 486 1276 1 484 1277 1 486 1278 1 495 1279 1 497 1280 1 510 1281 1 518 1282 1 517 1283 1 510 1284 1 493 1285 1 460 1286 1 434 1287 1 419 1288 1 413 1289 1 415 1290 1 420 1291 1 415 1292 1 406 1293 1 400 1294 1 400 1295 1 386 1296 1 554 1297 1 561 1298 1 567 1299 1 584 1300 1 556 1301 1 562 1302 1 582 1303 1 590 1304 1 570 1305 1 559 1306 1 558 1307 1 560 1308 1 566 1309 1 574 1310 1 560 1311 1 553 1312 1 540 1313 1 526 1314 1 517 1315 1 515 1316 1 513 1317 1 512 1318 1 513 1319 1 516 1320 1 521 1321 1 525 1322 1 552 1323 1 564 1324 1 556 1325 1 533 1326 1 509 1327 1 480 1328 1 465 1329 1 457 1330 1 449 1331 1 449 1332 1 443 1333 1 433 1334 1 422 1335 1 419 1336 1 580 1337 1 600 1338 1 620 1339 1 588 1340 1 592 1341 1 603 1342 1 601 1343 1 584 1344 1 591 1345 1 593 1346 1 599 1347 1 603 1348 1 592 1349 1 563 1350 1 577 1351 1 581 1352 1 575 1353 1 565 1354 1 560 1355 1 548 1356 1 544 1357 1 546 1358 1 551 1359 1 557 1360 1 562 1361 1 560 1362 1 539 1363 1 510 1364 1 490 1365 1 494 1366 1 537 1367 1 536 1368 1 513 1369 1 495 1370 1 500 1371 1 480 1372 1 603 1373 1 624 1374 1 626 1375 1 629 1376 1 632 1377 1 628 1378 1 619 1379 1 619 1380 1 624 1381 1 625 1382 1 639 1383 1 640 1384 1 600 1385 1 556 1386 1 613 1387 1 632 1388 1 627 1389 1 622 1390 1 612 1391 1 601 1392 1 587 1393 1 583 1394 1 590 1395 1 596 1396 1 559 1397 1 533 1398 1 501 1399 1 473 1400 1 467 1401 1 506 1402 1 570 1403 1 567 1404 1 538 1405 1 525 1406 1 539 1407 1 499 1408 1 438 1409 1 624 1410 1 637 1411 1 663 1412 1 680 1413 1 701 1414 1 701 1415 1 683 1416 1 663 1417 1 667 1418 1 678 1419 1 672 1420 1 680 1421 1 676 1422 1 629 1423 1 564 1424 1 659 1425 1 653 1426 1 628 1427 1 645 1428 1 633 1429 1 666 1430 1 653 1431 1 636 1432 1 625 1433 1 602 1434 1 541 1435 1 516 1436 1 491 1437 1 470 1438 1 475 1439 1 528 1440 1 561 1441 1 550 1442 1 537 1443 1 548 1444 1 575 1445 1 531 1446 1 465 1447 1 428 1448 1 681 1449 1 715 1450 1 770 1451 1 785 1452 1 778 1453 1 746 1454 1 715 1455 1 730 1456 1 761 1457 1 727 1458 1 713 1459 1 701 1460 1 646 1461 1 561 1462 1 638 1463 1 631 1464 1 614 1465 1 636 1466 1 618 1467 1 694 1468 1 705 1469 1 690 1470 1 663 1471 1 615 1472 1 552 1473 1 500 1474 1 470 1475 1 466 1476 1 487 1477 1 538 1478 1 533 1479 1 520 1480 1 518 1481 1 545 1482 1 610 1483 1 609 1484 1 553 1485 1 490 1486 1 451 1487 1 742 1488 1 722 1489 1 774 1490 1 817 1491 1 805 1492 1 765 1493 1 741 1494 1 783 1495 1 811 1496 1 758 1497 1 737 1498 1 726 1499 1 669 1500 1 587 1501 1 636 1502 1 633 1503 1 615 1504 1 621 1505 1 637 1506 1 689 1507 1 729 1508 1 746 1509 1 736 1510 1 706 1511 1 659 1512 1 568 1513 1 489 1514 1 481 1515 1 573 1516 1 680 1517 1 688 1518 1 621 1519 1 618 1520 1 705 1521 1 701 1522 1 757 1523 1 780 1524 1 661 1525 1 480 1526 1 860 1527 1 872 1528 1 757 1529 1 719 1530 1 836 1531 1 827 1532 1 786 1533 1 774 1534 1 807 1535 1 802 1536 1 780 1537 1 777 1538 1 769 1539 1 719 1540 1 642 1541 1 666 1542 1 642 1543 1 619 1544 1 672 1545 1 870 1546 1 748 1547 1 855 1548 1 891 1549 1 849 1550 1 931 1551 1 872 1552 1 905 1553 1 755 1554 1 633 1555 1 787 1556 1 998 1557 1 1007 1558 1 845 1559 1 852 1560 1 1050 1561 1 916 1562 1 971 1563 1 946 1564 1 816 1565 1 708 1566 1 905 1567 1 907 1568 1 788 1569 1 768 1570 1 893 1571 1 891 1572 1 845 1573 1 816 1574 1 822 1575 1 771 1576 1 885 1577 1 937 1578 1 913 1579 1 858 1580 1 703 1581 1 708 1582 1 737 1583 1 787 1584 1 888 1585 1 1104 1586 1 853 1587 1 1020 1588 1 1090 1589 1 1038 1590 1 1193 1591 1 1149 1592 1 1166 1593 1 1053 1594 1 843 1595 1 706 1596 1 902 1597 1 980 1598 1 1138 1599 1 1117 1600 1 1038 1601 1 1037 1602 1 1013 1603 1 970 1604 1 963 1605 1 593 1606 1 699 1607 1 719 1608 1 775 1609 1 875 1610 1 951 1611 1 911 1612 1 810 1613 1 996 1614 1 1046 1615 1 1054 1616 1 995 1617 1 898 1618 1 842 1619 1 1001 1620 1 1208 1621 1 1244 1622 1 1166 1623 1 1089 1624 1 880 1625 1 1024 1626 1 1012 1627 1 972 1628 1 1012 1629 1 1044 1630 1 1002 1631 1 1100 1632 1 1187 1633 1 1222 1634 1 1251 1635 1 1262 1636 1 1187 1637 1 1372 1638 1 1205 1639 1 963 1640 1 1372 1641 1 1070 1642 1 1168 1643 1 1070 1644 1 873 1645 1 1131 1646 1 1232 1647 1 1405 1648 1 1363 1649 1 1229 1650 1 1131 1651 1 1040 1652 1 1161 1653 1 1165 1654 1 1156 1655 1 1205 1656 1 1208 1657 1 1249 1658 1 1687 1659 1 1470 1660 1 1755 1661 1 1202 1662 1 1307 1663 1 1190 1664 1 965 1665 1 1360 1666 1 1447 1667 1 1428 1668 1 1437 1669 1 1348 1670 1 1169 1671 1 1292 1672 1 1530 1673 1 1659 1674 1 1388 1675 1 1264 1676 1 1202 1677 1 1640 1678 1 1694 1679 1 1647 ``` ] --- count: false ### Extraction .panel1-by-elev-sum-auto[ ```r de_elevation |> extract(bavaria) |> * summarise(across(elev, lst(min, max, mean))) ``` ] .panel2-by-elev-sum-auto[ ``` elev_min elev_max elev_mean 1 145 1755 517.601 ``` ] <style> .panel1-by-elev-sum-auto { color: black; width: 49%; hight: 32%; float: top; padding-left: 1%; font-size: 80% } .panel2-by-elev-sum-auto { color: black; width: 49%; hight: 32%; float: top; padding-left: 1%; font-size: 80% } .panel3-by-elev-sum-auto { color: black; width: NA%; hight: 33%; float: top; padding-left: 1%; font-size: 80% } </style> --- ### Extraction ```r (de_nuts1 <- de_nuts3 |> group_by(nuts = str_sub(nuts, 1, 3)) |> * summarise(geometry = st_union(geometry))) ``` ``` Simple feature collection with 16 features and 1 field Geometry type: GEOMETRY Dimension: XY Bounding box: xmin: 5.877085 ymin: 47.27011 xmax: 15.02282 ymax: 54.98211 Geodetic CRS: WGS 84 # A tibble: 16 × 2 nuts geometry <chr> <GEOMETRY [°]> 1 DE1 POLYGON ((9.182192 47.65589, 9.364413 47.62806, 9.495604 47.55145, 9.5… 2 DE2 POLYGON ((10.7292 50.23001, 10.61011 50.22799, 10.58959 50.31986, 10.4… 3 DE3 POLYGON ((13.39854 52.64819, 13.16426 52.5989, 13.13901 52.50206, 13.1… 4 DE4 POLYGON ((12.98468 53.16499, 12.92016 53.19372, 12.8793 53.18263, 12.8… 5 DE5 MULTIPOLYGON (((8.585106 53.20144, 8.48533 53.22712, 8.654899 53.10886… 6 DE6 MULTIPOLYGON (((10.30795 53.4332, 10.23668 53.49635, 10.17859 53.53497… 7 DE7 POLYGON ((8.99056 50.06712, 9.165176 50.10605, 9.270941 50.14205, 9.40… 8 DE8 POLYGON ((12.33175 53.31801, 12.74191 53.20144, 12.77713 53.18877, 12.… 9 DE9 MULTIPOLYGON (((6.930503 53.68451, 6.848968 53.69901, 6.809192 53.6793… 10 DEA POLYGON ((7.785898 50.93991, 7.851496 50.92583, 8.03969 50.69738, 8.12… 11 DEB POLYGON ((8.414774 49.59505, 8.37993 49.68695, 8.446378 49.7308, 8.448… 12 DEC POLYGON ((6.556986 49.41921, 6.723466 49.21883, 6.784494 49.16084, 6.9… 13 DED POLYGON ((13.65217 50.73036, 13.82971 50.73285, 14.36331 50.90343, 14.… 14 DEE POLYGON ((11.00878 52.49675, 10.93454 52.47179, 11.05106 52.37008, 11.… 15 DEF MULTIPOLYGON (((7.940571 54.20555, 7.888383 54.23613, 7.800922 54.2111… 16 DEG POLYGON ((10.38003 51.57562, 10.26577 51.48461, 9.928339 51.3753, 9.98… ``` --- count: false ### Extraction .panel1-de-elev-sum-non_seq[ ```r de_nuts1 |> print(width = 44) ``` ] .panel2-de-elev-sum-non_seq[ ``` Simple feature collection with 16 features and 1 field Geometry type: GEOMETRY Dimension: XY Bounding box: xmin: 5.877085 ymin: 47.27011 xmax: 15.02282 ymax: 54.98211 Geodetic CRS: WGS 84 # A tibble: 16 × 2 nuts geometry <chr> <GEOMETRY [°]> 1 DE1 POLYGON ((9.182192 47.65589, 9.364… 2 DE2 POLYGON ((10.7292 50.23001, 10.610… 3 DE3 POLYGON ((13.39854 52.64819, 13.16… 4 DE4 POLYGON ((12.98468 53.16499, 12.92… 5 DE5 MULTIPOLYGON (((8.585106 53.20144,… 6 DE6 MULTIPOLYGON (((10.30795 53.4332, … 7 DE7 POLYGON ((8.99056 50.06712, 9.1651… 8 DE8 POLYGON ((12.33175 53.31801, 12.74… 9 DE9 MULTIPOLYGON (((6.930503 53.68451,… 10 DEA POLYGON ((7.785898 50.93991, 7.851… 11 DEB POLYGON ((8.414774 49.59505, 8.379… 12 DEC POLYGON ((6.556986 49.41921, 6.723… 13 DED POLYGON ((13.65217 50.73036, 13.82… 14 DEE POLYGON ((11.00878 52.49675, 10.93… 15 DEF MULTIPOLYGON (((7.940571 54.20555,… 16 DEG POLYGON ((10.38003 51.57562, 10.26… ``` ] --- count: false ### Extraction .panel1-de-elev-sum-non_seq[ ```r de_nuts1 |> * bind_cols( * extract(de_elevation, de_nuts1) |> * nest(elev_rast = -ID) |> * select(-ID) * ) |> print(width = 44) ``` ] .panel2-de-elev-sum-non_seq[ ``` Simple feature collection with 16 features and 2 fields Geometry type: GEOMETRY Dimension: XY Bounding box: xmin: 5.877085 ymin: 47.27011 xmax: 15.02282 ymax: 54.98211 Geodetic CRS: WGS 84 # A tibble: 16 × 3 nuts geometry elev_rast * <chr> <GEOMETRY [°]> <list> 1 DE1 POLYGON ((9.182192 47.65… <tibble> 2 DE2 POLYGON ((10.7292 50.230… <tibble> 3 DE3 POLYGON ((13.39854 52.64… <tibble> 4 DE4 POLYGON ((12.98468 53.16… <tibble> 5 DE5 MULTIPOLYGON (((8.585106… <tibble> 6 DE6 MULTIPOLYGON (((10.30795… <tibble> 7 DE7 POLYGON ((8.99056 50.067… <tibble> 8 DE8 POLYGON ((12.33175 53.31… <tibble> 9 DE9 MULTIPOLYGON (((6.930503… <tibble> 10 DEA POLYGON ((7.785898 50.93… <tibble> 11 DEB POLYGON ((8.414774 49.59… <tibble> 12 DEC POLYGON ((6.556986 49.41… <tibble> 13 DED POLYGON ((13.65217 50.73… <tibble> 14 DEE POLYGON ((11.00878 52.49… <tibble> 15 DEF MULTIPOLYGON (((7.940571… <tibble> 16 DEG POLYGON ((10.38003 51.57… <tibble> ``` ] --- count: false ### Extraction .panel1-de-elev-sum-non_seq[ ```r de_nuts1 |> bind_cols( extract(de_elevation, de_nuts1) |> nest(elev_rast = -ID) |> select(-ID) ) |> * st_drop_geometry() |> print(width = 44) ``` ] .panel2-de-elev-sum-non_seq[ ``` # A tibble: 16 × 2 nuts elev_rast * <chr> <list> 1 DE1 <tibble [866 × 1]> 2 DE2 <tibble [1,679 × 1]> 3 DE3 <tibble [24 × 1]> 4 DE4 <tibble [765 × 1]> 5 DE5 <tibble [10 × 1]> 6 DE6 <tibble [19 × 1]> 7 DE7 <tibble [524 × 1]> 8 DE8 <tibble [617 × 1]> 9 DE9 <tibble [1,253 × 1]> 10 DEA <tibble [867 × 1]> 11 DEB <tibble [485 × 1]> 12 DEC <tibble [61 × 1]> 13 DED <tibble [464 × 1]> 14 DEE <tibble [529 × 1]> 15 DEF <tibble [415 × 1]> 16 DEG <tibble [404 × 1]> ``` ] --- count: false ### Extraction .panel1-de-elev-sum-non_seq[ ```r de_nuts1 |> bind_cols( extract(de_elevation, de_nuts1) |> nest(elev_rast = -ID) |> select(-ID) ) |> st_drop_geometry() |> * mutate(elev_sum = map(elev_rast, \(x) { * summarise(x, across(elev, lst(min, max, mean))) * })) |> print(width = 44) ``` ] .panel2-de-elev-sum-non_seq[ ``` # A tibble: 16 × 3 nuts elev_rast elev_sum <chr> <list> <list> 1 DE1 <tibble [866 × 1]> <tibble> 2 DE2 <tibble [1,679 × 1]> <tibble> 3 DE3 <tibble [24 × 1]> <tibble> 4 DE4 <tibble [765 × 1]> <tibble> 5 DE5 <tibble [10 × 1]> <tibble> 6 DE6 <tibble [19 × 1]> <tibble> 7 DE7 <tibble [524 × 1]> <tibble> 8 DE8 <tibble [617 × 1]> <tibble> 9 DE9 <tibble [1,253 × 1]> <tibble> 10 DEA <tibble [867 × 1]> <tibble> 11 DEB <tibble [485 × 1]> <tibble> 12 DEC <tibble [61 × 1]> <tibble> 13 DED <tibble [464 × 1]> <tibble> 14 DEE <tibble [529 × 1]> <tibble> 15 DEF <tibble [415 × 1]> <tibble> 16 DEG <tibble [404 × 1]> <tibble> ``` ] --- count: false ### Extraction .panel1-de-elev-sum-non_seq[ ```r de_nuts1 |> bind_cols( extract(de_elevation, de_nuts1) |> nest(elev_rast = -ID) |> select(-ID) ) |> st_drop_geometry() |> mutate(elev_sum = map(elev_rast, \(x) { summarise(x, across(elev, lst(min, max, mean))) })) |> * select(-elev_rast) |> print(width = 44) ``` ] .panel2-de-elev-sum-non_seq[ ``` # A tibble: 16 × 2 nuts elev_sum <chr> <list> 1 DE1 <tibble [1 × 3]> 2 DE2 <tibble [1 × 3]> 3 DE3 <tibble [1 × 3]> 4 DE4 <tibble [1 × 3]> 5 DE5 <tibble [1 × 3]> 6 DE6 <tibble [1 × 3]> 7 DE7 <tibble [1 × 3]> 8 DE8 <tibble [1 × 3]> 9 DE9 <tibble [1 × 3]> 10 DEA <tibble [1 × 3]> 11 DEB <tibble [1 × 3]> 12 DEC <tibble [1 × 3]> 13 DED <tibble [1 × 3]> 14 DEE <tibble [1 × 3]> 15 DEF <tibble [1 × 3]> 16 DEG <tibble [1 × 3]> ``` ] --- count: false ### Extraction .panel1-de-elev-sum-non_seq[ ```r de_nuts1 |> bind_cols( extract(de_elevation, de_nuts1) |> nest(elev_rast = -ID) |> select(-ID) ) |> st_drop_geometry() |> mutate(elev_sum = map(elev_rast, \(x) { summarise(x, across(elev, lst(min, max, mean))) })) |> select(-elev_rast) |> * unnest_wider(elev_sum) |> print(width = 44) ``` ] .panel2-de-elev-sum-non_seq[ ``` # A tibble: 16 × 4 nuts elev_min elev_max elev_mean <chr> <int> <int> <dbl> 1 DE1 90 1074 498. 2 DE2 145 1755 518. 3 DE3 39 51 43.7 4 DE4 1 156 63.8 5 DE5 -1 17 4.8 6 DE6 -5 48 15.3 7 DE7 83 737 302. 8 DE8 -4 116 38.5 9 DE9 -8 714 71.7 10 DEA 14 679 179. 11 DEB 84 590 321. 12 DEC 212 486 313. 13 DED 81 891 305. 14 DEE 19 580 116. 15 DEF -21 78 19.9 16 DEG 148 740 367. ``` ] --- count: false ### Extraction .panel1-de-elev-sum-non_seq[ ```r de_nuts1 |> bind_cols( extract(de_elevation, de_nuts1) |> nest(elev_rast = -ID) |> select(-ID) ) |> st_drop_geometry() |> mutate(elev_sum = map(elev_rast, \(x) { summarise(x, across(elev, lst(min, max, mean))) })) |> select(-elev_rast) |> unnest_wider(elev_sum) |> * arrange(desc(elev_mean)) |> print(width = 44) ``` ] .panel2-de-elev-sum-non_seq[ ``` # A tibble: 16 × 4 nuts elev_min elev_max elev_mean <chr> <int> <int> <dbl> 1 DE2 145 1755 518. 2 DE1 90 1074 498. 3 DEG 148 740 367. 4 DEB 84 590 321. 5 DEC 212 486 313. 6 DED 81 891 305. 7 DE7 83 737 302. 8 DEA 14 679 179. 9 DEE 19 580 116. 10 DE9 -8 714 71.7 11 DE4 1 156 63.8 12 DE3 39 51 43.7 13 DE8 -4 116 38.5 14 DEF -21 78 19.9 15 DE6 -5 48 15.3 16 DE5 -1 17 4.8 ``` ] <style> .panel1-de-elev-sum-non_seq { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-de-elev-sum-non_seq { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-de-elev-sum-non_seq { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## References - {sf} Function Reference. <https://r-spatial.github.io/sf/reference/index.html> - Lovelace, R., Nowosad, J. & Muenchow, J. (2019). _Geocomputation with R_. CRC Press. <https://r.geocompx.org/> - Pebesma, E., & Bivand, R. (2023). _Spatial Data Science: With Applications in R_. Chapman and Hall/CRC. <https://r-spatial.org/book/> --- class: middle ## Thank you! <br> <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg> <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> [long39ng](https://github.com/long39ng) <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M336.5 160C322 70.7 287.8 8 248 8s-74 62.7-88.5 152h177zM152 256c0 22.2 1.2 43.5 3.3 64h185.3c2.1-20.5 3.3-41.8 3.3-64s-1.2-43.5-3.3-64H155.3c-2.1 20.5-3.3 41.8-3.3 64zm324.7-96c-28.6-67.9-86.5-120.4-158-141.6 24.4 33.8 41.2 84.7 50 141.6h108zM177.2 18.4C105.8 39.6 47.8 92.1 19.3 160h108c8.7-56.9 25.5-107.8 49.9-141.6zM487.4 192H372.7c2.1 21 3.3 42.5 3.3 64s-1.2 43-3.3 64h114.6c5.5-20.5 8.6-41.8 8.6-64s-3.1-43.5-8.5-64zM120 256c0-21.5 1.2-43 3.3-64H8.6C3.2 212.5 0 233.8 0 256s3.2 43.5 8.6 64h114.6c-2-21-3.2-42.5-3.2-64zm39.5 96c14.5 89.3 48.7 152 88.5 152s74-62.7 88.5-152h-177zm159.3 141.6c71.4-21.2 129.4-73.7 158-141.6h-108c-8.8 56.9-25.6 107.8-50 141.6zM19.3 352c28.6 67.9 86.5 120.4 158 141.6-24.4-33.8-41.2-84.7-50-141.6h-108z"></path></svg> <https://lo-ng.netlify.app> <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M464 64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V112c0-26.51-21.49-48-48-48zm0 48v40.805c-22.422 18.259-58.168 46.651-134.587 106.49-16.841 13.247-50.201 45.072-73.413 44.701-23.208.375-56.579-31.459-73.413-44.701C106.18 199.465 70.425 171.067 48 152.805V112h416zM48 400V214.398c22.914 18.251 55.409 43.862 104.938 82.646 21.857 17.205 60.134 55.186 103.062 54.955 42.717.231 80.509-37.199 103.053-54.947 49.528-38.783 82.032-64.401 104.947-82.653V400H48z"></path></svg> [long.nguyen@uni-bielefeld.de](mailto:long.nguyen@uni-bielefeld.de) --- class: inverse center middle ## Appendix: Data Preparation --- ### GeoJSON of US states in AlbersUSA projection ```r library(sf) library(tidyverse) us_states_albers <- read_rds( "https://github.com/hrbrmstr/albersusa/raw/master/inst/extdata/states_sf.rda" ) |> select(fips_state, name) |> st_transform("EPSG:2163") |> rmapshaper::ms_simplify() write_sf(us_states_albers, "slides/data/us-states-albers.geojson") ``` --- ### US 2020 presidential election results .panelset[ .panel[.panel-name[State populations 2018] ```r us_pop <- httr::GET( "https://api.census.gov/data/2018/pep/population?get=POP&for=state:*" ) |> httr::content() |> tail(-1) |> map(set_names, "pop18", "fips_state") |> bind_rows() |> mutate(pop18 = as.numeric(pop18)) ``` ] .panel[.panel-name[Election results] ```r us_votes <- read_csv( "https://github.com/walkerke/census-with-r-book/raw/master/data/us_vote_2020.csv" ) |> select(state, called) |> left_join(x = st_drop_geometry(us_states_albers), by = join_by(name == state)) |> left_join(us_pop, by = join_by(fips_state)) write_rds(us_votes, "slides/data/us_votes.rds") ``` ] ] --- ### GeoJSON of German districts (NUTS-3) ```r giscoR::gisco_get_nuts(epsg = "3035", country = "DE", nuts_level = 3) |> select(nuts = NUTS_ID, name = NAME_LATN) |> write_sf("slides/data/de-nuts3.geojson") ``` --- ### Germany DEM raster ```r IsoriX::ElevRasterDE |> rast() |> `names<-`("elev") |> writeRaster("slides/data/de_elevation.tif", datatype = "INT2S") ```