Debian Codegraph @

tests/geometries/multigeometry_test.py

raw symbols SCIP
1 # Copyright (C) 2021 - 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 Verbosity
23 from fastkml.exceptions import GeometryError
24 from fastkml.geometry import MultiGeometry
25 from tests.base import Lxml
26 from tests.base import StdLibrary
27
28
29 class TestMultiPointStdLibrary(StdLibrary):
30 """Test with the standard library."""
31
32 def test_1_point(self) -> None:
33 """Test with one point."""
34 p = geo.MultiPoint([(1, 2)])
35
36 mg = MultiGeometry(geometry=p)
37
38 assert "coordinates>1.000000,2.000000</" in mg.to_string(precision=6)
39 assert "MultiGeometry>" in mg.to_string()
40 assert "Point>" in mg.to_string()
41
42 def test_2_points(self) -> None:
43 """Test with two points."""
44 p = geo.MultiPoint([(1, 2), (3, 4)])
45
46 mg = MultiGeometry(geometry=p)
47
48 assert "coordinates>1.000000,2.000000</" in mg.to_string(precision=6)
49 assert "coordinates>3.000000,4.000000</" in mg.to_string(precision=6)
50 assert "MultiGeometry>" in mg.to_string()
51 assert "Point>" in mg.to_string()
52
53 def test_2_points_read(self) -> None:
54 xml = (
55 '<kml:MultiGeometry xmlns:kml="http://www.opengis.net/kml/2.2">'
56 "<kml:extrude>0</kml:extrude><kml:tessellate>0</kml:tessellate>"
57 "<kml:Point><kml:coordinates>1.000000,2.000000</kml:coordinates></kml:Point>"
58 "<kml:Point><kml:coordinates>3.000000,4.000000"
59 "</kml:coordinates></kml:Point></kml:MultiGeometry>"
60 )
61
62 mg = MultiGeometry.from_string(xml)
63
64 assert mg.geometry == geo.MultiPoint([(1, 2), (3, 4)])
65
66
67 class TestMultiLineStringStdLibrary(StdLibrary):
68 def test_1_linestring(self) -> None:
69 """Test with one linestring."""
70 p = geo.MultiLineString([[(1, 2), (3, 4)]])
71
72 mg = MultiGeometry(geometry=p)
73
74 assert "coordinates>1.000000,2.000000 3.000000,4.000000</" in mg.to_string(
75 precision=6,
76 )
77 assert "MultiGeometry>" in mg.to_string()
78 assert "LineString>" in mg.to_string()
79
80 def test_2_linestrings(self) -> None:
81 """Test with two linestrings."""
82 p = geo.MultiLineString([[(1, 2), (3, 4)], [(5, 6), (7, 8)]])
83
84 mg = MultiGeometry(geometry=p)
85
86 assert "coordinates>1.000000,2.000000 3.000000,4.000000</" in mg.to_string(
87 precision=6,
88 )
89 assert "coordinates>5.000000,6.000000 7.000000,8.000000</" in mg.to_string(
90 precision=6,
91 )
92 assert "MultiGeometry>" in mg.to_string()
93 assert "LineString>" in mg.to_string()
94
95 def test_2_linestrings_read(self) -> None:
96 xml = (
97 '<kml:MultiGeometry xmlns:kml="http://www.opengis.net/kml/2.2">'
98 "<kml:extrude>0</kml:extrude><kml:tessellate>0</kml:tessellate>"
99 "<kml:LineString><kml:coordinates>1.000000,2.000000 3.000000,4.000000"
100 "</kml:coordinates></kml:LineString><kml:LineString>"
101 "<kml:coordinates>5.000000,6.000000 7.000000,8.000000</kml:coordinates>"
102 "</kml:LineString></kml:MultiGeometry>"
103 )
104
105 mg = MultiGeometry.from_string(xml, ns="")
106
107 assert mg.geometry == geo.MultiLineString([[(1, 2), (3, 4)], [(5, 6), (7, 8)]])
108
109
110 class TestMultiPolygonStdLibrary(StdLibrary):
111 def test_1_polygon(self) -> None:
112 """Test with one polygon."""
113 p = geo.MultiPolygon(
114 [(((1, 2), (3, 4), (5, 6), (1, 2)),)],
115 )
116
117 mg = MultiGeometry(ns="", geometry=p)
118
119 assert (
120 "coordinates>1.000000,2.000000 3.000000,4.000000 5.000000,6.000000 "
121 "1.000000,2.000000</" in mg.to_string(precision=6)
122 )
123 assert "MultiGeometry>" in mg.to_string()
124 assert "Polygon>" in mg.to_string()
125 assert "outerBoundaryIs>" in mg.to_string()
126 assert "innerBoundaryIs>" not in mg.to_string()
127
128 def test_1_polygons_with_holes(self) -> None:
129 """Test with one polygon with holes."""
130 p = geo.MultiPolygon(
131 [
132 (
133 ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
134 [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))],
135 ),
136 ],
137 )
138 mg = MultiGeometry(geometry=p)
139
140 assert (
141 "coordinates>0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 "
142 "1.000000,0.000000 0.000000,0.000000</" in mg.to_string(precision=6)
143 )
144 assert (
145 "coordinates>0.250000,0.250000 0.250000,0.500000 0.500000,0.500000 "
146 "0.500000,0.250000 0.250000,0.250000</" in mg.to_string(precision=6)
147 )
148 assert "MultiGeometry>" in mg.to_string()
149 assert "Polygon>" in mg.to_string()
150 assert "outerBoundaryIs>" in mg.to_string()
151 assert "innerBoundaryIs>" in mg.to_string()
152
153 def test_2_polygons(self) -> None:
154 """Test with two polygons."""
155 p = geo.MultiPolygon(
156 [
157 (
158 ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
159 [((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))],
160 ),
161 (((0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (1.0, 0.0)),),
162 ],
163 )
164
165 mg = MultiGeometry(geometry=p)
166
167 assert (
168 "coordinates>0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 "
169 "1.000000,0.000000 0.000000,0.000000</" in mg.to_string(precision=6)
170 )
171 assert (
172 "coordinates>0.100000,0.100000 0.100000,0.200000 0.200000,0.200000 "
173 "0.200000,0.100000 0.100000,0.100000</" in mg.to_string(precision=6)
174 )
175 assert (
176 "coordinates>0.000000,0.000000 0.000000,2.000000 1.000000,1.000000 "
177 "1.000000,0.000000 0.000000,0.000000</" in mg.to_string(precision=6)
178 )
179 assert "MultiGeometry>" in mg.to_string()
180 assert "Polygon>" in mg.to_string()
181 assert "outerBoundaryIs>" in mg.to_string()
182 assert "innerBoundaryIs>" in mg.to_string()
183
184 def test_2_polygons_read(self) -> None:
185 xml = (
186 '<MultiGeometry xmlns="http://www.opengis.net/kml/2.2">'
187 "<extrude>0</extrude><tessellate>0</tessellate>"
188 "<Polygon><outerBoundaryIs><LinearRing>"
189 "<coordinates>0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 "
190 "1.000000,0.000000 0.000000,0.000000</coordinates>"
191 "</LinearRing></outerBoundaryIs>"
192 "<innerBoundaryIs><LinearRing>"
193 "<coordinates>0.100000,0.100000 0.100000,0.200000 0.200000,0.200000 "
194 "0.200000,0.100000 0.100000,0.100000</coordinates>"
195 "</LinearRing></innerBoundaryIs></Polygon>"
196 "<Polygon><outerBoundaryIs><LinearRing>"
197 "<coordinates>0.000000,0.000000 0.000000,2.000000 1.000000,1.000000 "
198 "1.000000,0.000000 0.000000,0.000000</coordinates>"
199 "</LinearRing></outerBoundaryIs></Polygon></MultiGeometry>"
200 )
201
202 mg = MultiGeometry.from_string(xml)
203
204 assert mg.geometry == geo.MultiPolygon(
205 [
206 (
207 ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
208 [((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))],
209 ),
210 (((0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (1.0, 0.0)),),
211 ],
212 )
213
214
215 class TestGeometryCollectionStdLibrary(StdLibrary):
216 """Test heterogeneous geometry collections."""
217
218 def test_1_point(self) -> None:
219 """Test with one point."""
220 p = geo.GeometryCollection([geo.Point(1, 2)])
221
222 mg = MultiGeometry(geometry=p)
223
224 assert "coordinates>1.000000,2.000000</" in mg.to_string(precision=6)
225 assert "MultiGeometry>" in mg.to_string()
226 assert "Point>" in mg.to_string()
227
228 def test_geometries(self) -> None:
229 p = geo.Point(1, 2)
230 ls = geo.LineString(((1, 2), (2, 0)))
231 lr = geo.LinearRing(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
232 poly = geo.Polygon(
233 [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)],
234 [[(0.1, 0.1), (0.1, 0.9), (0.9, 0.9), (0.9, 0.1), (0.1, 0.1)]],
235 )
236 gc = geo.GeometryCollection([p, ls, lr, poly])
237
238 mg = MultiGeometry(geometry=gc)
239
240 assert "Point>" in mg.to_string()
241 assert "LineString>" in mg.to_string()
242 assert "LinearRing>" in mg.to_string()
243 assert "Polygon>" in mg.to_string()
244 assert "MultiGeometry>" in mg.to_string()
245
246 def test_multi_geometries(self) -> None:
247 p = geo.Point(1, 2)
248 ls = geo.LineString(((1, 2), (2, 0)))
249 lr = geo.LinearRing(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
250 poly = geo.Polygon(
251 [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)],
252 [[(0.1, 0.1), (0.1, 0.9), (0.9, 0.9), (0.9, 0.1), (0.1, 0.1)]],
253 )
254 gc = geo.GeometryCollection([p, ls, lr, poly])
255 mp = geo.MultiPolygon(
256 [
257 (
258 ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
259 [((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))],
260 ),
261 (((0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (1.0, 0.0)),),
262 ],
263 )
264 ml = geo.MultiLineString([[(1, 2), (3, 4)], [(5, 6), (7, 8)]])
265 mgc = geo.GeometryCollection([gc, mp, ml])
266 mg = MultiGeometry(ns="", geometry=mgc)
267
268 assert "Point>" in mg.to_string()
269 assert "LineString>" in mg.to_string()
270 assert "LinearRing>" in mg.to_string()
271 assert "Polygon>" in mg.to_string()
272 assert "MultiGeometry>" in mg.to_string()
273
274 def test_multi_geometries_verbose(self) -> None:
275 p = geo.Point(1, 2)
276 ls = geo.LineString(((1, 2), (2, 0)))
277 lr = geo.LinearRing(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
278 poly = geo.Polygon(
279 [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)],
280 [[(0.1, 0.1), (0.1, 0.9), (0.9, 0.9), (0.9, 0.1), (0.1, 0.1)]],
281 )
282 gc = geo.GeometryCollection([p, ls, lr, poly])
283 mp = geo.MultiPolygon(
284 [
285 (
286 ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
287 [((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))],
288 ),
289 (((0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (1.0, 0.0)),),
290 ],
291 )
292 ml = geo.MultiLineString([[(1, 2), (3, 4)], [(5, 6), (7, 8)]])
293 mgc = geo.GeometryCollection([gc, mp, ml])
294 mg = MultiGeometry(ns="", geometry=mgc)
295
296 xml = mg.to_string(verbosity=Verbosity.verbose)
297 assert xml.count("tessellate>0<") == 12 # points do not have tessellate
298 assert xml.count("extrude>0<") == 13
299 assert xml.count("altitudeMode") == 26
300 assert xml.count(">clampToGround<") == 13
301
302 def test_geometry_error(self) -> None:
303 """Test GeometryError."""
304 p = geo.MultiPoint(((1.0, 2.0),))
305
306 with pytest.raises(GeometryError):
307 MultiGeometry(geometry=p, kml_geometries=(MultiGeometry(geometry=p),))
308
309 def test_multi_geometries_read(self) -> None:
310 xml = (
311 '<MultiGeometry xmlns="http://www.opengis.net/kml/2.2">'
312 "<extrude>0</extrude><tessellate>0</tessellate>"
313 "<MultiGeometry><Point><coordinates>1.000000,2.000000</coordinates></Point>"
314 "<LineString><coordinates>1.000000,2.000000 2.000000,0.000000</coordinates>"
315 "</LineString><LinearRing><coordinates>0.000000,0.000000 0.000000,1.000000 "
316 "1.000000,1.000000 1.000000,0.000000 0.000000,0.000000</coordinates>"
317 "</LinearRing><Polygon><outerBoundaryIs><LinearRing>"
318 "<coordinates>0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 "
319 "1.000000,0.000000 0.000000,0.000000</coordinates></LinearRing>"
320 "</outerBoundaryIs><innerBoundaryIs><LinearRing>"
321 "<coordinates>0.100000,0.100000 0.100000,0.900000 0.900000,0.900000 "
322 "0.900000,0.100000 0.100000,0.100000</coordinates></LinearRing>"
323 "</innerBoundaryIs></Polygon></MultiGeometry>"
324 "<MultiGeometry><Polygon><outerBoundaryIs><LinearRing>"
325 "<coordinates>0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 "
326 "1.000000,0.000000 0.000000,0.000000</coordinates></LinearRing>"
327 "</outerBoundaryIs><innerBoundaryIs><LinearRing>"
328 "<coordinates>0.100000,0.100000 0.100000,0.200000 0.200000,0.200000 "
329 "0.200000,0.100000 0.100000,0.100000</coordinates></LinearRing>"
330 "</innerBoundaryIs></Polygon>"
331 "<Polygon><outerBoundaryIs><LinearRing>"
332 "<coordinates>0.000000,0.000000 0.000000,2.000000 1.000000,1.000000 "
333 "1.000000,0.000000 0.000000,0.000000</coordinates></LinearRing>"
334 "</outerBoundaryIs></Polygon></MultiGeometry><MultiGeometry><LineString>"
335 "<coordinates>1.000000,2.000000 3.000000,4.000000</coordinates>"
336 "</LineString><LineString><coordinates>5.000000,6.000000 7.000000,8.000000"
337 "</coordinates></LineString></MultiGeometry></MultiGeometry>"
338 )
339
340 mg = MultiGeometry.from_string(xml)
341
342 assert mg.geometry == geo.GeometryCollection(
343 (
344 geo.GeometryCollection(
345 (
346 geo.Point(1.0, 2.0),
347 geo.LineString(((1.0, 2.0), (2.0, 0.0))),
348 geo.Polygon(
349 (
350 (0.0, 0.0),
351 (0.0, 1.0),
352 (1.0, 1.0),
353 (1.0, 0.0),
354 (0.0, 0.0),
355 ),
356 (
357 (
358 (0.1, 0.1),
359 (0.1, 0.9),
360 (0.9, 0.9),
361 (0.9, 0.1),
362 (0.1, 0.1),
363 ),
364 ),
365 ),
366 geo.LinearRing(
367 (
368 (0.0, 0.0),
369 (0.0, 1.0),
370 (1.0, 1.0),
371 (1.0, 0.0),
372 (0.0, 0.0),
373 ),
374 ),
375 ),
376 ),
377 geo.MultiPolygon(
378 (
379 (
380 (
381 (0.0, 0.0),
382 (0.0, 1.0),
383 (1.0, 1.0),
384 (1.0, 0.0),
385 (0.0, 0.0),
386 ),
387 (
388 (
389 (0.1, 0.1),
390 (0.1, 0.2),
391 (0.2, 0.2),
392 (0.2, 0.1),
393 (0.1, 0.1),
394 ),
395 ),
396 ),
397 (((0.0, 0.0), (0.0, 2.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),),
398 ),
399 ),
400 geo.MultiLineString(
401 (((1.0, 2.0), (3.0, 4.0)), ((5.0, 6.0), (7.0, 8.0))),
402 ),
403 ),
404 )
405
406 def test_empty_multi_geometries_read(self) -> None:
407 xml = (
408 '<MultiGeometry xmlns="http://www.opengis.net/kml/2.2">'
409 "<extrude>0</extrude><tessellate>0</tessellate>"
410 "<MultiGeometry></MultiGeometry></MultiGeometry>"
411 )
412
413 mg = MultiGeometry.from_string(xml)
414
415 assert mg.geometry is None
416 assert "MultiGeometry" in mg.to_string()
417 assert "coordinates" not in mg.to_string()
418 assert not hasattr(mg, "extrude")
419 assert not hasattr(mg, "tessellate")
420
421
422 class TestMultiPointLxml(Lxml, TestMultiPointStdLibrary):
423 """Test with lxml."""
424
425
426 class TestMultiLineStringLxml(Lxml, TestMultiLineStringStdLibrary):
427 """Test with lxml."""
428
429
430 class TestMultiPolygonLxml(Lxml, TestMultiPolygonStdLibrary):
431 """Test with lxml."""
432
433
434 class TestGeometryCollectionLxml(Lxml, TestGeometryCollectionStdLibrary):
435 """Test with lxml."""