[GIS] Query for WAY that connects two INTERSECTIONS

open-source-routing-machineoverpass-api

I'm trying the retrieve the "WAY" that passes two given "INTERSECTIONS".

What I do is query for the NODE that intersects the INTERSECTION using the locations of this intersections (which is returned by OSRM). I use the locations as a bounding box in overpass:
nodes(north, west,south,east);
way(bn);
out;

The result is there, but all ways attached to the two nodes are returned. How do I filter so only the WAY that passes both NODEs is returned?

Best Answer

The following description assumes that you have a list of GPS positions, as well as the OSRM map matching service and Overpass API available.

(1) Use OSRM Map Matching service to identify nearest roads for a trip

OSRM Map matching service accepts a list of GPS positions, or some polyline and matches it to the corresponding OSM ways. This will be the first step of the overall process and returns a list of OSM nodes ids for further processing.

Example: http://router.project-osrm.org/route/v1/driving/13.388860,52.517037;13.397634,52.529407;13.428555,52.523219?annotations=nodes

I used parameter annotation=nodes to include all OSM node ids in the result:

{"annotation":{"nodes":[659394041,3836987625,3275541361,26871569,3836987634,3836987635,3836987636,588159289,311718989,3836987640,...,3030636206],"datasources"

See https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#match-service for details.

(2) Fetch ways for node ids via Overpass API

Based on the previous result, extract all OSM node ids and pass those node ids to Overpass API. The following query then retrieves all corresponding highways for those node ids:

node(id:659394041,3836987625,3275541361,26871569,3836987634,3836987635,3836987636,...,2164561317,3324300228,3030636206);
way[highway](bn);
out geom;

Overpass turbo link: http://overpass-turbo.eu/s/pOT

Same query including OSM nodes from both legs in OSRM response: http://overpass-turbo.eu/s/pOW - that's actually a trip from Friedrichstraße via Torstraße to Platz der Vereinten Nationen in Berlin, Germany.

Ways according to a list of node ids

To find a way which contains all of a given list of node ids, you can use the following approach:

node(id:659394041,3836987625)->.allnodes;

way[highway](bn.allnodes)->.ways;

foreach .ways -> .singleway (
   node.allnodes(w.singleway);
   way.singleway(bn)(if:count(nodes) == allnodes.count(nodes));
   out geom;
);

overpass turbo link: http://overpass-turbo.eu/s/pPq

Related Question