File: test_functions.py

package info (click to toggle)
pygeoif 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 368 kB
  • sloc: python: 4,104; makefile: 4
file content (453 lines) | stat: -rw-r--r-- 12,551 bytes parent folder | download
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"""Test geometric functions."""

import itertools
import math
import random

import pytest

from pygeoif.functions import centroid
from pygeoif.functions import compare_coordinates
from pygeoif.functions import compare_geo_interface
from pygeoif.functions import convex_hull
from pygeoif.functions import dedupe
from pygeoif.functions import signed_area


def circle_ish(x, y, r, steps):
    pts = []
    steps = max(steps, 3)
    for step in range(steps):
        phi = 2 * math.pi * step / steps
        pts.append((r * math.cos(phi) + x, r * math.sin(phi) + y))
    pts.append((x + r, y))
    return pts


def star_ish(x, y, r, steps):
    pts = []
    steps = max(steps, 3)
    for step in range(steps):
        phi = 2 * math.pi * step / steps
        pts.append(
            (random.randrange(1, r + 1) * math.cos(phi) + x, r * math.sin(phi) + y),
        )
    pts.append((x + r, y))
    return pts


def spiral_ish(x, y, r, steps):
    pts = []
    for step in range(1, steps):
        phi = math.pi * step / steps
        pts.append((step * r * math.cos(phi) + x, step * r * math.sin(phi) + y))
    pts.append((x + r, y))
    return pts


def crescent_ish(x, y, r, steps):
    pts = []
    for step in range(1, steps):
        phi = math.pi * step / steps
        pts.append((step * r * 2 * math.cos(phi) + x, step * r * math.sin(phi) + y))
    for step in range(steps, 0, -1):
        phi = math.pi * step / steps
        pts.append((step * r * math.cos(phi) + x, step * r * math.sin(phi) + y))
    pts.append(pts[0])
    return pts


def test_signed_area() -> None:
    a0 = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
    a1 = [(0, 0, 1), (0, 2, 2), (2, 2, 3), (2, 0, 4), (0, 0, 1)]
    assert signed_area(a0) == signed_area(a1) == -4
    assert centroid(a0)[1] == centroid(a1)[1] == -4


def test_signed_area2() -> None:
    a0 = [(0, 0), (0, 1), (1, 1), (0, 0)]
    assert centroid(a0)[1] == signed_area(a0)


def test_centroid_line() -> None:
    a0 = [(0, 0), (1, 1), (0, 0)]
    assert centroid(a0) == ((math.nan, math.nan), 0)


def test_signed_area_0_3d() -> None:
    assert signed_area(((0.0, 0.0, 0.0), (0.0, 0.0, 0.0))) == 0.0


def test_signed_area_0_2d() -> None:
    assert signed_area(((0.0, 0.0), (0.0, 0.0), (0.0, 0.0))) == 0.0


def test_signed_area_circle_ish() -> None:
    for i in range(100):
        x = random.randrange(20)
        y = random.randrange(20)
        r = random.randrange(1, 20 + i)
        pts = []
        steps = random.randrange(3, 30 + i)
        pts = circle_ish(x, y, r, steps)

        center1, area1 = centroid(pts)
        center2, area2 = centroid(list(reversed(pts)))

        # both area computations should be approximately equal
        assert math.isclose(area1, signed_area(pts))
        assert math.isclose(area2, signed_area(list(reversed(pts))))
        assert center1, area1 == (center2, area2)
        assert math.isclose(center1[0], x, abs_tol=0.000_000_1)
        assert math.isclose(center1[1], y, abs_tol=0.000_000_1)
        # we are computing an approximation of math.pi
        if steps > 12:
            assert 3.0 * r**2 < area1 < 3.2 * r**2
        if steps > 30:
            assert 3.1 * r**2 < area1 < 3.2 * r**2


def test_signed_area_crescent_ish() -> None:
    for i in range(100):
        x = random.randrange(20) - i
        y = random.randrange(20 + i)
        r = random.randrange(1, 20)
        pts = []
        steps = random.randrange(4, 20)
        pts = crescent_ish(x, y, r, steps)

        center1, area1 = centroid(pts)
        center2, area2 = centroid(list(reversed(pts)))

        assert math.isclose(area1, signed_area(pts))
        assert math.isclose(area2, signed_area(list(reversed(pts))))
        assert center1, area1 == (center2, area2)


def test_empty_hull() -> None:
    assert convex_hull([]) == []


def test_point() -> None:
    pts = [(0, 0)]

    hull = convex_hull(pts)

    assert hull == [(0, 0)]


def test_line() -> None:
    pts = [(0, 0), (1, 1)]

    hull = convex_hull(pts)

    assert hull == [(0, 0), (1, 1)]


