Debian Codegraph @

tests/geometries/linearring_test.py

raw symbols SCIP
1 # Copyright (C) 2023 - 2024 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.exceptions import KMLParseError
23 from fastkml.geometry import LinearRing
24 from tests.base import Lxml
25 from tests.base import StdLibrary
26
27
28 class TestLinearRing(StdLibrary):
29 def test_init(self) -> None:
30 """Test the init method."""
31 lr = geo.LinearRing(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
32
33 linear_ring = LinearRing(geometry=lr)
34
35 assert linear_ring.geometry == lr
36 assert linear_ring.altitude_mode is None
37 assert linear_ring.extrude is None
38
39 def test_to_string(self) -> None:
40 """Test the to_string method."""
41 lr = geo.LinearRing(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
42
43 linear_ring = LinearRing(geometry=lr)
44
45 assert "LinearRing" in linear_ring.to_string()
46 assert (
47 "coordinates>0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 "
48 "1.000000,0.000000 0.000000,0.000000</"
49 in linear_ring.to_string(precision=6)
50 )
51
52 def test_from_string(self) -> None:
53 """Test the from_string method."""
54 linear_ring = LinearRing.from_string(
55 '<kml:LinearRing xmlns:kml="http://www.opengis.net/kml/2.2">'
56 "<kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.0,1.0 "
57 "0.000000,0.000000</kml:coordinates>"
58 "</kml:LinearRing>",
59 )
60
61 assert linear_ring.geometry == geo.LinearRing(((0, 0), (1, 0), (1, 1), (0, 0)))
62
63 def test_empty_from_string(self) -> None:
64 """Test the from_string method with an empty LinearRing."""
65 linear_ring = LinearRing.from_string(
66 '<kml:LinearRing xmlns:kml="http://www.opengis.net/kml/2.2">'
67 "<kml:coordinates></kml:coordinates>"
68 "</kml:LinearRing>",
69 )
70
71 assert not linear_ring.geometry
72
73 def test_no_coordinates_from_string(self) -> None:
74 """Test the from_string method with an empty LinearRing."""
75 linear_ring = LinearRing.from_string(
76 '<kml:LinearRing xmlns:kml="http://www.opengis.net/kml/2.2">'
77 "</kml:LinearRing>",
78 )
79
80 assert not linear_ring.geometry
81
82 def test_from_string_invalid_coordinates_non_numerical(self) -> None:
83 """Test the from_string method with non numerical coordinates."""
84 with pytest.raises(
85 KMLParseError,
86 match=r"^Invalid coordinates in",
87 ):
88 LinearRing.from_string(
89 '<kml:LinearRing xmlns:kml="http://www.opengis.net/kml/2.2">'
90 "<kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.0,1.0 "
91 "0.000000,0.000000 1.000000,a</kml:coordinates>"
92 "</kml:LinearRing>",
93 )
94
95 def test_mixed_2d_3d_coordinates_from_string_relaxed(self) -> None:
96 """Test the from_string method with mixed 2D and 3D coordinates."""
97 linear_ring = LinearRing.from_string(
98 '<kml:LinearRing xmlns:kml="http://www.opengis.net/kml/2.2">'
99 "<kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.0,1.0 "
100 "0.000000,0.000000 1.000000,2.000000,3.000000</kml:coordinates>"
101 "</kml:LinearRing>",
102 strict=False,
103 )
104
105 assert not linear_ring
106
107
108 class TestLinearRingLxml(Lxml, TestLinearRing):
109 """Test with lxml."""