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 February, 2019 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**: ```r library(tidyverse) ... ``` (*you can copy paste this example and add your code further down, but do not fill in your code in this section*) Your snippets:library ```r ``` ## Problems loading `sf` and `tidyverse` in same session Everybody today had problems loading correctly `sf` and `tidyverse`. We all got this: ```r > library(sf) > library(tidyverse) Error in get(genname, envir = envir) : object 'group_map' not found In addition: Warning message: package ‘tidyverse’ was built under R version 3.5.2 Error in get(genname, envir = envir) : object 'group_split' not found -- Attaching packages --------------------------------------- tidyverse 1.2.1 -- v ggplot2 3.1.0 v purrr 0.3.0 v tibble 2.0.99.9000 v dplyr 0.7.8 v tidyr 0.8.2 v stringr 1.3.1 v readr 1.3.1 v forcats 0.3.0 -- Conflicts ------------------------------------------ tidyverse_conflicts() -- x dplyr::filter() masks stats::filter() x dplyr::lag() masks stats::lag() Warning messages: 1: package ‘readr’ was built under R version 3.5.2 2: package ‘purrr’ was built under R version 3.5.2 ``` Well, actually we are not the only ones: check [here](https://github.com/r-spatial/sf/issues/969#issuecomment-467367771)! And check the answer provided by the head developer of `sf`! Just update `dplyr` to version 0.8.0! Restart R (Ctrl+Shift+10) and then: ``` devtools::install_github("tidyverse/dplyr") ``` if you load tidyverse now, you can notice the new version: 0.8.0.9002. ```r > library(tidyverse) -- Attaching packages --------------------------------------- tidyverse 1.2.1 -- v ggplot2 3.1.0 v purrr 0.3.0 v tibble 2.0.1.9001 v dplyr 0.8.0.9002 v tidyr 0.8.2 v stringr 1.3.1 v readr 1.3.1 v forcats 0.3.0 -- Conflicts ------------------------------------------ tidyverse_conflicts() -- x dplyr::filter() masks stats::filter() x dplyr::lag() masks stats::lag() Warning messages: 1: package ‘tidyverse’ was built under R version 3.5.2 2: package ‘readr’ was built under R version 3.5.2 3: package ‘purrr’ was built under R version 3.5.2 ``` Now, if you load `sf` no errors anymore occur: ```r > library(sf) Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3 Warning message: package ‘sf’ was built under R version 3.5.2 ``` ## Alternative way to specify file paths ```r filename <- "mydata.csv" paste0("../data/", filename) file.path("../data", filename) file.path("..", "data", filename) ``` ```r library(here) filename <- here("data", "mydata.csv") ``` ## Trick to run unzip only when needed ```r # file.exists checks if a file exists # ! is the negation if (!file.exists("../data/be_10km.shp")) { # this is only run when "be_10km.shp" doesn't exists in "../data" unzip("../data/20190226_utm10_bel.zip", exdir = "../data") } ``` ## Challenge 1 ### Peter 1. Load EEA reference grid: ```r grid <- st_read("../data/20190226_utm10_bel", "be_10km.shp") ``` 2. Create an sf data.frame from crab data (= adding a geometry column to original data.frame): ```r spatial_crab_df <- st_as_sf( crab_df, coords = c("decimalLongitude", "decimalLatitude"), crs = 4326 ) ``` Note that `x` = longitude (horizontal on map) and `y` = latitude (vertical on map). Don't forget to define the CRS (`4326` for WGS84). 3. Compare CRS: ```r st_crs(grid) st_crs(spatial_crab_df) ``` ### Floris ```r # 1. read shapefile UTM utm10bel <- st_read("../data/20190226_utm10_bel/be_10km.shp") # 2. set CRS of crab_df str(crab_df, give.attr = FALSE) class(crab_df) spatial_crab_df <- st_as_sf(crab_df, coords = c("decimalLongitude", "decimalLatitude")) spatial_crab_df st_crs(spatial_crab_df) <- 4326 # or at once: # spatial_crab_df <- st_as_sf(crab_df, # coords = c("decimalLongitude", "decimalLatitude"), crs = 4326) spatial_crab_df class(spatial_crab_df) spatial_crab_df$geometry # 3. compare crs st_crs(spatial_crab_df) st_crs(utm10bel) ``` ## Challenge 2 ### floris ```r # 1. How many occurrences of Chinese mitten crab in each cell SINCE 2015? crabcounts <- utm10bel %>% st_join(spatial_crab_df %>% # hint: use the CRS of the POLYGONS! st_transform(crs = 3035), left = FALSE) %>% filter(year >= 2015) %>% group_by(CELLCODE) %>% summarise(number_occ = n()) %>% st_drop_geometry crabcounts # 2. Find the 5 cells with the highest number of occurrences. crabcounts %>% arrange(desc(number_occ)) %>% slice(1:5) ## Alternative crabcounts %>% top_n(5, number_occ) ``` Spatial joins: st_join(sf-object1, sf-object2, join = ) - "join" argument is used for the topological operator - default topological operator used by st_join() is st_intersects() - when more than one row of the 2nd object topologically match the 1st, then rows of the 1st are duplicated! Resource: - https://geocompr.robinlovelace.net/ - https://geocompr.robinlovelace.net/spatial-operations.html#spatial-vec ### Damiano Thanks Floris to show how to use the powerful and general function `st_join()`! Here below how to do it by using `st_intersection()` which also relies on `st_intersects()`: ```r # get CRS of the grid crs_belgium <- st_crs(belgium_grid) # convert CRS of points to CRS of grid spatial_crab_df <- spatial_crab_df %>% filter(year >= 2015) %>% st_transform(crs_belgium) # points on grid assign_grid_to_pts <- st_intersection( x = spatial_crab_df, y = belgium_grid) # or via pipe assign_grid_to_pts <- spatial_crab_df %>% st_intersection(eugrid_shape) # counts pts in grid n_pts_grid <- assign_grid_to_pts %>% group_by(CELLCODE) %>% count() %>% ungroup() # get the 5 cells with the highest number of pts n_pts_grid_top5 <- n_pts_grid %>% top_n(n = 5, wt = n) n_pts_grid_top5 ``` ## Challenge 3 ### Ivy ```r # 1. Read areas from first layer ("ps_hbtrl") st_layers("data/20190226_ps_hbtrl") HabRL <- st_read("data/20190226_ps_hbtrl/ps_hbtrl.shp", crs = 31370) # 2. Create a buffer of 1km around the protected areas st_crs(HabRL)$units # in "m", dus 1km = 1000m HabRL_buffer <- st_buffer(HabRL, dist = 1000) # 3. How far is the nearest Chinese mitten crab for each area? spatial_crab_df3 <- st_transform(spatial_crab_df, crs = 31370) Afstand <- spatial_crab_df3 %>% st_distance(HabRL_buffer) %>% # distance matrix met punten in de rijen en areas in de kolommen as.data.frame() ``` ### Damiano ```r # Get layers in folder 20190226_ps_hbtrl st_layers("../data/20190226_ps_hbtrl") # Read Habitatrichtlijngebieden from folder habitats <- st_read("../data/20190226_ps_hbtrl", layer = "ps_hbtrl") # you can read the right layer directly by passing the right file: ps_hbtrl.shp habitats <- st_read("../data/20190226_ps_hbtrl/ps_hbtrl.shp") # CRS not directly found (EPSG: NA) although the rest is present: st_crs(habitats) # assign CRS from metadata: Belge 1972 / Belgian Lambert 72 (EPSG:31370) found in the link habitats <- habitats %>% st_transform(crs = 31370) st_crs(habitats) # check class of geomtry: point? polygon? multipolygon? class(habitats$geometry) # Create buffer of 1km (check the measurement unit you are using by checking the last # part of message returned by st_crs(habitats)), or by running st_crs(habitats)$units. it will return "m", isn't? st_crs(habitats)$units habitats_buffer <- habitats %>% st_buffer(dist = 1000) # Change CRS of crab data spatial_crab_df_2015 <- spatial_crab_df_2015 %>% st_transform(crs = 31370) # Get an idea what I am doing ggplot() + geom_sf(data = habitats_buffer) + geom_sf(data = spatial_crab_df_2015) # Measure distances from all pts to all polygons distances <- st_distance(spatial_crab_df_2015, habitats_buffer, by_element = FALSE) distances_df <- as_data_frame(distances) # add the least distance of the crabs to each buffered area as column least_dist habitats_buffer <- habitats_buffer %>% mutate(least_dist = map_dbl(distances_df, min, na.rm = TRUE)) # get a look View(habitats_buffer) ```

    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