What is an efficient way to convert a set of ordered latitude longitude pairs into a Linestring in PostGIS?

StackOverflow https://stackoverflow.com/questions/21937119

  •  14-10-2022
  •  | 
  •  

Question

I can convert it into WKT and then to a Linestring but I think there are more efficient ways to do it.

No correct solution

OTHER TIPS

Presuming your data looks like this:

enter image description here

You may convert the set of ordered longitude/latitude pairs into linestrings like this:

SELECT ST_AsText(ST_MakeLine(q.f, q.t)) FROM
  (SELECT (SELECT ST_MakePoint(c.long_from, c.lat_from) AS f),
  (SELECT ST_MakePoint(c.long_to, c.lat_to) AS t) 
   FROM lonlatset c) AS q;

And here's the result:

enter image description here

Try to remember that it is very important to respect the proper coordinates order: first the longitude, and second the latitude!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top