STRtree Nearest method always returns one geometry
See original GitHub issueCreate two points that are equidistant to a Line.
from shapely.strtree import STRtree
from shapely import wkt
from shapely.geometry import box, Point, LineString
keys = []
keys.append(Point(1.5, 3))
keys.append(Point(1, 2.5))
query = LineString([Point(1,2),Point(3,4)])
for i, g in enumerate(keys):
bbox_intersects = box(*g.bounds).intersects(query)
print(i, bbox_intersects, g.distance(query))
# 0 False 0.3535533905932738
# 1 False 0.3535533905932738
Create the tree and query for the nearest neighbor. Only one geometry will be returned.
tree = STRtree(keys)
result = tree.nearest(query)
print(result)
# POINT (1 2.5)
Does anyone know of an efficient workaround that always guarantees the nearest neighbor between two geometries and doesn’t yield false negatives?
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
STRtree Nearest method always returns one geometry #967
Create the tree and query for the nearest neighbor. Only one geometry will be returned. tree = STRtree(keys) result = tree.nearest(query) print( ...
Read more >STRTree — Shapely 2.0.0 documentation - Read the Docs
Return the index of the nearest geometry in the tree for each input geometry based on distance within two-dimensional Cartesian space.
Read more >Index (org.locationtech.jts:jts-core 1.18.0 API)
Computes the closest points on two line segments. closestPoints(Geometry, Geometry) - Static method in class org.locationtech.jts.operation.distance.
Read more >Shapely Documentation - Read the Docs
Note: These methods will always return a geometric object. An intersection of disjoint ... Returns the nearest geometry in strtree to geom.
Read more >Index (JTS Topology Suite version 1.12) - Tsusiat Software
CentroidLine: Adds the linear components of by a Geometry to the centroid total. add(Coordinate[]) ... Method in class com.vividsolutions.jts.index.strtree.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah, I see what you’re saying. In that case I would probably try something like:
this gist describes what I mean. It returns both points for a couple of different symmetric situations like the one you posted above:
@tomplex, I’ve adapted your fantastic solution to my problem.
This is probably python’s only rtree solution for exact nearest neighbors search that guarantees 100% recall and generalizes to multiple geometries.
I wasn’t able to test performance, but I hope it scales to couple million geometries.
Once again thank you!
Example dataset: