Home > Python, Tips > Calculating Distance Between Latitude Longitude Pairs in Python

Calculating Distance Between Latitude Longitude Pairs in Python

January 24th, 2009

Here is a code snippet that I found very helpful thanks to zachary:

I needed to calculate a simple distance between two latitude, longitude pairs in miles and this did the trick.  Apparently, its not the most accurate way to do it since the Earth is not a perfect sphere, but its close enough (an error of 0.5% at most I believe).

import math
#
# The following formulas are adapted from the Aviation Formulary
# http://williams.best.vwh.net/avform.htm
#
nauticalMilePerLat = 60.00721
nauticalMilePerLongitude = 60.10793
rad = math.pi / 180.0
milesPerNauticalMile = 1.15078
def calcDistance(lat1, lon1, lat2, lon2):
"""
Caclulate distance between two lat lons in NM
"""
yDistance = (lat2 - lat1) * nauticalMilePerLat
xDistance = (math.cos(lat1 * rad) + math.cos(lat2 * rad)) *
                (lon2 - lon1) * (nauticalMilePerLongitude / 2)
distance = math.sqrt( yDistance**2 + xDistance**2 )
return distance * milesPerNauticalMile

Hopefully someone will find this useful as well.

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Furl
  • LinkedIn
  • Reddit
  • StumbleUpon
  • TwitThis

Gee Python, Tips

  1. February 4th, 2009 at 12:01 | #1

    Yep, someone found this useful. In fact, considering the difficulty I was having translating an existing formula in PHP you have saved me having to learn a whole new language.

    Thank you.

  2. February 21st, 2009 at 23:27 | #2

    +1, thanks.

    I wanted it in meters so I looked up that conversion and here’s an adaptation:

    # adapted from http://www.zachary.com/s/blog/2005/01/12/python_zipcode_geo-programming
    import math

    nauticalMilePerLat = 60.00721
    nauticalMilePerLongitude = 60.10793
    rad = math.pi / 180.0
    metersPerNauticalMile = 1852

    def metersGeoDistance(lat1, lon1, lat2, lon2):
    “”"
    Calculate distance between two lat lons in miles
    “”"
    yDistance = (lat2 – lat1) * nauticalMilePerLat
    xDistance = (math.cos(lat1 * rad) + math.cos(lat2 * rad)) * (lon2 – lon1) * (nauticalMilePerLongitude / 2)

    distance = math.sqrt( yDistance**2 + xDistance**2 )

    return distance * metersPerNauticalMile

  3. February 21st, 2009 at 23:35 | #3

    After writing and posting that, I found this clean function,
    http://code.google.com/p/geolocator/source/browse/trunk/geolocator/gislib.py

    from this library,
    http://code.google.com/p/geolocator/

    which can be easy_install-ed from PyPi,
    http://pypi.python.org/pypi/geolocator

  4. k
    February 25th, 2009 at 10:12 | #4

    THANK YOU! My colleagues and I also found this helpful. :)

  5. t
    July 23rd, 2009 at 04:11 | #5

    thank you, this saved us some time

  6. Kashyap Puranik
    March 27th, 2010 at 08:40 | #6

    Thanks :) ! It was useful. I had to calculate distance between two IP addresses. I used http://api.hostip.info and the above code to get it.

  7. Dave
    June 7th, 2010 at 23:14 | #7

    Thank you, this is exactly what I was looking for :)

  1. No trackbacks yet.