0

I am completely new to GeoDjango, and I am trying to calculate distances between objects.

I am doing everything as per this post https://coderwall.com/p/k1gg1a/distance-calculation-in-geodjango , but my results differ from what Google Maps tell me.

When I try to calculate distance between London and Paris, I get this

london = GEOSGeometry('POINT(-0.056922 51.480415)', srid=4326)
paris = GEOSGeometry('POINT(2.350918 48.867744)', srid=4326)
london = london.transform(900913, clone=True)
paris = paris.transform(900913, clone=True)
print(london.distance(paris))

>>> 527450.6633622452

Which looks like it's about 527 km.

However, Google Maps tell me that the distance is 341 km: enter image description here

What am I doing wrong?

kurtgn
  • 8,140
  • 13
  • 55
  • 91

1 Answers1

0

You're not doing anything wrong. Google maps is (I think) returning the Great Circle Distance while GEOS (which is an acronym for Geometry Engine - Open Source) is returning the Euclidean distance (GEOS is more about geometry and topology than geography). If you want to approximate the Great Circle Distance, you could try the Haversine Formula with the following code taken from this question:

>>>from math import radians, cos, sin, asin, sqrt
...
>>>def haversine(lon1, lat1, lon2, lat2):
...   # convert decimal degrees to radians 
...    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
...   # haversine formula 
...   dlon = lon2 - lon1 
...   dlat = lat2 - lat1 
...   a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
...   c = 2 * asin(sqrt(a)) 
...   r = 6371 # Radius of earth in kilometers. Use 3956 for miles
...   return c * r

>>>haversine(-0.056922, 51.480415, 2.350918, 48.867744)
>>>337.303...
Community
  • 1
  • 1
Alexander
  • 1,245
  • 2
  • 15
  • 22