diff --git a/episodes/.DS_Store b/episodes/.DS_Store new file mode 100644 index 00000000..bd4890ce Binary files /dev/null and b/episodes/.DS_Store differ diff --git a/episodes/18-import-and-visualise-osm-data.Rmd b/episodes/18-import-and-visualise-osm-data.Rmd index 4c4ebc11..a6843488 100644 --- a/episodes/18-import-and-visualise-osm-data.Rmd +++ b/episodes/18-import-and-visualise-osm-data.Rmd @@ -27,7 +27,7 @@ knitr::opts_chunk$set(warning = FALSE, message = FALSE) ## What is OpenStreetMap? -OpenStreetMap (OSM) is a collaborative project which aims at mapping the world and sharing geospatial data in an open way. Anyone can contribute, by mapping geographical objects they encounter, by adding topical information on existing map objects (their name, function, capacity, etc.), or by mapping buildings and roads from satellite imagery (cf. [HOT: Humanitarian OpenStreetMap Team](https://www.hotosm.org/)). +OpenStreetMap (OSM) is a collaborative project which aims at mapping the world and sharing geospatial data in an open way. Anyone can contribute, by mapping geographical objects they encounter, by adding topical information on existing map objects (their name, function, capacity, etc.), or by mapping buildings and roads from satellite imagery. This information is then validated by other users and eventually added to the common "map" or information system. This ensures that the information is accessible, open, verified, accurate and up-to-date. @@ -50,10 +50,13 @@ assign("has_internet_via_proxy", TRUE, environment(curl::has_internet)) The first thing to do is to define the area within which you want to retrieve data, aka the *bounding box*. This can be defined easily using a place name and the package `nominatimlite` to access the free Nominatim API provided by OpenStreetMap. -We are going to look at *Brielle* together, but you can also work with the small cities of *Naarden*, *Geertruidenberg*, *Gorinchem*, *Enkhuizen* or *Dokkum*. +We are going to look at *Brielle* together. +::::::::::::::::::::::::::::::::::::: callout +Beware that downloading and analysing the data for larger cities might be long, slow and cumbersome on your machine. If you choose another location to work with, please try to choose a city of similar size! +:::::::::::::::::::::::::::::::::::::::::::::::: We first geocode our spatial text search and extract the corresponding polygon (`geo_lite_sf`) and then extract its bounding box (`st_bbox`). @@ -65,6 +68,11 @@ library(nominatimlite) nominatim_polygon <- geo_lite_sf(address = "Brielle", points_only = FALSE) bb <- st_bbox(nominatim_polygon) bb + +### OR +#install.packages("osmdata") +# library(osmdata) +# bb <- getbb("Brielle") ``` ::::::::::::::::::::::::::::::::::::: callout @@ -187,22 +195,62 @@ buildings$build_date <- if_else(start_date < 1900, 1900, start_date) ggplot(data = buildings) + geom_sf(aes(fill = build_date, colour=build_date)) + scale_fill_viridis_c(option = "viridis")+ - scale_colour_viridis_c(option = "viridis") + scale_colour_viridis_c(option = "viridis") + + coord_sf(datum = st_crs(28992)) ``` So this reveals the historical centre of Brielle (or the city you chose) and the various urban extensions through time. Anything odd? What? Around the centre? Why these limits / isolated points? +## Replicability + +We have produced a proof a concept on Brielle, but can we factorise our work to be replicable with other small fortified cities? You can use any of the following cities: *Naarden*, *Geertruidenberg*, *Gorinchem*, *Enkhuizen* or *Dokkum*. + +We might replace the name in the first line and run everything again. Or we can create a function. +```{r reproducibility} +extract_buildings <- function(cityname, year=1900){ + nominatim_polygon <- geo_lite_sf(address = cityname, points_only = FALSE) + bb <- st_bbox(nominatim_polygon) + + x <- opq(bbox = bb) %>% + add_osm_feature(key = 'building') %>% + osmdata_sf() + + buildings <- x$osm_polygons %>% + st_transform(.,crs=28992) + + start_date <- as.numeric(buildings$start_date) + + buildings$build_date <- if_else(start_date < year, year, start_date) + ggplot(data = buildings) + + geom_sf(aes(fill = build_date, colour=build_date)) + + scale_fill_viridis_c(option = "viridis")+ + scale_colour_viridis_c(option = "viridis") + + ggtitle(paste0("Old buildings in ",cityname)) + + coord_sf(datum = st_crs(28992)) +} + +#test on Brielle +extract_buildings("Brielle, NL") + +#test on Naarden +extract_buildings("Naarden, NL") + +``` ::::::::::::::::::::::::::::::::::::: challenge ## Challenge: import an interactive basemap layer under the buildings with `Leaflet` (20min) -- Check out the [leaflet package documentation](https://rstudio.github.io/leaflet/) -- Plot a basemap in Leaflet and try different tiles in the [basemap documentation](https://rstudio.github.io/leaflet/basemaps.html) -- Transform the buildings into WGS84 projection and add them to the basemap layer with the `addPolygons()` function. -- Have the `fillColor` of these polygons represent the `build_date` variable. See the [choropleth documentation](https://rstudio.github.io/leaflet/choropleths.html) for use of colors. Tip: use the examples given in the documentation and replace the variable names where needed. +Leaflet is a ["open-source JavaScript library for mobile-friendly interactive maps"](https://leafletjs.com/). Within R, the `leaflet` package allows you to build such interactive maps. As with `ggplot2`, you build a map with a collection of layers. In this case, you will have the leaflet basemap, some tiles, and shapes on top (such as markers, polygons, etc.). + +- Check out the [leaflet package documentation](https://rstudio.github.io/leaflet/) and GDCU cheatsheet. +- Plot a basemap using `leaflet` +- Add a layer of tiles, for instance provider tiles [basemap documentation](https://rstudio.github.io/leaflet/basemaps.html) +- Transform the buildings into WGS84 projection +- Add the building layer to your leaflet map using the `addPolygons()` function. +- Use the `fillColor` of these polygons represent the `build_date` variable. See the [choropleth documentation](https://rstudio.github.io/leaflet/choropleths.html) DCU cheatsheet for how to use of colors in polygons. :::::::::::::::::::::::: solution @@ -216,8 +264,13 @@ library(leaflet) buildings2 <- buildings %>% st_transform(.,crs=4326) +# leaflet(buildings2) %>% +# addTiles() %>% +# addPolygons(fillColor = ~colorQuantile("YlGnBu", -build_date)(-build_date)) + + # For a better visual rendering, try: + leaflet(buildings2) %>% -# addTiles() addProviderTiles(providers$CartoDB.Positron) %>% addPolygons(color = "#444444", weight = 0.1, smoothFactor = 0.5, opacity = 0.2, fillOpacity = 0.8, @@ -226,6 +279,7 @@ leaflet(buildings2) %>% bringToFront = TRUE)) ``` + ::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::: diff --git a/episodes/19-basic-gis-with-r-sf.Rmd b/episodes/19-basic-gis-with-r-sf.Rmd index 669b18c8..860a8666 100644 --- a/episodes/19-basic-gis-with-r-sf.Rmd +++ b/episodes/19-basic-gis-with-r-sf.Rmd @@ -17,7 +17,7 @@ After completing this episode, participants should be able to… - Perform geoprocessing operations such as unions, joins and intersections with dedicated functions from the `sf` package - Compute the area of spatial polygons - Create buffers and centroids -- Map the results +- Map and save the results :::::::::::::::::::::::::::::::::::::::::::::::: ```{r setup, include=FALSE} @@ -31,6 +31,8 @@ library(sf) library(osmdata) library(leaflet) library(nominatimlite) +library(lwgeom) +library(here) assign("has_internet_via_proxy", TRUE, environment(curl::has_internet)) ``` @@ -69,7 +71,9 @@ buildings$start_date <- as.numeric(buildings$start_date) old_buildings <- buildings %>% filter(start_date <= old) - ggplot(data = old_buildings) + geom_sf(colour="red") + ggplot(data = old_buildings) + + geom_sf(colour="red") + + coord_sf(datum = st_crs(28992)) ``` @@ -99,7 +103,9 @@ st_crs(old_buildings) buffer_old_buildings <- st_buffer(x = old_buildings, dist = distance) -ggplot(data = buffer_old_buildings) + geom_sf() +ggplot(data = buffer_old_buildings) + + geom_sf() + + coord_sf(datum = st_crs(28992)) ``` @@ -134,7 +140,8 @@ centroids_old <- st_centroid(old_buildings) %>% ggplot() + geom_sf(data = single_old_buffer, aes(fill=ID)) + - geom_sf(data = centroids_old) + geom_sf(data = centroids_old) + + coord_sf(datum = st_crs(28992)) ``` ## Intersection @@ -160,7 +167,8 @@ Now, we would like to distinguish conservation areas based on the number of hist begin = 0.6, end = 1, direction = -1, - option = "B") + option = "B") + + coord_sf(datum = st_crs(28992)) ``` `st_intersection` here adds the attributes of the intersected polygon buffers to the data table of the centroids. This means we will now know about each centroid, the ID of its intersected polygon-buffer, and a variable called "n" which is population with 1 for everyone. This means that all centroids will have the same weight when aggregated. @@ -169,18 +177,24 @@ We aggregate them by ID number (`group_by(ID)`) and sum the variable `n` to know ### Final output: -Let's map this layer over the initial map of individual buildings. +Let's map this layer over the initial map of individual buildings, and save the result. ```{r} -ggplot() + +p <- ggplot() + geom_sf(data = buildings) + geom_sf(data = single_buffer, aes(fill=n_buildings), colour = NA) + scale_fill_viridis_c(alpha = 0.6, begin = 0.6, end = 1, direction = -1, - option = "B") + option = "B") + + coord_sf(datum = st_crs(28992)) + p + +ggsave(filename = "fig/ConservationBrielle.png", + plot = p) + ``` ::::::::::::::::::::::::::::::::::::: challenge @@ -227,14 +241,21 @@ centroid_by_buffer <- centroids_buffers %>% single_buffer <- single_old_buffer %>% mutate(n_buildings = centroid_by_buffer$n) - ggplot() + + +pnew <- ggplot() + geom_sf(data = buildings) + geom_sf(data = single_buffer, aes(fill = n_buildings), colour = NA) + scale_fill_viridis_c(alpha = 0.6, begin = 0.6, end = 1, direction = -1, - option = "B") + option = "B") + + coord_sf(datum = st_crs(28992)) + + pnew + +ggsave(filename = "fig/ConservationBrielle_newrules.png", + plot = pnew) ``` :::::::::::::::::::::::: diff --git a/instructors/4-gis-slides.html b/instructors/4-gis-slides.html index cd154305..778dabc3 100644 --- a/instructors/4-gis-slides.html +++ b/instructors/4-gis-slides.html @@ -445,7 +445,7 @@

