Project-OSRM / osrm-backend

Open Source Routing Machine - C++ backend
http://map.project-osrm.org
BSD 2-Clause "Simplified" License
6.18k stars 3.28k forks source link

Return elevation data in OSRM response #6846

Open Redy1908 opened 2 months ago

Redy1908 commented 2 months ago

Feature

Please describe the feature you would like to see in OSRM. Images are often a good way to illustrate your requested feature.

When integrating third party raster data it would be advantageous to incorporate elevation data for the respective locations in the response. This information could be leveraged in numerous ways, such as plotting a graph to illustrate the elevation trend along the route.

The following is a STEP extracted from an OSRM response with the added elevation field:

{
   "geometry":"...",
   "maneuver":{
      "bearing_after":299,
      "bearing_before":0,
      "location":[
         "lat",
         "long",
         ELEVATION
      ],
      "modifier":"right",
      "type":"depart"
   },
   "mode":"driving",
   "driving_side":"right",
   "name":"Street Name",
   "intersections":[
      {
         "out":0,
         "entry":[
            true
         ],
         "bearings":[
            299
         ],
         "location":[
            "lat",
            "long",
            ELEVATION
         ]
      }
   ],
   "weight":71.4,
   "duration":22,
   "distance":141.3
}
erlenddahl commented 2 weeks ago

I need this as well, did you ever implement it, @Redy1908?

If not, I can probably hack it in place for my own/unofficial use, but if any of the maintainers want me to make it a pull request, and have any pointers as to where and how it should be done, please let me know.

Redy1908 commented 2 weeks ago

@erlenddahl Yes, but given the need for a fast solution, i decided not to hack the C++ layer. Instead i utilized PostGIS, an extension for PostgreSQL that allows for the manipulation of geospatial data. After importing the elevation data into PostGIS i queried the elevation data using the overview polyline provided by OSRM (?overview=full) as input:

SELECT ST_Value(rast, (dp).geom) As val
FROM (SELECT (ST_DumpPoints(ST_GeomFromText('OSRM_POLYLINE', 4326))) as dp) as points, dem
WHERE ST_Intersects(rast, (dp).geom)
ORDER BY (dp).path[1];

This query returns a list of the elevations for all points within the polyline.

For reference you can query also single points:

SELECT ST_Value(rast, ST_SetSRID(ST_MakePoint(LON, LAT), 4326))
FROM dem
WHERE ST_Intersects(rast, ST_SetSRID(ST_MakePoint(LON, LAT), 4326));
erlenddahl commented 2 weeks ago

Thank you for the speedy and detailed response! Perhaps that's easier in my case as well. I need it to be very fast, but it looks like we can make OSRM return the node ids as part of the response (&annotations=true). That could, perhaps, be a faster way of retrieving the relevant lines from the database than using ST_Intersects.