geopy distance calculation pitfall

10 December 2007   1 comment   Python

Powered by Fusion×

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
       return d.kilometers

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


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!
Thank you for posting a comment

Your email will never ever be published

Related posts

Kill Ugly Radio - A Frank Zappa blog 06 December 2007
WSSE Authentication and Apache 13 December 2007
Related by keywords:
To readline() or readlines() 12 March 2004
bool is instance of int in Python 05 December 2008
Reciprocal lesson about gender perspectives 02 September 2011
Nginx vs. Squid 17 March 2009
How and why to use django-mongokit (aka. Django to MongoDB) 08 March 2010
Nasty surprise of Django cache 09 December 2008
IssueTrackerProduct now officially abandoned 30 March 2012
Google Calendar, iCalendar Validator but not bloody Apple iCal 09 April 2009
On the command line no one can hear you screen. Or can they? 03 May 2012
In Django, how much faster is it to aggregate? 27 October 2010
tempfile in Python standard library 07 February 2006
Random ID generator for Zope 02 September 2005