def test_line_minimal() -> None:
    pts = [(0, 0), (1, 1), (1, 0)]

    hull = convex_hull(pts)

    assert hull == ((0, 0), (1, 0), (1, 1), (0, 0))


def test_line2() -> None:
    pts = ((x, x) for x in range(5))

    hull = convex_hull(pts)

    assert hull == [(0, 0), (4, 4)]


def test_line3() -> None:
    pts = ((x, x) for x in range(3))

    hull = convex_hull(pts)

    assert hull == [(0, 0), (2, 2)]


def test_square() -> None:
    pts = list(itertools.product(range(100), range(100)))
    hull = convex_hull(pts)
    assert hull == ((0, 0), (99, 0), (99, 99), (0, 99), (0, 0))


def test_triangle() -> None:
    pts = []
    for x in range(100):
        pts.extend((x, y) for y in range(x + 1))
    hull = convex_hull(pts)
    assert hull == ((0, 0), (99, 0), (99, 99), (0, 0))


def test_trapezoid() -> None:
    pts = []
    for x in range(100):
        pts.extend((x, y) for y in range(-x - 1, x + 1))
    hull = convex_hull(pts)
    assert hull == ((0, -1), (99, -100), (99, 99), (0, 0), (0, -1))


def test_circles() -> None:
    for _ in range(10):
        x = random.randrange(20)
        y = random.randrange(20)
        r = random.randrange(1, 20)
        steps = random.randrange(4, 20)
        pts = circle_ish(x, y, r, steps)

        hull = convex_hull(pts)

        assert set(hull) == set(pts)
        assert len(hull) == len(pts)


def test_spiral() -> None:
    for _ in range(10):
        x = random.randrange(20)
        y = random.randrange(20)
        pts = []
        steps = random.randrange(4, 20)
        spiral_ish(x, y, 1, steps)

        hull = convex_hull(pts)

        assert set(hull) == set(pts)
        assert len(hull) == len(pts)


def test_crescent() -> None:
    for _ in range(10):
        x = random.randrange(20)
        y = random.randrange(20)
        pts = []
        steps = random.randrange(4, 20)
        crescent_ish(x, y, 1, steps)

        hull = convex_hull(pts)
        assert len(hull) == len(pts) / 2


def test_star() -> None:
    for _ in range(10):
        x = random.randrange(20)
        y = random.randrange(20)
        pts = []
        steps = random.randrange(4, 20)
        star_ish(x, y, 1, steps)

        hull = convex_hull(pts)

        assert set(hull).issubset(set(pts))
        assert len(hull) <= len(pts)


def test_random() -> None:
    """The convex hull of an exiting hull must be the same as the hull itself."""
    for i in range(100):
        pts = (
            (random.randrange(-x, x + 1), random.randrange(-x, x + 1))
            for x in range(i + 1)
        )
        hull = convex_hull(pts)

        assert convex_hull(hull) == hull
        if len(hull) > 3:
            _, area = centroid(tuple(hull))
            assert math.isclose(area, signed_area(hull))


def test_dedupe_point() -> None:
    assert dedupe(((1, 2, 3),) * 10) == ((1, 2, 3),)


def test_dedupe_line() -> None:
    assert dedupe(((1, 2, 3), (4, 5, 6)) * 3) == (
        (1, 2, 3),
        (4, 5, 6),
        (1, 2, 3),
        (4, 5, 6),
        (1, 2, 3),
        (4, 5, 6),
    )


def test_dedupe_line2() -> None:
    assert dedupe(((1, 2, 3),) * 2 + ((4, 5, 6),) * 3) == ((1, 2, 3), (4, 5, 6))


@pytest.mark.parametrize(
    ("numbers", "expected"),
    [
        ((0, 0), True),
        ((0, 1), False),
        ((1, 0), False),
        ((1, 1), True),
        ((0.3, 0.2 + 0.1), True),
        ((1, "1"), False),
        (("10", 10), False),
        ((None, 1), False),
        ((1, None), False),
        ((None, None), False),
        ((1, 1.0), True),
        ((1.0, 1), True),
        ((math.inf, math.inf), True),
        ((-math.inf, -math.inf), True),
        ((math.inf, -math.inf), False),
        ((math.nan, math.nan), False),
    ],
)
def test_compare_numbers(numbers: tuple[float, float], expected: bool) -> None:
    """Compare numbers for equality."""
    assert compare_coordinates(*numbers) is expected