Learning objectives

-

What is OpenStreetMap? 🗺

+

What is OpenStreetMap?

@@ -463,7 +463,7 @@

What is OpenStreetMap? 🗺

-

What is OpenStreetMap? 🗾

+

What is OpenStreetMap?

Anyone can contribute, by:

-

OSM 🗾

+

OSM

The OSM information system relies on :

-

The Bounding Box ⬛

+

The Bounding Box

We first geocode our spatial text search and extract the corresponding polygon (geo_lite_sf) and then extract its bounding box (st_bbox).

nominatim_polygon <- nominatimlite::geo_lite_sf(address = "Brielle", points_only = FALSE)
@@ -501,7 +501,7 @@ 

The Bounding Box ⬛

-

The Bounding Box 🔳

+

The Bounding Box

A Problem with download? Try:

assign("has_internet_via_proxy", TRUE, environment(curl::has_internet))
@@ -735,11 +735,11 @@ 

Mapping urbanisation in Brielle

-

⏰ Challenge

+

Challenge

Import an interactive basemap layer under the buildings with Leaflet

-
+
20:00
@@ -754,11 +754,11 @@

⏰ Challenge

-

⏰ To submit your work in Zoom:

+

To submit your work in Zoom:

-

⏰ One solution

+

One solution

buildings2 <- buildings %>%
   st_transform(.,crs=4326)
