File: test_geometry.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 (739 lines) | stat: -rw-r--r-- 23,821 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import warnings

import numpy as np
import pytest

import shapely
from shapely import LinearRing, LineString, MultiPolygon, Point, Polygon
from shapely.testing import assert_geometries_equal
from shapely.tests.common import (
    all_types,
    empty as empty_geometry_collection,
    empty_line_string,
    empty_line_string_z,
    empty_point,
    empty_point_z,
    empty_polygon,
    equal_geometries_abnormally_yield_unequal,
    geometry_collection,
    geometry_collection_z,
    ignore_invalid,
    ignore_warnings,
    line_string,
    line_string_nan,
    line_string_z,
    linear_ring,
    multi_line_string,
    multi_line_string_z,
    multi_point,
    multi_point_z,
    multi_polygon,
    multi_polygon_z,
    point,
    point_m,
    point_z,
    point_zm,
    polygon,
    polygon_with_hole,
    polygon_with_hole_z,
    polygon_z,
)


def test_get_num_points():
    actual = shapely.get_num_points(all_types + (None,)).tolist()
    assert actual == [0, 3, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


def test_get_num_interior_rings():
    actual = shapely.get_num_interior_rings(all_types + (None,)).tolist()
    assert actual == [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


def test_get_num_geometries():
    actual = shapely.get_num_geometries(all_types + (None,)).tolist()
    assert actual == [1, 1, 1, 1, 1, 2, 1, 2, 2, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]


@pytest.mark.parametrize(
    "geom",
    [
        point,
        polygon,
        multi_point,
        multi_line_string,
        multi_polygon,
        geometry_collection,
    ],
)
def test_get_point_non_linestring(geom):
    actual = shapely.get_point(geom, [0, 2, -1])
    assert shapely.is_missing(actual).all()


@pytest.mark.parametrize("geom", [line_string, linear_ring])
def test_get_point(geom):
    n = shapely.get_num_points(geom)
    actual = shapely.get_point(geom, [0, -n, n, -(n + 1)])
    assert_geometries_equal(actual[0], actual[1])
    assert shapely.is_missing(actual[2:4]).all()


@pytest.mark.parametrize(
    "geom",
    [
        point,
        line_string,
        linear_ring,
        multi_point,
        multi_line_string,
        multi_polygon,
        geometry_collection,
    ],
)
def test_get_exterior_ring_non_polygon(geom):
    actual = shapely.get_exterior_ring(geom)
    assert shapely.is_missing(actual).all()


def test_get_exterior_ring():
    actual = shapely.get_exterior_ring([polygon, polygon_with_hole])
    assert (shapely.get_type_id(actual) == shapely.GeometryType.LINEARRING).all()


@pytest.mark.parametrize(
    "geom",
    [
        point,
        line_string,
        linear_ring,
        multi_point,
        multi_line_string,
        multi_polygon,
        geometry_collection,
    ],
)
def test_get_interior_ring_non_polygon(geom):
    actual = shapely.get_interior_ring(geom, [0, 2, -1])
    assert shapely.is_missing(actual).all()


def test_get_interior_ring():
    actual = shapely.get_interior_ring(polygon_with_hole, [0, -1, 1, -2])
    assert_geometries_equal(actual[0], actual[1])
    assert shapely.is_missing(actual[2:4]).all()


@pytest.mark.parametrize("geom", [point, line_string, linear_ring, polygon])
def test_get_geometry_simple(geom):
    actual = shapely.get_geometry(geom, [0, -1, 1, -2])
    assert_geometries_equal(actual[0], actual[1])
    assert shapely.is_missing(actual[2:4]).all()


@pytest.mark.parametrize(
    "geom", [multi_point, multi_line_string, multi_polygon, geometry_collection]
)
def test_get_geometry_collection(geom):
    n = shapely.get_num_geometries(geom)
    actual = shapely.get_geometry(geom, [0, -n, n, -(n + 1)])
    assert_geometries_equal(actual[0], actual[1])
    assert shapely.is_missing(actual[2:4]).all()


def test_get_type_id():
    actual = shapely.get_type_id(all_types + (None,)).tolist()
    assert actual == [0, 1, 2, 3, 3, 4, 5, 6, 7, 7, 0, 1, 3, 4, 5, 6, 4, 5, 6, 7, -1]


def test_get_dimensions():
    actual = shapely.get_dimensions(all_types + (None,)).tolist()
    assert actual == [0, 1, 1, 2, 2, 0, 1, 2, 1, -1, 0, 1, 2, 0, 1, 2, 0, 1, 2, 1, -1]


def test_get_coordinate_dimension():
    actual = shapely.get_coordinate_dimension([point, point_z, None]).tolist()
    assert actual == [2, 3, -1]


def test_get_num_coordinates():
    actual = shapely.get_num_coordinates(all_types + (None,)).tolist()
    assert actual == [1, 3, 5, 5, 10, 2, 2, 10, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


def test_get_srid():
    """All geometry types have no SRID by default; None returns -1"""
    actual = shapely.get_srid(all_types + (None,)).tolist()
    assert actual == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1]


def test_get_set_srid():
    actual = shapely.set_srid(point, 4326)
    assert shapely.get_srid(point) == 0
    assert shapely.get_srid(actual) == 4326


@pytest.mark.parametrize(
    "func",
    [
        shapely.get_x,
        shapely.get_y,
        shapely.get_z,
        pytest.param(
            shapely.get_m,
            marks=pytest.mark.skipif(
                shapely.geos_version < (3, 12, 0), reason="GEOS < 3.12"
            ),
        ),
    ],
)
@pytest.mark.parametrize(
    "geom",
    np.array(all_types)[shapely.get_type_id(all_types) != shapely.GeometryType.POINT],
)
def test_get_xyz_no_point(func, geom):
    assert np.isnan(func(geom))


def test_get_x():
    assert shapely.get_x([point, point_z]).tolist() == [2.0, 2.0]


def test_get_y():
    assert shapely.get_y([point, point_z]).tolist() == [3.0, 3.0]


def test_get_z():
    assert shapely.get_z([point_z]).tolist() == [4.0]


def test_get_z_2d():
    assert np.isnan(shapely.get_z(point))


@pytest.mark.skipif(
    shapely.geos_version < (3, 12, 0),
    reason="M coordinates not supported with GEOS < 3.12",
)
def test_get_m():
    assert shapely.get_m([point_m, point_zm]).tolist() == [5.0, 5.0]
    assert np.isnan(shapely.get_m(point))
    assert np.isnan(shapely.get_m(point_z))


@pytest.mark.parametrize("geom", all_types)
def test_new_from_wkt(geom):
    actual = shapely.from_wkt(str(geom))
    if equal_geometries_abnormally_yield_unequal(geom):
        # abnormal test
        with pytest.raises(AssertionError):
            assert_geometries_equal(actual, geom)
    else:
        # normal test
        assert_geometries_equal(actual, geom)


def test_adapt_ptr_raises():
    point = Point(2, 2)
    with pytest.raises(AttributeError):
        point._geom += 1


@pytest.mark.parametrize("geom", all_types)
def test_set_unique(geom):
    a = {geom, shapely.transform(geom, lambda x: x)}
    assert len(a) == 1


def test_set_nan():
    # Although NaN != NaN, you cannot have multiple "NaN" points in a set
    # This is because "NaN" coordinates in a geometry are considered as equal.
    with ignore_invalid():
        a = set(shapely.linestrings([[[np.nan, np.nan], [np.nan, np.nan]]] * 10))
    assert len(a) == 1  # same objects: NaN == NaN (as geometry coordinates)


def test_set_nan_same_objects():
    # You can't put identical objects in a set.
    # x = float("nan"); set([x, x]) also returns a set with 1 element
    a = set([line_string_nan] * 10)
    assert len(a) == 1


@pytest.mark.parametrize(
    "geom",
    [
        point,
        multi_point,
        line_string,
        multi_line_string,
        polygon,
        multi_polygon,
        geometry_collection,
        empty_point,
        empty_line_string,
        empty_polygon,
        empty_geometry_collection,
        np.array([None]),
        np.empty_like(np.array([None])),
    ],
)
def test_get_parts(geom):
    expected_num_parts = shapely.get_num_geometries(geom)
    if expected_num_parts == 0:
        expected_parts = []
    else:
        expected_parts = shapely.get_geometry(geom, range(expected_num_parts))

    parts = shapely.get_parts(geom)
    assert len(parts) == expected_num_parts
    assert_geometries_equal(parts, expected_parts)


def test_get_parts_array():
    # note: this also verifies that None is handled correctly
    # in the mix; internally it returns -1 for count of geometries
    geom = np.array([None, empty_line_string, multi_point, point, multi_polygon])
    expected_parts = []
    for g in geom:
        for i in range(shapely.get_num_geometries(g)):
            expected_parts.append(shapely.get_geometry(g, i))

    parts = shapely.get_parts(geom)
    assert len(parts) == len(expected_parts)
    assert_geometries_equal(parts, expected_parts)


def test_get_parts_geometry_collection_multi():
    """On the first pass, the individual Multi* geometry objects are returned
    from the collection.  On the second pass, the individual singular geometry
    objects within those are returned.
    """
    geom = shapely.geometrycollections([multi_point, multi_line_string, multi_polygon])
    expected_num_parts = shapely.get_num_geometries(geom)
    expected_parts = shapely.get_geometry(geom, range(expected_num_parts))

    parts = shapely.get_parts(geom)
    assert len(parts) == expected_num_parts
    assert_geometries_equal(parts, expected_parts)

    expected_subparts = []
    for g in np.asarray(expected_parts):
        for i in range(shapely.get_num_geometries(g)):
            expected_subparts.append(shapely.get_geometry(g, i))

    subparts = shapely.get_parts(parts)
    assert len(subparts) == len(expected_subparts)
    assert_geometries_equal(subparts, expected_subparts)


def test_get_parts_return_index():
    geom = np.array([multi_point, point, multi_polygon])
    expected_parts = []
    expected_index = []
    for i, g in enumerate(geom):
        for j in range(shapely.get_num_geometries(g)):
            expected_parts.append(shapely.get_geometry(g, j))
            expected_index.append(i)

    parts, index = shapely.get_parts(geom, return_index=True)
    assert len(parts) == len(expected_parts)
    assert_geometries_equal(parts, expected_parts)
    assert np.array_equal(index, expected_index)


@pytest.mark.parametrize(
    "geom",
    ([[None]], [[empty_point]], [[multi_point]], [[multi_point, multi_line_string]]),
)
def test_get_parts_invalid_dimensions(geom):
    """Only 1D inputs are supported"""
    with pytest.raises(ValueError, match="Array should be one dimensional"):
        shapely.get_parts(geom)


@pytest.mark.parametrize("geom", [point, line_string, polygon])
def test_get_parts_non_multi(geom):
    """Non-multipart geometries should be returned identical to inputs"""
    assert_geometries_equal(geom, shapely.get_parts(geom))


@pytest.mark.parametrize("geom", [None, [None], []])
def test_get_parts_None(geom):
    assert len(shapely.get_parts(geom)) == 0


@pytest.mark.parametrize("geom", ["foo", ["foo"], 42])
def test_get_parts_invalid_geometry(geom):
    with pytest.raises(TypeError, match="One of the arguments is of incorrect type."):
        shapely.get_parts(geom)


def test_get_parts_deprecate_positional():
    with pytest.deprecated_call(
        match="positional argument `return_index` for `get_parts` is deprecated"
    ):
        shapely.get_parts(multi_point, False)


@pytest.mark.parametrize(
    "geom",
    [
        point,
        multi_point,
        line_string,
        multi_line_string,
        polygon,
        multi_polygon,
        geometry_collection,
        empty_point,
        empty_line_string,
        empty_polygon,
        empty_geometry_collection,
        None,
    ],
)
def test_get_rings(geom):
    if (shapely.get_type_id(geom) != shapely.GeometryType.POLYGON) or shapely.is_empty(
        geom
    ):
        rings = shapely.get_rings(geom)
        assert len(rings) == 0
    else:
        rings = shapely.get_rings(geom)
        assert len(rings) == 1
        assert rings[0] == shapely.get_exterior_ring(geom)


def test_get_rings_holes():
    rings = shapely.get_rings(polygon_with_hole)
    assert len(rings) == 2
    assert rings[0] == shapely.get_exterior_ring(polygon_with_hole)
    assert rings[1] == shapely.get_interior_ring(polygon_with_hole, 0)


def test_get_rings_return_index():
    geom = np.array([polygon, None, empty_polygon, polygon_with_hole])
    expected_parts = []
    expected_index = []
    for i, g in enumerate(geom):
        if g is None or shapely.is_empty(g):
            continue
        expected_parts.append(shapely.get_exterior_ring(g))
        expected_index.append(i)
        for j in range(shapely.get_num_interior_rings(g)):
            expected_parts.append(shapely.get_interior_ring(g, j))
            expected_index.append(i)

    parts, index = shapely.get_rings(geom, return_index=True)
    assert len(parts) == len(expected_parts)
    assert_geometries_equal(parts, expected_parts)
    assert np.array_equal(index, expected_index)


@pytest.mark.parametrize("geom", [[[None]], [[polygon]]])
def test_get_rings_invalid_dimensions(geom):
    """Only 1D inputs are supported"""
    with pytest.raises(ValueError, match="Array should be one dimensional"):
        shapely.get_parts(geom)


def test_get_rings_deprecate_positional():
    with pytest.deprecated_call(
        match="positional argument `return_index` for `get_rings` is deprecated"
    ):
        shapely.get_rings(polygon, False)


def test_get_precision():
    geometries = all_types + (point_z, empty_point, empty_line_string, empty_polygon)
    # default is 0
    actual = shapely.get_precision(geometries).tolist()
    assert actual == [0] * len(geometries)

    geometry = shapely.set_precision(geometries, 1)
    actual = shapely.get_precision(geometry).tolist()
    assert actual == [1] * len(geometries)


def test_get_precision_none():
    assert np.all(np.isnan(shapely.get_precision([None])))


@pytest.mark.parametrize("mode", ("valid_output", "pointwise", "keep_collapsed"))
def test_set_precision(mode):
    initial_geometry = Point(0.9, 0.9)
    assert shapely.get_precision(initial_geometry) == 0

    with ignore_warnings((3, 10, 0), UserWarning):
        # GEOS < 3.10 emits warning for 'pointwise'
        geometry = shapely.set_precision(initial_geometry, 0, mode=mode)
    assert shapely.get_precision(geometry) == 0
    assert_geometries_equal(geometry, initial_geometry)

    with ignore_warnings((3, 10, 0), UserWarning):
        geometry = shapely.set_precision(initial_geometry, 1, mode=mode)
    assert shapely.get_precision(geometry) == 1
    assert_geometries_equal(geometry, Point(1, 1))
    # original should remain unchanged
    assert_geometries_equal(initial_geometry, Point(0.9, 0.9))


def test_set_precision_drop_coords():
    # setting precision of 0 will not drop duplicated points in original
    geometry = shapely.set_precision(LineString([(0, 0), (0, 0), (0, 1), (1, 1)]), 0)
    assert_geometries_equal(geometry, LineString([(0, 0), (0, 0), (0, 1), (1, 1)]))

    # setting precision will remove duplicated points
    geometry = shapely.set_precision(geometry, 1)
    assert_geometries_equal(geometry, LineString([(0, 0), (0, 1), (1, 1)]))


@pytest.mark.parametrize("mode", ("valid_output", "pointwise", "keep_collapsed"))
def test_set_precision_z(mode):
    with ignore_warnings((3, 10, 0), UserWarning):
        # GEOS < 3.10 emits warning for 'pointwise'
        geometry = shapely.set_precision(Point(0.9, 0.9, 0.9), 1, mode=mode)
    assert shapely.get_precision(geometry) == 1
    assert_geometries_equal(geometry, Point(1, 1, 0.9))


@pytest.mark.parametrize("mode", ("valid_output", "pointwise", "keep_collapsed"))
def test_set_precision_nan(mode):
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")  # GEOS emits warnings
        actual = shapely.set_precision(line_string_nan, 1, mode=mode)
        assert_geometries_equal(actual, line_string_nan)


def test_set_precision_none():
    assert shapely.set_precision(None, 0) is None


def test_set_precision_grid_size_nan():
    assert shapely.set_precision(Point(0.9, 0.9), np.nan) is None


@pytest.mark.parametrize(
    "geometry,mode,expected",
    [
        (
            Polygon([(2, 2), (4, 2), (3.2, 3), (4, 4), (2, 4), (2.8, 3), (2, 2)]),
            "valid_output",
            MultiPolygon(
                [
                    Polygon([(4, 2), (2, 2), (3, 3), (4, 2)]),
                    Polygon([(2, 4), (4, 4), (3, 3), (2, 4)]),
                ]
            ),
        ),
        pytest.param(
            Polygon([(2, 2), (4, 2), (3.2, 3), (4, 4), (2, 4), (2.8, 3), (2, 2)]),
            "pointwise",
            Polygon([(2, 2), (4, 2), (3, 3), (4, 4), (2, 4), (3, 3), (2, 2)]),
            marks=pytest.mark.skipif(
                shapely.geos_version < (3, 10, 0),
                reason="pointwise does not work pre-GEOS 3.10",
            ),
        ),
        (
            Polygon([(2, 2), (4, 2), (3.2, 3), (4, 4), (2, 4), (2.8, 3), (2, 2)]),
            "keep_collapsed",
            MultiPolygon(
                [
                    Polygon([(4, 2), (2, 2), (3, 3), (4, 2)]),
                    Polygon([(2, 4), (4, 4), (3, 3), (2, 4)]),
                ]
            ),
        ),
        (LineString([(0, 0), (0.1, 0.1)]), "valid_output", LineString()),
        pytest.param(
            LineString([(0, 0), (0.1, 0.1)]),
            "pointwise",
            LineString([(0, 0), (0, 0)]),
            marks=pytest.mark.skipif(
                shapely.geos_version < (3, 10, 0),
                reason="pointwise does not work pre-GEOS 3.10",
            ),
        ),
        (
            LineString([(0, 0), (0.1, 0.1)]),
            "keep_collapsed",
            LineString([(0, 0), (0, 0)]),
        ),
        pytest.param(
            LinearRing([(0, 0), (0.1, 0), (0.1, 0.1), (0, 0.1), (0, 0)]),
            "valid_output",
            LinearRing(),
            marks=pytest.mark.skipif(
                shapely.geos_version == (3, 10, 0), reason="Segfaults on GEOS 3.10.0"
            ),
        ),
        pytest.param(
            LinearRing([(0, 0), (0.1, 0), (0.1, 0.1), (0, 0.1), (0, 0)]),
            "pointwise",
            LinearRing([(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]),
            marks=pytest.mark.skipif(
                shapely.geos_version < (3, 10, 0),
                reason="pointwise does not work pre-GEOS 3.10",
            ),
        ),
        pytest.param(
            LinearRing([(0, 0), (0.1, 0), (0.1, 0.1), (0, 0.1), (0, 0)]),
            "keep_collapsed",
            # See https://trac.osgeo.org/geos/ticket/1135#comment:5
            LineString([(0, 0), (0, 0), (0, 0)]),
            marks=pytest.mark.skipif(
                shapely.geos_version < (3, 10, 0),
                reason="this collapsed into an invalid linearring pre-GEOS 3.10",
            ),
        ),
        (
            Polygon([(0, 0), (0.1, 0), (0.1, 0.1), (0, 0.1), (0, 0)]),
            "valid_output",
            Polygon(),
        ),
        pytest.param(
            Polygon([(0, 0), (0.1, 0), (0.1, 0.1), (0, 0.1), (0, 0)]),
            "pointwise",
            Polygon([(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]),
            marks=pytest.mark.skipif(
                shapely.geos_version < (3, 10, 0),
                reason="pointwise does not work pre-GEOS 3.10",
            ),
        ),
        (
            Polygon([(0, 0), (0.1, 0), (0.1, 0.1), (0, 0.1), (0, 0)]),
            "keep_collapsed",
            Polygon(),
        ),
    ],
)
def test_set_precision_collapse(geometry, mode, expected):
    """Lines and polygons collapse to empty geometries if vertices are too close"""
    actual = shapely.set_precision(geometry, 1, mode=mode)
    assert_geometries_equal(
        # force to 2D because of various dimension issues; GEOS GH-1152
        shapely.force_2d(actual),
        expected,
        normalize=shapely.geos_version == (3, 9, 0),
    )


def test_set_precision_intersection():
    """Operations should use the most precise precision grid size of the inputs"""

    box1 = shapely.normalize(shapely.box(0, 0, 0.9, 0.9))
    box2 = shapely.normalize(shapely.box(0.75, 0, 1.75, 0.75))

    assert shapely.get_precision(shapely.intersection(box1, box2)) == 0

    # GEOS will use and keep the most precise precision grid size
    box1 = shapely.set_precision(box1, 0.5)
    box2 = shapely.set_precision(box2, 1)
    out = shapely.intersection(box1, box2)
    assert shapely.get_precision(out) == 0.5
    assert_geometries_equal(out, LineString([(1, 1), (1, 0)]))


@pytest.mark.parametrize("preserve_topology", [False, True])
def set_precision_preserve_topology(preserve_topology):
    # the preserve_topology kwarg is deprecated (ignored)
    with pytest.warns(UserWarning):
        actual = shapely.set_precision(
            LineString([(0, 0), (0.1, 0.1)]),
            1.0,
            preserve_topology=preserve_topology,
        )
    assert_geometries_equal(shapely.force_2d(actual), LineString())


@pytest.mark.skipif(shapely.geos_version >= (3, 10, 0), reason="GEOS >= 3.10")
def set_precision_pointwise_pre_310():
    # using 'pointwise' emits a warning
    with pytest.warns(UserWarning):
        actual = shapely.set_precision(
            LineString([(0, 0), (0.1, 0.1)]),
            1.0,
            mode="pointwise",
        )
    assert_geometries_equal(shapely.force_2d(actual), LineString())


@pytest.mark.parametrize("flags", [np.array([0, 1]), 4, "foo"])
def set_precision_illegal_flags(flags):
    # the preserve_topology kwarg is deprecated (ignored)
    with pytest.raises((ValueError, TypeError)):
        shapely.lib.set_precision(line_string, 1.0, flags)


def test_empty():
    """Compatibility with empty_like, see GH373"""
    g = np.empty_like(np.array([None, None]))
    assert shapely.is_missing(g).all()


# corresponding to geometry_collection_z:
geometry_collection_2 = shapely.geometrycollections([point, line_string])


@pytest.mark.parametrize(
    "geom,expected",
    [
        (point, point),
        (point_z, point),
        (empty_point, empty_point),
        (empty_point_z, empty_point),
        (line_string, line_string),
        (line_string_z, line_string),
        (empty_line_string, empty_line_string),
        (empty_line_string_z, empty_line_string),
        (polygon, polygon),
        (polygon_z, polygon),
        (polygon_with_hole, polygon_with_hole),
        (polygon_with_hole_z, polygon_with_hole),
        (multi_point, multi_point),
        (multi_point_z, multi_point),
        (multi_line_string, multi_line_string),
        (multi_line_string_z, multi_line_string),
        (multi_polygon, multi_polygon),
        (multi_polygon_z, multi_polygon),
        (geometry_collection_2, geometry_collection_2),
        (geometry_collection_z, geometry_collection_2),
    ],
)
def test_force_2d(geom, expected):
    actual = shapely.force_2d(geom)
    assert shapely.get_coordinate_dimension(actual) == 2
    assert_geometries_equal(actual, expected)


@pytest.mark.parametrize(
    "geom,expected",
    [
        (point, point_z),
        (point_z, point_z),
        (empty_point, empty_point_z),
        (empty_point_z, empty_point_z),
        (line_string, line_string_z),
        (line_string_z, line_string_z),
        (empty_line_string, empty_line_string_z),
        (empty_line_string_z, empty_line_string_z),
        (polygon, polygon_z),
        (polygon_z, polygon_z),
        (polygon_with_hole, polygon_with_hole_z),
        (polygon_with_hole_z, polygon_with_hole_z),
        (multi_point, multi_point_z),
        (multi_point_z, multi_point_z),
        (multi_line_string, multi_line_string_z),
        (multi_line_string_z, multi_line_string_z),
        (multi_polygon, multi_polygon_z),
        (multi_polygon_z, multi_polygon_z),
        (geometry_collection_2, geometry_collection_z),
        (geometry_collection_z, geometry_collection_z),
    ],
)
def test_force_3d(geom, expected):
    actual = shapely.force_3d(geom, z=4)
    assert shapely.get_coordinate_dimension(actual) == 3
    assert_geometries_equal(actual, expected)