So I have two points that I want to calculate the distance between. If I use:

  ST_Distance_Spheroid(point_a, point_b, 'SPHEROID["GRS 1980",6378137,298.257222101]') / 1000
       VALUES (
         ST_GeomFromText('POINT(116.330533333333 39.976266666667)'),
         ST_GeomFromText('POINT(116.34415 39.97875)')
     ) AS query (point_a, point_b);

I get the value 1.1953967900687876km. I think this is the correct value since when I use any online distance calculator I get this result; however, if I put the two points into a LINESTRING and then compute the length I get a different number:

  ST_Length2D_Spheroid(trajectory, 'SPHEROID["GRS 1980",6378137,298.257222101]') / 1000
       VALUES (
         ST_GeomFromEWKT('SRID=4326;LINESTRING(39.976266666667 116.330533333333, 39.97875 116.34415)')
       )) AS query(trajectory);

I get the value 1.5228519672706637km. I don't understand why this is happening. In the case of just two points shouldn't these distance bet the same?

Thanks in advance.

  • Just because it's endorsed by an online service doesn't make it right. Distances over the earth are only measured accurately using physical measurements. Projections, spheroids and ellipsoids are models, that just means the service uses the same or similar model. Your points and lines are in geographic coordinates so the distance is a model on a model (projection onto the spheroid). That is a big difference though! Perhaps one is a straight "as the crow flies" distance and the other is measuring on the surface of the spheroid. Commented Aug 5, 2014 at 2:39
  • Ya I didn't assume that online services are correct and this is why I decided to play around with postgis and try to figure it out. It's interesting that these two different models are off by over 300m on less than 2km. Is this something I should expect? Any idea on which model is more correct? Commented Aug 5, 2014 at 3:00
  • 1
    I would have to go to the source code or at least refer to the documentation for each function. 300m is a lot! I would expect that they would differ only by centimetres which makes me think that either they don't do what we assume or one of them has a serious bug. Commented Aug 5, 2014 at 3:05

1 Answer 1


There actually is no difference between the two functions, which both yield 1.195 km. The problem is that in your question the axis order is flipped for trajectory, so you are seeing different answers than you expect.

  ST_AsLatLonText(point_a) AS point_a_latlon,
  ST_AsLatLonText(point_b) AS point_b_latlon,
  ST_Distance_Spheroid(point_a, point_b, spheroid) / 1000 AS ST_Distance_Spheroid_km,
  ST_Length2D_Spheroid(ST_MakeLine(point_a, point_b), spheroid) / 1000 AS ST_Length2D_Spheroid_km
  ST_GeomFromText('POINT(116.330533333333 39.976266666667)', 4326) AS point_a,
  ST_GeomFromText('POINT(116.34415 39.97875)', 4326) AS point_b,
  'SPHEROID["GRS 1980",6378137,298.257222101]'::spheroid
) AS f;
-[ RECORD 1 ]-----------+-------------------------------
point_a_latlon          | 39°58'34.560"N 116°19'49.920"E
point_b_latlon          | 39°58'43.500"N 116°20'38.940"E
st_distance_spheroid_km | 1.19539679006871
st_length2d_spheroid_km | 1.19539679006871
  • Yes, I see that. Obvious. So is the order long lat a convention in GIS in general? I got it flipped because when most people talk about position it's always lat long. Commented Aug 5, 2014 at 5:44
  • For PostGIS and most other GIS software, the internal representation is always (long lat), which is essentially (x y). The "spoken" order is of course flipped, which is why this is often confusing. The ST_AsLatLonText has them flipped, since it is pretty to see and say.
    – Mike T
    Commented Aug 5, 2014 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.