class: center, top ![:scale 30%](/assets/images/coding_club_logo_1.png) # 26 OCTOBER 2021 ## 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 ## Introduction: open software While preparing the coding club of May, we found [this](https://github.com/tidyverse/dplyr/issues/5891#event-5404935191) bug in `dplyr` package ![:scale 80%](/assets/images/20211026/20211026_issue_solved_dplyr.png) --- class: left, top ## Introduction: open software While preparing the coding club of September, we found [this](https://github.com/plotly/plotly.R/issues/2031) bug in `plotly` package ![:scale 80%](/assets/images/20211026/20211026_issue_solved_plotly.png) --- class: left, top ## Introduction: open software R and its packages are not static: software is written by humans. And humans can make errors. If you find something wrong in a R package, please contact the package maintainer!
Open software: it's good for you, for us, for everyone. --- class: center, middle ![:scale 90%](/assets/images/20211026/20211026_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/20211026/20211026_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/VSZBnLm-SE6N8fBKOG1zYw?both
--- class: left, top # Download data and code Today we will use an R script and some datasets. Download* the R script in `src ` and datasets in `data`. - R script: [20211026/20211026_challenges.R](https://github.com/inbo/coding-club/blob/master/src/20211026/20211026_challenges.R) - Dataset with GBIF observations of Himalayan balsam (_Impatiens glandulifera_) collected by INBO in 2016** [20211026/20211026_impatiens_glandulifera.txt](https://github.com/inbo/coding-club/blob/master/data/20211026/20211026_impatiens_glandulifera.txt). Source: [this](https://doi.org/10.15468/dl.q523g8) GBIF download - the Flemish protected areas: [`20211026/20211026_protected_areas.gpkg`](https://github.com/inbo/coding-club/blob/master/data/20211026/20211026_protected_areas.gpkg). Source:[geopunt](https://www.geopunt.be/catalogus/datasetfolder/a84a87f5-5607-4019-a8db-9d52a827786b)
* __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 ## Spatial coordinates Two types of spatila coordinates: - **projected** (or Cartesian) coordinates: refer to points on a flat space - **unprojected** or **geographic** coordinates,: refer to angles (latitude, longitude) pointing to locations on a sphere (or ellipsoid). 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! --- background-image: url(/assets/images/background_challenge_1.png) class: left, top # Challenge 1 1. Transform `impatiens_df` to a geospatial data.frame using `sf` package. Note that GBIF data are stored using WGS 84. Hint: find which numeric code is associated with WGS84 coordinate reference system. 2. How many layers does the geospatial file `20211026_protected_areas.gpkg` contain? 3. Read 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. Are the CRS of `prot_areas` and `spatial_impatiens_df` the same? --- class: left, top ### CRS by 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/20211026/20211026_epsg_codes.png) --- background-image: url(/assets/images/background_challenge_2.png) class: left, top # Challenge 2 1. Transform both `prot_areas` and `spatial_impatiens_df` to [European Terrestrial Reference System 1989](https://epsg.io/3035), the coordinate reference system used at EU level 2. Write the transformed data as a geopackage file called `prot_areas_and_impatiens_trs1989.gpkg` with two layers: the first called `prot_areas`, containing the protected areas, the second layer, `impatiens_obs`, containing the observations of Himalayan balsam 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_impatiens_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 "Bos- en heidegebieden ten oosten van Antwerpen"? 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? ![:scale 60%](/assets/images/20211026/20211026_wood_east_antwerp_impatiens.png) --- 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: based on the answer of 2 and 3, how to add to `prot_areas_3035` a column called `n_impatiens` with the number of observations for each protected area? --- class: left, top # Resources - [solutions](https://github.com/inbo/coding-club/blob/master/src/20211026/20211026_challenges_solutions.R) and [video recording](https://vimeo.com/640317182) are available - [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 in R](https://inbo.github.io/tutorials/tutorials/spatial_standards_vector/) - [`sf` cheatsheet](https://github.com/inbo/coding-club/blob/master/cheat_sheets/20211026_cheat_sheet_sf.pdf) - What your favorite [map projection](https://xkcd.com/977/) says 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, top ![:scale 30%](/assets/images/coding_club_logo_1.png) Room: Herman Teirlinck - 1.69 - Paul Janssen
Date: 25/11/2021, van 10:00 tot 12:00
(registration announced via DG_useR@inbo.be)