class: center, top ![:scale 30%](/assets/images/coding_club_logo_1.png) # 25 OCTOBER 2022 ## INBO coding club Herman Teirlinck
01.05 - Isala Van Diest --- class: left, top ## ROOMIE: room reservation !! Do not forget to confirm the room reservation via _roomie_ !! --- class: left, top # The INBO coding club effect In the challenge solutions of last year about this topic, we wrote: ``` > # deleting a layer is not possible yet, but will be implemented soon # See https://github.com/r-spatial/sf/issues/1828 ``` We wrote an issue, and Edzer Pebesma added a function to sf package! ![:scale 90%](/assets/images/20221025/20221025_coding_club_effect.png) --- class: center, middle ![:scale 90%](/assets/images/20221025/20221025_spatialdata_badge.png) --- class: left, top Install the package: ```r install.packages("sf") ``` Load the package: ```r library(sf) ``` --- class: center, top ![:scale 100%](/assets/images/20221025/20221025_sf_cheatsheet.png)
sf [cheatsheet](https://github.com/inbo/coding-club/blob/master/cheat_sheets/20211026_cheat_sheet_sf.pdf) See also this very nice [article with some examples](https://r-spatial.github.io/sf/articles/sf2.html) and the excellent book [geocomputation with R](https://geocompr.robinlovelace.net/). --- class: center, top ### How to get started? Check the [Each session setup](https://inbo.github.io/coding-club/gettingstarted.html#each-session-setup) to get started. ### First time coding club? Check the [First time setup](https://inbo.github.io/coding-club/gettingstarted.html#first-time-setup) section to setup. --- class: left, top ![:scale 100%](/assets/images/coding_club_sticky_concept.png) --- class: center, top ### Share your code during the coding session! Go to https://hackmd.io/rBatlgcTREOTnAYCs7VLrQ?both
--- class: left, top # Introduction: sp package Once upon a time... ![:scale 100%](/assets/images/20221025/20221025_sp_package.png) Still, [sp](https://edzer.github.io/sp) package has been used by a lot of researchers/developers. Some packages could still depend on it. --- class: left, top # Introduction: sf package - simple features ![:scale 100%](/assets/images/20221025/20221025_simple_features_sf.png) --- class: left, top # Introduction: sf package - simple features ![:scale 100%](/assets/images/20221025/20221025_sf_object.png) --- class: left, top # Download data and code You can download the material of today: - automatically via `inborutils::setup_codingclub_session()`* - manually** from GitHub folders [coding-club/data/20221025](https://github.com/inbo/coding-club/tree/master/data/20221025) and [coding-club/src/20221025](https://github.com/inbo/coding-club/tree/master/src/20221025)
__\* Note__: you can use the date in "YYYYMMDD" format to download the coding club material of a specific day, e.g. run `setup_codingclub_session("20220428")` to download the coding club material of April, 28 2022. If date is omitted, i.e. `setup_codingclub_session()`, the date of today is used. For all options, check the [tutorial online](https://inbo.github.io/tutorials/tutorials/r_setup_codingclub_session/).
__\*\* Note__: check the getting started instructions on [how to download a single file](https://inbo.github.io/coding-club/gettingstarted.html#each-session-setup)
--- class: left, top # Data description - [20221025_ludwigia_aquaticum.txt](https://github.com/inbo/coding-club/blob/master/data/20221025/20221025_Ludwigia_grandiflora.txt): dataset with GBIF observations of water primrose (EN) - waterteunisbloem (NL) (_Ludwigia grandiflora_) in Flanders from 2021. Source: GBIF.org (24 October 2022) GBIF Occurrence Download https://doi.org/10.15468/dl.sg4x9v - [20221025_protected_areas.gpkg](https://github.com/inbo/coding-club/blob/master/data/20221025/20221025_protected_areas.gpkg): the Flemish protected areas. Source: [geopunt](https://www.geopunt.be/catalogus/datasetfolder/a84a87f5-5607-4019-a8db-9d52a827786b) - [20221025_flemish_provinces.rds](https://github.com/inbo/coding-club/blob/master/data/20221025/20221025_flemish_provinces.rds): the Belgian provinces as an sp object and saved as a rds file. Source: the R package [BelgiumMaps.StatBel](https://www.bnosac.be/index.php/blog/55-belgiummaps-statbel-r-package-with-administrative-boundaries-of-belgium) --- class: left, top # Spatial coordinates Two types of spatial coordinates: - **projected** (or Cartesian) coordinates: refer to points on a flat space. The coordinates are the distance along the x and y axis - **unprojected** or **geographic** coordinates: refer to angles (latitude, longitude) pointing to locations on a sphere (or ellipsoid). For the math lovers: the flat space is also referred to as R
2
, the sphere as S
2
. No geospatial data without specifying the **Coordinate Reference System** (CRS) you work with! And what is a projection? A way to visualize a surface of a sphere on a flat space. --- background-image: url(/assets/images/background_challenge_1.png) class: left, top # Challenge 1 Using the cheatsheet and starting from the code provided: 1. Create a geospatial data.frame called `spatial_ludwigia_df` starting from `ludwigia_df`. Note that GBIF data are stored using WGS 84. Hint: find first which numeric code is associated with WGS84 coordinate reference system and use the cheatsheet. 2. How many layers does the geospatial file `20221025_protected_areas.gpkg` contain? 3. Import the layer `ps_hbtrl`: call it `prot_areas` 4. What is the CRS declared by user? Does it coincide with the real Geographic Coordinate Reference System (GEOCRS)? 5. Do `prot_areas` and `spatial_ludwigia_df` have the same CRS? 6. Read the Belgian provinces rds file as `be_provinces_sp` (the code is given!). What is the class of this variable? From which package? How to transform it to a sf object? 7. Extract the Flemish provinces. Hint: do it as you would do it in a standard data.frame. The motto of the sf package is "Spatial data, simplified" for a reason! --- class: left, top # Intermezzo: CRS & EPSG codes A common way to specify the CRS is by providing the EPSG (numeric) code. EPSG stands for European Petroleum Survey Group and is an organization that maintains a geodetic parameter database with standard codes, the EPSG codes, for coordinate systems, datums, spheroids, units and such alike. There are a lot of EPSG codes! Full list: https://spatialreference.org/ ![:scale 80%](/assets/images/20221025/20221025_epsg_codes.png) --- background-image: url(/assets/images/background_challenge_2.png) class: left, top # Challenge 2 1. Transform both `prot_areas` and `spatial_ludwigia_df` to [European Terrestrial Reference System 1989](https://epsg.io/3035) (EPSG: 3035), the coordinate reference system used at EU level 2. Write the transformed data as a geopackage file called `prot_areas_and_ludwigia_3035.gpkg` with two layers: the first called `prot_areas`, containing the protected areas, the second layer, `ludwigia_obs`, containing the observations of water primrose 3. Due to spatial uncertainty (gridded data, GPS uncertainty, etc.) observations should not be idealized as points in space, but as circles. Create such circles using the values store in column `coordinateUncertaintyInMeters` for `spatial_ludwigia_df_3035` --- class: left, top ## Intermezzo: what happened in 2020? Up to version 0.9.x `sf` returned warnings to the user when it was doing R
2
flat computations with data using geographical coordinates (spherical coords, e.g. WGS84) with messages like ``` although coordinates are longitude/latitude, st_intersects assumes that they are planar ``` Starting with version 1.0, `sf` uses the new package `s2` (Dunnington, Pebesma, Rubak 2020) for **spherical geometry**! They made our life much easier. Nice to know: s2 package is a wrapper around the C++ `s2geometry` library, written by Google and used e.g. in Google Maps, Google Earth Engine, Bigquery GIS. More info in the vignette [Spherical geometry in sf using s2geometry](https://r-spatial.github.io/sf/articles/sf7.html). --- background-image: url(/assets/images/background_challenge_3.png) class: left, top # Challenge 3 Using data saved in CRS 3035: 1. Find which observations, as points, are _contained_ in each protected area? 2. But we should better check which observations, as circles (!), _intersect_ each protected area. How to do it? 3. So, how many observations in the "Demervallei"? 4. Sometimes it's interesting to calculate the centroid of a polygon, e.g. for visualizations. Easy by using sf function `st_centroids()`. However, you get an error while calculating the centroids of `prot_areas`. What does it mean? How to solve the problem? --- class: left, top # Bonus challenge 1. How to get only the observations, as circles, **totally** contained in protected areas? Hint: check the cheat sheet 2. Not a sf question, but still nice to solve: how to add to `prot_areas_3035` a column called `n_ludwigia` with the number of observations for each protected area? --- class: left, top # Resources - Challenges [solutions](https://github.com/inbo/coding-club/blob/master/src/20221025/20221025_challenges_solutions.R) - Edited [video recording](https://vimeo.com/764880947) available on the Vimeo's INBO coding club channel - [Geocomputation with R](https://geocompr.robinlovelace.net/) - [Spatial Data Science](https://keen-swartz-3146c4.netlify.com/) - INBO tutorial on [how to use open vector file formats](https://inbo.github.io/tutorials/tutorials/spatial_standards_vector/) such as geopackage in R - [`sf` cheatsheet](https://github.com/inbo/coding-club/blob/master/cheat_sheets/20221025_cheat_sheet_sf.pdf) even if it is far from being the best cheatsheet ever - All [articles](https://r-spatial.github.io/sf/articles/sf1.html) on sf package documentation webpage are great material and very well written - What does your favorite [map projection](https://xkcd.com/977/) say about you? Extremely funny! - Keynote "Spherical geometry" of Edzer Pebesma at the OpenGeoHub summer school 2021: [slides](https://edzer.github.io/spherical_geometry/#1) - [`sf` article with some examples](https://r-spatial.github.io/sf/articles/sf2.html) - Keynote "R spatial" of Edzer Pebesma at the useR! 2021 conference: [video](https://www.youtube.com/watch?v=cK08bxUJn5A) and [slides](https://edzer.github.io/UseR2021/#1) - everything what Edzer Pebesma says or write, actually --- class: center, middle ![:scale 30%](/assets/images/coding_club_logo_1.png) Room: Herman Teirlinck - 01.17 - Clara Peeters
Date: 24/11/2022, van 10:00 tot 12:30
(registration announced via DG_useR@inbo.be)