Feature request: Add STRtree.nearest function
See original GitHub issueNote
Since Shapely support no more than GEOS 3.4.0 right now, it will be great to add other functions like GEOSSTRtree_nearest in GEOS 3.6.0. So I was trying to add this function by myself and meet some problems.
Problems
GEOS source code is like:
const GEOSGeometry *
GEOSSTRtree_nearest (geos::index::strtree::STRtree *tree,
const geos::geom::Geometry *g)
{
return GEOSSTRtree_nearest_r( handle, tree, g);
}
const void* GEOSSTRtree_nearest_generic(GEOSSTRtree *tree,
const void* item,
const GEOSGeometry* itemEnvelope,
GEOSDistanceCallback distancefn,
void* userdata)
{
return GEOSSTRtree_nearest_generic_r( handle, tree, item, itemEnvelope, distancefn, userdata);
}
Then I added code in strtree.py, ctypes_declarations.py and test.py and tried to run the test but got the error Segmentation fault: 11 strtree.py
from shapely.geometry.base import geom_factory
class STRtree:
def nearest(self, geom):
if self._n_geoms == 0:
return None
return geom_factory(lgeos.GEOSSTRtree_nearest(self._tree_handle, geom._geom))
ctypes_declarations.py
def prototype(lgeos, geos_version):
if geos_version >= (3, 6, 0):
lgeos.GEOSSTRtree_nearest.argtypes = [c_void_p, c_void_p]
lgeos.GEOSSTRtree_nearest.restype = c_void_p
test.py
from shapely.geometry import Point, Polygon
from shapely.strtree import STRtree
a = []
a.append(Polygon([(1,0),(2,0),(2,1),(1,1)]))
a.append(Polygon([(0,2),(1,2),(1,3),(0,3)]))
a.append(Polygon([(-1.5,0),(-2.5,0),(-2.5,-1),(-1.5,-1)]))
tree = STRtree(a)
tree.nearest(Point(0,0))
I thought my problem is about data processing between the ctypes and geometry, so I tried to reimplement to shared_path function but still got something wrong.
from shapely.geometry import LineString
from shapely.geometry.base import geom_factory
from ctypes import CDLL, c_void_p
dll = CDLL('/Users/*****/anaconda3/lib/python3.7/site-packages/shapely/.dylibs/libgeos_c.1.dylib')
shared_path = dll.GEOSSharedPaths
shared_path.restype = c_void_p
shared_path.argtypes = [c_void_p, c_void_p]
g1 = LineString([((0, 0), (1, 1)), ((-1, 0), (1, 0))])
g2 = LineString([((0, 0), (1, 1)), ((-2, 0), (1, 5))])
geom_factory(shared_path(g1._geom, g2._geom))
Could you help me figure this out, thx!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Feature request: Add STRtree.nearest function #668 - GitHub
A question while trying to understand this nearest function. Say I am trying to find nearest road for a point. So the STR...
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. Note. 'nearest'...
Read more >Get nearest feature points from memory - GIS Stack Exchange
I was looking at either JTS or GeoTools (which uses JTS under the hood), but couldn't find any examples of "find the nearest...
Read more >python - Shapely STRtree way too slow? - Stack Overflow
I tried using both the nearest geometry query with sets of nodes as well as the ... 'id', uuid) points.append(point) tree = STRtree(points) ......
Read more >NetTopologySuite/NetTopologySuite - Gitter
I have a question about the library, i'm trying to find an implementation of a spatial index that i can use to find...
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
Thanks for the advice @sgillies , I think it’s working now. strtree.py
ctypes_declarations.py
test.py
Output
POINT (0 0.5)
I’ve wrote a test and I think the nearest function check every line in MultiLineString to see if the nearest line is inside MultiLineString. Here is my script @asif-rehan test.py
Output
MULTILINESTRING ((0 0, 1 0), (0 3, 1 3))