From e760cb43528636cad1714a5aa5ac039ed75c4354 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Fri, 22 Mar 2024 13:12:50 +0000 Subject: [PATCH] differences for PR #41 --- .DS_Store | Bin 6148 -> 6148 bytes 18-import-and-visualise-osm-data.md | 82 +++++++++++++++--- 19-basic-gis-with-r-sf.md | 65 +++++++------- 4-gis-slides.html | 67 +++++++------- ...se-osm-data-rendered-reproducibility-1.png | Bin 0 -> 75065 bytes ...se-osm-data-rendered-reproducibility-2.png | Bin 0 -> 60159 bytes ...se-osm-data-rendered-unnamed-chunk-8-1.png | Bin 76111 -> 73519 bytes ...-with-r-sf-rendered-unnamed-chunk-10-1.png | Bin 0 -> 61764 bytes ...s-with-r-sf-rendered-unnamed-chunk-2-1.png | Bin 15583 -> 10950 bytes ...s-with-r-sf-rendered-unnamed-chunk-4-1.png | Bin 11130 -> 7462 bytes ...s-with-r-sf-rendered-unnamed-chunk-6-1.png | Bin 20320 -> 15876 bytes ...s-with-r-sf-rendered-unnamed-chunk-7-1.png | Bin 17667 -> 13009 bytes ...s-with-r-sf-rendered-unnamed-chunk-8-1.png | Bin 73081 -> 70392 bytes ...s-with-r-sf-rendered-unnamed-chunk-9-1.png | Bin 73893 -> 71275 bytes fig/ConservationBrielle.png | Bin 0 -> 557148 bytes fig/ConservationBrielle_newrules.png | Bin 0 -> 569876 bytes md5sum.txt | 54 ++++++------ 17 files changed, 167 insertions(+), 101 deletions(-) create mode 100644 fig/18-import-and-visualise-osm-data-rendered-reproducibility-1.png create mode 100644 fig/18-import-and-visualise-osm-data-rendered-reproducibility-2.png create mode 100644 fig/19-basic-gis-with-r-sf-rendered-unnamed-chunk-10-1.png create mode 100644 fig/ConservationBrielle.png create mode 100644 fig/ConservationBrielle_newrules.png diff --git a/.DS_Store b/.DS_Store index 001027e649a1d4e79bfee5e2f2f2e3d77c1f14cf..bd4890cebb66a2d5381c1e80355c1f941275e913 100644 GIT binary patch delta 66 zcmZoMXfc=|#>B`mF;Q%yo+6MA*v-f>nTP51=2GTKEE@~z7&o(X@N)oFY%XN}&ODi4 T#FB#n2pAa{m^KH9Y+(ifTW}C+ delta 194 zcmZoMXfc=|#>B)qF;Q%yo+2a9#DLw41(+Bac_#BPzOH9wNMp!k$YDrjD9%YY3{K9^ zEnq+ZB-z}27nh`*{3M_x2eT|sJxk6}M<5$TMG65G1sUizZ02T~$g-K8gP#NF!p)3K X- @@ -285,15 +296,56 @@ So this reveals the historical centre of Brielle (or the city you chose) and the 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 +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") +``` + + + +```r +#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 @@ -308,8 +360,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, @@ -318,8 +375,9 @@ leaflet(buildings2) %>% bringToFront = TRUE)) ``` -
- +
+ + ::::::::::::::::::::::::::::::::: diff --git a/19-basic-gis-with-r-sf.md b/19-basic-gis-with-r-sf.md index aeaaf48f..aca2ead1 100644 --- a/19-basic-gis-with-r-sf.md +++ b/19-basic-gis-with-r-sf.md @@ -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 :::::::::::::::::::::::::::::::::::::::::::::::: @@ -30,6 +30,8 @@ library(sf) library(osmdata) library(leaflet) library(nominatimlite) +library(lwgeom) +library(here) assign("has_internet_via_proxy", TRUE, environment(curl::has_internet)) ``` @@ -74,7 +76,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)) ``` @@ -153,7 +157,9 @@ PROJCRS["Amersfoort / RD New", 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)) ``` @@ -189,7 +195,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)) ``` @@ -218,7 +225,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)) ``` @@ -229,21 +237,29 @@ 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 ``` + +```r +ggsave(filename = "fig/ConservationBrielle.png", + plot = p) +``` ::::::::::::::::::::::::::::::::::::: challenge @@ -289,17 +305,26 @@ 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 ``` + +```r +ggsave(filename = "fig/ConservationBrielle_newrules.png", + plot = pnew) +``` :::::::::::::::::::::::: @@ -314,21 +339,9 @@ single_buffer <- single_old_buffer %>% ```r single_buffer$area <- sf::st_area(single_buffer) %>% units::set_units(., km^2) -``` -```{.error} -Error in st_area.sfc(st_geometry(x), ...): package lwgeom required, please install it first -``` - -```r single_buffer$old_buildings_per_km2 <- as.numeric(single_buffer$n_buildings / single_buffer$area) -``` - -```{.error} -Error in `[[<-.data.frame`(`*tmp*`, i, value = numeric(0)): replacement has 0 rows, data has 159 -``` -```r ggplot() + geom_sf(data = buildings) + geom_sf(data = single_buffer, aes(fill=old_buildings_per_km2), colour = NA) + @@ -339,13 +352,7 @@ Error in `[[<-.data.frame`(`*tmp*`, i, value = numeric(0)): replacement has 0 ro option = "B") ``` -```{.error} -Error in `geom_sf()`: -! Problem while computing aesthetics. -ℹ Error occurred in the 2nd layer. -Caused by error: -! object 'old_buildings_per_km2' not found -``` + diff --git a/4-gis-slides.html b/4-gis-slides.html index cd154305..778dabc3 100644 --- a/4-gis-slides.html +++ b/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:

  • adding information on existing map objects

  • @@ -472,7 +472,7 @@

    What is OpenStreetMap? 🗾

-

OSM 🗾

+

OSM

The OSM information system relies on :

  • geometrical objects (i.e. points, lines, polygons)

  • @@ -488,7 +488,7 @@

    How to extract geospatial data from Open Street Map with R?

    You can also try with Naarden, Geertruidenberg, Gorinchem, Enkhuizen or Dokkum if you prefer.

-

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!