Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Fry-IT

Fry-IT is the company I work for

Photos

Photoalbum, both old and new.

Zope

What I have and am doing with Zope

Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 

geopy distance calculation pitfall


10th of December 2007

Geopy is a great little Python library for working with geocoding and distances using various online services such as Google's geocoder API.

Today I spent nearly half an hour trying to debug what was going on with my web application since I was getting this strange error:

 AttributeError: 'VincentyDistance' object has no attribute '_kilometers'

To debug it I wrapped the calls to geopy.distance.distance in various try-and-except statements to see what was going on and run these queries in a separate file. After a while I discovered that was what so special about the coordinates I fed it was that the coordinates where the same. Now my wrapping function looks like this instead:

 from geopy import distance as geopy_distance
 def geocode_distance((x1, y1), (x2, y2), unit='km'):
    if (x1, y1) == (x2, y2):
        return 0
    d = geopy_distance.distance((x1, y1), (x2, y2))
    if unit == 'miles':
        return d.miles
    else:
        return d.kilometers

Here's the mailing list thread about the very same issue



Comment

bruce - 5th April 2009  [«« Reply to this]
python has a great structure but i find it impossible to get used to the infinite number of libraries. for example, this sample code uses geopy so it doesn't run on my system. your link to geopy is broken!
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.