How to get Start and End node id of a certain way with way_id

I would like to get Start node id and End node id of a certain way in openstreetmap data. I have the following code in Jupyter notebook.
import pandas as pd
import geopandas as gpd
region = “switzerland”
handler = StreetsHandler()
osm_file = f"{region}-latest.osm.pbf"

start data file processing

handler.apply_file(osm_file, locations=True, idx=‘flex_mem’)

show stats

print(f"num_relations: {handler.num_relations}“)
print(f"num_ways: {handler.num_ways}”)
print(f"num_nodes: {handler.num_nodes}“)
print(f"num_roads: {handler.num_roads}”)

create dataframes

street_relations_df = pd.DataFrame(handler.street_relations)
street_relation_members_df = pd.DataFrame(handler.street_relation_members)
street_ways_df = pd.DataFrame(handler.street_ways)
street_roads_df = pd.DataFrame(handler.street_roads)

print(street_ways_df.iloc[89])

w_id 4087346
geo LINESTRING (8.857527 46.2049215, 8.8575004 46…
cycleway NaN
highway tertiary
lane_markings NaN

overtaking:hgv:backward NaN
hgv:lanes:backward NaN
bridge:support NaN
temporary:highway NaN
turn:lanes:both_ways NaN
Name: 89, Length: 1054, dtype: object

How could I get the Start node ID and End node ID of the way with w_id=4087346?

1 Like

The start and end are just the first and last entry from your “geo LINESTRING”, in your example “8.857527 46.2049215”

Those are the coordinates of the start node, though, not the id.

1 Like

Thank you. Is there any library or API to get the node ID of this point with long, lat?

This overpass query will provide you metadata for the way in json format. The Instruction out geom provides both id and geometry lists.

I would consider checking documentation of that specific library that you seem to be using to process OSM data.

It is one of many tools that can be used for that, and definitely not the only one and likely not even main/typical.

1 Like

Thank you very much for your answer. It is what I was looking for. Now, if I have a certain number of ways for example, two here, is there a possibility to loop through text file with the way IDs instead of typing each way?

[out:json];
way(4087346);
out geom;
way(4248968);
out geom;

Thank you very much

1 Like

With the way instruction, you can provide a list of id’s like below :
way(id:4087346,128908801,128908803);

That is perfect. Thank you for the excellent instructions.

If you know the way ID you can also use directly the OpenStreetMap API to get the details of the way.

For a human way to review things I sometimes open the way using the web browser, for example w4087346, it has the list of nodes and also indicates if it is part of a relation.

1 Like

If you know the way ID you can also use directly the OpenStreetMap API to get the details of the way.

the API is reserved to editing OpenStreetMap though, if you are going to make lots of queries you should use a different api, e.g. overpass.

2 Likes

You unfortunately have left out two very important pieces of information in your post: the name of the library you are using, which is pyosmium, and the implementation of your handler function StreetsHandler. The way() callback in latter gives you the node ids. You need to do something along the lines of:

class StreetsHandler(osmium.SimpleHandler):

    ...

    def way(self, w):
        firstnode, lastnode = w.nodes[0], w.nodes[-1]
    ...

Save the node ids with all the other function from the way and them you can access it after apply_file() has run.

2 Likes