Skip to content

Commit

Permalink
Fix error handling in STRtree (#432)
Browse files Browse the repository at this point in the history
* Fix error handling in STRtree

* Formatting

* Update CHANGELOG.rst

Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>

Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
  • Loading branch information
caspervdw and jorisvandenbossche committed Nov 14, 2021
1 parent c70ce4a commit cfa4256
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Version 0.12 (unreleased)

**Bug fixes**

* ...
* Raise ``GEOSException`` in the rare case when predicate evalution in ``STRtree.query``
errors. Previously, the exceptions were ignored silently and the geometry was added
to the result (as if the predicate returned ``True``) (#432).


**Acknowledgments**
Expand Down
5 changes: 5 additions & 0 deletions pygeos/tests/test_strtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ def test_query_unsupported_predicate(tree):
tree.query(pygeos.points(1, 1), predicate="disjoint")


def test_query_predicate_errors(tree):
with pytest.raises(pygeos.GEOSException):
tree.query(pygeos.linestrings([1, 1], [1, float("nan")]), predicate="touches")


def test_query_tree_with_none():
# valid GEOS binary predicate, but not supported for query
tree = pygeos.STRtree(
Expand Down
6 changes: 5 additions & 1 deletion src/strtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ static char evaluate_predicate(void* context, FuncGEOS_YpY_b* predicate_func,
GEOSGeometry* target_geom;
const GEOSPreparedGeometry* prepared_geom_tmp;
npy_intp i, size;
char predicate_result;

if (prepared_geom == NULL) {
// geom was not previously prepared, prepare it now
Expand All @@ -302,7 +303,10 @@ static char evaluate_predicate(void* context, FuncGEOS_YpY_b* predicate_func,
get_geom(pg_geom, &target_geom);

// keep the geometry if it passes the predicate
if (predicate_func(context, prepared_geom_tmp, target_geom)) {
predicate_result = predicate_func(context, prepared_geom_tmp, target_geom);
if (predicate_result == 2) {
return PGERR_GEOS_EXCEPTION;
} else if (predicate_result == 1) {
kv_push(GeometryObject**, *out_geoms, pg_geom_loc);
(*count)++;
}
Expand Down

0 comments on commit cfa4256

Please sign in to comment.