File: test_predicates.py

package info (click to toggle)
python-shapely 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 18,648; ansic: 6,615; makefile: 88; sh: 62
file content (414 lines) | stat: -rw-r--r-- 13,714 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from functools import partial

import numpy as np
import pytest

import shapely
from shapely import LinearRing, LineString, Point
from shapely.tests.common import (
    all_types,
    all_types_m,
    all_types_z,
    all_types_zm,
    empty,
    geometry_collection,
    ignore_invalid,
    line_string,
    linear_ring,
    point,
    polygon,
)

UNARY_PREDICATES = (
    shapely.has_z,
    pytest.param(
        shapely.has_m,
        marks=pytest.mark.skipif(
            shapely.geos_version < (3, 12, 0), reason="GEOS < 3.12"
        ),
    ),
    shapely.is_empty,
    shapely.is_simple,
    shapely.is_ring,
    shapely.is_closed,
    shapely.is_valid,
    shapely.is_missing,
    shapely.is_geometry,
    shapely.is_valid_input,
    shapely.is_prepared,
    shapely.is_ccw,
)

BINARY_PREDICATES = (
    shapely.disjoint,
    shapely.touches,
    shapely.intersects,
    shapely.crosses,
    shapely.within,
    shapely.contains,
    shapely.contains_properly,
    shapely.overlaps,
    shapely.covers,
    shapely.covered_by,
    pytest.param(
        partial(shapely.dwithin, distance=1.0),
        marks=pytest.mark.skipif(
            shapely.geos_version < (3, 10, 0), reason="GEOS < 3.10"
        ),
    ),
    shapely.equals,
    shapely.equals_exact,
    shapely.equals_identical,
)

BINARY_PREPARED_PREDICATES = BINARY_PREDICATES[:-2]

XY_PREDICATES = (
    (shapely.contains_xy, shapely.contains),
    (shapely.intersects_xy, shapely.intersects),
)


@pytest.mark.parametrize("geometry", all_types + all_types_z)
@pytest.mark.parametrize("func", UNARY_PREDICATES)
def test_unary_array(geometry, func):
    actual = func([geometry, geometry])
    assert actual.shape == (2,)
    assert actual.dtype == np.bool_


@pytest.mark.parametrize("func", UNARY_PREDICATES)
def test_unary_with_kwargs(func):
    out = np.empty((), dtype=np.uint8)
    actual = func(point, out=out)
    assert actual is out
    assert actual.dtype == np.uint8


@pytest.mark.parametrize("func", UNARY_PREDICATES)
def test_unary_missing(func):
    if func in (shapely.is_valid_input, shapely.is_missing):
        assert func(None)
    else:
        assert not func(None)


@pytest.mark.parametrize("a", all_types)
@pytest.mark.parametrize("func", BINARY_PREDICATES)
def test_binary_array(a, func):
    with ignore_invalid(shapely.is_empty(a) and shapely.geos_version < (3, 12, 0)):
        # Empty geometries give 'invalid value encountered' in all predicates
        # (see https://github.com/libgeos/geos/issues/515)
        actual = func([a, a], point)
    assert actual.shape == (2,)
    assert actual.dtype == np.bool_


@pytest.mark.parametrize("func", BINARY_PREDICATES)
def test_binary_with_kwargs(func):
    out = np.empty((), dtype=np.uint8)
    actual = func(point, point, out=out)
    assert actual is out
    assert actual.dtype == np.uint8


@pytest.mark.parametrize("func", BINARY_PREDICATES)
def test_binary_missing(func):
    actual = func(np.array([point, None, None]), np.array([None, point, None]))
    assert (~actual).all()


def test_binary_empty_result():
    a = LineString([(0, 0), (3, 0), (3, 3), (0, 3)])
    b = LineString([(5, 1), (6, 1)])
    with ignore_invalid(shapely.geos_version < (3, 12, 0)):
        # Intersection resulting in empty geometries give 'invalid value encountered'
        # (https://github.com/shapely/shapely/issues/1345)
        assert shapely.intersection(a, b).is_empty


