Calculate distance between 2 latitude/longitude Point

1,249 views
Skip to first unread message

Alexandre González

unread,
Jul 28, 2010, 9:19:21 AM7/28/10
to django...@googlegroups.com
Hi! I'm using the Django GEOS API [1] in my project to see the distance between two users.

I get the coordinates from google maps in latitude/longitude mode and I need to calculate the distance between them. I'm testing with .distance() method at GEOSGeometry but I receive a strange value.

This is the result of one of my test:

In [45]: Point(40.96312364002175, -5.661885738372803).distance(Point(40.96116097790996, -5.66283792257309))
Out[45]: 0.0021814438604553388

In Google Maps Distance Calculator [2] I can see that the result is: 0.145 miles = 0.233 km = 0.126 nautical miles = 233 meters = 764.436 feet

A friend told me on Google that perhaps I must calculate the distance in UTMs and the results surely is a lat/long distance. To test it I need pygps that is a project not maintained to import LLtoUTMfrom from LatLongUTMconversion

Any idea about this?

[2] http://www.daftlogic.com/projects-google-maps-distance-calculator.htm

--
Please, don't send me files with extensions: .doc, .docx, .xls, .xlsx, .ppt and/or .pptx
http://mirblu.com

Bill Freeman

unread,
Jul 28, 2010, 1:00:27 PM7/28/10
to django...@googlegroups.com
Having done my geo distance by "hand" instead of Django GEOS, it may not
be relevant, but have you tried multiplying the result you get by the radius of
the earth? Most formulas for this kind of thing are applicable to any
size sphere,
not just the earth, and return the distance as an angle, as seen from the center
of the sphere, and typically in radians.

Bill

2010/7/28 Alexandre González <agonz...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

Alexandre González

unread,
Jul 28, 2010, 1:11:11 PM7/28/10
to django...@googlegroups.com
I've tested it yet multiplying in miles and kilometers but the result if K miles/kms instead the correct result.

Thanks for your reply.

!!CONDORIOUS!!

unread,
Jul 28, 2010, 1:34:00 PM7/28/10
to django...@googlegroups.com
(lat , lon in radians)

pt 1 = lat, lon, alt=0
pt 2 = lat, lon, alt=0

p1_gcc = gd2gcc(p1)
p1_gcc = gd2gcc(p2)

gd2gcc ( is a standard operation found on the itnernet)

dis = sqrt(sum(pow(p1_gcc-p2-gcc, 2)))

cheers,

2010/7/28 Alexandre González <agonz...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

--
Condor
310.951.1177
condo...@gmail.com

:%s/war/peace/g

Alexandre González

unread,
Jul 29, 2010, 5:24:09 AM7/29/10
to django...@googlegroups.com
I going to try it now, but the unique results that I obtain in google searching [1] gd2gcc is this thread :)


Did you make some mistake writting it?

Thanks,
Álex González

Alexandre González

unread,
Jul 29, 2010, 6:23:18 AM7/29/10
to django...@googlegroups.com
I've solved translating a JavaScript function to python:

from math import *

RADIUS = 6371 #Earth's mean raadius in km

def distance(origin, destiny):
    (latitude1, longitude1) = (origin[0], origin[1])
    (latitude2, longitude2) = (destiny[0], destiny[1])

    dLat = radians(latitude1 - latitude2)
    dLong = radians(longitude1 - longitude2)

    # matter of faith
    a = sin(dLat/2) * sin(dLat/2) + cos(radians(latitude1)) * cos(radians(latitude2)) * sin(dLong/2) * sin(dLong/2)
    c = 2 * atan2(sqrt(a), sqrt(1-a))

    return (RADIUS * c)

# a test
origin = (40.96312364002175, -5.661885738372803)
destiny = (40.96116097790996, -5.66283792257309)
print distance(origin, destiny)


2010/7/29 Alexandre González <agonz...@gmail.com>

Alex Robbins

unread,
Jul 29, 2010, 9:56:05 AM7/29/10
to Django users
Geopy has a distance function build in, if you want something a little
more widely used/tested:
http://code.google.com/p/geopy/wiki/GettingStarted#Calculating_distances

Alex

On Jul 29, 5:23 am, Alexandre González <agonzale...@gmail.com> wrote:
> I've solved translating a JavaScript function to python:
>
> from math import *
>
> RADIUS = 6371 #Earth's mean raadius in km
>
> def distance(origin, destiny):
>     (latitude1, longitude1) = (origin[0], origin[1])
>     (latitude2, longitude2) = (destiny[0], destiny[1])
>
>     dLat = radians(latitude1 - latitude2)
>     dLong = radians(longitude1 - longitude2)
>
>     # matter of faith
>     a = sin(dLat/2) * sin(dLat/2) + cos(radians(latitude1)) *
> cos(radians(latitude2)) * sin(dLong/2) * sin(dLong/2)
>     c = 2 * atan2(sqrt(a), sqrt(1-a))
>
>     return (RADIUS * c)
>
> # a test
> origin = (40.96312364002175, -5.661885738372803)
> destiny = (40.96116097790996, -5.66283792257309)
> print distance(origin, destiny)
>
> 2010/7/29 Alexandre González <agonzale...@gmail.com>
>
>
>
> > I going to try it now, but the unique results that I obtain in google
> > searching [1] gd2gcc is this thread :)
>
> > [1]
> >http://www.google.com/search?hl=en&safe=off&q=gd2gcc&aq=f&aqi=&aql=&o...
>
> > <http://www.google.com/search?hl=en&safe=off&q=gd2gcc&aq=f&aqi=&aql=&o...>Did
> > you make some mistake writting it?
>
> > Thanks,
> > Álex González
>
> > On Wed, Jul 28, 2010 at 19:34, !!CONDORIOUS!! <condor.c...@gmail.com>wrote:
>
> >> (lat , lon in radians)
>
> >> pt 1 = lat, lon, alt=0
> >> pt 2 = lat, lon, alt=0
>
> >> p1_gcc = gd2gcc(p1)
> >> p1_gcc = gd2gcc(p2)
>
> >> gd2gcc ( is a standard operation found on the itnernet)
>
> >> dis = sqrt(sum(pow(p1_gcc-p2-gcc, 2)))
>
> >> cheers,
>
> >> 2010/7/28 Alexandre González <agonzale...@gmail.com>:
> >> > django-users...@googlegroups.com<django-users%2Bunsu...@googlegroups.com>
> >> .
> >> > For more options, visit this group at
> >> >http://groups.google.com/group/django-users?hl=en.
>
> >> --
> >> Condor
> >> 310.951.1177
> >> condor.c...@gmail.com
>
> >> :%s/war/peace/g
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Django users" group.
> >> To post to this group, send email to django...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> django-users...@googlegroups.com<django-users%2Bunsu...@googlegroups.com>
> >> .

Tomi Pieviläinen

unread,
Jul 29, 2010, 10:13:10 AM7/29/10
to Django users
> *
> In [45]: Point(40.96312364002175,
> -5.661885738372803).distance(Point(40.96116097790996, -5.66283792257309))
> Out[45]: 0.0021814438604553388

The results are what you get from the db. The thing is, you can't
assume that the distance in degrees is the same everywhere, so you
can't use a simple formula like multiplying by the circumference at
equator. Instead let the db do the work for you by telling what
coordinate system your input is and what do you want as output. Good
examples can be found in PostGIS (Postgresql with GIS) documentation:

http://postgis.refractions.net/documentation/manual-1.5/ST_Distance.html

Reply all
Reply to author
Forward
0 new messages