Debian Codegraph @

tests/geometries/linestring_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 import exceptions
23 from fastkml.enums import Verbosity
24 from fastkml.exceptions import GeometryError
25 from fastkml.exceptions import KMLParseError
26 from fastkml.geometry import Coordinates
27 from fastkml.geometry import LineString
28 from tests.base import Lxml
29 from tests.base import StdLibrary
30
31
32 class TestLineString(StdLibrary):
33 def test_init(self) -> None:
34 """Test the init method."""
35 ls = geo.LineString(((1, 2), (2, 0)))
36
37 line_string = LineString(geometry=ls)
38
39 assert line_string.geometry == ls
40 assert line_string.altitude_mode is None
41 assert line_string.extrude is None
42
43 def test_unequal_coordinates(self) -> None:
44 coords = Coordinates()
45 lines = LineString()
46
47 assert lines != coords
48
49 def test_geometry_error(self) -> None:
50 """Test GeometryError."""
51 p = geo.LineString(((1, 2), (2, 0)))
52 q = Coordinates(ns="ns")
53
54 with pytest.raises(GeometryError):
55 LineString(geometry=p, kml_coordinates=q)
56
57 def test_to_string(self) -> None:
58 """Test the to_string method."""
59 ls = geo.LineString(((1, 2), (2, 0)))
60
61 line_string = LineString(geometry=ls)
62
63 assert "LineString" in line_string.to_string()
64 assert (
65 "coordinates>1.000000,2.000000 2.000000,0.000000</"
66 in line_string.to_string(precision=6)
67 )
68
69 def test_from_string(self) -> None:
70 """Test the from_string method."""
71 linestring = LineString.from_string(
72 '<LineString xmlns="http://www.opengis.net/kml/2.2">'
73 "<extrude>1</extrude>"
74 "<tessellate>1</tessellate>"
75 "<coordinates>"
76 "-122.364383,37.824664,0 -122.364152,37.824322,0"
77 "</coordinates>"
78 "</LineString>",
79 )
80
81 assert linestring.geometry == geo.LineString(
82 ((-122.364383, 37.824664, 0), (-122.364152, 37.824322, 0)),
83 )
84
85 def test_mixed_2d_3d_coordinates_from_string(self) -> None:
86 linestring = LineString.from_string(
87 '<LineString xmlns="http://www.opengis.net/kml/2.2">'
88 "<extrude>1</extrude>"
89 "<tessellate>1</tessellate>"
90 "<coordinates>"
91 "-122.364383,37.824664 -122.364152,37.824322,0"
92 "</coordinates>"
93 "</LineString>",
94 )
95
96 assert not linestring
97
98 def test_mixed_2d_3d_coordinates_from_string_relaxed(self) -> None:
99 line_string = LineString.from_string(
100 '<LineString xmlns="http://www.opengis.net/kml/2.2">'
101 "<extrude>1</extrude>"
102 "<tessellate>1</tessellate>"
103 "<coordinates>"
104 "-122.364383,37.824664 -122.364152,37.824322,0"
105 "</coordinates>"
106 "</LineString>",
107 strict=False,
108 )
109
110 assert not line_string
111
112 def test_empty_from_string(self) -> None:
113 """Test the from_string method with an empty LineString."""
114 linestring = LineString.from_string(
115 '<LineString xmlns="http://www.opengis.net/kml/2.2">'
116 "<extrude>1</extrude>"
117 "<tessellate>1</tessellate>"
118 "<coordinates>"
119 "</coordinates>"
120 "</LineString>",
121 )
122
123 assert not linestring.geometry
124
125 def test_no_coordinates_from_string(self) -> None:
126 """Test the from_string method with no coordinates."""
127 linestring = LineString.from_string(
128 '<LineString xmlns="http://www.opengis.net/kml/2.2">'
129 "<extrude>1</extrude>"
130 "<tessellate>1</tessellate>"
131 "</LineString>",
132 )
133
134 assert not linestring.geometry
135
136 def test_from_string_invalid_coordinates_non_numerical(self) -> None:
137 """Test the from_string method with invalid coordinates."""
138 with pytest.raises(
139 KMLParseError,
140 match=r"^Invalid coordinates in",
141 ):
142 LineString.from_string(
143 '<LineString xmlns="http://www.opengis.net/kml/2.2">'
144 "<extrude>1</extrude>"
145 "<tessellate>1</tessellate>"
146 "<coordinates>"
147 "foo,bar"
148 "</coordinates>"
149 "</LineString>",
150 )
151
152 def test_from_string_invalid_coordinates_nan(self) -> None:
153 line_string = LineString.from_string(
154 '<LineString xmlns="http://www.opengis.net/kml/2.2">'
155 "<extrude>false</extrude>"
156 "<tessellate>true</tessellate>"
157 "<coordinates>"
158 "-70.64950,-35.31494,2178 -70.64898,-35.31520,2121 "
159 "-70.65240,-35.32666,1930 -70.65347,-35.32906,NaN "
160 "-70.65340,-35.33055,1640 -70.64347,-35.34734,1468"
161 "</coordinates>"
162 "</LineString>",
163 )
164
165 assert line_string.geometry
166 assert len(line_string.geometry.coords) == 5
167 assert line_string.to_string()
168
169 def test_from_string_invalid_extrude(self) -> None:
170 """Test the from_string method."""
171 with pytest.raises(
172 exceptions.KMLParseError,
173 ):
174 LineString.from_string(
175 '<LineString id="my-id" targetId="target_id" '
176 'xmlns="http://www.opengis.net/kml/2.2">'
177 "<extrude>invalid</extrude>"
178 "</LineString>",
179 )
180
181 def test_from_string_invalid_tessellate(self) -> None:
182 """Test the from_string method."""
183 with pytest.raises(
184 exceptions.KMLParseError,
185 ):
186 LineString.from_string(
187 '<LineString id="my-id" targetId="target_id" '
188 'xmlns="http://www.opengis.net/kml/2.2">'
189 "<tessellate>invalid</tessellate>"
190 "</LineString>",
191 )
192
193 def test_to_string_terse_default(self) -> None:
194 ls = geo.LineString(((1, 2), (2, 0)))
195 line_string = LineString(geometry=ls, extrude=False, tessellate=False)
196
197 xml = line_string.to_string(verbosity=Verbosity.terse)
198
199 assert "tessellate" not in xml
200 assert "extrude" not in xml
201
202 def test_to_string_terse(self) -> None:
203 ls = geo.LineString(((1, 2), (2, 0)))
204 line_string = LineString(geometry=ls, extrude=True, tessellate=True)
205
206 xml = line_string.to_string(verbosity=Verbosity.terse)
207
208 assert "tessellate>1</" in xml
209 assert "extrude>1</" in xml
210
211 def test_to_string_verbose_default(self) -> None:
212 ls = geo.LineString(((1, 2), (2, 0)))
213 line_string = LineString(geometry=ls, extrude=False, tessellate=False)
214
215 xml = line_string.to_string(verbosity=Verbosity.verbose)
216
217 assert "tessellate>0</" in xml
218 assert "extrude>0</" in xml
219
220 def test_to_string_verbose(self) -> None:
221 ls = geo.LineString(((1, 2), (2, 0)))
222 line_string = LineString(geometry=ls, extrude=True, tessellate=True)
223
224 xml = line_string.to_string(verbosity=Verbosity.verbose)
225
226 assert "tessellate>1</" in xml
227 assert "extrude>1</" in xml
228
229 def test_to_string_verbose_none(self) -> None:
230 ls = geo.LineString(((1, 2), (2, 0)))
231 line_string = LineString(geometry=ls)
232
233 xml = line_string.to_string(verbosity=Verbosity.verbose)
234
235 assert "tessellate>0</" in xml
236 assert "extrude>0</" in xml
237
238
239 class TestLineStringLxml(Lxml, TestLineString):
240 """Test with lxml."""