@pytest.mark.parametrize("a", all_types)
@pytest.mark.parametrize("func, func_bin", XY_PREDICATES)
def test_xy_array(a, func, func_bin):
    with ignore_invalid(shapely.is_empty(a) and shapely.geos_version < (3, 12, 0)):
        # Empty geometries give 'invalid value encountered' in all predicates
        # (see https://github.com/libgeos/geos/issues/515)
        actual = func([a, a], 2, 3)
        expected = func_bin([a, a], Point(2, 3))
    assert actual.shape == (2,)
    assert actual.dtype == np.bool_
    np.testing.assert_allclose(actual, expected)


@pytest.mark.parametrize("a", all_types)
@pytest.mark.parametrize("func, func_bin", XY_PREDICATES)
def test_xy_array_broadcast(a, func, func_bin):
    a2 = shapely.transform(a, lambda x: x)  # makes a copy
    with ignore_invalid(shapely.is_empty(a) and shapely.geos_version < (3, 12, 0)):
        # Empty geometries give 'invalid value encountered' in all predicates
        # (see https://github.com/libgeos/geos/issues/515)
        actual = func(a2, [0, 1, 2], [1, 2, 3])
        expected = func_bin(a, [Point(0, 1), Point(1, 2), Point(2, 3)])
    np.testing.assert_allclose(actual, expected)


@pytest.mark.parametrize("func", [funcs[0] for funcs in XY_PREDICATES])
def test_xy_array_2D(func):
    polygon2 = shapely.transform(polygon, lambda x: x)  # makes a copy
    actual = func(polygon2, [0, 1, 2], [1, 2, 3])
    expected = func(polygon2, [[0, 1], [1, 2], [2, 3]])
    np.testing.assert_allclose(actual, expected)


@pytest.mark.parametrize("func, func_bin", XY_PREDICATES)
def test_xy_prepared(func, func_bin):
    actual = func(_prepare_with_copy([polygon, line_string]), 2, 3)
    expected = func_bin([polygon, line_string], Point(2, 3))
    np.testing.assert_allclose(actual, expected)


@pytest.mark.parametrize("func", [funcs[0] for funcs in XY_PREDICATES])
def test_xy_with_kwargs(func):
    out = np.empty((), dtype=np.uint8)
    point2 = shapely.transform(point, lambda x: x)  # makes a copy
    actual = func(point2, point2.x, point2.y, out=out)
    assert actual is out
    assert actual.dtype == np.uint8


@pytest.mark.parametrize("func", [funcs[0] for funcs in XY_PREDICATES])
def test_xy_missing(func):
    actual = func(
        np.array([point, point, point, None]),
        np.array([point.x, np.nan, point.x, point.x]),
        np.array([point.y, point.y, np.nan, point.y]),
    )
    np.testing.assert_allclose(actual, [True, False, False, False])


def test_equals_exact_tolerance():
    # specifying tolerance
    p1 = shapely.points(50, 4)
    p2 = shapely.points(50.1, 4.1)
    actual = shapely.equals_exact([p1, p2, None], p1, tolerance=0.05)
    np.testing.assert_allclose(actual, [True, False, False])
    assert actual.dtype == np.bool_
    actual = shapely.equals_exact([p1, p2, None], p1, tolerance=0.2)
    np.testing.assert_allclose(actual, [True, True, False])
    assert actual.dtype == np.bool_

    # default value for tolerance
    assert shapely.equals_exact(p1, p1).item() is True
    assert shapely.equals_exact(p1, p2).item() is False

    # an array of tolerances
    actual = shapely.equals_exact(p1, p2, tolerance=[0.05, 0.2, np.nan])
    np.testing.assert_allclose(actual, [False, True, False])


