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

163 views
Skip to first unread message

Ben Sizer

unread,
Dec 24, 2006, 9:54:09 PM12/24/06
to geopy
This seems to be some odd sort of interaction with properties and maybe
subclasses, but I can't get the distance() stuff to work properly.
Here's the end of a traceback, with hopefully all the relevant
information:

File "F:\code\pyprojects\kproj\controllers.py", line 164, in index
theDistance = geography.xdistance(myLoc,theirLoc)
File "F:\code\pyprojects\kproj\geography.py", line 46, in xdistance
return geopy.distance.distance(x,y).kilometers
File
"F:\code\Python24\lib\site-packages\geopy-0.93-py2.4.egg\geopy\distance.py",
line 344, in kilometers
AttributeError: 'VincentyDistance' object has no attribute
'_kilometers'

If I change .kilometers to .miles, I get a similar error:

File "F:\code\pyprojects\kproj\controllers.py", line 164, in index
theDistance = geography.xdistance(myLoc,theirLoc)
File "F:\code\pyprojects\kproj\geography.py", line 46, in xdistance
return geopy.distance.distance(x,y).miles
File
"F:\code\Python24\lib\site-packages\geopy-0.93-py2.4.egg\geopy\distance.py",
line 92, in miles
File
"F:\code\Python24\lib\site-packages\geopy-0.93-py2.4.egg\geopy\distance.py",
line 344, in kilometers
AttributeError: 'VincentyDistance' object has no attribute
'_kilometers'

Same sort of thing if I change it to .km or .mi.

Any suggestions?

--
Ben Sizer

Brian Beck

unread,
Dec 24, 2006, 10:01:40 PM12/24/06
to geopy
Hi Ben,

I think I've seen this before, but I can't remember the code that
produced it (it was fixable). Can you post the piece of your
geography.py that used geopy.distance.distance? What values are x and
y, specifically?

Thanks.

Ben Sizer

unread,
Dec 25, 2006, 6:01:09 PM12/25/06
to geopy

No need for that, as I think I found the issue. If x and y are exactly
equal or are the same object, this happens. My guess is that there's
some division by zero or a similar exception thrown before it reaches
the self._kilometers = s line, but caught elsewhere so that the end
application never sees it.

I can put in a special check in my code for now, but it would be great
if this could be fixed, if I've correctly identified the problem that
is!

--
Ben Sizer

Brian Beck

unread,
Dec 25, 2006, 6:04:57 PM12/25/06
to ge...@googlegroups.com
On 12/25/06, Ben Sizer <kyl...@gmail.com> wrote:
> No need for that, as I think I found the issue. If x and y are exactly
> equal or are the same object, this happens. My guess is that there's
> some division by zero or a similar exception thrown before it reaches
> the self._kilometers = s line, but caught elsewhere so that the end
> application never sees it.

Interesting, I thought the algorithm handled this properly, thanks for
the heads up.

--
Brian Beck
Adventurer of the First Order

Gary Bernhardt

unread,
Dec 25, 2006, 8:19:08 PM12/25/06
to ge...@googlegroups.com
On 12/25/06, Ben Sizer <kyl...@gmail.com> wrote:

No need for that, as I think I found the issue. If x and y are exactly
equal or are the same object, this happens. My guess is that there's
some division by zero or a similar exception thrown before it reaches
the self._kilometers = s line, but caught elsewhere so that the end
application never sees it.

You almost had it.  It's a premature return, not an exception.  From distance.py, line 284:

            if sin_sigma == 0:
                return 0 # Coincident points

That's the only place in the calculate function where it actually returns a value.  It looks like the function was originally written to return the distance, but was later modified to set self._kilometers as its output.  The attached patch fixes it.  And the results:

>>> geopy.distance.VincentyDistance((1.0, 1.0), (1.0, 1.0)).kilometers
0

coincident_points.diff
Reply all
Reply to author
Forward
0 new messages