Search code examples
postgresqlpostgis

Postgis ST_Distance_Sphere giving about 1.7 too high result


I am new into Postgis and spatial stuff and I am struggling with quite simple query.

I have two records in places for test, where column addressLocation is a POINT with following values:

(51.122711,17.031819)
(51.122805,17.035522)

I am trying to make a query:

SELECT *
FROM places
WHERE ST_Distance_Sphere("addressLocation"::geometry, ST_MakePoint(51.122711, 17.033686)) <= 255;

51.122711, 17.033686 Is about in the center between both of these points and distance measured on Google maps is about 125 and 128 meters.

The issue is that (51.122805,17.035522) got into results with 205 as limit and the other one with 210.

I was looking through the PostGIS docs and cannot find any explanation for such inaccuracy.


Solution

  • Coordinates in PostGIS must be expressed as longitude/latitude, while in Google Map they are expressed as latitude/longitude.

    Your query is computing distances in Yemen:

    Select ST_DistanceSphere(st_geomFromText('POINT(51.122711 17.031819)'),
    st_geomFromText('POINT(51.122711 17.033686)'));
     st_distancesphere
    -------------------
          207.60121386
    

    While by swapping the coordinates, the points are in Poland and the distance is:

    Select ST_DistanceSphere(st_geomFromText('POINT(17.031819 51.122711)'),
     st_geomFromText('POINT(17.033686 51.122711)'));
     st_distancesphere
    -------------------
          130.30184168