Debian Codegraph @

tests/geometries/coordinates_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 coordinates class."""
18
19 from fastkml.geometry import Coordinates
20 from tests.base import Lxml
21 from tests.base import StdLibrary
22
23
24 class TestCoordinates(StdLibrary):
25 def test_coordinates(self) -> None:
26 """Test the init method."""
27 coords = ((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))
28
29 coordinates = Coordinates(coords=coords)
30
31 assert coordinates.to_string(precision=6).strip() == (
32 '<kml:coordinates xmlns:kml="http://www.opengis.net/kml/2.2">'
33 "0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 "
34 "1.000000,0.000000 0.000000,0.000000"
35 "</kml:coordinates>"
36 )
37
38 def test_coordinates_from_string(self) -> None:
39 """Test the from_string method."""
40 coordinates = Coordinates.from_string(
41 '<kml:coordinates xmlns:kml="http://www.opengis.net/kml/2.2">'
42 "0.000000,0.000000 1.000000,0.000000 1.0,1.0 0.000000,0.000000"
43 "</kml:coordinates>",
44 )
45
46 assert coordinates.coords == [(0, 0), (1, 0), (1, 1), (0, 0)]
47
48 def test_coordinates_from_string_with_whitespace(self) -> None:
49 """Test the from_string method with whitespace."""
50 coordinates = Coordinates.from_string(
51 '<kml:coordinates xmlns:kml="http://www.opengis.net/kml/2.2">\n'
52 "-123.9404499372,49.169275246690,17 -123.940493701601,49.1694596207446,17 "
53 "-123.940356261489,49.16947180231761,17 -123.940306243,49.169291706171,17 "
54 "-123.940449937288,49.16927524669021,17 \n"
55 "</kml:coordinates>",
56 )
57
58 assert coordinates.coords == [
59 (-123.9404499372, 49.16927524669, 17.0),
60 (-123.940493701601, 49.1694596207446, 17.0),
61 (-123.940356261489, 49.16947180231761, 17.0),
62 (-123.940306243, 49.169291706171, 17.0),
63 (-123.940449937288, 49.16927524669021, 17.0),
64 ]
65
66 def test_coordinates_from_string_parse_error_relaxed(self) -> None:
67 """Test the from_string method with a parse error."""
68 coordinates = Coordinates.from_string(
69 '<kml:coordinates xmlns:kml="http://www.opengis.net/kml/2.2">'
70 "0.a,b.000000 1.0,1.0"
71 "</kml:coordinates>",
72 strict=False,
73 )
74
75 assert not coordinates.coords
76
77
78 class TestCoordinatesLxml(Lxml, TestCoordinates):
79 pass