Convert OSM Node to lat/lon

Viewed 331

So to briefly describe what I'm doing - I have a CSV of ~65,000 driving routes with start postcodes and end postcodes and I want to get for each route a list of lat/lon coordinates along the route.

So my first step was to go to OSRM project API and take out the nodes along a route using the following code

route = requests.get(f'http://router.project-osrm.org/route/v1/driving/{StartLL};{EndLL}?alternatives=false&annotations=nodes')
routejson = route.json()
route_nodes = routejson['routes'][0]['legs'][0]['annotation']['nodes']

Next I now want to take those nodes and convert them to lat/lon coordinates but I've found an API to do this from openstreetmaps, but it will take forever looping through 66,000 records each with a couple of hundred/thousand nodes.

for node in route_nodes:
    response_xml = requests.get(f'https://api.openstreetmap.org/api/0.6/node/{node}')
    response_xml_as_string = response_xml.content
    responseXml = ET.fromstring(response_xml_as_string)
    for child in responseXml.iter('node'):
        RouteNodeLL.append((float(child.attrib['lat']), float(child.attrib['lon'])))

I know there's a .osm.pbf file I can download but I have no idea how to extract the nodes out of it.

Is there a way I could do this? even if it means completely reworking the logic/idea?

1 Answers

I know there's a .osm.pbf file I can download but I have no idea how to extract the nodes out of it.

Take a look at osmium-tool and how to extract objects by ID.

Quoting from the manual:

The following command will get the nodes 17 and 18, the way 42, and the relation 3 out of the file:

osmium getid input.osm.pbf n17 n18 w42 r3 -o output.osm.pbf

In order to get the osm.pbf file for your region take a look at the OSM wiki, specifically at country and area extracts.

Related