HackMD
  • Prime
    Prime  Full-text search on all paid plans
    Search anywhere and reach everything in a Workspace with Prime plan.
    Got it
      • Create new note
      • Create a note from template
    • Prime  Full-text search on all paid plans
      Prime  Full-text search on all paid plans
      Search anywhere and reach everything in a Workspace with Prime plan.
      Got it
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # INBO CODING CLUB 26 October 2021 Welcome! ## Share your code snippet If you want to share your code snippet, copy paste your snippet within a section of three backticks (```): As an **example**: ``` library(tidyverse) ``` (*you can copy paste this example and add your code further down*) ## Participants Put your name + " | " and add a "*" each time you solve a challenge (see below). Example below, Damiano solved 2 challenges Name | Challenges --- | --- Damiano Oldoni | ** Lynn Pallemaerts |** Loïc van Doorn |** Joost Vanoverbeke | Hans Van Calster | *** Emma Cartuyvels|* Stien Heremans |* Marijke Thoonen |* Amber Mertens |*** Camille Van Eupen |** Jasmijn Hillaert|* Adriaan Seynaeve| Jeroen Speybroeck|** Geert Spanoghe | Jolien Goossens |*** ## Challenge 1 Hans ``` impatiens_df <- read_tsv("./data/20211026/20211026_impatiens_glandulifera.txt") #' 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. impatiens_sf <- impatiens_df %>% st_as_sf(coords = c("decimalLongitude", "decimalLatitude"), crs = 4326) #' How many layers does the geospatial file 20211026_protected_areas1.gpkg contain? st_layers(dsn = "./data/20211026/20211026_protected_areas.gpkg") #' Read the layer `ps_hbtrl`: call it `prot_areas` prot_areas <- read_sf("./data/20211026/20211026_protected_areas.gpkg", layer = "ps_hbtrl") prot_areas #' What is the coordinate reference system declared by user? Does it coincide #' with the real Geographic Coordinate Reference System (GEOCRS)? st_crs(prot_areas) #' Check that the CRS of `prot_areas` and `spatial_impatiens_df` are the same waldo::compare(st_crs(prot_areas), st_crs(impatiens_sf)) # apart from the input name, they are the same ``` Jolien ``` #' 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. impatiens_sp = st_as_sf(impatiens_df, coords = c("decimalLongitude", "decimalLatitude"), crs = 4326) #' How many layers does the geospatial file 20211026_protected_areas1.gpkg contain? st_layers(dsn = "./data/20211026/20211026_protected_areas.gpkg") #' Read the layer `ps_hbtrl`: call it `prot_areas` prot_areas = st_read("./data/20211026/20211026_protected_areas.gpkg", layer = "ps_hbtrl") #' What is the coordinate reference system declared by user? Does it coincide #' with the real Geographic Coordinate Reference System (GEOCRS)? st_crs(prot_areas) #' Check that the CRS of `prot_areas` and `spatial_impatiens_df` are the same st_crs(prot_areas) == st_crs(impatiens_sp) ``` Emma: ``` #' 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. spatial_impatiens_df <- st_as_sf(impatiens_df, crs = "EPSG:4326", coords = c("decimalLongitude", "decimalLatitude")) #' How many layers does the geospatial file 20211026_protected_areas.gpkg contain? st_layers("./data/20211026/20211026_protected_areas.gpkg") #' Read the layer `ps_hbtrl`: call it `prot_areas` prot_areas <- st_read("./data/20211026/20211026_protected_areas.gpkg", "ps_hbtrl") #' What is the coordinate reference system declared by user? Does it coincide #' with the real Geographic Coordinate Reference System (GEOCRS)? st_crs(prot_areas) #' Check that the CRS of `prot_areas` and `spatial_impatiens_df` are the same st_crs(prot_areas) st_crs(spatial_impatiens_df) ``` Jeroen S ``` setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) library(tidyverse) library(sf) impatiens_df <- read_tsv("20211026_impatiens_glandulifera.txt") #' 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. imp_sf <- st_as_sf(impatiens_df, coords = c("decimalLongitude", "decimalLatitude")) st_crs(imp_sf)<- 4326 st_crs(imp_sf) #' How many layers does the geospatial file 20211026_protected_areas1.gpkg contain? st_layers("20211026_protected_areas.gpkg") #' Read the layer `ps_hbtrl`: call it `prot_areas` prot_areas <- st_read("20211026_protected_areas.gpkg", layer = "ps_hbtrl") #' What is the coordinate reference system declared by user? Does it coincide #' with the real Geographic Coordinate Reference System (GEOCRS)? #' Check that the CRS of `prot_areas` and `spatial_impatiens_df` are the same ifelse(st_crs(prot_areas) == st_crs(imp_sf),"same","not the same") ``` Camille ``` library(tidyverse) library(sf) impatiens_df <- read_tsv("./data/20211026/20211026_impatiens_glandulifera.txt") #' 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. impatiens_sd <- st_as_sf(impatiens_df, coords = 17:16, crs = 4326) #' How many layers does the geospatial file 20211026_protected_areas.gpkg contain? st_layers('./data/20211026/20211026_protected_areas.gpkg') #' Read the layer `ps_hbtrl`: call it `prot_areas` prot_areas <- st_read('./data/20211026/20211026_protected_areas.gpkg', layer = 'ps_hbtrl') #' What is the coordinate reference system declared by user? Does it coincide #' with the real Geographic Coordinate Reference System (GEOCRS)? st_crs(prot_areas) #' Check that the CRS of `prot_areas` and `spatial_impatiens_df` are the same st_crs(prot_areas) == st_crs(impatiens_sd) ``` Amber ``` #' 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. impatiens_sf <- st_as_sf(impatiens_df, coords = c("decimalLongitude","decimalLatitude"), crs = 4326) #' How many layers does the geospatial file 20211026_protected_areas.gpkg contain? st_read("./data/20211026/20211026_protected_areas.gpkg") st_layers("./data/20211026/20211026_protected_areas.gpkg") # there are two layers: ps_hbtrl, ps_hbtrl_deel #' Read the layer `ps_hbtrl`: call it `prot_areas` prot_areas <- st_read("./data/20211026/20211026_protected_areas.gpkg", "ps_hbtrl") #' What is the coordinate reference system declared by user? Does it coincide #' with the real Geographic Coordinate Reference System (GEOCRS)? # CRS declared is WGS84 st_crs(prot_areas) # ID = 4326, so yes #' Check that the CRS of `prot_areas` and `spatial_impatiens_df` are the same if (st_crs(prot_areas) == st_crs(impatiens_sf)) { print("crs is the same") } else { print("crs not the same") } ``` ## Challenge 2 Hans ``` #' 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 prot_areas <- prot_areas %>% st_transform(crs = 3035) spatial_impatiens_df_3035 <- impatiens_sf %>% st_transform(crs = 3035) plot(prot_areas %>% st_geometry()) plot(spatial_impatiens_df_3035 %>% st_geometry()) #' 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 prot_areas %>% st_write(dsn = "./temp/prot_areas_and_impatiens_trs1989.gpkg", layer = "prot_areas") spatial_impatiens_df_3035 %>% st_write(dsn = "./temp/prot_areas_and_impatiens_trs1989.gpkg", layer = "impatiens_obs") #' 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` spatial_impatiens_df_3035 %>% st_buffer(dist = .$coordinateUncertaintyInMeters) %>% st_geometry() %>% plot() ``` Jolien ``` #' 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 prot_areas_etrs = st_transform(prot_areas, crs = 3035) impatiens_etrs = st_transform(impatiens_sp, crs = 3035) #' 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 st_write(prot_areas_etrs, layer = "prot_areas", dsn = "./data/20211026/prot_areas_and_impatiens_trs1989.gpkg") st_write(impatiens_etrs, layer = "impatiens_obs", dsn = "./data/20211026/prot_areas_and_impatiens_trs1989.gpkg") st_layers(dsn = "./data/20211026/prot_areas_and_impatiens_trs1989.gpkg") #' 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` st_crs(impatiens_etrs)$units # m -> ok impatiens_etrs_buffer = st_buffer(impatiens_etrs, impatiens_etrs$coordinateUncertaintyInMeters) impatiens_etrs_buffer %>% st_geometry() %>% plot() ``` Lynn ``` prot_areas_3035 <- st_transform(prot_areas, crs=3035) impatiens_3035 <- st_transform(impatiens_sf, crs=3035) prot_areas_3035 %>% st_write(dsn = "./data/20211026/prot_areas_and_impatiens_trs1989.gpkg", layer = "prot_areas") impatiens_3035 %>% st_write(dsn = "./data/20211026/prot_areas_and_impatiens_trs1989.gpkg", layer = "impatiens_obs") impatiens_3035 %>% st_buffer(dist = .$coordinateUncertaintyInMeters) %>% # st_geometry() %>% ggplot() + geom_sf() ``` Amber ``` ## CHALLENGE 2 #' 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 impatiens_sf_89 <- st_transform(impatiens_sf, 3035) st_crs(impatiens_sf_89) prot_areas_89 <- st_transform(prot_areas, 3035) st_crs(prot_areas_89) #' 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 st_write(prot_areas_89, "./data/20211026/prot_areas_and_impatiens_trs1989.gpkg", "prot_areas") st_write(impatiens_sf_89, "./data/20211026/prot_areas_and_impatiens_trs1989.gpkg", "impatiens_obs", append = TRUE) st_layers("./data/20211026/prot_areas_and_impatiens_trs1989.gpkg") #' 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` impatiens_sf_89_circles <- st_buffer(impatiens_sf_89, dist = impatiens_sf_89$coordinateUncertaintyInMeters) ``` Camille ``` ## CHALLENGE 2 #' 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 prot_areas_ETR <- prot_areas %>% st_transform(3035) impatiens_sd_ETR <- impatiens_sd %>% st_transform(3035) #' 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 st_write(prot_areas_ETR, dsn = "./data/20211026/prot_areas_and_impatiens_trs1989.gpkg", layer = 'prot_areas', delete_dsn = T) st_write(impatiens_sd_ETR, dsn = "./data/20211026/prot_areas_and_impatiens_trs1989.gpkg", layer = 'impatiens_obs') st_layers("./data/20211026/prot_areas_and_impatiens_trs1989.gpkg") ## Q: How to delete a layer from an existing geopackage? #' 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` impatiens_circles <- impatiens_sd_ETR %>% st_buffer(dist = impatiens_sd_ETR$coordinateUncertaintyInMeters) plot(st_geometry(impatiens_circles)) plot(st_geometry(impatiens_sd_ETR), col = 'red',pch = 16,cex = 0.2, add = T) ``` ## INTERMEZZO ## Challenge 3 ``` #' Using data in CRS 3035: #' 1. Find which observations, as points, are _contained_ in each protected area? spatial_impatiens_df_3035 %>% st_filter(prot_areas, join = st_contains) #' But we should better check which observations, as circles (!), _intersect_ #' each protected area. How to do it? impatiens_in_protected <- spatial_impatiens_df_3035 %>% st_buffer(dist = .$coordinateUncertaintyInMeters) %>% st_join(prot_areas, left = FALSE, join = st_intersects) #' So, how many observations in the protected area "Bos- en heidegebieden ten #' oosten van Antwerpen"? impatiens_in_protected %>% filter(NAAM == "Bos- en heidegebieden ten oosten van Antwerpen") %>% count() #' 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 issue? prot_areas_centroids <- prot_areas %>% st_centroid() ``` Jolien ``` #' Using data in CRS 3035: #' 1. Find which observations, as points, are _contained_ in each protected area? protarcont = st_contains(prot_areas_etrs, impatiens_etrs) plot(prot_areas_etrs) prot_areas_etrs %>% st_geometry() %>% plot() #' But we should better check which observations, as circles (!), _intersect_ #' each protected area. How to do it? protarsect = st_intersects(prot_areas_etrs, impatiens_etrs) #' So, how many observations in the protected area "Bos- en heidegebieden ten #' oosten van Antwerpen"? prot_areas_etrs %>% filter(NAAM == "Bos- en heidegebieden ten oosten van Antwerpen") %>% st_intersects(impatiens_etrs) #' 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 issue? prot_areas_etrs %>% st_centroid() %>% st_geometry() %>% plot() prot_areas_etrs %>% st_geometry() %>% plot() ``` Amber ``` ## CHALLENGE 3 #' Using data in CRS 3035: #' 1. Find which observations, as points, are _contained_ in each protected area? obs_in_prot_areas <- st_contains(prot_areas_89, impatiens_sf_89) #' But we should better check which observations, as circles (!), _intersect_ #' each protected area. How to do it? obs_intersect_prot_areas <- st_intersects(prot_areas_89, impatiens_sf_89_circles) #' So, how many observations in the protected area "Bos- en heidegebieden ten #' oosten van Antwerpen"? row_nr <- which(prot_areas_89$NAAM == "Bos- en heidegebieden ten oosten van Antwerpen") intersection_3 <- st_intersects(prot_areas_89, impatiens_sf_89_circles)[row_nr] # result is a list of 1 element nr_obs <- length(intersection_3[[1]]) # 4 observations found #' 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 issue? st_centroid(prot_areas_89) # this works st_centroid(prot_areas) # this does not work because 2 features with invalid geometry prot_areas_valid <- st_make_valid(prot_areas) st_centroid(prot_areas_valid) ``` ## Bonus challenge Amber ``` #' 2. How to get only the observations, as circles, **totally** contained in #' protected areas? Hint: check the cheat sheet obs_completely_within_prot_areas <- st_covered_by(impatiens_sf_89_circles, prot_areas_89) ```

    Import from clipboard

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub

        Please sign in to GitHub and install the HackMD app on your GitHub repo. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully