knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", 
  error=TRUE
)

Slides: https://nowosad.github.io/SIGR2021/workshop1/workshop1_rl.html#1

Example datasets

Why use example Datasets

size of a sample dataset: - small enough to be fast - but with edge cases

You can have multiple datasets (small/large, differents areas)

Robin recommends to create an example dataset to save time. Documenting it is recommended but not mandatory.

ActDev example: know where to stop R: R is the backend, the webapplication to visualise the data is in JS.

library(help ="datasets") # full list of datasets

Listed datasets are available directly from R.

Plot 2 datasets

plot(trees)
plot(LakeHuron)

Get information on data in a package

help(package = "spData")
?AirPassengers
library(spData)
plot(nz) # if list error, maybe because sf is not loaded yet and plot don't know how to plot sf objects

plot(nz$geom)

Make a copy to work on is good practice.

nz2 = nz
plot(nz)
library(nzelect)

nz_lonlat = sf::st_transform(nz, 4326)
names(voting_places)

# create points from coordinates
voting_places_sf = sf::st_as_sf(voting_places, coords = c("longitude", "latitude"))

plot(sf::st_geometry(nz_lonlat))
plot(voting_places_sf, add = TRUE)

Spatial subsetting

Filtering voting places that are inside NZ island

vsub = voting_places_sf[nz,] # Error message because mismatching CRS issues
sf::st_crs(voting_places_sf, error=TRUE) # NA
sf::st_crs(nz) # EPSG:2193

Voting_places_sf does not have a crs

Fixing issues

voting_places_sf <- sf::st_set_crs(voting_places_sf, st_crs(4326))
sf::st_crs(voting_places_sf)
nz_lonlat <- sf::st_transform(crs = st_crs(4326))
vsub = voting_places_sf[nz_lonlat,]
plot(sf::st_geometry(nz_lonlat))
plot(vsub, add = TRUE)

Using OSM data

Quite informal but very very rich Interesting when you can't access official data

Simple requests: {osmdata}

library(osmdata)
schools_nz_osm <- opq(bbox = sf::st_bbox(nz_lonlat)) %>%
  add_osm_feature(key = "amenity", value = "school")

Working with messy data

skimr:skim : resume data

source outside code

source("https://raw.githubusercontent.com/Nowosad/SIGR2021/master/workshop1/osm_combine.R")

# osm_combine() is available in the environnment

Big OSM data

For large dataset (> 100 MB)

library(osmextract)

Needs a place name, and parameters (date type (e.g. "amenity") and an SQL query for the OSM server)

Tidyverse

library(spData)
library(sf)
library(tidyverse)

nz %>%
  filter(Island == "South")

Geometry operations

download online data



Bakaniko/notesSIGR2021 documentation built on Dec. 17, 2021, 10:45 a.m.