Search code examples
sqlpostgresqlgispostgis

Alphanumberic output from ST_MakeLine


I'm trying to convert lat/lon to linestring. Basically, grouping the columns lat and lon, making a point, and creating a linestring.

Table:

+------------+----------+-----------+------------+---------+--------+
|  link_id   | seq_num  |    lat    |    lon     | z_coord | zlevel |
+------------+----------+-----------+------------+---------+--------+
| "16777220" | "0"      | "4129098" | "-7192948" |         |      0 |
| "16777220" | "999999" | "4129134" | "-7192950" |         |      0 |
| "16777222" | "0"      | "4128989" | "-7193030" |         |      0 |
| "16777222" | "1"      | "4128975" | "-7193016" |         |      0 |
| "16777222" | "2"      | "4128940" | "-7193001" |         |      0 |
| "16777222" | "3"      | "4128917" | "-7192998" |         |      0 |
| "16777222" | "4"      | "4128911" | "-7193002" |         |      0 |
+------------+----------+-----------+------------+---------+--------+

My code:

select link_id, ST_SetSRID(ST_MakeLine(ST_MakePoint((lon::double precision / 100000), (lat::double precision / 100000))),4326) as geometry
from public.rdf_link_geometry
group by link_id
limit 50

geometry output column example: "0102000020E6100000020000004F92AE997CFB51C021E527D53EA54440736891ED7CFB51C021020EA14AA54440"

^^ What is this? how did it get formatted in such a way? I expected a linestring, something like

geometry
7.123 50.123,7.321 50.321
7.321 50.321,7.321 50.321

Data format for link_id is bingint, and for geometry it says geometry

SOLUTION:

select link_id, ST_AsText(ST_SetSRID(ST_MakeLine(ST_MakePoint(
(lon::double precision / 100000), (lat::double precision / 100000))),4326)) as geometry
from public.rdf_link_geometry
group by link_id
limit 50

Solution

  • The output is a geometry, which you can display as text using st_asText

    select st_asText('0102000020E6100000020000004F92AE997CFB51C021E527D53EA54440736891ED7CFB51C021020EA14AA54440');
                        st_astext
    --------------------------------------------------
     LINESTRING(-71.92948 41.29098,-71.9295 41.29134)
    

    That being said, should you have more than 2 points, you could order them to create a meaningful line:

    select st_makeline(geom ORDER BY seqID) from tbl;