-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from caspervdw/setops-tests
Tests for set operations
- Loading branch information
Showing
2 changed files
with
61 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,) |