owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
26 March 2020
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*)
### Deelnemers
No yellow sticky notes today. Just put a "|" if you are present and add a "*" each time you solve a challenge.
E.g. Damiano Oldoni (he is present and 1st challenge solved) | *
Patrik Oosterlynck |***
An Leyssen |**
Raïsa Carmen |
Els De bie |
Yasmine Verzelen |
Peter Van Gossum
Wouter Van Landuyt
Katrijn Alaerts
Sander Devisscher
Martijn Bollen |**
Jim Casaer
Leen Govaere |
An Vanden Broeck |
Erika Van den Bergh|
Ruben Elsen
Wim Mertens|
Dries Adriaens
Nicolas Noé
Maxime Sweetlove
Arno Thomaes |
Stien Heremans
Tanja Milotic
joost vanoverbeke
Pieterjan Verhelst |
Luc De Bruyn | **
Anja Leyman|**
Toon Spanhove
Loïc van Doorn |
Maarten Stevens
Mathias Wackenier |***
Emma Cartuyvels |***
Els Lommelen |**
Anneleen Rutten |*
Anton van de Putte ||
Hans Van Calster |
### Challenge 1
Emma:
```
butterfly_df %>% head(n = 10) %>% View
spatial_butterfly_df <- st_as_sf(butterfly_df, coords = c(6:7), crs = 4326)
spatial_atalanta_df <- spatial_butterfly_df %>%
filter(species == "Atalanta")
grids <- st_read("data/20200326_utm10_bel.gpkg")
# Oplossing crs
grids <- st_transform(grids, 4326)
municipalities <- st_read("data/20200326_Belgian_municipalities.geojson")
st_crs(spatial_butterfly_df)
st_crs(grids)
st_crs(municipalities)
```
Anneleen:
```
butterfly_df <- read_csv("data/20200326_butterflies.txt", na = "")
crs_wgs <- CRS("+init=epsg:4326")
lonlat <- cbind(butterfly_df$decimal_longitude,
butterfly_df$decimal_latitude)
spatial_butterfly_df <- SpatialPointsDataFrame(lonlat,
data = butterfly_df,
proj4string = crs_wgs)
spatial_atalanta_df <- spatial_butterfly_df %>%
subset(species == "Atalanta")
Belgium_grid <- st_read ("data/20200326_utm10_bel.gpkg")
gemeentes_grid <- st_read ("data/20200326_Belgian_municipalities.geojson")
st_crs(spatial_butterfly_df)
st_crs(Belgium_grid)
st_crs(gemeentes_grid)
```
Luc
met %>%
```
st_read("20200326 Spatial/20200326_utm10_bel.gpkg") %>%
st_set_crs(3035) -> BelgRefGrid
```
Damiano
```
spatial_butterfly_df <- st_as_sf(butterfly_df,
coords = c("decimal_longitude",
"decimal_latitude"),
crs = 4326)
spatial_atalanta_df <-
spatial_butterfly_df %>%
filter(species == "Atalanta")
utm10_belgium <- st_read("data/20200326_utm10_bel.gpkg")
st_crs(utm10_belgium)
st_crs(utm10_belgium) <- 3035
st_crs(utm10_belgium)
bel_mun <- st_read("data/20200326_Belgian_municipalities.geoj
```
### Challenge 2
Luc
```
st_read("20200326 Spatial/20200326_utm10_bel.gpkg") %>%
st_transform(4326) -> BelgRefGrid
spatial_atalanta_df %>%
st_intersection(BelgMunicipal) %>%
st_set_geometry(NULL) %>% # om tabelletje te maken zonder # coordinaten
group_by(NameDut) %>%
summarise(Aantal = n())
```
Emma:
```
grids <- st_transform(grids, 3035)
spatial_atalanta_df <- st_transform(spatial_atalanta_df, 3035)
municipalities <- st_transform(municipalities, 3035)
perUTM <- st_intersection(spatial_atalanta_df, grids) %>%
group_by(CELLCODE) %>%
summarise(Aantal_obs = n())
perGem <- st_intersection(spatial_atalanta_df, municipalities) %>%
group_by(NameDut) %>%
summarise(Aantal_obs = n())
```
Damiano: Note that intersecting geometries using longitude-latitude should be avoided. See explanation about warning got from sf:
https://r-spatial.github.io/sf/articles/sf6.html#although-coordinates-are-longitudelatitude-xxx-assumes-that-they-are-planar
Wim:
##### 1
utm10_sf <- st_transform(utm10_sf,crs = 4326)
st_intersection(atal_sf, utm10_sf)
##### 2
st_intersection(atal_sf, utm10_sf) %>%
group_by(CELLCODE) %>%
count()
##### 3
Gem_sf %>%
select(NameDut) %>%
st_intersection( x = atal_sf)
# 4
Gem_sf %>% select(NameDut) %>%
st_intersection( x = atal_sf) %>%
group_by(NameDut) %>%
count()
### Challenge 3
Emma:
```
st_layers("data/20200326_protected_areas.gpkg")
hab_flanders <- st_read("data/20200326_protected_areas.gpkg", layer = "ps_hbtrl")
hab_flanders <- st_transform(hab_flanders, 31370)
hab_flanders <- st_transform(hab_flanders, 3035)
st_intersection(hab_flanders, municipalities) %>%
filter(NAAM == "Kalmthoutse Heide")
st_intersection(hab_flanders, municipalities) %>%
filter(NAAM == "Voerstreek")
```
ELs:
hab_flanders <- st_read("./data/ps_hbtrl.shp")
#no EPSG code
st_crs(hab_flanders) <- 31370
municipalities_31370 <- municipalities %>%
st_transform(31370)
hab_flanders_intersect <- hab_flanders %>%
st_intersection(municipalities_31370)
hab_KALH <- hab_flanders_intersect %>%
filter(NAAM == "Kalmthoutse Heide")
#--> essen en kalmthout
hab_VOER <- hab_flanders_intersect %>%
filter(NAAM == "Voerstreek")
#--> Dalhem, Fourons, Plombières
Patrik:
```
protect_area_fl <- st_layers("20200326_protected_areas.gpkg")
hab_flanders <- st_read("20200326_protected_areas.gpkg", "ps_hbtrl")
st_crs(hab_flanders)
hab_flanders <- st_transform(hab_flanders, 31370)
muni <- st_transform(muni, 31370)
intersect_muni_hab_flanders <- st_intersection(hab_flanders, muni)
filter <- intersect_muni_hab_flanders %>%
filter(NAAM == "Kalmthoutse Heide" | NAAM =="Voerstreek")
```
### BONUS
Emma:
```
ps_hbtrl <- st_read("data/20190226_ps_hbtrl/ps_hbtrl.shp")
st_crs(ps_hbtrl)
ps_hbtrl <- st_transform(ps_hbtrl, 3035)
centroids <- st_centroid(municipalities)
distances <- st_distance(spatial_atalanta_df, centroids)
```
### Useful extra resource
When to reproject:
https://geocompr.robinlovelace.net/reproj-geo-data.html#when-to-reproject