Skip to content

Instantly share code, notes, and snippets.

@taraskaduk
Created September 27, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taraskaduk/063ec657c6752c763994243b57f5bd3b to your computer and use it in GitHub Desktop.
Save taraskaduk/063ec657c6752c763994243b57f5bd3b to your computer and use it in GitHub Desktop.
Get admin boundaries as sf objects using {osmdata} package
library(osmdata)
library(dplyr)
library(ggplot2)
#Define a place of interest
place <- "Kyiv Ukraine"
#First, get all admin boundaries associated with your request
all_boundaries <- opq(place) %>%
add_osm_feature(key = "boundary",
value = c("administrative")) %>%
osmdata_sf() %>%
#This next step is optional, depending on what the call returns
unname_osmdata_sf() %>%
.$osm_multipolygons
ggplot(data = all_boundaries) +
geom_sf()
#The previous call returns more than one boundary.
#Inspect the data and choose the object you need.
boundary <- all_boundaries %>%
filter(osm_id == 421866) %>%
#If you're after geometries only,
#you won't need all other 200 some columns
dplyr::select()
ggplot(data = boundary) +
geom_sf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment