Skip to content

Instantly share code, notes, and snippets.

@d-qn
Last active October 15, 2020 16:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-qn/4f1c4ed80a4fd6a76cd1153c89f56134 to your computer and use it in GitHub Desktop.
Save d-qn/4f1c4ed80a4fd6a76cd1153c89f56134 to your computer and use it in GitHub Desktop.
Make a minimal arty city map with R and openstreetmap data
library(tidyverse)
library(sf)
library(osmdata) # to get openstreetmap geo data
# settings
bb <- getbb('Lausanne, Switzerland') # 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") +
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")
#ggsave("lausanne1.png")
@d-qn
Copy link
Author

d-qn commented Oct 12, 2020

Resulting map with
bb <- getbb('Lausanne, Switzerland')

lausanne1

@d-qn
Copy link
Author

d-qn commented Oct 12, 2020

bb <- getbb('Geneva, Switzerland')

geneva

@Pleclown
Copy link

Hello,
Is there a way to limit the box ? I've tried with "Nantes, France", and the result is... less than optimal :)
Nantes1

@d-qn
Copy link
Author

d-qn commented Oct 13, 2020

Yes, you should add.

coord_sf(xlim = c(ymin, ymax), 
           ylim = c(ymin, ymax)), 
           expand = F)

You can retrieve the bbox coordinates returned by getbb to pass it back to coord_sf(). If you reprojected OSM data, you would need to also reproject the bbox coordinates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment