<!-- class: inverse, center, title-slide, middle --> class: center, middle <style> g { color: rgb(0,130,155) } r { color: rgb(174,77,41) } y { color: rgb(177,148,40) } </style> # Lecture 07: Geospatial Data Sciences # and Economic Spatial Models ## <img src="figs/bse_primary_logo.png" style="width: 35%" /><br><br>Bruno Conte ## 18-19/Feb/2026 --- # Geospatial Data and Spatial Models: Schedule 1. ~~Introduction to (spatial) data and programming in `R`~~ **[08/Jan/2026]** 2. ~~Week 2-4: Vector spatial data~~ **[14 - 29/Jan/2026]** 3. Week 5-7: Raster spatial data + (basic) interactive tools **[05 - 19/Feb/2026]** - ~~Week 5: Raster basics and unary operations using `sf` and `terra`~~ - ~~Week 6: Vector-raster operations~~ - Week 7: Interactive tools with `leaflet` and APIs 4. Week 8-10: Spatial models and applications with data **[25/Feb - 12 Mar/2026]**<br> <br> 5. <span style="color: rgb(177,148,40)">Take-home exam</span> **[27/Mar/2026]** --- class: center, middle # Before that: Instructions for the Final Exam --- # Geospatial Data and Spatial Models: Evaluation 1. Practical assignments (3 x 10%, in teams) 2. <span style="color: rgb(0,130,155)">Take-home exam</span> (70%, `.PDF` on Classroom, **<r>in groups</r>**): - Research idea: spatial data + problem = **research question** (related studies?) - Replication of tasks: data + tools `\(\leftrightarrow\)` empirical, quastion-based motivation - <y>Deadline</y>: March 27 11:59 pm - <u>Criteria</u>: - Motivation/research question: 40% - Implementation: 40% - "Presentation/neatness": 20% (what is a poorly presented document?) - Recall: deliver a **unique document** (and not PDF + code in separate files)! - Next: discussion on 3 past exams --- # Comments on the course evaluation (1/3) - <u>First example</u>: relationship between - Distance to city centers - Division between rural/urban worlds - City of Rennes as the "subject" (empirical object of interst) - <g>Combined data</g> on: - Election outcomes - Distance to downtown Rennes - Pattern visualization + regression analysis (plus) - <u>Strenghts</u>: well motivated and written, <y>clean implementation</y> of the course's tools --- # Comments on the course evaluation (2/3) - <u>Second example</u>: relationship between - Presence of soil organic carbon (SOC) - City development - Based on Fernihough and O’Rourke (2021)'s project on coal presence - Not a "novel" idea, but challenging adaptation and complete implementation (DiD) - <g>Combined data</g> on: - SOC within city buffers - City population over time - Pattern visualization + regression analysis = no relationship (?) - <u>Strenghts</u>: clean question, well motivated, <y>neatly written and implemented</y> --- # Comments on the course evaluation (3/3) - <u>Third example</u>: relationship between - EU's Regional Policy Funds - Poverty levels in Italy - Investigated a discontinuity on regional level-fund assignment - Inspired by other studies on the rules of fund assignment (Giua, 2017) - <g>Combined data</g> on: - Fund assignment and economic outcomes across Italian municipalities - Many data sources <r>were not spatial</r>, but discontinuity required *spatial operations* - <u>Strenghts</u>: strong motivation, <y>convincing evidence</y> of a potential research project --- # Course's Final Project/Term Paper/Evaluation: Takeaways - <u>Final project</u>: great opportunity to <g>give your ideas a try</g> - Can you implement spatial data tools to (try to) answer a creative reasearch question? - Questions do not need to be <y>exclusively related to economics</y> - DS-driven projects: ML applications of image classification, building pipelines of (geospatial) data retriving, ... - Is your question ambitious enough? Convince me of it! - <r>Any questions?</r> --- # Main references for this class 1. Agafonkin, V., 2023 <g>**Leaflet for R**</g>; in: [<u>https://rstudio.github.io/leaflet/</u>](https://rstudio.github.io/leaflet/) 2. Lovelace, R., Nowosad, J. and Muenchow, J., 2019. Geocomputation with R. Chapman and Hall/CRC. 3. Pebesma, E., 2018. Simple Features for R: Standardized Support for Spatial Vector Data. The R Journal 10 (1), 439-446 4. Wickham, H. and Grolemund, G., 2016. R for data science: import, tidy, transform, visualize, and model data. " O'Reilly Media, Inc.". --- # Geospatial data and interactive tools .pull-left[ So far, course content focused on data processing and <g>static data visualization</g> - Loading and processing spatial data - <r>Visualizing</r> spatial data in maps ] .pull-right[ <img src="figs/class03/unnamed-chunk-11-1.png" style="width: 100%" /> ] --- # Geospatial data and interactive tools .pull-left[ So far, course content focused on data processing and <g>static data visualization</g> - Loading and processing spatial data - <r>Visualizing</r> spatial data in maps - <y>Investigating data patterns</y> with plots (e.g., line, densities) ] .pull-right[ <img src="figs/raster08.png" style="width: 100%" /> ] --- # Geospatial data and interactive tools .pull-left[ <u>Interactive tools</u> with `leaflet`: * Create and insert <r>interactive dashboards</r> in `Rmarkdowns` * Develop [<u>interactive apps</u>](https://shiny.posit.co/r/gallery/interactive-visualizations/superzip-example/) if combined with `shiny` (not in this course!) ] -- .pull-right[ <img src="figs/class03/unnamed-chunk-11-1.png" style="width: 100%" /> ] --- # Geospatial data and interactive tools .pull-left[ <u>Interactive tools</u> with `leaflet`: * Create and insert <r>interactive dashboards</r> in `Rmarkdowns` * Develop [<u>interactive apps</u>](https://shiny.posit.co/r/gallery/interactive-visualizations/superzip-example/) if combined with `shiny` (not in this course!) <u>This class</u>: geospatial data + `leaflet` basics * Embedding `sf` data into `leaflet` dashboards * Customizing visualization * <y>Before:</y> is it (really) useful? ] .pull-right[
] --- # Geospatial data and interactive tools .pull-left[ <u>Road map for today:</u> * `leaflet`'s <g>map widget</g> and base maps * Incorporating <r>`sf` features</r>: * `POINT` data, popups, and labels * `LINESTRING` and `POLYGON` data * Incorporating <y>`terra` rasters</y> Main exposition in `01_class.07.R` * Tutorials [<u>here</u>](https://rstudio.github.io/leaflet/), [<u>here</u>](https://leafletjs.com/reference.html), and [<u>here</u>](https://leafletjs.com/examples.html) ] .pull-right[ ] --- # Geospatial data and interactive tools: the map widget .pull-left[ The <u>map widget</u>: interactive map window * `leaflet()`: "creates" the map widget * Customized options (e.g., zoom) ```r library(leaflet) # Creating widget: m <- leaflet() # empty m # Print the map ``` <u>Add data as layers</u> as in `ggplot()` * Use <g>**standard projection**</g> `EPSG:4326`!! ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ * <u>Adding `POINT`</u> data with: * `addCircles()` or `addMarkers()` * Example with `nz_height` ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map * addCircles(data = nz_height) # add layer m # Print the map ``` ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ * <u>Adding `POINT`</u> data with: * `addCircles()` or `addMarkers()` * Example with `nz_height` ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map * addMarkers(data = nz_height) # add layer m # Print the map ``` ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ * <u>Adding `POLYGONS`</u> data with `addPolygons()` * Example with `nz_height` + `nz` ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map * addPolygons(data = nz) %>% # add polygons addMarkers(data = nz_height) # add points m # Print the map ``` * `sf LINESTRING`: use `addPolylines()` ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ * <u>Adding backgroud</u> with `addTiles()` ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map * addTiles() %>% # add the background addMarkers(data = nz_height) # add points m # Print the map ``` * <r>Standard source:</r> Open Street Maps ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ <u>Customizing the view</u> with `setView()` * Setting coordiantes and zoom (0 to 18): ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map * setView( * # Mt. Cook coordinates: * lng = 170.141717, lat = -43.595296, * zoom = 3 * ) %>% addTiles() %>% # add the background addMarkers(data = nz_height) # add points m # Print the map ``` ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ <u>Customizing the view</u> with `setView()` * Setting coordiantes and zoom (0 to 18): ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map * setView( * # Mt. Cook coordinates: * lng = 170.141717, lat = -43.595296, * zoom = 13 * ) %>% addTiles() %>% # add the background addMarkers(data = nz_height) # add points m # Print the map ``` ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ <u>Change backgroud:</u> `addProviderTiles()` ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map * addProviderTiles( # custom background * provider = providers$CartoDB.Positron * ) %>% addMarkers(data = nz_height) # add points m # Print the map ``` <u>All providers:</u> `leaflet::providers` ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ <u>Adding popus</u> with `addMarkers()` ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map addTiles() %>% # add the background addMarkers( data = nz_height, * popup = ~elevation ) m # Print the map ``` <r>Important:</r> must be a `character`! ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ <u>Adding labels</u> with `addMarkers()` ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map addTiles() %>% # add the background addMarkers( data = nz_height, * label = ~elevation ) m # Print the map ``` <r>Important:</r> must be a `character`! ] .pull-right[
] --- # Geospatial data and interactive tools: the map widget .pull-left[ <u>Adding rasters</u> with `addRasterImage()` ```r library(leaflet) library(spData) # Creating widget: m <- leaflet() %>% # empty map addTiles() %>% * addRasterImage(r.elev) %>% # add a raster addCircles(data = nz_height) m # Print the map ``` * <g>**Watch out**</g> the raster extent! * Must be a `raster::raster()` object! * Is it (really) <r>**useful**</r>? ] .pull-right[
] --- class: center, middle # Spatial Data with APIs --- # Spatial Data with APIs <g>API</g> = Application Programming Interface - Used by web pages/systems, useful for us to retrieve data! .pull-left[ Example: [Long-lat:](https://www.latlong.net/convert-address-to-lat-long.html)</u> geocoding tool - Suppose we want to search for "Barcelona UPF" ] .pull-right[ .center[ <img src="figs/latlong1.png" style="width: 85%" /> ] ] --- # Spatial Data with APIs <g>API</g> = Application Programming Interface - Used by web pages/systems, useful for us to retrieve data! .pull-left[ Example: [Long-lat:](https://www.latlong.net/convert-address-to-lat-long.html)</u> geocoding tool - Suppose we want to search for "Barcelona UPF" - Typing it + click = result Is there an <r>automated way?</r> ] .pull-right[ .center[ <img src="figs/latlong2.png" style="width: 85%" /> ] ] --- # Spatial Data with APIs <u>Example</u>: https://geocode.maps.co/ API (register for an API key!) .pull-left[ Detailed exposition on `00_class07.R` - Permits automated "search+retrieve" tasks - Locate BCN `\(\rightarrow\)` `sf` object - Use additional `httr` library ] .pull-right[ .center[ <img src="figs/latlong3.png" style="width: 85%" /> ] ] --- class: center, middle # Your turn: Hands-on --- # Hands-on: your turn! (1/3) .pull-left[ Use this class' slides + `01_class07.R` to: - Reproduce [<u>Lecture 02's image</u>](https://brunoconteleite.github.io/08-geospatial-ds-bse/00_class02.html#19) as a `leaflet` widget - Use the `sf` objects as inputs - Add them all as <r>multiple layers</r> ] .pull-right[ .center[ <img src="figs/class02/unnamed-chunk-1-1.png" style="width: 90%" /> ] ] --- # Hands-on: your turn! (1/3) .pull-left[ Use this class' slides + `01_class07.R` to: - Reproduce [<u>Lecture 02's image</u>](https://brunoconteleite.github.io/08-geospatial-ds-bse/00_class02.html#19) as a `leaflet` widget - Use the `sf` objects as inputs - Add them all as <r>multiple layers</r> - <y>Visualize it:</y> ] .pull-right[
] --- # Hands-on: your turn! (2/3) .pull-left[ Use this class' slides + `01_class07.R` to: - Plot a subsample of <g>houses sold in Ohio</g> (`spData::house`) with `leaflet` widget - Use `sample_n(20)` to subsample the raw data (very large!) - <u>Recall:</u> it must be a `sf`, rightly projected object - <r>Show house prices</r> with a `label` - Use a customized background map tile - <y>Visualize it:</y> ] .pull-right[
] --- # Hands-on: your turn! (3/3) .pull-left[ Use the https://geocode.maps.co/ API to find > <r>UPF Barcelona</r> (space = "%20") Combine with BCN boundaries (<u>[here](https://opendata-ajuntament.barcelona.cat/data/dataset/a64ae340-d8f3-4fac-a9c8-50b08f9e1d9b/resource/98dc2539-af66-45b3-b4d2-30b4ba8af272/download)</u>) - Plot all results ] .pull-right[ .center[ <img src="figs/latlong4.png" style="width: 100%" /> ] ] --- # Hands-on: your turn! (3/3) .pull-left[ Use the https://geocode.maps.co/ API to find > <r>UPF Barcelona</r> (space = "%20") Combine with BCN boundaries (<u>[here](https://opendata-ajuntament.barcelona.cat/data/dataset/a64ae340-d8f3-4fac-a9c8-50b08f9e1d9b/resource/98dc2539-af66-45b3-b4d2-30b4ba8af272/download)</u>) - Plot all results Can you zoom for boundaries **<y>within 1.5 km from all UPF campi?</y>** ] .pull-right[ .center[ <img src="figs/latlong5.png" style="width: 100%" /> ] ] --- class: center, middle # Your turn: Hands-on