Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I want to do is quite simple, I have 2 locations defined by gps coords, lat / long like returned by google maps:

I need to calculate the distance between them. I know I could use the google API but I'll be processing bulk queries so I'd rather do it on my own server.

I've spent a few hours with the docs, installed geodjango OK, but I can't seem to find an example of this. Everything in the literature is way more complicated than I need.

Thanks for any feedback

share|improve this question
up vote 7 down vote accepted

The answer seems to be in this Google Groups thread:

from django.contrib.gis.geos import Point, GEOSGeometry
pnt = GEOSGeometry('SRID=4326;POINT(40.396764 -3.68042)')
pnt2 = GEOSGeometry('SRID=4326;POINT( 48.835797 2.329102  )')
pnt.distance(pnt2) * 100
share|improve this answer
Remember that POINT() takes the form POINT(X Y). You were probably trying to give an example for Madrid, but the points that you use are actually in the Indian Ocean and Kenya, respectively. – user7171 Apr 26 '12 at 18:08
What is the purpose of * 100? – Cristian Ciupitu Jul 29 '12 at 3:35
shouldn't you transform the points into a projection in meters? Use the UTM zone for better accuracy if you know it. >>> pnt.transform(900913) >>> pnt2.transform(900913) >>> pnt.distance(pnt2) 1153485.9216961625 – monkut Dec 7 '12 at 1:15
It seems like Point is unused – Oleg Tikhonov Feb 7 at 23:44

I think it's better use pyproj:

geod = pyproj.Geod(ellps='WGS84') 
angle1,angle2,distance = geod.inv(long1, lat1, long2, lat2)

See more:

share|improve this answer

If you want an answer that doesn't use a geodjango library or function. look for some questions and answers with the tag. They give you formulas that will work with any language or framework. One such question is Distance between GPS coordinates

share|improve this answer
I would rather use geodjango since I presume the most efficient way to do bulk calculations is with the internal logic of a spatial db. And the rest of my site is django, so it would be nice to get everything consistent under the same framework. – MadMaardigan Mar 17 '12 at 6:37

You can also use the Python code of Sven Marnach for getting the result that you want to. I have added a line of code for getting the result in meters.


from math import sin, cos, radians, degrees, acos

def calc_dist(lat_a, long_a, lat_b, long_b):
    lat_a = radians(lat_a)
    lat_b = radians(lat_b)
    long_diff = radians(long_a - long_b)
    distance = (sin(lat_a) * sin(lat_b) +
                cos(lat_a) * cos(lat_b) * cos(long_diff))
    resToMile = degrees(acos(distance)) * 69.09
    resToMt = resToMile / 0.00062137119223733
    return resToMt
share|improve this answer

I liked a lot the solution i have seen once with help of django and geopy. Nevertheless, i changed the code a bit in order to have the freedom to enter more than just two points.

from django.contrib.gis.geos import Point
from geopy.distance import distance as geopy_distance
from itertools import tee, izip

def pairwise(iterable):
    a, b= tee(iterable)
    next(b, None)
    return izip(a,b)

chicago = Point(41.50, 87.37)
san_francisco = Point(37.47, 122.26)
st_louis = Point(38.62, 90.19)
washington = Point(38.53, 77.02)

points = (washington, st_louis, chicago, san_francisco)

d = sum(geopy_distance(a,b).meters for (a,b) in pairwise(points))

distance_km = d/1000
distance_miles = distance_km*0.621371

print "Distance in kilometre: ",distance_km
print "Distance in miles: ",distance_miles
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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