Created
September 27, 2020 16:58
-
-
Save taraskaduk/063ec657c6752c763994243b57f5bd3b to your computer and use it in GitHub Desktop.
Get admin boundaries as sf objects using {osmdata} package
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
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