TransWikia.com

Converting the result of pgr_dijkstra routing to an encoded polyline

Geographic Information Systems Asked by Gregory Nowik on February 15, 2021

I have this query for routing which is working, and would like it to give me an encoded polyline as a result instead of segmented geometries because I want to display it easily on a mobile Google MapView.

Select geom from pgr_dijkstra(
 'Select gid as id, source, target, st_length(geom) as cost from network', 1,3000, false) as di
 JOIN network pt
 ON di.edge = pt.gid ;

As I understand I must use ST_AsEncodedPolyline() function but I don’t understand how to use the result of pgr_dijkstra() in a way it be used as parameter for ST_AsEncodedPolyline().

One Answer

Just ST_Union the results:

SELECT ST_AsEncodedPolyline(ST_Union(geom), 5) AS enc
FROM   pgr_Dijkstra(
         '
          SELECT gid AS id, source, target, ST_Length(geom) AS cost
          FROM network
         ',
         1,
         3000,
         FALSE
) AS   di
JOIN   network AS pt
  ON   di.edge = pt.gid
;

While precision := 5 is the default, it is good to remember what precision you used; if the decoding application uses a different value, your geometries become useless. As per the PostGIS docs, Google Maps uses a coordinate precision of 5.

ST_Collect performs a lot faster than ST_Union, but does not dissolve any imprecision (e.g. overlaps) in the collected set of LineStrings; given only a single (or a few) routes, with a few hundred segments or so, the difference will likely be neglect-able.

Answered by geozelot on February 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP