Repository: Toblerity/Shapely
Pull request #1097 (2021-03-03 20:46:03)

Warn about upcoming STRtree changes
In the constructor itself.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/37630174/badge)](https://coveralls.io/builds/37630174)

Coverage increased (+0.01%) to 85.158% when pulling **58819aace93de4d39c1d1eeaf85d9f6ce7bd2d0a on strtree-warning** into **3c7001907e858c18c56c0bf36c8587f067cd28bf on master**.


Patch:
diff --git a/CHANGES.txt b/CHANGES.txt
index 58d0203..a306159 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -13,9 +13,13 @@ version 2.0.0.
 - geometry .empty()
 - geometry .to_wkb() and .to_wkt()
 - geometry .ctypes and .__array_interface__
+- multi-part geometry .__len__
 
 Geometry objects will become immutable in version 2.0.0.
 
+The STRtree class will be entirely changed in 2.0.0. The exact future API is
+not yet decided, but will be decided before 1.8.0 is released.
+
 Deprecation warnings will be emitted in 1.8a1 when any of these features are
 used.
 
diff --git a/shapely/strtree.py b/shapely/strtree.py
index 70bee19..3d7f0fc 100644
--- a/shapely/strtree.py
+++ b/shapely/strtree.py
@@ -18,8 +18,12 @@ References
      https://www.cs.odu.edu/~mln/ltrs-pdfs/icase-1997-14.pdf
 """
 
-from shapely.geos import lgeos
 import ctypes
+from warnings import warn
+
+from shapely.errors import ShapelyDeprecationWarning
+from shapely.geos import lgeos
+
 
 class STRtree:
     """
@@ -72,6 +76,11 @@ class STRtree:
     """
 
     def __init__(self, geoms):
+        warn(
+            "STRtree will be completely changed in 2.0.0. The exact API is not yet decided, but will be documented before 1.8.0",
+            ShapelyDeprecationWarning,
+            stacklevel=2,
+        )
         # filter empty geometries out of the input
         geoms = [geom for geom in geoms if not geom.is_empty]
         self._n_geoms = len(geoms)
@@ -217,7 +226,7 @@ class STRtree:
                 dist = ctypes.cast(distance, ctypes.POINTER(ctypes.c_double))
                 lgeos.GEOSDistance(geom1._geom, geom2._geom, dist)
                 return 1
-            except:
+            except Exception:
                 return 0
 
         item = lgeos.GEOSSTRtree_nearest_generic(self._tree_handle, ctypes.py_object(geom), envelope._geom, \


Test patch:
diff --git a/tests/test_strtree.py b/tests/test_strtree.py
index 4cca258..ea5df39 100644
--- a/tests/test_strtree.py
+++ b/tests/test_strtree.py
@@ -4,18 +4,21 @@ import pickle
 import subprocess
 import sys
 
-from shapely.strtree import STRtree
-from shapely.geometry import Point, Polygon
+import pytest
 
+from shapely.errors import ShapelyDeprecationWarning
+from shapely.geometry import Point, Polygon
 from shapely import strtree
+from shapely.strtree import STRtree
 
-from tests.conftest import requires_geos_342
+from .conftest import requires_geos_342
 
 
 @requires_geos_342
 def test_query():
     points = [Point(i, i) for i in range(10)]
-    tree = STRtree(points)
+    with pytest.warns(ShapelyDeprecationWarning):
+        tree = STRtree(points)
     results = tree.query(Point(2, 2).buffer(0.99))
     assert len(results) == 1
     results = tree.query(Point(2, 2).buffer(1.0))
@@ -30,7 +33,8 @@ def test_insert_empty_geometry():
     """
     empty = Polygon()
     geoms = [empty]
-    tree = STRtree(geoms)
+    with pytest.warns(ShapelyDeprecationWarning):
+        tree = STRtree(geoms)
     assert tree._n_geoms == 0
     query = Polygon([(0, 0), (1, 1), (2, 0), (0, 0)])
     results = tree.query(query)
@@ -46,7 +50,8 @@ def test_query_empty_geometry():
     empty = Polygon()
     point = Point(1, 0.5)
     geoms = [empty, point]
-    tree = STRtree(geoms)
+    with pytest.warns(ShapelyDeprecationWarning):
+        tree = STRtree(geoms)
     assert tree._n_geoms == 1
     query = Polygon([(0, 0), (1, 1), (2, 0), (0, 0)])
     results = tree.query(query)
@@ -60,7 +65,8 @@ def test_references():
     empty = Polygon()
     point = Point(1, 0.5)
     geoms = [empty, point]
-    tree = STRtree(geoms)
+    with pytest.warns(ShapelyDeprecationWarning):
+        tree = STRtree(geoms)
     assert tree._n_geoms == 1
 
     empty = None
@@ -75,7 +81,8 @@ def test_references():
 
 @requires_geos_342
 def test_safe_delete():
-    tree = STRtree([])
+    with pytest.warns(ShapelyDeprecationWarning):
+        tree = STRtree([])
 
     _lgeos = strtree.lgeos
     strtree.lgeos = None
@@ -90,7 +97,9 @@ def test_pickle_persistence():
     """
     Don't crash trying to use unpickled GEOS handle.
     """
-    tree = STRtree([Point(i, i).buffer(0.1) for i in range(3)])
+
+    with pytest.warns(ShapelyDeprecationWarning):
+        tree = STRtree([Point(i, i).buffer(0.1) for i in range(3)])
     pickled_strtree = pickle.dumps(tree)
     unpickle_script_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "unpickle-strtree.py")
     proc = subprocess.Popen(
diff --git a/tests/test_strtree_nearest.py b/tests/test_strtree_nearest.py
index 05e342c..d21c1c1 100644
--- a/tests/test_strtree_nearest.py
+++ b/tests/test_strtree_nearest.py
@@ -1,19 +1,29 @@
 from . import unittest
 
+import pytest
+
+from shapely.errors import ShapelyDeprecationWarning
 from shapely.geometry import Point, Polygon
 from shapely.geos import geos_version
 from shapely.strtree import STRtree
 
+
 @unittest.skipIf(geos_version < (3, 6, 0), 'GEOS 3.6.0 required')
 class STRTreeNearest(unittest.TestCase):
     def test_nearest(self):
-        tree = STRtree([
-            Polygon([(1,0),(2,0),(2,1),(1,1)]),
-            Polygon([(0,2),(1,2),(1,3),(0,3)]),
-            Point(0,0.5)])
-        result = tree.nearest(Point(0,0))
-        self.assertEqual(result, Point(0,0.5))
-        result = tree.nearest(Point(0,4))
-        self.assertEqual(result, Polygon([(0,2),(1,2),(1,3),(0,3)]))
-        result = tree.nearest(Polygon([(-0.5,-0.5),(0.5,-0.5),(0.5,0.5),(-0.5,0.5)]))
-        self.assertEqual(result, Point(0,0.5))
+        with pytest.warns(ShapelyDeprecationWarning):
+            tree = STRtree(
+                [
+                    Polygon([(1, 0), (2, 0), (2, 1), (1, 1)]),
+                    Polygon([(0, 2), (1, 2), (1, 3), (0, 3)]),
+                    Point(0, 0.5),
+                ]
+            )
+        result = tree.nearest(Point(0, 0))
+        self.assertEqual(result, Point(0, 0.5))
+        result = tree.nearest(Point(0, 4))
+        self.assertEqual(result, Polygon([(0, 2), (1, 2), (1, 3), (0, 3)]))
+        result = tree.nearest(
+            Polygon([(-0.5, -0.5), (0.5, -0.5), (0.5, 0.5), (-0.5, 0.5)])
+        )
+        self.assertEqual(result, Point(0, 0.5))