Skip to content

Commit

Permalink
Swap order of geom and value in initdata
Browse files Browse the repository at this point in the history
Add tests of storing an enumeration
  • Loading branch information
Sean Gillies committed Mar 13, 2021
1 parent 0af0ab8 commit 297ae43
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
6 changes: 3 additions & 3 deletions shapely/strtree.py
Expand Up @@ -133,15 +133,15 @@ def _iteritems(self, initdata):
geom = obj
value = obj
else:
geom, value = obj
value, geom = obj
if not geom.is_empty:
yield geom, value
yield value, geom

def _init_tree(self, initdata):
if initdata:
node_capacity = 10
self._tree = lgeos.GEOSSTRtree_create(node_capacity)
for geom, value in self._iteritems(initdata):
for value, geom in self._iteritems(initdata):
lgeos.GEOSSTRtree_insert(
self._tree, geom._geom, ctypes.py_object(value)
)
Expand Down
19 changes: 16 additions & 3 deletions tests/test_strtree.py
Expand Up @@ -27,6 +27,19 @@ def test_query(geoms, query_geom, num_results):
assert len(results) == num_results


@requires_geos_342
@pytest.mark.parametrize("geoms", [[Point(i, i) for i in range(10)]])
@pytest.mark.parametrize(
"query_geom,expected",
[(Point(2, 2).buffer(0.99), [2]), (Point(2, 2).buffer(1.0), [1, 2, 3])],
)
def test_query_idx_values(geoms, query_geom, expected):
with pytest.warns(ShapelyDeprecationWarning):
tree = STRtree(enumerate(geoms))
results = tree.query(query_geom)
assert sorted(results) == sorted(expected)


@requires_geos_342
@pytest.mark.parametrize("geoms", [[Point(i, i) for i in range(10)]])
@pytest.mark.parametrize(
Expand Down Expand Up @@ -55,7 +68,7 @@ def callback(value):
)
def test_query_cb_str(geoms, values, query_geom, num_results):
with pytest.warns(ShapelyDeprecationWarning):
tree = STRtree(zip(geoms, values))
tree = STRtree(zip(values, geoms))

results = []

Expand All @@ -76,7 +89,7 @@ def callback(value):
)
def test_query_cb_dict(geoms, values, query_geom, num_results):
with pytest.warns(ShapelyDeprecationWarning):
tree = STRtree(zip(geoms, values))
tree = STRtree(zip(values, geoms))

results = []

Expand Down Expand Up @@ -159,7 +172,7 @@ def test_pickle_persistence():
Don't crash trying to use unpickled GEOS handle.
"""
with pytest.warns(ShapelyDeprecationWarning):
tree = STRtree([(Point(i, i).buffer(0.1), "Hi!") for i in range(3)])
tree = STRtree([("Hi!", 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")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_strtree_nearest.py
Expand Up @@ -44,5 +44,5 @@ def test_nearest_geom(geoms, query_geom):
@pytest.mark.parametrize("query_geom", [Point(0, 0.4)])
def test_nearest_value(geoms, values, query_geom):
with pytest.warns(ShapelyDeprecationWarning):
tree = STRtree(zip(geoms, values))
tree.nearest(query_geom) == "Ahoy!"
tree = STRtree(zip(values, geoms))
tree.nearest(query_geom) == "Ahoy!"

0 comments on commit 297ae43

Please sign in to comment.