@pytest.mark.parametrize(
    ("points", "expected"),
    [
        (((1, 2), [1, 2]), True),
        (((1, 2), [1, 3]), False),
        (((1, 2), [2, 2]), False),
        (((1, 2, 0), (1, 2)), False),
        (((1, 2), (1, 2, 0)), False),
        (((1, 2, 0), [1, 2, 0]), True),
        (((1, 2, 0), [1, 2, 1]), False),
        (((1, 2, 0), [1, 3, 0]), False),
        (((1, 2, 0), [2, 2, 0]), False),
        (((0.3, 0.3), (0.1 + 0.2, 0.1 + 0.2)), True),
        (((0.3, 0.3), ("0.3", "0.3")), False),
    ],
)
def test_compare_points(points, expected: bool) -> None:
    """Compare a single set of coordinates."""
    assert compare_coordinates(*points) is expected


@pytest.mark.parametrize(
    ("lines", "expected"),
    [
        ((((1, 2), (3, 4)), ((1, 2), (3, 4))), True),
        ((((1, 2), (3, 4)), [[1, 2], [3, 4]]), True),
        ((((1, 2), (3, 4)), ((1, 2), (3, 5))), False),
        ((((1, 2), (3, (3, 5))), ((1, 2), (3, (3, 5)))), True),
        ((((1, 2), (3, 4)), ((1, 2), (3, 4), (3, 4))), False),
    ],
)
def test_compare_lines(lines, expected: bool) -> None:
    """Compare a sequence of coordinates."""
    assert compare_coordinates(*lines) is expected


@pytest.mark.parametrize(
    ("polygons", "expected"),
    [
        (
            (
                (((1, 2), (3, 4)), ((5, 6), (7, 8))),
                (((1, 2), (3, 4)), ((5, 6), (7, 8))),
            ),
            True,
        ),
        (
            (
                (((1, 2), (3, 4)), ((5, 6), (7, 8))),
                (((1, 2), (3, 4)), ((5, 6), (7, 8)), ((5, 6), (7, 8))),
            ),
            False,
        ),
        (
            (
                (((1, 2), (3, 4)), ((5, 6), (7, 8))),
                (((1, 2), (3, 4)), (((5, 6), (7, 8)), ((5, 6), (7, 8)))),
            ),
            False,
        ),
        (
            (
                [[[1, 2], [3, 4]], ([[5, 6], (7, 8)], ([5, 6], [7, 8]))],
                (((1, 2), (3, 4)), (((5, 6), (7, 8)), ((5, 6), (7, 8)))),
            ),
            True,
        ),
    ],
)
def test_compare_polygons(polygons, expected: bool) -> None:
    """Compare nested sequences of coordinates."""
    assert compare_coordinates(*polygons) is expected


def test_compare_eq_geo_interface() -> None:
    geo_if = {
        "geometries": (
            {
                "geometries": (
                    {
                        "geometries": (
                            {
                                "bbox": (0, 0, 0, 0),
                                "coordinates": (0, 0),
                                "type": "Point",
                            },
                            {
                                "bbox": (0, 0, 2, 2),
                                "coordinates": ((0, 0), (1, 1), (1, 2), (2, 2)),
                                "type": "MultiPoint",
                            },
                        ),
                        "type": "GeometryCollection",
                    },
                    {
                        "bbox": (0, 0, 3, 1),
                        "coordinates": ((0, 0), (3, 1)),
                        "type": "LineString",
                    },
                ),
                "type": "GeometryCollection",
            },
            {"coordinates": (((0, 0), (1, 1), (1, 0), (0, 0)),), "type": "Polygon"},
            {
                "bbox": (0, 0, 2, 2),
                "coordinates": (
                    ((0, 0), (0, 2), (2, 2), (2, 0), (0, 0)),
                    ((1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)),
                ),
                "type": "Polygon",
            },
            {"coordinates": (0, 0), "type": "Point"},
            {"bbox": (-1, -1, -1, -1), "coordinates": (-1, -1), "type": "Point"},
            {"coordinates": ((0, 0), (1, 1), (1, 0), (0, 0)), "type": "LinearRing"},
            {
                "bbox": (0, 0, 1, 1),
                "coordinates": ((0, 0), (1, 1)),
                "type": "LineString",
            },
        ),
        "type": "GeometryCollection",
    }

    assert compare_geo_interface(geo_if, geo_if) is True


def test_compare_neq_geo_interface() -> None:
    geo_if1 = {
        "type": "Point",
        "bbox": (0, 1, 0, 1),
        "coordinates": (0.0, 1.0, 2.0),
    }
    geo_if2 = {
        "coordinates": (0.0, 1.0, 3.0),
    }

    assert compare_geo_interface(geo_if1, geo_if2) is False


def test_compare_neq_empty_geo_interface() -> None:
    geo_if = {
        "type": "Point",
        "bbox": (0, 1, 0, 1),
        "coordinates": (0.0, 1.0, 2.0),
    }

    assert compare_geo_interface(geo_if, {}) is False