File: test_feature.py

package info (click to toggle)
pygeoif 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 356 kB
  • sloc: python: 4,039; makefile: 4
file content (200 lines) | stat: -rw-r--r-- 7,148 bytes parent folder | download | duplicates (2)
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
"""Test Feature and FeatureCollection."""

import unittest

import pytest

from pygeoif import feature
from pygeoif import geometry


class TestFeature:
    def setup_method(self) -> None:
        self.a = geometry.Polygon(
            ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
        )
        self.b = geometry.Polygon(
            ((0.0, 0.0), (0.0, 2.0), (2.0, 1.0), (2.0, 0.0), (0.0, 0.0)),
        )
        self.f1 = feature.Feature(self.a)
        self.f2 = feature.Feature(self.b)
        self.f3 = feature.Feature(self.a, {}, feature_id="1")
        self.fc = feature.FeatureCollection([self.f1, self.f2])

    def test_feature_eq(self) -> None:
        assert self.f1 == feature.Feature(self.a)
        assert self.f3 == feature.Feature(self.a, {}, feature_id="1")

    def test_feature_neq_no_geo_interface(self) -> None:
        assert self.f1 != object()

    def test_feature_neq_no_geo_interface_geometry(self) -> None:
        assert self.f1 != unittest.mock.Mock(__geo_interface__={})

    def test_feature_neq_no_geo_interface_type(self) -> None:
        assert self.f1 != unittest.mock.Mock(__geo_interface__={"type": "foo"})

    def test_feature_neq_no_geo_interface_features(self) -> None:
        assert self.f1 != unittest.mock.Mock(__geo_interface__={"type": "Feature"})

    def test_feature(self) -> None:
        pytest.raises(TypeError, feature.Feature)
        assert self.f1.__geo_interface__ == {
            "type": "Feature",
            "bbox": (0.0, 0.0, 1.0, 1.0),
            "geometry": {
                "type": "Polygon",
                "bbox": (0.0, 0.0, 1.0, 1.0),
                "coordinates": (
                    ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
                ),
            },
            "properties": {},
        }
        self.f1.properties["coords"] = {"cube": (0, 0, 0)}
        assert self.f1.__geo_interface__ == {
            "type": "Feature",
            "bbox": (0.0, 0.0, 1.0, 1.0),
            "geometry": {
                "type": "Polygon",
                "bbox": (0.0, 0.0, 1.0, 1.0),
                "coordinates": (
                    ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
                ),
            },
            "properties": {"coords": {"cube": (0, 0, 0)}},
        }
        assert self.f1.geometry.bounds == (0.0, 0.0, 1.0, 1.0)
        del self.f1.properties["coords"]

    def test_feature_with_id(self) -> None:
        assert self.f3.id == "1"
        assert self.f3.__geo_interface__ == {
            "type": "Feature",
            "bbox": (0.0, 0.0, 1.0, 1.0),
            "geometry": {
                "type": "Polygon",
                "bbox": (0.0, 0.0, 1.0, 1.0),
                "coordinates": (
                    ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
                ),
            },
            "id": "1",
            "properties": {},
        }

    def test_feature_repr(self) -> None:
        assert (
            repr(self.f3) == "Feature("
            "Polygon(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),),"
            " {}, '1')"
        )

    def test_feature_repr_eval(self) -> None:
        assert (
            eval(
                repr(self.f2),
                {},
                {"Polygon": geometry.Polygon, "Feature": feature.Feature},
            ).__geo_interface__
            == self.f2.__geo_interface__
        )

    def test_featurecollection(self) -> None:
        pytest.raises(TypeError, feature.FeatureCollection)
        pytest.raises(TypeError, feature.FeatureCollection, None)
        assert len(list(self.fc.features)) == 2
        assert len(self.fc) == 2
        assert self.fc.bounds == (0.0, 0.0, 2.0, 2.0)
        assert [self.f1, self.f2] == list(self.fc)
        assert self.fc.__geo_interface__ == {
            "bbox": (0.0, 0.0, 2.0, 2.0),
            "features": (
                {
                    "bbox": (0.0, 0.0, 1.0, 1.0),
                    "geometry": {
                        "bbox": (0.0, 0.0, 1.0, 1.0),
                        "coordinates": (
                            (
                                (0.0, 0.0),
                                (0.0, 1.0),
                                (1.0, 1.0),
                                (1.0, 0.0),
                                (0.0, 0.0),
                            ),
                        ),
                        "type": "Polygon",
                    },
                    "properties": {},
                    "type": "Feature",
                },
                {
                    "bbox": (0.0, 0.0, 2.0, 2.0),
                    "geometry": {
                        "bbox": (0.0, 0.0, 2.0, 2.0),
                        "coordinates": (
                            (
                                (0.0, 0.0),
                                (0.0, 2.0),
                                (2.0, 1.0),
                                (2.0, 0.0),
                                (0.0, 0.0),
                            ),
                        ),
                        "type": "Polygon",
                    },
                    "properties": {},
                    "type": "Feature",
                },
            ),
            "type": "FeatureCollection",
        }

    def test_featurecollection_eq(self) -> None:
        assert self.fc == feature.FeatureCollection([self.f1, self.f2])

    def test_featurecollection_neq_no_geo_interface(self) -> None:
        assert self.fc != object()

    def test_featurecollection_neq_no_geo_interface_geometry(self) -> None:
        assert self.fc != unittest.mock.Mock(__geo_interface__={})

    def test_featurecollection_neq_no_geo_interface_features(self) -> None:
        assert self.fc != unittest.mock.Mock(
            __geo_interface__={"type": "FeatureCollection"},
        )

    def test_featurecollection_neq_no_geo_interface_len_features(self) -> None:
        assert self.fc != feature.FeatureCollection([self.f1])

    def test_featurecollection_repr(self) -> None:
        assert (
            repr(self.fc) == "FeatureCollection("
            "(Feature("
            "Polygon(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),),"
            " {}, None), "
            "Feature("
            "Polygon(((0.0, 0.0), (0.0, 2.0), (2.0, 1.0), (2.0, 0.0), (0.0, 0.0)),), "
            "{}, None)))"
        )

    def test_featurecollection_repr_eval(self) -> None:
        assert (
            eval(
                repr(self.fc),
                {},
                {
                    "Polygon": geometry.Polygon,
                    "Feature": feature.Feature,
                    "FeatureCollection": feature.FeatureCollection,
                },
            ).__geo_interface__
            == self.fc.__geo_interface__
        )

    def test_featurecollection_bounds(self) -> None:
        ls1 = geometry.LineString(((0, 1), (1, 1)))
        ls2 = geometry.LineString(((2, 3), (3, 4)))
        fc = feature.FeatureCollection([feature.Feature(ls1), feature.Feature(ls2)])

        assert fc.bounds == (0, 1, 3, 4)