How to extract all edges of a single OSM way using OSMnx?

I’m trying to figure out how to use OSMnx to get all the edges that make up one OSM way. By way I mean the edges that are part of the same street.

I can get the graph and find the nearest edge to a point like this:

import osmnx as ox

point = (48.865306, 2.327491)
G = ox.graph_from_point(point, dist=250)
G = ox.project_graph(G, to_latlong=True)

nearest_edge = ox.distance.nearest_edges(G, point[1], point[0])

But I can’t figure out how to get all the edges for the whole street. For example, I want to find all the edges for Rue de Rivoli.

I know from JOSM that this way has ID 1274070500 and 16 nodes. Is there a way to get this info using OSMnx? How can I find all the edges that belong to this way?

Any help would be great. Thanks!

hey tom, u might try ox.graph_from_xml with a custom overpass query.

for instance, use:
query = f'way(1274070500); (._;>;);out;'
then pass it to ox.downloader.overpass_request. that should pull the full way. hope it does the trick!

I’ve dealt with a similar issue before. Here’s what worked for me:

Instead of using graph_from_point, try using ox.graph_from_place with a specific area name. This gives you more control over the area you’re working with.

Then, you can use ox.get_edge_attributes to extract specific attributes for all edges. Something like:

G = ox.graph_from_place('Paris, France')
edge_names = ox.get_edge_attributes(G, 'name')

This will give you a dictionary of all edge IDs and their names. You can then filter this to find all edges with the name ‘Rue de Rivoli’.

For the specific OSM way ID, you might need to use the OSM API directly. OSMnx doesn’t always preserve original OSM IDs.

Hope this helps you out!

I’ve encountered this issue before when working with OSMnx. One approach that’s worked well for me is to use the OSM API directly in conjunction with OSMnx. Here’s a method you could try:

First, use the OSM API to fetch the way data:

import requests

way_id = 1274070500
url = f'https://api.openstreetmap.org/api/0.6/way/{way_id}/full.json'
response = requests.get(url)
way_data = response.json()

Then, extract the node coordinates from the way data and use them to create a custom graph in OSMnx:

nodes = [(node['lat'], node['lon']) for node in way_data['elements'] if node['type'] == 'node']
G = ox.graph_from_point(nodes[0], dist=1)  # Create a small graph
G = ox.add_path(G, nodes)  # Add the way as a path

This should give you a graph containing only the edges of the specific way you’re interested in. You can then use standard OSMnx functions to analyze this graph.