1

I am trying to create a service that returns the shortest path for a given start and end destination. What I did was used shortest path query provided by pgrouting to calculate the vertex and edges that form the shortest path. Then for each of the edges I calculated their corresponding coordinates and send them as points in a gpx file. Now when I plotted the coordinates and joined them by a straight line, it gave me a path but not the curve that I needed.

I am wondering what is the best way to send that information, I mean the path information. Currently I am just sending the coordinates. So lets say for a long curve path, do I need to actually send all the intermediate points in the curve to actually plot it like a curve or is there other way.

What is the standard procedure?

1 Answer 1

1

Maybe another suitable format could be GeoJSON. PostGIS can return edge geometries in GeoJSON format with ST_AsGeoJSON, so all you need to do is wrapping all edges into a GeoJSON "FeatureCollection".

You can find a simple example in the pgRouting workshop: http://workshop.pgrouting.org/chapters/php_server.html#geojson-output

You can create KML, GML, GPX or other formats in a similar way, but GeoJSON is probably the easiest one.

Update:

If you need GPX, how does your file look like? Like this should be correct:

<?xml version="1.0" encoding="UTF-8"?>
<gpx 
    xmlns="http://www.topografix.com/GPX/1/1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" 
    creator="" version="1.1">

    <metadata>[...]</metadata>

    <trk>
        <name>Track name</name>
<!-- Loop through your edge segments -->
        <trkseg>
            [List of points]
            <extensions></extensions>
        </trkseg>
<!-- Loop end -->
    </trk>
</gpx>

<trkseg> is described in http://www.topografix.com/GPX/1/1/#type_trksegType. You need to modify your PostgreSQL query and put the geometry in the right list format for each edge.

3
  • no I need that to be in the gpx format Commented Apr 28, 2012 at 2:27
  • Yeah I have tried that. For the edges that I get by running the shortest path query, I get the corresponding source and target's coordinates from there and I send the coordinates. However, I think I need to use the geometry feature as well, because just joining the points by straight line doesn't work. So how can I accomplish this.Could you please elaborate more? What do you mean by put the geometry in the right list format? Commented Apr 28, 2012 at 11:27
  • The format you have given is the same as I have used. But the issue is the edges given by the query sometimes, they have overlaps and when I display it, it doesn't look good. I have just sent the coordinates now for each edge. But it's not working in my case. So it will be like <trkseg>all points all edges in the order returned by the query </trkseg> Commented Apr 28, 2012 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.