Search code examples
postgresqlgoogle-mapspostgis

PostGIS: Linestring length


I am using PostGIS to calculate length of a user-defined linestring. The column is defined as geography(LineString,4326). The linestring is represented by this GeoJSON:

"track": {
  "type": "LineString",
  "coordinates": [
    [
      49.364325571013,
      16.785549033597
    ],
    [
      49.363254969491,
      16.642149334451
    ]
  ]
}

SELECT ST_Length("geography") FROM table; returns 15945.7486086962 but the length measured on Google Maps is ~10 km.

What am I doing wrong? How to measure the length to get the same value as from Google Maps?


Solution

  • I believe it is the classic issue of switching x,y positions.

    Considering x,y:

    SELECT 
        ST_Length(
          ST_GeogFromText('SRID=4326;LINESTRING(49.364325571013 16.785549033597,49.363254969491 16.642149334451)'),true);
    
        st_length     
    ------------------
     15869.9069442778
    

    and the "same" LineString switching to y,x ..

    SELECT 
       ST_Length(
         ST_GeogFromText('SRID=4326;LINESTRING(16.785549033597 49.364325571013,16.642149334451 49.363254969491)'),true)
    
        st_length     
    ------------------
     10416.8606521809