@@ -772,8 +772,8 @@ 

⏰ One solution

highlightOptions = highlightOptions(color = "white", weight = 2, bringToFront = TRUE))
-
- +
+
@@ -812,7 +812,7 @@

Objectives:

-

the ‘sf’ package 📦

+

the ‘sf’ package

-

the ‘sf’ cheatsheet 🗾

+

the ‘sf’ cheatsheet

-

the ‘sf’ cheatsheet 🗾

+

the ‘sf’ cheatsheet

-

Conservation in Brielle, NL 🏭

+

Conservation in Brielle, NL

Let’s focus on old buildings and imagine we’re in charge of their conservation. We want to know how much of the city would be affected by a non-construction zone of 100m around pre-1800 buildings.

Let’s select them and see where they are.

-

Conservation in Brielle, NL 🏫

+

Conservation in Brielle, NL

old <- 1800 # year prior to which you consider a building old
 
@@ -851,12 +851,12 @@ 

Conservation in Brielle, NL 🏫

summary(buildings$start_date)
   Length     Class      Mode 
-    10604 character character 
+ 10601 character character
-

Conservation in Brielle, NL 🏨

+

Conservation in Brielle, NL

buildings$start_date <- as.numeric(as.character(buildings$start_date))
 
@@ -966,13 +966,14 @@ 

