Skip to content

Instantly share code, notes, and snippets.

@oliviergimenez
Created October 20, 2020 18:36
Show Gist options
  • Save oliviergimenez/03dda7141d35079bf01fb5eb94a6e449 to your computer and use it in GitHub Desktop.
Save oliviergimenez/03dda7141d35079bf01fb5eb94a6e449 to your computer and use it in GitHub Desktop.
map highways in Switzerland - France
# map highways in Switzerland - France
# load packages
library(tidyverse)
library(sf)
library(osmdata)
library(mapview)
# define zone of interest
lonmin <- 6
lonmax <- 8.5
latmin <- 46.1
latmax <- 47.7
bb <- st_sf(a = 1:2, geom = st_sfc(st_point(c(lonmin,latmin)), st_point(c(lonmax,latmax))))
st_bbox(bb)
# get osm data
x <- bb %>%
st_bbox() %>%
opq() %>%
add_osm_feature("route", value = "road") %>%
osmdata_sf()
# pick lines
osm <- x$osm_lines %>%
st_transform(crs = 21781)
# clean up road
routes <- osm %>%
filter(highway %in% c('motorway', 'motorway_link', 'trunk', 'trunk_link',
'primary', 'primary_link',
'secondary', 'secondary_link',
'tertiary', 'tertiary_link', 'unclassified')) %>%
mutate(type = as.factor(highway)) %>%
select(name, hgv, type, lanes) %>%
mutate(type = droplevels(type)) %>%
mutate(road_type = case_when(
type %in% c('motorway', 'motorway_link', 'trunk', 'trunk_link') ~ "aut", # stable territory in winter
type %in% c('primary', 'primary_link') ~ "ppa",
type %in% c('secondary', 'secondary_link') ~ "reg",
type %in% c('tertiary', 'tertiary_link', 'unclassified') ~ "loc"))
# visualize roads
ggplot() +
geom_sf(data = routes, lwd = 0.1, colour = "blue")
# get highways
highways <- routes %>%
mutate(road_type = as_factor(road_type)) %>%
filter(road_type == "aut")
# visualize highway
ggplot() +
geom_sf(data = highways, lwd = 0.1)
# locate highways on a map of Europe - interactively
mapview(highways)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment