Skip to content

Commit

Permalink
Merge pull request #27 from caspervdw/setops-tests
Browse files Browse the repository at this point in the history
Tests for set operations
  • Loading branch information
caspervdw committed Aug 19, 2019
2 parents e885283 + e65768b commit de5ed5c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
36 changes: 14 additions & 22 deletions pygeos/set_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ def intersection_all(geometries, axis=0, **kwargs):
Parameters
----------
geometries : array_like
axis : int, tuple or None
Axis or axes along which the operation is performed. The default
axis : int
Axis along which the operation is performed. The default (zero)
performs the operation over the first dimension of the input array.
axis may be negative, in which case it counts from the last to the
first axis. If axis is None, the operation is performed over all the
axes. If it is a tuple of ints, a reduction is performed on multiple
axes.
first axis.
See also
--------
Expand Down Expand Up @@ -116,13 +114,11 @@ def symmetric_difference_all(geometries, axis=0, **kwargs):
Parameters
----------
geometries : array_like
axis : int, tuple or None
Axis or axes along which the operation is performed. The default
axis : int
Axis along which the operation is performed. The default (zero)
performs the operation over the first dimension of the input array.
axis may be negative, in which case it counts from the last to the
first axis. If axis is None, the operation is performed over all the
axes. If it is a tuple of ints, a reduction is performed on multiple
axes.
first axis.
See also
--------
Expand All @@ -132,10 +128,10 @@ def symmetric_difference_all(geometries, axis=0, **kwargs):
--------
>>> line_1 = Geometry("LINESTRING(0 0, 2 2)")
>>> line_2 = Geometry("LINESTRING(1 1, 3 3)")
>>> intersection_all([line_1, line_2])
<pygeos.Geometry LINESTRING (1 1, 2 2)>
>>> intersection_all([[line_1, line_2, Empty]], axis=1).tolist()
[<pygeos.Empty>]
>>> symmetric_difference_all([line_1, line_2])
<pygeos.Geometry MULTILINESTRING ((0 0, 1 1), (2 2, 3 3))>
>>> symmetric_difference_all([[line_1, line_2, Empty]], axis=1).tolist()
[<pygeos.Geometry MULTILINESTRING ((0 0, 1 1), (2 2, 3 3))>]
"""
return ufuncs.symmetric_difference.reduce(geometries, axis=axis, **kwargs)

Expand Down Expand Up @@ -169,13 +165,11 @@ def union_all(geometries, axis=0, **kwargs):
Parameters
----------
geometries : array_like
axis : int, tuple or None
Axis or axes along which the operation is performed. The default
axis : int
Axis along which the operation is performed. The default (zero)
performs the operation over the first dimension of the input array.
axis may be negative, in which case it counts from the last to the
first axis. If axis is None, the operation is performed over all the
axes. If it is a tuple of ints, a reduction is performed on multiple
axes.
first axis.
See also
--------
Expand All @@ -201,7 +195,5 @@ def union_all(geometries, axis=0, **kwargs):
np.asarray(geometries), axis=axis, start=geometries.ndim
)
# create_collection acts on the inner axis
collections = ufuncs.create_collection(
geometries, GeometryType.GEOMETRYCOLLECTION
)
collections = ufuncs.create_collection(geometries, GeometryType.GEOMETRYCOLLECTION)
return ufuncs.unary_union(collections, **kwargs)
65 changes: 47 additions & 18 deletions pygeos/test/test_set_operations.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
import pytest
import pygeos
import numpy as np
from pygeos import Geometry

from .common import point
from .common import point, all_types

SET_OPERATIONS = (
pygeos.difference,
pygeos.intersection,
pygeos.symmetric_difference,
pygeos.union,
)

def test_intersection():
poly1, poly2 = pygeos.box(0, 0, 10, 10), pygeos.box(5, 5, 20, 20)
actual = pygeos.intersection(poly1, poly2)
expected = pygeos.box(5, 5, 10, 10)
assert pygeos.equals(actual, expected)
REDUCE_SET_OPERATIONS = (
(pygeos.intersection_all, pygeos.intersection),
(pygeos.symmetric_difference_all, pygeos.symmetric_difference),
(pygeos.union_all, pygeos.union),
)

reduce_test_data = [
pygeos.box(0, 0, 5, 5),
pygeos.box(2, 2, 7, 7),
pygeos.box(4, 4, 9, 9),
]


@pytest.mark.parametrize("a", all_types)
@pytest.mark.parametrize("func", SET_OPERATIONS)
def test_set_operation_array(a, func):
actual = func([a, a], point)
assert actual.shape == (2,)
assert isinstance(actual[0], Geometry)


def test_union():
poly1, poly2 = pygeos.box(0, 0, 10, 10), pygeos.box(10, 0, 20, 10)
actual = pygeos.union(poly1, poly2)
expected = pygeos.box(0, 0, 20, 10)
@pytest.mark.parametrize("n", range(1, 4))
@pytest.mark.parametrize("func, related_func", REDUCE_SET_OPERATIONS)
def test_set_operation_reduce_1dim(n, func, related_func):
actual = func(reduce_test_data[:n])
# perform the reduction in a python loop and compare
expected = reduce_test_data[0]
for i in range(1, n):
expected = related_func(expected, reduce_test_data[i])
assert pygeos.equals(actual, expected)


def test_intersection_nan():
actual = pygeos.intersection(
np.array([point, np.nan, np.nan, point, None, None, point]),
np.array([np.nan, point, np.nan, None, point, None, point]),
)
assert pygeos.equals(actual[-1], point)
assert pygeos.is_empty(actual[:-1]).all()
@pytest.mark.parametrize("func, related_func", REDUCE_SET_OPERATIONS)
def test_set_operation_reduce_axis(func, related_func):
data = [[point] * 2] * 3 # shape = (3, 2)
actual = func(data)
assert actual.shape == (2,)
actual = func(data, axis=0) # default
assert actual.shape == (2,)
actual = func(data, axis=1)
assert actual.shape == (3,)
actual = func(data, axis=-1)
assert actual.shape == (3,)

0 comments on commit de5ed5c

Please sign in to comment.