def test_equals_exact_normalize():
    l1 = LineString([(0, 0), (1, 1)])
    l2 = LineString([(1, 1), (0, 0)])
    # default requires same order of coordinates
    assert not shapely.equals_exact(l1, l2)
    assert shapely.equals_exact(l1, l2, normalize=True)


def test_equals_identical():
    # more elaborate tests are done at the Geometry.__eq__ level
    # requires same order of coordinates
    l1 = LineString([(0, 0), (1, 1)])
    l2 = LineString([(1, 1), (0, 0)])
    assert not shapely.equals_identical(l1, l2)

    # checks z-dimension (in contrast to equals_exact)
    l1 = LineString([(0, 0, 0), (1, 1, 0)])
    l2 = LineString([(0, 0, 1), (1, 1, 1)])
    assert not shapely.equals_identical(l1, l2)
    assert shapely.equals_exact(l1, l2)

    # NaNs in same place are equal (in contrast to equals_exact)
    with ignore_invalid():
        l1 = LineString([(0, np.nan), (1, 1)])
        l2 = LineString([(0, np.nan), (1, 1)])
    assert shapely.equals_identical(l1, l2)
    assert not shapely.equals_exact(l1, l2)


@pytest.mark.skipif(shapely.geos_version < (3, 10, 0), reason="GEOS < 3.10")
def test_dwithin():
    p1 = shapely.points(50, 4)
    p2 = shapely.points(50.1, 4.1)
    actual = shapely.dwithin([p1, p2, None], p1, distance=0.05)
    np.testing.assert_equal(actual, [True, False, False])
    assert actual.dtype == np.bool_
    actual = shapely.dwithin([p1, p2, None], p1, distance=0.2)
    np.testing.assert_allclose(actual, [True, True, False])
    assert actual.dtype == np.bool_

    # an array of distances
    actual = shapely.dwithin(p1, p2, distance=[0.05, 0.2, np.nan])
    np.testing.assert_allclose(actual, [False, True, False])


@pytest.mark.parametrize("geometry", all_types)
def test_has_z_has_m_all_types(geometry):
    assert not shapely.has_z(geometry)
    if shapely.geos_version >= (3, 12, 0):
        assert not shapely.has_m(geometry)


# The next few tests skip has_z/has_m with empty geometries
# See https://github.com/libgeos/geos/issues/888


@pytest.mark.parametrize("geometry", all_types_z)
def test_has_z_has_m_all_types_z(geometry):
    if shapely.is_empty(geometry):
        pytest.skip("GEOSHasZ with EMPTY geometries is inconsistent")
    assert shapely.has_z(geometry)
    if shapely.geos_version >= (3, 12, 0):
        assert not shapely.has_m(geometry)


@pytest.mark.skipif(
    shapely.geos_version < (3, 12, 0),
    reason="M coordinates not supported with GEOS < 3.12",
)
@pytest.mark.parametrize("geometry", all_types_m)
def test_has_m_all_types_m(geometry):
    if shapely.is_empty(geometry):
        pytest.skip("GEOSHasM with EMPTY geometries is inconsistent")
    assert not shapely.has_z(geometry)
    assert shapely.has_m(geometry)


@pytest.mark.skipif(
    shapely.geos_version < (3, 12, 0),
    reason="M coordinates not supported with GEOS < 3.12",
)
@pytest.mark.parametrize("geometry", all_types_zm)
def test_has_z_has_m_all_types_zm(geometry):
    if shapely.is_empty(geometry):
        pytest.skip("GEOSHasZ with EMPTY geometries is inconsistent")
    assert shapely.has_z(geometry)
    assert shapely.has_m(geometry)


@pytest.mark.parametrize(
    "geometry,expected",
    [
        (point, False),
        (line_string, False),
        (linear_ring, True),
        (empty, False),
    ],
)
def test_is_closed(geometry, expected):
    assert shapely.is_closed(geometry) == expected


def test_relate():
    p1 = shapely.points(0, 0)
    p2 = shapely.points(1, 1)
    actual = shapely.relate(p1, p2)
    assert isinstance(actual, str)
    assert actual == "FF0FFF0F2"


