| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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.exceptions import KMLParseError |
| 25 |
from fastkml.geometry import Coordinates |
| 26 |
from fastkml.geometry import Point |
| 27 |
from tests.base import Lxml |
| 28 |
from tests.base import StdLibrary |
| 29 |
|
| 30 |
|
| 31 |
class TestPoint(StdLibrary): |
| 32 |
"""Test the Point class.""" |
| 33 |
|
| 34 |
def test_init(self) -> None: |
| 35 |
"""Test the init method.""" |
| 36 |
p = geo.Point(1, 2) |
| 37 |
|
| 38 |
point = Point(geometry=p) |
| 39 |
|
| 40 |
assert point.geometry == p |
| 41 |
assert point.altitude_mode is None |
| 42 |
assert point.extrude is None |
| 43 |
|
| 44 |
def test_geometry_error(self) -> None: |
| 45 |
"""Test GeometryError.""" |
| 46 |
p = geo.Point(1, 2) |
| 47 |
q = Coordinates(ns="ns") |
| 48 |
|
| 49 |
with pytest.raises(GeometryError): |
| 50 |
Point(geometry=p, kml_coordinates=q) |
| 51 |
|
| 52 |
def test_to_string_2d(self) -> None: |
| 53 |
"""Test the to_string method.""" |
| 54 |
p = geo.Point(1, 2) |
| 55 |
|
| 56 |
point = Point(geometry=p) |
| 57 |
|
| 58 |
assert "Point" in point.to_string() |
| 59 |
assert "coordinates>1.000000,2.000000</" in point.to_string(precision=6) |
| 60 |
|
| 61 |
def test_to_string_3d(self) -> None: |
| 62 |
"""Test the to_string method.""" |
| 63 |
p = geo.Point(1, 2, 3) |
| 64 |
|
| 65 |
point = Point(geometry=p) |
| 66 |
|
| 67 |
assert "Point" in point.to_string() |
| 68 |
assert "coordinates>1.000000,2.000000,3.000000</" in point.to_string( |
| 69 |
precision=6, |
| 70 |
) |
| 71 |
|
| 72 |
def test_to_string_terse_default(self) -> None: |
| 73 |
"""Test the to_string method, exclude default for extrude in terse mode.""" |
| 74 |
p = geo.Point(1, 2) |
| 75 |
|
| 76 |
point = Point(geometry=p, extrude=False) |
| 77 |
|
| 78 |
assert "coordinates>" in point.to_string(verbosity=Verbosity.terse) |
| 79 |
assert "extrude" not in point.to_string(verbosity=Verbosity.terse) |
| 80 |
|
| 81 |
def test_to_string_terse_non_default(self) -> None: |
| 82 |
"""Test the to_string method, include extrude when true in terse mode.""" |
| 83 |
p = geo.Point(1, 2) |
| 84 |
|
| 85 |
point = Point(geometry=p, extrude=True) |
| 86 |
|
| 87 |
assert "coordinates>" in point.to_string(verbosity=Verbosity.terse) |
| 88 |
assert "extrude>1</" in point.to_string(verbosity=Verbosity.terse) |
| 89 |
|
| 90 |
def test_to_string_verbose_default(self) -> None: |
| 91 |
"""Test the to_string method, include default for extrude in verbose mode.""" |
| 92 |
p = geo.Point(1, 2) |
| 93 |
|
| 94 |
point = Point(geometry=p, extrude=False) |
| 95 |
|
| 96 |
assert "coordinates>" in point.to_string(verbosity=Verbosity.verbose) |
| 97 |
assert "extrude>0</" in point.to_string(verbosity=Verbosity.verbose) |
| 98 |
|
| 99 |
def test_to_string_verbose_non_default(self) -> None: |
| 100 |
"""Test the to_string method, include extrude when true in verbose mode.""" |
| 101 |
p = geo.Point(1, 2) |
| 102 |
|
| 103 |
point = Point(geometry=p, extrude=True) |
| 104 |
|
| 105 |
assert "coordinates>" in point.to_string(verbosity=Verbosity.verbose) |
| 106 |
assert "extrude>1</" in point.to_string(verbosity=Verbosity.verbose) |
| 107 |
|
| 108 |
def test_to_string_verbose_none(self) -> None: |
| 109 |
"""Test the to_string method, include extrude when true in verbose mode.""" |
| 110 |
p = geo.Point(1, 2) |
| 111 |
|
| 112 |
point = Point(geometry=p, extrude=False) |
| 113 |
|
| 114 |
assert "coordinates>" in point.to_string(verbosity=Verbosity.verbose) |
| 115 |
assert "extrude>0</" in point.to_string(verbosity=Verbosity.verbose) |
| 116 |
|
| 117 |
def test_to_string_2d_precision_0(self) -> None: |
| 118 |
"""Test the to_string method.""" |
| 119 |
p = geo.Point(1, 2) |
| 120 |
|
| 121 |
point = Point(geometry=p) |
| 122 |
|
| 123 |
assert "coordinates>1,2</" in point.to_string(precision=0) |
| 124 |
|
| 125 |
def test_to_string_3d_precision_0(self) -> None: |
| 126 |
"""Test the to_string method.""" |
| 127 |
p = geo.Point(1, 2, 3) |
| 128 |
|
| 129 |
point = Point(geometry=p) |
| 130 |
|
| 131 |
assert "coordinates>1,2,3</" in point.to_string(precision=0) |
| 132 |
|
| 133 |
def test_to_string_2d_precision_10(self) -> None: |
| 134 |
"""Test the to_string method.""" |
| 135 |
p = geo.Point(1, 2) |
| 136 |
|
| 137 |
point = Point(geometry=p) |
| 138 |
|
| 139 |
assert "coordinates>1.0000000000,2.0000000000</" in point.to_string( |
| 140 |
precision=10, |
| 141 |
) |
| 142 |
|
| 143 |
def test_to_string_3d_precision_10(self) -> None: |
| 144 |
"""Test the to_string method.""" |
| 145 |
p = geo.Point(1, 2, 3) |
| 146 |
|
| 147 |
point = Point(geometry=p) |
| 148 |
|
| 149 |
assert ( |
| 150 |
"coordinates>1.0000000000,2.0000000000,3.0000000000</" |
| 151 |
in point.to_string(precision=10) |
| 152 |
) |
| 153 |
|
| 154 |
def test_point_unequal(self) -> None: |
| 155 |
"""Test the __eq__ method.""" |
| 156 |
p = geo.Point(1, 2) |
| 157 |
point = Point(geometry=p) |
| 158 |
|
| 159 |
assert point != p |
| 160 |
|
| 161 |
def test_to_string_empty_geometry(self) -> None: |
| 162 |
"""Test the to_string method.""" |
| 163 |
point = Point(geometry=geo.Point(None, None)) |
| 164 |
|
| 165 |
assert not point |
| 166 |
|
| 167 |
def test_from_string_2d(self) -> None: |
| 168 |
"""Test the from_string method for a 2 dimensional point.""" |
| 169 |
point = Point.from_string( |
| 170 |
'<Point xmlns="http://www.opengis.net/kml/2.2">' |
| 171 |
"<coordinates>1.000000,2.000000</coordinates>" |
| 172 |
"</Point>", |
| 173 |
) |
| 174 |
|
| 175 |
assert point.geometry == geo.Point(1, 2) |
| 176 |
assert point.altitude_mode is None |
| 177 |
assert point.extrude is None |
| 178 |
|
| 179 |
def test_from_string_uppercase_altitude_mode_relaxed(self) -> None: |
| 180 |
"""Test the from_string method for an uppercase altitude mode.""" |
| 181 |
point = Point.from_string( |
| 182 |
'<Point xmlns="http://www.opengis.net/kml/2.2">' |
| 183 |
"<altitudeMode>RELATIVETOGROUND</altitudeMode>" |
| 184 |
"<coordinates>1.000000,2.000000</coordinates>" |
| 185 |
"</Point>", |
| 186 |
strict=False, |
| 187 |
) |
| 188 |
|
| 189 |
assert point.geometry == geo.Point(1, 2) |
| 190 |
assert point.altitude_mode |
| 191 |
assert point.altitude_mode.value == "relativeToGround" |
| 192 |
|
| 193 |
def test_from_string_uppercase_altitude_mode_strict(self) -> None: |
| 194 |
"""Test the from_string method for an uppercase altitude mode.""" |
| 195 |
with pytest.raises( |
| 196 |
KMLParseError, |
| 197 |
match=r"Value RELATIVETOGROUND is not a valid value for Enum AltitudeMode$", |
| 198 |
): |
| 199 |
Point.from_string( |
| 200 |
'<Point xmlns="http://www.opengis.net/kml/2.2">' |
| 201 |
"<altitudeMode>RELATIVETOGROUND</altitudeMode>" |
| 202 |
"<coordinates>1.000000,2.000000</coordinates>" |
| 203 |
"</Point>", |
| 204 |
) |
| 205 |
|
| 206 |
def test_from_string_invalid_altitude_mode_strict(self) -> None: |
| 207 |
with pytest.raises( |
| 208 |
KMLParseError, |
| 209 |
match=r"^Error parsing '<", |
| 210 |
): |
| 211 |
Point.from_string( |
| 212 |
'<Point xmlns="http://www.opengis.net/kml/2.2">' |
| 213 |
"<altitudeMode>INVALID</altitudeMode>" |
| 214 |
"<coordinates>1.000000,2.000000</coordinates>" |
| 215 |
"</Point>", |
| 216 |
) |
| 217 |
|
| 218 |
def test_from_string_invalid_altitude_mode_relaxed(self) -> None: |
| 219 |
point = Point.from_string( |
| 220 |
'<Point xmlns="http://www.opengis.net/kml/2.2">' |
| 221 |
"<altitudeMode>invalid</altitudeMode>" |
| 222 |
"<coordinates>1.000000,2.000000</coordinates>" |
| 223 |
"</Point>", |
| 224 |
strict=False, |
| 225 |
) |
| 226 |
|
| 227 |
assert point.geometry == geo.Point(1, 2) |
| 228 |
assert not point.altitude_mode |
| 229 |
|
| 230 |
def test_from_string_3d(self) -> None: |
| 231 |
"""Test the from_string method for a 3 dimensional point.""" |
| 232 |
point = Point.from_string( |
| 233 |
'<Point xmlns="http://www.opengis.net/kml/2.2">' |
| 234 |
"<extrude>1</extrude>" |
| 235 |
"<tessellate>1</tessellate>" |
| 236 |
"<altitudeMode>absolute</altitudeMode>" |
| 237 |
"<coordinates>1.000000,2.000000,3.000000</coordinates>" |
| 238 |
"</Point>", |
| 239 |
) |
| 240 |
|
| 241 |
assert point.geometry == geo.Point(1, 2, 3) |
| 242 |
assert point.altitude_mode |
| 243 |
assert point.altitude_mode.value == "absolute" |
| 244 |
assert point.extrude |
| 245 |
|
| 246 |
def test_empty_from_string(self) -> None: |
| 247 |
"""Test the from_string method.""" |
| 248 |
point = Point.from_string( |
| 249 |
"<Point/>", |
| 250 |
ns="", |
| 251 |
) |
| 252 |
|
| 253 |
assert not point |
| 254 |
|
| 255 |
def test_empty_from_string_relaxed(self) -> None: |
| 256 |
"""Test that no error is raised when the geometry is empty and not strict.""" |
| 257 |
point = Point.from_string( |
| 258 |
"<Point/>", |
| 259 |
ns="", |
| 260 |
strict=False, |
| 261 |
) |
| 262 |
|
| 263 |
assert point.geometry is None |
| 264 |
|
| 265 |
def test_from_string_empty_coordinates(self) -> None: |
| 266 |
point = Point.from_string( |
| 267 |
'<Point xmlns="http://www.opengis.net/kml/2.2"><coordinates/></Point>', |
| 268 |
) |
| 269 |
|
| 270 |
assert not point |
| 271 |
assert point.geometry is None |
| 272 |
|
| 273 |
def test_from_string_invalid_coordinates(self) -> None: |
| 274 |
point = Point.from_string( |
| 275 |
'<Point xmlns="http://www.opengis.net/kml/2.2">' |
| 276 |
"<coordinates>1</coordinates></Point>", |
| 277 |
) |
| 278 |
|
| 279 |
assert not point |
| 280 |
|
| 281 |
def test_from_string_invalid_coordinates_4d(self) -> None: |
| 282 |
point = Point.from_string( |
| 283 |
'<Point xmlns="http://www.opengis.net/kml/2.2">' |
| 284 |
"<coordinates>1,2,3,4</coordinates></Point>", |
| 285 |
) |
| 286 |
assert not point |
| 287 |
|
| 288 |
def test_from_string_invalid_coordinates_non_numerical(self) -> None: |
| 289 |
with pytest.raises( |
| 290 |
KMLParseError, |
| 291 |
match=r"^Invalid coordinates in", |
| 292 |
): |
| 293 |
Point.from_string( |
| 294 |
'<Point xmlns="http://www.opengis.net/kml/2.2">' |
| 295 |
"<coordinates>a,b,c</coordinates></Point>", |
| 296 |
) |
| 297 |
|
| 298 |
|
| 299 |
class TestPointLxml(Lxml, TestPoint): |
| 300 |
"""Test with lxml.""" |