13

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
ChristopherDBerry
  • 363
  • 1
  • 2
  • 9

6 Answers6

10

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
ChristopherDBerry
  • 363
  • 1
  • 2
  • 9
  • 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. –  Apr 26 '12 at 18:08
  • 8
    What is the purpose of * 100? – Cristian Ciupitu Jul 29 '12 at 03: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 07 '12 at 01:15
  • It seems like Point is unused – Oleg Belousov Feb 07 '16 at 23:44
  • 1
    GEOSGeometry performs linear distance calculations, even if SRID=4326 is specified. See https://docs.djangoproject.com/en/1.10/ref/contrib/gis/geos/#django.contrib.gis.geos.GEOSGeometry.distance – Rushi Agrawal Dec 17 '16 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. – Jonathan Richards Nov 13 '18 at 05:13
6

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/

JJD
  • 1,511
  • 1
  • 17
  • 30
Ana Sousa
  • 61
  • 1
  • 1
4

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
Virako
  • 149
  • 1
  • 2
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
urcm
  • 22,533
  • 4
  • 57
  • 109
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
g07kore
  • 311
  • 2
  • 9
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

mhoran_psprep
  • 1,001
  • 1
  • 6
  • 9
  • 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. – ChristopherDBerry Mar 17 '12 at 06:37