5

I want to calculate the distance between lat/lon points. I tried the following queries:

Select 
ST_DISTANCE(ST_GeomFromText('POINT(-73.97923 40.75571)', 4326), ST_GeomFromText('POINT(-73.974509 40.757108)', 4326)) 

which gives 0.00492364143698933 as a result.

and

Select 
ST_DISTANCE_SPHERE(ST_GeomFromText('POINT(-73.97923 40.75571)', 4326), ST_GeomFromText('POINT(-73.974509 40.757108)', 4326))

which gives 426.951968421872 as result.

If I look at the distance between this point in Google Maps http://goo.gl/maps/GvhFM I get 1,1 miles (~1770m) as a result. Why this mismatch? How can I get the real (Google Maps) distance?

  • 2
    The PostGIS distance is pretty accurate. The correct distance between the two points specified in this question is 427.823 meters (WGS84 ellipsoid). BTW, when the coordinates are reversed, the distance should be 528.66 meters. – whuber Jun 3 '13 at 15:30
4

Are you sure those are the correct coordinates? On Google Maps, the best way to grab the coordinates is to click on the small "Maps Labs" link and enable "LatLng Marker". Then right-click on both locations, and click "drop LatLng marker".

This will show:

-73.9740, 40.7638

and

-73.9845, 40.7490

(I've rearranged to be in longitude, latitude order to make it easier).

I verified that the distance between these points is approximately 1,800m, which agrees with Google's measurement, which shows "1.8 km" (truncated to 1 decimal place).

Aside: note that a straight line on Google Maps is a loxodrome, not a geodesic, so it won't be exactly the same. However, at this scale, the difference will be negligible.

| improve this answer | |
  • I didn't get the lat/lng from the URL. Used the mouse pointer..but nevertheless Select ST_DISTANCE_SPHERE(ST_GeomFromText('POINT(40.75571 -73.97923)', 4326), ST_GeomFromText('POINT(40.757108 -73.974509)', 4326)) returns 526.702666151268 and ST_DISTANCE 0.00492364143698933 – rintim4711 May 28 '13 at 15:05
  • 2
    I believe WKT requires POINT coordinates to be specified in the order longitude, latitude, so you have them the wrong way around. – Jason Davies May 28 '13 at 15:08
  • This returns ST_DISTACNE: 0.00492364143698933, ST_DISTANCE_SPHERE: 426.951968421872 which is still not the result Google gives me :( – rintim4711 May 28 '13 at 15:13
  • 1
    No, Google Maps is truncating the number to one decimal place, so you need to include a margin of error. If you click "show options" and pick "km", it shows "1.8 km". – Jason Davies May 28 '13 at 19:02
  • 1
    The URL parameters simply do not reflect the positions of the points; the URL only specifies the centre of the browser window, and the zoom level (position of one of the corners). To get the positions of the points, you need to use LatLng markers, as I mention in my answer. – Jason Davies Jun 3 '13 at 15:31
0

You have your latitude and longitude reversed. POINT(40 -73) is down near the south pole, below South Africa. I'm guessing you actually want locations in the continental USA, around POINT(-73 40).

Coordinate order rule: first the X, then the Y.

| improve this answer | |
  • Then I get 426.951968421872 as a result from DISTANCE_SPHERE, which is not the 1,8km Google reported – rintim4711 Jun 3 '13 at 13:55
  • Great Circle Calculator also agrees with PostGIS, williams.best.vwh.net/gccalc.htm so maybe reconfirm how you are doing the Google calculation? – Paul Ramsey Jun 3 '13 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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