Debian Codegraph @

tests/geometries/polygon_test.py

raw symbols SCIP
1 # Copyright (C) 2023 Christian Ledermann
2 #
3 # This library is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 2.1 of the License, or (at your option)
6 # any later version.
7 #
8 # This library is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this library; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 """Test the geometry classes."""
18
19 import pygeoif.geometry as geo
20 import pytest
21
22 from fastkml.enums import AltitudeMode
23 from fastkml.enums import Verbosity
24 from fastkml.exceptions import GeometryError
25 from fastkml.geometry import OuterBoundaryIs
26 from fastkml.geometry import Polygon
27 from tests.base import Lxml
28 from tests.base import StdLibrary
29
30
31 class TestStdLibrary(StdLibrary):
32 """Test with the standard library."""
33
34 def test_equal(self) -> None:
35 """Test equality."""
36 poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
37
38 polygon1 = Polygon(ns="", geometry=poly)
39 polygon2 = Polygon(ns="", geometry=poly)
40
41 assert polygon1 == polygon2
42
43 def test_not_equal(self) -> None:
44 """Test inequality."""
45 polygon = Polygon(ns="")
46 boundary = OuterBoundaryIs(ns="")
47
48 assert polygon != boundary
49
50 def test_exterior_only(self) -> None:
51 """Test exterior only."""
52 poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
53
54 polygon = Polygon(ns="", geometry=poly)
55
56 assert "outerBoundaryIs>" in polygon.to_string()
57 assert "innerBoundaryIs>" not in polygon.to_string()
58 assert "LinearRing>" in polygon.to_string()
59 assert (
60 "0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 "
61 "1.000000,0.000000 0.000000,0.000000"
62 ) in polygon.to_string(precision=6)
63
64 def test_exterior_interior(self) -> None:
65 """Test exterior and interior."""
66 poly = geo.Polygon(
67 [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)],
68 [[(0.1, 0.1), (0.1, 0.9), (0.9, 0.9), (0.9, 0.1), (0.1, 0.1)]],
69 )
70
71 polygon = Polygon(ns="", geometry=poly)
72
73 assert "outerBoundaryIs>" in polygon.to_string()
74 assert "innerBoundaryIs>" in polygon.to_string()
75 assert "LinearRing>" in polygon.to_string()
76 assert (
77 "0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 "
78 "1.000000,0.000000 0.000000,0.000000"
79 ) in polygon.to_string(precision=6)
80 assert (
81 "0.100000,0.100000 0.100000,0.900000 0.900000,0.900000 "
82 "0.900000,0.100000 0.100000,0.100000"
83 ) in polygon.to_string(precision=6)
84
85 def test_exterior_interior_tessellate_extrude_altitude_mode(self) -> None:
86 """
87 Test exterior and interior with tessellate, extrude and altitude mode.
88
89 This should be set on the Polygon level, not on the LinearRing level.
90 """
91 poly = geo.Polygon(
92 [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)],
93 [[(0.1, 0.1), (0.1, 0.9), (0.9, 0.9), (0.9, 0.1), (0.1, 0.1)]],
94 )
95 polygon = Polygon(
96 ns="",
97 geometry=poly,
98 extrude=True,
99 tessellate=True,
100 altitude_mode=AltitudeMode.relative_to_ground,
101 )
102
103 xml = polygon.to_string()
104 assert xml.count("extrude>1</") == 1
105 assert xml.count("tessellate>1</") == 1
106 assert xml.count("altitudeMode") == 2
107 assert xml.count(">relativeToGround</") == 1
108
109 def test_to_string_terse_default(self) -> None:
110 """Test the to_string method, exclude default for extrude in terse mode."""
111 poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
112
113 polygon = Polygon(ns="", geometry=poly, extrude=False)
114
115 assert "extrude>0</" not in polygon.to_string(verbosity=Verbosity.terse)
116
117 def test_to_string_terse_non_default(self) -> None:
118 """Test the to_string method, include extrude when true in terse mode."""
119 poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
120
121 polygon = Polygon(ns="", geometry=poly, extrude=True)
122
123 assert "extrude>1</" in polygon.to_string(verbosity=Verbosity.terse)
124
125 def test_to_string_verbose_default(self) -> None:
126 """Test the to_string method, include default for extrude in verbose mode."""
127 poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
128
129 polygon = Polygon(ns="", geometry=poly, extrude=False)
130
131 assert "extrude>0</" in polygon.to_string(verbosity=Verbosity.verbose)
132
133 def test_to_string_verbose_non_default(self) -> None:
134 """Test the to_string method, include extrude when true in verbose mode."""
135 poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
136
137 polygon = Polygon(ns="", geometry=poly, extrude=True)
138
139 assert "extrude>1</" in polygon.to_string(verbosity=Verbosity.verbose)
140
141 def test_to_string_verbose_none(self) -> None:
142 """Test the to_string method, include extrude when true in verbose mode."""
143 poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
144
145 polygon = Polygon(ns="", geometry=poly)
146
147 assert "extrude>0</" in polygon.to_string(verbosity=Verbosity.verbose)
148
149 def test_geometry_error(self) -> None:
150 """Test GeometryError."""
151 poly = geo.Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
152 ob = OuterBoundaryIs(ns="")
153
154 with pytest.raises(GeometryError):
155 Polygon(geometry=poly, outer_boundary=ob)
156
157 def test_from_string_exterior_only(self) -> None:
158 """Test exterior only."""
159 doc = """<kml:Polygon xmlns:kml="http://www.opengis.net/kml/2.2">
160 <kml:outerBoundaryIs>
161 <kml:LinearRing>
162 <kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000
163 0.000000,0.000000</kml:coordinates>
164 </kml:LinearRing>
165 </kml:outerBoundaryIs>
166 </kml:Polygon>"""
167
168 polygon2 = Polygon.from_string(doc)
169
170 assert polygon2.geometry == geo.Polygon([(0, 0), (1, 0), (1, 1), (0, 0)])
171
172 def test_from_string_interiors_only(self) -> None:
173 """Test polygon without exterior."""
174 doc = """<kml:Polygon xmlns:kml="http://www.opengis.net/kml/2.2">
175 <kml:innerBoundaryIs>
176 <kml:LinearRing>
177 <kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000
178 0.000000,0.000000</kml:coordinates>
179 </kml:LinearRing>
180 </kml:innerBoundaryIs>
181 </kml:Polygon>"""
182
183 assert not Polygon.from_string(doc)
184
185 def test_from_string_exterior_wo_linearring(self) -> None:
186 """Test exterior when no LinearRing in outer boundary."""
187 doc = """<kml:Polygon xmlns:kml="http://www.opengis.net/kml/2.2">
188 <kml:outerBoundaryIs>
189 <kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000
190 0.000000,0.000000</kml:coordinates>
191 </kml:outerBoundaryIs>
192 </kml:Polygon>"""
193
194 assert not Polygon.from_string(doc)
195
196 def test_from_string_interior_wo_linearring(self) -> None:
197 """Test interior when no LinearRing in inner boundary."""
198 doc = """<kml:Polygon xmlns:kml="http://www.opengis.net/kml/2.2">
199 <kml:outerBoundaryIs>
200 <kml:LinearRing>
201 <kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000
202 0.000000,0.000000</kml:coordinates>
203 </kml:LinearRing>
204 </kml:outerBoundaryIs>
205 <kml:innerBoundaryIs>
206 <kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000
207 0.000000,0.000000</kml:coordinates>
208 </kml:innerBoundaryIs>
209 </kml:Polygon>"""
210
211 poly = Polygon.from_string(doc)
212
213 assert poly.geometry == geo.Polygon(
214 ((0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)),
215 )
216
217 def test_from_string_exterior_interior(self) -> None:
218 doc = """<kml:Polygon xmlns:kml="http://www.opengis.net/kml/2.2">
219 <kml:outerBoundaryIs>
220 <kml:LinearRing>
221 <kml:coordinates>-1.000000,-1.000000 2.000000,-1.000000 2.000000,2.000000
222 -1.000000,-1.000000</kml:coordinates>
223 </kml:LinearRing>
224 </kml:outerBoundaryIs>
225 <kml:innerBoundaryIs>
226 <kml:LinearRing>
227 <kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000
228 0.000000,0.000000</kml:coordinates>
229 </kml:LinearRing>
230 </kml:innerBoundaryIs>
231 </kml:Polygon>
232 """
233
234 polygon = Polygon.from_string(doc)
235
236 assert polygon.geometry == geo.Polygon(
237 [(-1, -1), (2, -1), (2, 2), (-1, -1)],
238 [[(0, 0), (1, 0), (1, 1), (0, 0)]],
239 )
240
241 def test_from_string_exterior_mixed_interior_relaxed(self) -> None:
242 doc = """<kml:Polygon xmlns:kml="http://www.opengis.net/kml/2.2">
243 <kml:outerBoundaryIs>
244 <kml:LinearRing>
245 <kml:coordinates>-1.000000,-1.000000 2.000000,-1.000000 2.000000,2.000000
246 -1.000000,-1.000000</kml:coordinates>
247 </kml:LinearRing>
248 </kml:outerBoundaryIs>
249 <kml:innerBoundaryIs>
250 <kml:LinearRing>
251 <kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000
252 0.000000,0.000000,1.00000</kml:coordinates>
253 </kml:LinearRing>
254 </kml:innerBoundaryIs>
255 </kml:Polygon>
256 """
257
258 polygon = Polygon.from_string(doc, strict=False)
259
260 assert polygon.geometry == geo.Polygon(
261 ((-1.0, -1.0), (2.0, -1.0), (2.0, 2.0), (-1.0, -1.0)),
262 )
263
264 def test_empty_polygon(self) -> None:
265 """Test empty polygon."""
266 doc = (
267 '<Polygon xmlns="http://www.opengis.net/kml/2.2"><tessellate>1</tessellate>'
268 "<outerBoundaryIs><LinearRing><coordinates/></LinearRing></outerBoundaryIs>"
269 "</Polygon>"
270 )
271
272 polygon = Polygon.from_string(doc)
273
274 assert not polygon.geometry
275 assert polygon.outer_boundary is not None
276 assert isinstance(polygon.outer_boundary, OuterBoundaryIs)
277 assert len(polygon.inner_boundaries) == 0
278 assert "tessellate>1</" in polygon.to_string()
279
280
281 class TestLxml(Lxml, TestStdLibrary):
282 """Test with lxml."""