Skip to content

Instantly share code, notes, and snippets.

@tallcoleman
Last active February 9, 2025 03:19
Show Gist options
  • Select an option

  • Save tallcoleman/ca13b632e31e829ff2063af84ccdb6b0 to your computer and use it in GitHub Desktop.

Select an option

Save tallcoleman/ca13b632e31e829ff2063af84ccdb6b0 to your computer and use it in GitHub Desktop.
Directly query bike parking features in OpenStreetMap
# Example of querying amenity=bicycle_parking using direct overpass query in r
# Limiting output based on existing area from Overpass API
# Documentation - see:
# https://docs.ropensci.org/osmdata/articles/osmdata.html#extracting-osm-data-from-a-query
# https://docs.ropensci.org/osmdata/reference/osmdata_sf.html
# set up packages
install.packages("osmdata")
install.packages("glue")
library(osmdata)
library(glue)
# set up variable for overpass area
area_name <- "Toronto"
# more robust method would be to use specific area IDs ("3600" + relation id)
# e.g. `area(id:3600324211)->.searchArea;` for Toronto
# see: https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL#By_element_id
# set up query
q <- glue('[out:xml][timeout:30];
area["name"="{area_name}"]->.searchArea;
nwr[~"amenity$"~"^bicycle_parking$"](area.searchArea);
(._;>;);
out;')
# query notes
# `~"amenity$"~"^bicycle_parking$"` includes amenity=bicycle_parking with lifecycle prefixes
# (if you don't want lifecycle prefixed results, "amenity"="bicycle_parking" is fine)
# `(._;>;);` is required to get the geometry for lines/polygons (adds in child nodes to the output)
bicycle_parking_sf <- osmdata_sf(q)
# OTHER QUERY EXAMPLES
# stop_area relations
q <- glue('[out:xml][timeout:30];
area["name"="{area_name}"]->.searchArea;
rel["public_transport"="stop_area"](area.searchArea);
(._;>;);
out;')
# to link stop_area relations to entrance nodes, it looks like you'll have to use osmdata_sc (Silicate format)
# instead of / alongside the Simple Features format, since the Simple Features format doesn't seem to have the
# columns required to link the relation to its child nodes.
# train station entrances
q <- glue('[out:xml][timeout:30];
area["name"="{area_name}"]->.searchArea;
node["railway"~"entrance"](area.searchArea);
out;')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment