Created
October 13, 2020 09:28
-
-
Save oliviergimenez/ade3c05727853af207261b40361cd665 to your computer and use it in GitHub Desktop.
Make a map of Montpellier with R and openstreetmap data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# code from https://gist.github.com/d-qn/4f1c4ed80a4fd6a76cd1153c89f56134 | |
library(tidyverse) | |
library(sf) | |
library(osmdata) # to get openstreetmap geo data | |
# settings | |
# olivier: get data on Montpellier | |
bb <- getbb('Montpellier, France') # define the bbox, will be used to fetch OSM data within that box | |
dest_proj <- 2056 | |
# OSM overpass query | |
q <- opq(bbox = bb) | |
## Get OSM geo data, reproject and discard unused features | |
q_bg <- q %>% | |
add_osm_feature(key = 'building') %>% | |
osmdata_sf() | |
# bind polygons and multipolygons buildings into one data.frame | |
buildings <- q_bg$osm_polygons %>% | |
st_transform(dest_proj) %>% | |
select(name, osm_id, building) | |
buildings <- bind_rows( | |
buildings, | |
q_bg$osm_multipolygons %>% | |
st_transform(dest_proj) %>% | |
select(name, osm_id, building) | |
) %>% | |
st_make_valid() | |
# all roads | |
q_rd <- q %>% | |
add_osm_feature(key = 'highway') %>% | |
osmdata_sf() | |
# recode roads type to numeric | |
roads <- q_rd$osm_lines %>% | |
st_transform(dest_proj) %>% | |
select(name, osm_id, highway) %>% | |
mutate(type = case_when( | |
highway %in% c("motorway", "motorway_link") ~ 1, | |
highway %in% c("primary", "primary_link") ~ 2, | |
highway %in% c("secondary", "secondary_link") ~ 3, | |
highway %in% c("tertiary", "tertiary_link") ~ 4, | |
T ~ 5 | |
)) | |
# all railway s | |
q_ry <- q %>% | |
add_osm_feature(key = 'railway') %>% | |
osmdata_sf() | |
railways <- q_ry$osm_lines %>% | |
st_transform(dest_proj) %>% | |
select(railway) %>% | |
mutate(type = case_when( | |
railway %in% c("rail", "light_rail") ~ 1, | |
railway %in% c("subway") ~ 2, | |
T ~ 3 | |
)) | |
ggplot() + | |
geom_sf(data = buildings, | |
fill = "#ECD89DFF", | |
size = 0, alpha = 0.25) + | |
geom_sf(data = roads, | |
aes(colour = type, size =1/type), | |
colour = "white") + | |
# olivier: remove railways | |
# geom_sf(data = railways, size = 0.2, | |
# colour = "#EEBCB1FF" )+ | |
scale_size(range = c(0.03, 0.2)) + | |
scale_alpha(range = c(0.8, 0.98)) + | |
theme_void() + | |
theme(plot.background = element_rect(fill = "#293133", "293133"), | |
legend.position = "none") | |
# save | |
# olivier: add resolution dpi = 600 | |
ggsave("montpeul.png", dpi = 600) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment