Skip to content

Instantly share code, notes, and snippets.

@tobych
Created February 6, 2025 23:30
Show Gist options
  • Save tobych/a65ca136240a923cc7aff1e3b19a7018 to your computer and use it in GitHub Desktop.
Save tobych/a65ca136240a923cc7aff1e3b19a7018 to your computer and use it in GitHub Desktop.
# Get a list of IDs for all ways in the gdf
way_ids = gdf_bike.loc['way'].index.tolist()
# get a bbox, as per features_from_point()
bbox = ox.utils_geo.bbox_from_point(center, 5000)
# get a polygon we can use to query Overpass, as per features_from_bbox()
polygon = ox.utils_geo.bbox_to_poly(bbox)
# build results generator
response_jsons = ox._overpass._download_overpass_features(polygon, tags_bike)
# get the elements, assuming the generator only yielded one response
elements = next(response_jsons)['elements']
# find the element that is the relation for our bicycle route
for element in elements:
if element['type'] == 'relation' and element['id'] == 14156760:
ctr_relation = element
len(ctr_relation['members'])
# show which ways in the relation we do not already have in the gdf
ids_of_ways_in_the_relation = []
for member in ctr_relation['members']:
if member['type'] == 'way':
way_id = member['ref']
print(way_id, way_id in way_ids)
ids_of_ways_in_the_relation.append(way_id)
# double-check
# we have this
gdf_bike.loc['way', 1348430779]
# but not this
# gdf_bike.loc['way', 1174919664] # TODO negate this
# build Overpass query string to get the ways referenced by the relation
ids_of_ways_to_get = ids_of_ways_in_the_relation
query_str = f"[out:json];way(id:{','.join(map(str, ids_of_ways_to_get))});out tags;"
# build and execute the query
from collections import OrderedDict
response_json = ox._overpass._overpass_request(OrderedDict(data=query_str))
elements = response_json['elements']
len(elements)
# tell us about these ways
for element in elements:
tags = element['tags']
if 'highway' in tags:
note = (f"highway {tags['highway']}")
else:
note = '?'
print(f"{element['id']:-10} {note}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment