Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use strtree to find an element nearest to it but not its own? #1106

Closed
liunx7594 opened this issue Mar 19, 2021 · 4 comments · Fixed by #1166
Closed

How to use strtree to find an element nearest to it but not its own? #1106

liunx7594 opened this issue Mar 19, 2021 · 4 comments · Fixed by #1166
Assignees
Milestone

Comments

@liunx7594
Copy link

Expected behavior and actual behavior.

How to use strtree to find an element nearest to it but not its own?My code is as follows, what I expect to find is (1,1), not (0,0).
If I want to start from (0,0), find the nearest point (1,1), and then find the second nearest point (2,2), how do I operate?

Steps to reproduce the problem.

from shapely.strtree import STRtree
from shapely.geometry import Point

points=[Point(0,0),Point(1,1),Point(2,2)]

tree=STRtree(points)

pos=tree.nearest(Point(0,0))
print(pos)

#POINT (0 0)

Operating system

windows 10 20H2
python3.7.0 64-bit (conda)

Shapely version and provenance

Shapely-1.7.1 from pip install

@sgillies
Copy link
Contributor

sgillies commented Mar 22, 2021

I'm sorry to say @liunx7594 that the tree's nearest() method is behaving correctly here. However, I do have an idea for adding a new method to the STRtree API in version 1.8.0, one that would allow you to exclude the query geometry from the result. See #1094 .

@sgillies sgillies self-assigned this Mar 22, 2021
@sgillies sgillies added this to the 1.8 milestone Mar 22, 2021
@liunx7594
Copy link
Author

OK, I'm looking forward to the next features

@liunx7594
Copy link
Author

By the way, it's really a great library, and thank you so much for creating it

@tomplex
Copy link
Contributor

tomplex commented Mar 24, 2021

I wrote a gist solving a similar problem someone posted here, might help you as well if I'm understanding your issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants