sukri12 / pysal

Automatically exported from code.google.com/p/pysal
Other
0 stars 0 forks source link

using pysal.cg.KDTree.query with k=1 does not convert to arc distances #206

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
>> from pysal.cg import KDTree
>> from pysal.cg import sphere
>> x=[(115,35), (112, 30)]
>> tree = KDTree(x, radius=sphere.RADIUS_EARTH_KM, distance_metric = "Arc")
>> tree.query((110,31), k=1)

What is the expected output? What do you see instead?
Incorrect
>> tree.query((110,31), k=1)
Out[5]: (array(0.0347710053012962), 1)
Expected Output: (array(221.53723588), 1)

Correct
>> tree.query((110,31), k=2)
Out[6]: (array([ 221.53723588,  644.20848247]), array([1, 0]))

What version of the product are you using? On what operating system?
pysal==1.3.0
ScientificPython==2.8
numpy==1.6.1

Please provide any additional information below.

I believe the bug is in this code:
d,i = scipy.spatial.KDTree.query(self, self._toXYZ(x), k, eps=eps, 
distance_upper_bound = distance_upper_bound)
        dims = len(d.shape)
        r = self.radius
        if dims == 1:
            #TODO: implement linear2arcdist on numpy arrays
            d = [sphere.linear2arcdist(x,r) for x in d]
        elif dims == 2:
            d = [[sphere.linear2arcdist(x,r) for x in row] for row in d]
        return numpy.array(d),i

in the case where k=1, dims will be 0 which is not handled

Original issue reported on code.google.com by adam.h...@gmail.com on 11 Apr 2012 at 12:57

GoogleCodeExporter commented 8 years ago
Thanks for the bug report and suggested fix.  Fixed r1208 in the SVN.

This probably isn't major enough to get back ported to pysal 1.3.x, but it will 
be in 1.4 release due out in July. In the meantime feel free to install from 
the trunk, or make the change to your local install.

Original comment by schmi...@gmail.com on 11 Apr 2012 at 2:54