Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sorry to be asking yet another geodjango newbie question.

What I want to do is quite simple, 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.

Thanks for any feedback

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

Answer seems to be in this google groups thread:

http://groups.google.com/group/geodjango/browse_thread/thread/b1f6e726cdcb402c/25dcef195dd093f5?lnk=gst&q=distance+between+points#25dcef195dd093f5

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
1  
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
add comment

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/

share|improve this answer
add comment

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
add comment

you can also use Sven Marnach from SE python code here for getting result that you want to.. i have added a line 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

i hope it helps you....

share|improve this answer
add comment

Your Answer

 
discard

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.