12

I have 2 locations defined by gps coords, lat / long like returned by google maps:

http://gmaps-samples.googlecode.com/svn/trunk/geocoder/singlegeocode.html

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.

9

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
| improve this answer | |
  • 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 '12 at 18:08
  • 7
    What is the purpose of * 100? – Cristian Ciupitu Jul 29 '12 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 '12 at 1:15
  • It seems like Point is unused – Oleg Belousov Feb 7 '16 at 23:44
  • 1
    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. – Jonathan Richards Nov 13 '18 at 5:13
5

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/

| improve this answer | |
3

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
| improve this answer | |
  • 1
    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. – Jonathan Richards Nov 13 '18 at 5:13
1

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.

Code:

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
| improve this answer | |
0

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

| 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
0

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
| improve this answer | |

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