@pytest.mark.parametrize("g1, g2", [(point, None), (None, point), (None, None)])
def test_relate_none(g1, g2):
    assert shapely.relate(g1, g2) is None


def test_relate_pattern():
    g = shapely.linestrings([(0, 0), (1, 0), (1, 1)])
    polygon = shapely.box(0, 0, 2, 2)
    assert shapely.relate(g, polygon) == "11F00F212"
    assert shapely.relate_pattern(g, polygon, "11F00F212")
    assert shapely.relate_pattern(g, polygon, "*********")
    assert not shapely.relate_pattern(g, polygon, "F********")


def test_relate_pattern_empty():
    with ignore_invalid(shapely.geos_version < (3, 12, 0)):
        # Empty geometries give 'invalid value encountered' in all predicates
        # (see https://github.com/libgeos/geos/issues/515)
        assert shapely.relate_pattern(empty, empty, "*" * 9).item() is True


@pytest.mark.parametrize("g1, g2", [(point, None), (None, point), (None, None)])
def test_relate_pattern_none(g1, g2):
    assert shapely.relate_pattern(g1, g2, "*" * 9).item() is False


def test_relate_pattern_incorrect_length():
    with pytest.raises(shapely.GEOSException, match="Should be length 9"):
        shapely.relate_pattern(point, polygon, "**")

    with pytest.raises(shapely.GEOSException, match="Should be length 9"):
        shapely.relate_pattern(point, polygon, "**********")


@pytest.mark.parametrize("pattern", [b"*********", 10, None])
def test_relate_pattern_non_string(pattern):
    with pytest.raises(TypeError, match="expected string"):
        shapely.relate_pattern(point, polygon, pattern)


def test_relate_pattern_non_scalar():
    with pytest.raises(ValueError, match="only supports scalar"):
        shapely.relate_pattern([point] * 2, polygon, ["*********"] * 2)


@pytest.mark.parametrize(
    "geom, expected",
    [
        (LinearRing([(0, 0), (0, 1), (1, 1), (0, 0)]), False),
        (LinearRing([(0, 0), (1, 1), (0, 1), (0, 0)]), True),
        (LineString([(0, 0), (0, 1), (1, 1), (0, 0)]), False),
        (LineString([(0, 0), (1, 1), (0, 1), (0, 0)]), True),
        (LineString([(0, 0), (1, 1), (0, 1)]), False),
        (LineString([(0, 0), (0, 1), (1, 1)]), False),
        (point, False),
        (polygon, False),
        (geometry_collection, False),
        (None, False),
    ],
)
def test_is_ccw(geom, expected):
    assert shapely.is_ccw(geom) == expected


def _prepare_with_copy(geometry):
    """Prepare without modifying in-place"""
    geometry = shapely.transform(geometry, lambda x: x)  # makes a copy
    shapely.prepare(geometry)
    return geometry


@pytest.mark.parametrize("a", all_types)
@pytest.mark.parametrize("func", BINARY_PREPARED_PREDICATES)
def test_binary_prepared(a, func):
    with ignore_invalid(shapely.is_empty(a) and shapely.geos_version < (3, 12, 0)):
        # Empty geometries give 'invalid value encountered' in all predicates
        # (see https://github.com/libgeos/geos/issues/515)
        actual = func(a, point)
        result = func(_prepare_with_copy(a), point)
    assert actual == result


@pytest.mark.parametrize("geometry", all_types)
def test_is_prepared_true(geometry):
    assert shapely.is_prepared(_prepare_with_copy(geometry))


@pytest.mark.parametrize("geometry", all_types + (None,))
def test_is_prepared_false(geometry):
    assert not shapely.is_prepared(geometry)


def test_contains_properly():
    # polygon contains itself, but does not properly contains itself
    assert shapely.contains(polygon, polygon).item() is True
    assert shapely.contains_properly(polygon, polygon).item() is False