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.

6 Answers 6


The answer seems to be in this Google Groups thread:

from django.contrib.gis.geos import GEOSGeometry
pnt = GEOSGeometry('SRID=4326;POINT(40.396764 -3.68042)')
pnt2 = GEOSGeometry('SRID=4326;POINT( 48.835797 2.329102  )')
pnt.distance(pnt2) * 100
  • 3
    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, 2012 at 18:08
  • 8
    What is the purpose of * 100? Jul 29, 2012 at 3:35
  • 4
    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, 2012 at 1:15
  • 1
    GEOSGeometry performs linear distance calculations, even if SRID=4326 is specified. See docs.djangoproject.com/en/1.10/ref/contrib/gis/geos/… Dec 17, 2016 at 11:17
  • 3
    Do not use this answer. The distance function does not respect the SRID in any form and will simply give distance on a 2d plane. Nov 13, 2018 at 5:13

I think it's better use pyproj:

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

See more: http://blog.tremily.us/posts/pyproj/


You can use Point too.

from django.contrib.gis.geos import Point
p1 = Point(37.2676483,-6.9273579)
p2 = Point(37.2653293,-6.9249401)
distance = p1.distance(p2)
distance_in_km = distance * 100
  • 3
    Do not use this answer. The distance function does not respect the SRID in any form and will simply give distance on a 2d plane. Nov 13, 2018 at 5:13

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

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

  • 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. Mar 17, 2012 at 6:37

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

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