File: test_emptiness.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 (98 lines) | stat: -rw-r--r-- 2,362 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
import math

import numpy as np
import pytest

from shapely import (
    GeometryCollection,
    LinearRing,
    LineString,
    MultiLineString,
    MultiPoint,
    MultiPolygon,
    Point,
    Polygon,
)
from shapely.geometry import mapping, shape
from shapely.geometry.base import BaseGeometry, EmptyGeometry


def empty_generator():
    return iter([])


class TestEmptiness:
    def test_empty_class(self):
        with pytest.warns(FutureWarning):
            g = EmptyGeometry()
        assert g.is_empty

    def test_empty_base(self):
        with pytest.warns(FutureWarning):
            g = BaseGeometry()
        assert g.is_empty

    def test_empty_point(self):
        assert Point().is_empty

    def test_empty_multipoint(self):
        assert MultiPoint().is_empty

    def test_empty_geometry_collection(self):
        assert GeometryCollection().is_empty

    def test_empty_linestring(self):
        assert LineString().is_empty
        assert LineString(None).is_empty
        assert LineString([]).is_empty
        assert LineString(empty_generator()).is_empty

    def test_empty_multilinestring(self):
        assert MultiLineString([]).is_empty

    def test_empty_polygon(self):
        assert Polygon().is_empty
        assert Polygon(None).is_empty
        assert Polygon([]).is_empty
        assert Polygon(empty_generator()).is_empty

    def test_empty_multipolygon(self):
        assert MultiPolygon([]).is_empty

    def test_empty_linear_ring(self):
        assert LinearRing().is_empty
        assert LinearRing(None).is_empty
        assert LinearRing([]).is_empty
        assert LinearRing(empty_generator()).is_empty


def test_numpy_object_array():
    geoms = [Point(), GeometryCollection()]
    arr = np.empty(2, object)
    arr[:] = geoms


def test_shape_empty():
    empty_mp = MultiPolygon()
    empty_json = mapping(empty_mp)
    empty_shape = shape(empty_json)
    assert empty_shape.is_empty


@pytest.mark.parametrize(
    "geom",
    [
        Point(),
        LineString(),
        Polygon(),
        MultiPoint(),
        MultiLineString(),
        MultiPolygon(),
        GeometryCollection(),
        LinearRing(),
    ],
)
def test_empty_geometry_bounds(geom):
    """The bounds of an empty geometry is a tuple of NaNs"""
    assert len(geom.bounds) == 4
    assert all(math.isnan(v) for v in geom.bounds)