Centroids

Centroids

-
sf::sf_use_s2(FALSE) # s2 works with geographic projections, so to calculate centroids in projected CRS units (meters), we need to disable it.
-centroids_old <- st_centroid(old_buildings) %>%
-  st_transform(.,crs=28992)  
-
-ggplot() + 
-    geom_sf(data = single_old_buffer, aes(fill=ID)) +
-    geom_sf(data = centroids_old)
+
sf::sf_use_s2(FALSE)  # s2 works with geographic projections, so to calculate centroids in projected CRS units (meters), we need to disable it
+
+centroids_old <- st_centroid(old_buildings) %>%
+  st_transform(.,crs=28992)  
+
+ggplot() + 
+    geom_sf(data = single_old_buffer, aes(fill=ID)) +
+    geom_sf(data = centroids_old)
@@ -1031,12 +1032,12 @@

Final Output

-

⏰Challenge: Conservation rules have changed!

+

Challenge: Conservation rules have changed!

The historical threshold now applies to all pre-WWII buildings, but the distance to these building is reduced to 10m.

Can you map the number of all buildings per 10m fused buffer?

-
+
10:00
@@ -1106,10 +1107,10 @@

Problem

-

⏰ Challenge: visualise the density of old buildings

+

Challenge: visualise the density of old buildings

-
+
10:00
@@ -1160,7 +1161,7 @@

What’s next?

-

🦜 A few words of caution

+

A few words of caution

We have taught you to think, type, try, test, read the documentation. This is not only the old fashion way, but the foundation.

When you encounter a bug, a challenge, a question that we have not covered, you could always make use of:

    @@ -1170,12 +1171,12 @@

    🦜 A few words of caution

-

🦜 A few words of caution

+

A few words of caution

Be careful to keep it as a help, tool and support whose quality you can still assess.

They can provide fixes that you do not understand, answers that don’t make sense, and even wrong answers! So build yourself some foundations before you get into them.

-

🐰The end… wait!

+

The end… wait!

Any questions?

@@ -1225,7 +1226,7 @@

Call for helpers!

This strengthens the community… and it can bring you GS Credits.

-

🐰The end… for real!

+

The end… for real!