| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Test the geometry classes.""" |
| 18 |
|
| 19 |
import pytest |
| 20 |
from pygeoif import geometry as geo |
| 21 |
|
| 22 |
from fastkml.abstract_geometry import _Geometry |
| 23 |
from fastkml.enums import AltitudeMode |
| 24 |
from fastkml.geometry import LinearRing |
| 25 |
from fastkml.geometry import LineString |
| 26 |
from fastkml.geometry import MultiGeometry |
| 27 |
from fastkml.geometry import Point |
| 28 |
from fastkml.geometry import Polygon |
| 29 |
from fastkml.geometry import create_kml_geometry |
| 30 |
from tests.base import Lxml |
| 31 |
from tests.base import StdLibrary |
| 32 |
|
| 33 |
|
| 34 |
class TestGetGeometry(StdLibrary): |
| 35 |
def test_altitude_mode(self) -> None: |
| 36 |
doc = """<kml:Point xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 37 |
<kml:coordinates>0.000000,1.000000</kml:coordinates> |
| 38 |
<kml:altitudeMode>clampToGround</kml:altitudeMode> |
| 39 |
</kml:Point>""" |
| 40 |
|
| 41 |
g = Point.from_string(doc) |
| 42 |
|
| 43 |
assert g.altitude_mode == AltitudeMode("clampToGround") |
| 44 |
|
| 45 |
def test_gx_altitude_mode(self) -> None: |
| 46 |
doc = ( |
| 47 |
'<kml:Point xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 48 |
'xmlns:gx="http://www.google.com/kml/ext/2.2">' |
| 49 |
"<kml:coordinates>0.000000,1.000000</kml:coordinates>" |
| 50 |
"<gx:altitudeMode>clampToSeaFloor</gx:altitudeMode>" |
| 51 |
"</kml:Point>" |
| 52 |
) |
| 53 |
g = Point.from_string(doc) |
| 54 |
|
| 55 |
assert g.altitude_mode == AltitudeMode("clampToSeaFloor") |
| 56 |
|
| 57 |
def test_extrude(self) -> None: |
| 58 |
doc = """<kml:Point xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 59 |
<kml:coordinates>0.000000,1.000000</kml:coordinates> |
| 60 |
<kml:extrude>1</kml:extrude> |
| 61 |
</kml:Point>""" |
| 62 |
|
| 63 |
g = Point.from_string(doc) |
| 64 |
|
| 65 |
assert g.extrude is True |
| 66 |
|
| 67 |
def test_tessellate(self) -> None: |
| 68 |
doc = """<kml:Point xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 69 |
<kml:coordinates>0.000000,1.000000</kml:coordinates> |
| 70 |
<kml:tessellate>1</kml:tessellate> |
| 71 |
</kml:Point>""" |
| 72 |
|
| 73 |
g = Point.from_string(doc) |
| 74 |
|
| 75 |
assert not hasattr(g, "tessellate") |
| 76 |
|
| 77 |
def test_point(self) -> None: |
| 78 |
doc = """<kml:Point xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 79 |
<kml:coordinates>0.000000,1.000000</kml:coordinates> |
| 80 |
</kml:Point>""" |
| 81 |
|
| 82 |
g = Point.from_string(doc) |
| 83 |
|
| 84 |
assert g.geometry |
| 85 |
assert g.geometry.__geo_interface__ == { |
| 86 |
"type": "Point", |
| 87 |
"bbox": (0.0, 1.0, 0.0, 1.0), |
| 88 |
"coordinates": (0.0, 1.0), |
| 89 |
} |
| 90 |
|
| 91 |
def test_linestring(self) -> None: |
| 92 |
doc = """<kml:LineString xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 93 |
<kml:coordinates>0.000000,0.000000 1.000000,1.000000</kml:coordinates> |
| 94 |
</kml:LineString>""" |
| 95 |
|
| 96 |
g = LineString.from_string(doc) |
| 97 |
|
| 98 |
assert g.geometry |
| 99 |
assert g.geometry.__geo_interface__ == { |
| 100 |
"type": "LineString", |
| 101 |
"bbox": (0.0, 0.0, 1.0, 1.0), |
| 102 |
"coordinates": ((0.0, 0.0), (1.0, 1.0)), |
| 103 |
} |
| 104 |
|
| 105 |
def test_linearring(self) -> None: |
| 106 |
doc = """<kml:LinearRing xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 107 |
<kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 |
| 108 |
0.000000,0.000000</kml:coordinates> |
| 109 |
</kml:LinearRing> |
| 110 |
""" |
| 111 |
|
| 112 |
g = LinearRing.from_string(doc) |
| 113 |
|
| 114 |
assert g.geometry |
| 115 |
assert g.geometry.__geo_interface__ == { |
| 116 |
"type": "LinearRing", |
| 117 |
"bbox": (0.0, 0.0, 1.0, 1.0), |
| 118 |
"coordinates": ((0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)), |
| 119 |
} |
| 120 |
|
| 121 |
def test_polygon(self) -> None: |
| 122 |
doc = """<kml:Polygon xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 123 |
<kml:outerBoundaryIs> |
| 124 |
<kml:LinearRing> |
| 125 |
<kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 |
| 126 |
0.000000,0.000000</kml:coordinates> |
| 127 |
</kml:LinearRing> |
| 128 |
</kml:outerBoundaryIs> |
| 129 |
</kml:Polygon> |
| 130 |
""" |
| 131 |
|
| 132 |
g = Polygon.from_string(doc) |
| 133 |
|
| 134 |
assert g.geometry |
| 135 |
assert g.geometry.__geo_interface__ == { |
| 136 |
"type": "Polygon", |
| 137 |
"bbox": (0.0, 0.0, 1.0, 1.0), |
| 138 |
"coordinates": (((0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)),), |
| 139 |
} |
| 140 |
|
| 141 |
def test_polygon_with_inner_boundary(self) -> None: |
| 142 |
doc = """<kml:Polygon xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 143 |
<kml:outerBoundaryIs> |
| 144 |
<kml:LinearRing> |
| 145 |
<kml:coordinates>-1.000000,-1.000000 2.000000,-1.000000 2.000000,2.000000 |
| 146 |
-1.000000,-1.000000</kml:coordinates> |
| 147 |
</kml:LinearRing> |
| 148 |
</kml:outerBoundaryIs> |
| 149 |
<kml:innerBoundaryIs> |
| 150 |
<kml:LinearRing> |
| 151 |
<kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 |
| 152 |
0.000000,0.000000</kml:coordinates> |
| 153 |
</kml:LinearRing> |
| 154 |
</kml:innerBoundaryIs> |
| 155 |
</kml:Polygon> |
| 156 |
""" |
| 157 |
|
| 158 |
g = Polygon.from_string(doc) |
| 159 |
|
| 160 |
assert g.geometry |
| 161 |
assert g.geometry.__geo_interface__ == { |
| 162 |
"type": "Polygon", |
| 163 |
"bbox": (-1.0, -1.0, 2.0, 2.0), |
| 164 |
"coordinates": ( |
| 165 |
((-1.0, -1.0), (2.0, -1.0), (2.0, 2.0), (-1.0, -1.0)), |
| 166 |
((0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)), |
| 167 |
), |
| 168 |
} |
| 169 |
|
| 170 |
def test_multipoint(self) -> None: |
| 171 |
doc = """ |
| 172 |
<kml:MultiGeometry xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 173 |
<kml:Point> |
| 174 |
<kml:coordinates>0.000000,1.000000</kml:coordinates> |
| 175 |
</kml:Point> |
| 176 |
<kml:Point> |
| 177 |
<kml:coordinates>1.000000,1.000000</kml:coordinates> |
| 178 |
</kml:Point> |
| 179 |
</kml:MultiGeometry> |
| 180 |
""" |
| 181 |
|
| 182 |
g = MultiGeometry.from_string(doc) |
| 183 |
|
| 184 |
assert len(g.geometry) == 2 |
| 185 |
|
| 186 |
def test_multilinestring(self) -> None: |
| 187 |
doc = """ |
| 188 |
<kml:MultiGeometry xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 189 |
<kml:LineString> |
| 190 |
<kml:coordinates>0.000000,0.000000 1.000000,0.000000</kml:coordinates> |
| 191 |
</kml:LineString> |
| 192 |
<kml:LineString> |
| 193 |
<kml:coordinates>0.000000,1.000000 1.000000,1.000000</kml:coordinates> |
| 194 |
</kml:LineString> |
| 195 |
</kml:MultiGeometry> |
| 196 |
""" |
| 197 |
|
| 198 |
g = MultiGeometry.from_string(doc) |
| 199 |
|
| 200 |
assert len(g.geometry) == 2 |
| 201 |
|
| 202 |
def test_multipolygon(self) -> None: |
| 203 |
doc = """ |
| 204 |
<kml:MultiGeometry xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 205 |
<kml:Polygon> |
| 206 |
<kml:outerBoundaryIs> |
| 207 |
<kml:LinearRing> |
| 208 |
<kml:coordinates>-1.000000,-1.000000 2.000000,-1.000000 |
| 209 |
2.000000,2.000000 -1.000000,-1.000000</kml:coordinates> |
| 210 |
</kml:LinearRing> |
| 211 |
</kml:outerBoundaryIs> |
| 212 |
<kml:innerBoundaryIs> |
| 213 |
<kml:LinearRing> |
| 214 |
<kml:coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 |
| 215 |
0.000000,0.000000</kml:coordinates> |
| 216 |
</kml:LinearRing> |
| 217 |
</kml:innerBoundaryIs> |
| 218 |
</kml:Polygon> |
| 219 |
<kml:Polygon> |
| 220 |
<kml:outerBoundaryIs> |
| 221 |
<kml:LinearRing> |
| 222 |
<kml:coordinates>3.000000,0.000000 4.000000,0.000000 4.000000,1.000000 |
| 223 |
3.000000,0.000000</kml:coordinates> |
| 224 |
</kml:LinearRing> |
| 225 |
</kml:outerBoundaryIs> |
| 226 |
</kml:Polygon> |
| 227 |
</kml:MultiGeometry> |
| 228 |
""" |
| 229 |
|
| 230 |
g = MultiGeometry.from_string(doc) |
| 231 |
|
| 232 |
assert g.geometry is not None |
| 233 |
assert len(g.geometry) == 2 |
| 234 |
|
| 235 |
def test_multi_ns(self) -> None: |
| 236 |
doc = """ |
| 237 |
<kml:MultiGeometry xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 238 |
<kml:Point> |
| 239 |
<kml:coordinates>0.000000,1.000000</kml:coordinates> |
| 240 |
</kml:Point> |
| 241 |
<gx:Track xmlns:gx="http://www.google.com/kml/ext/2.2"> |
| 242 |
<kml:when>2000-01-01</kml:when> |
| 243 |
<gx:coord>1.0 1.0</gx:coord> |
| 244 |
</gx:Track> |
| 245 |
</kml:MultiGeometry> |
| 246 |
""" |
| 247 |
|
| 248 |
g = MultiGeometry.from_string(doc) |
| 249 |
|
| 250 |
assert len(g.geometry) == 2 |
| 251 |
|
| 252 |
def test_geometrycollection(self) -> None: |
| 253 |
doc = """ |
| 254 |
<kml:MultiGeometry xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 255 |
<kml:Polygon> |
| 256 |
<kml:outerBoundaryIs> |
| 257 |
<kml:LinearRing> |
| 258 |
<kml:coordinates>3,0 4,0 4,1 3,0</kml:coordinates> |
| 259 |
</kml:LinearRing> |
| 260 |
</kml:outerBoundaryIs> |
| 261 |
</kml:Polygon> |
| 262 |
<kml:Point> |
| 263 |
<kml:coordinates>0.000000,1.000000</kml:coordinates> |
| 264 |
</kml:Point> |
| 265 |
<kml:LineString> |
| 266 |
<kml:coordinates>0.000000,0.000000 1.000000,1.000000</kml:coordinates> |
| 267 |
</kml:LineString> |
| 268 |
<kml:LinearRing> |
| 269 |
<kml:coordinates>0.0,0.0 1.0,0.0 1.0,1.0 0.0,1.0 0.0,0.0</kml:coordinates> |
| 270 |
</kml:LinearRing> |
| 271 |
</kml:MultiGeometry> |
| 272 |
""" |
| 273 |
|
| 274 |
g = MultiGeometry.from_string(doc) |
| 275 |
|
| 276 |
assert len(g.geometry) == 4 |
| 277 |
|
| 278 |
def test_geometrycollection_with_linearring(self) -> None: |
| 279 |
doc = """ |
| 280 |
<kml:MultiGeometry xmlns:kml="http://www.opengis.net/kml/2.2"> |
| 281 |
<kml:LinearRing> |
| 282 |
<kml:coordinates>3.0,0.0 4.0,0.0 4.0,1.0 3.0,0.0</kml:coordinates> |
| 283 |
</kml:LinearRing> |
| 284 |
<kml:LinearRing> |
| 285 |
<kml:coordinates>0.0,0.0 1.0,0.0 1.0,1.0 0.0,0.0</kml:coordinates> |
| 286 |
</kml:LinearRing> |
| 287 |
</kml:MultiGeometry> |
| 288 |
""" |
| 289 |
|
| 290 |
g = MultiGeometry.from_string(doc) |
| 291 |
|
| 292 |
assert g.geometry |
| 293 |
assert len(g.geometry) == 2 |
| 294 |
assert g.geometry.geom_type == "GeometryCollection" |
| 295 |
|
| 296 |
|
| 297 |
class TestGeometry(StdLibrary): |
| 298 |
"""Test the _Geometry class.""" |
| 299 |
|
| 300 |
def test_init(self) -> None: |
| 301 |
"""Test the init method.""" |
| 302 |
g = _Geometry() |
| 303 |
|
| 304 |
assert g.ns == "{http://www.opengis.net/kml/2.2}" |
| 305 |
assert g.target_id == "" |
| 306 |
assert g.id == "" |
| 307 |
assert g.altitude_mode is None |
| 308 |
|
| 309 |
def test_init_with_args(self) -> None: |
| 310 |
"""Test the init method with arguments.""" |
| 311 |
g = _Geometry( |
| 312 |
ns="", |
| 313 |
target_id="target_id", |
| 314 |
id="id", |
| 315 |
altitude_mode=AltitudeMode.clamp_to_ground, |
| 316 |
) |
| 317 |
|
| 318 |
assert g.ns == "" |
| 319 |
assert g.target_id == "target_id" |
| 320 |
assert g.id == "id" |
| 321 |
assert g.altitude_mode == AltitudeMode.clamp_to_ground |
| 322 |
|
| 323 |
def test_to_string(self) -> None: |
| 324 |
"""Test the to_string method.""" |
| 325 |
g = _Geometry() |
| 326 |
|
| 327 |
assert "http://www.opengis.net/kml/2.2" in g.to_string() |
| 328 |
assert "targetId=" not in g.to_string() |
| 329 |
assert "id=" not in g.to_string() |
| 330 |
assert "extrude" not in g.to_string() |
| 331 |
assert "altitudeMode" not in g.to_string() |
| 332 |
assert "tessellate" not in g.to_string() |
| 333 |
|
| 334 |
def test_to_string_with_args(self) -> None: |
| 335 |
"""Test the to_string method.""" |
| 336 |
g = _Geometry( |
| 337 |
ns="{http://www.opengis.net/kml/2.3}", |
| 338 |
target_id="target_id", |
| 339 |
id="my-id", |
| 340 |
extrude=True, |
| 341 |
altitude_mode=AltitudeMode.relative_to_ground, |
| 342 |
tessellate=True, |
| 343 |
) |
| 344 |
|
| 345 |
assert "http://www.opengis.net/kml/2.3" in g.to_string() |
| 346 |
assert 'targetId="target_id"' in g.to_string() |
| 347 |
assert 'id="my-id"' in g.to_string() |
| 348 |
assert "extrude" not in g.to_string() |
| 349 |
assert "altitudeMode>relativeToGround<" not in g.to_string() |
| 350 |
assert "tessellate" not in g.to_string() |
| 351 |
|
| 352 |
def test_from_string(self) -> None: |
| 353 |
"""Test the from_string method.""" |
| 354 |
g = _Geometry.from_string( |
| 355 |
'<_Geometry id="my-id" targetId="target_id" ' |
| 356 |
'xmlns="http://www.opengis.net/kml/2.2">' |
| 357 |
"<extrude>1</extrude>" |
| 358 |
"<altitudeMode>relativeToGround</altitudeMode>" |
| 359 |
"<tessellate>1</tessellate>" |
| 360 |
"</_Geometry>", |
| 361 |
) |
| 362 |
|
| 363 |
assert g.ns == "{http://www.opengis.net/kml/2.2}" |
| 364 |
assert g.target_id == "target_id" |
| 365 |
assert g.id == "my-id" |
| 366 |
assert g.altitude_mode is None |
| 367 |
assert not hasattr(g, "tessellate") |
| 368 |
assert not hasattr(g, "extrude") |
| 369 |
|
| 370 |
def test_from_minimal_string(self) -> None: |
| 371 |
g = _Geometry.from_string( |
| 372 |
'<_Geometry xmlns="http://www.opengis.net/kml/2.2/" />', |
| 373 |
) |
| 374 |
|
| 375 |
assert g.ns == "{http://www.opengis.net/kml/2.2}" |
| 376 |
assert g.target_id == "" |
| 377 |
assert g.id == "" |
| 378 |
assert g.altitude_mode is None |
| 379 |
|
| 380 |
def test_from_string_omitting_ns(self) -> None: |
| 381 |
"""Test the from_string method.""" |
| 382 |
g = _Geometry.from_string( |
| 383 |
'<kml:_Geometry xmlns:kml="http://www.opengis.net/kml/2.2" ' |
| 384 |
'id="my-id" targetId="target_id">' |
| 385 |
"<kml:extrude>1</kml:extrude>" |
| 386 |
"<kml:altitudeMode>relativeToGround</kml:altitudeMode>" |
| 387 |
"<kml:tessellate>1</kml:tessellate>" |
| 388 |
"</kml:_Geometry>", |
| 389 |
) |
| 390 |
|
| 391 |
assert g.ns == "{http://www.opengis.net/kml/2.2}" |
| 392 |
assert g.target_id == "target_id" |
| 393 |
assert g.id == "my-id" |
| 394 |
assert g.altitude_mode is None |
| 395 |
assert not hasattr(g, "tessellate") |
| 396 |
assert not hasattr(g, "extrude") |
| 397 |
|
| 398 |
|
| 399 |
class TestCreateKmlGeometry(StdLibrary): |
| 400 |
def test_create_kml_geometry_point(self) -> None: |
| 401 |
"""Test the create_kml_geometry function.""" |
| 402 |
g = create_kml_geometry(geo.Point(0, 1)) |
| 403 |
|
| 404 |
assert isinstance(g, Point) |
| 405 |
assert g.geometry |
| 406 |
assert g.geometry.__geo_interface__ == { |
| 407 |
"type": "Point", |
| 408 |
"bbox": (0.0, 1.0, 0.0, 1.0), |
| 409 |
"coordinates": (0.0, 1.0), |
| 410 |
} |
| 411 |
assert "Point>" in g.to_string() |
| 412 |
assert "coordinates>0.000000,1.000000</" in g.to_string(precision=6) |
| 413 |
|
| 414 |
def test_create_kml_geometry_linestring(self) -> None: |
| 415 |
"""Test the create_kml_geometry function.""" |
| 416 |
g = create_kml_geometry(geo.LineString([(0, 0), (1, 1)])) |
| 417 |
|
| 418 |
assert isinstance(g, LineString) |
| 419 |
assert g.geometry |
| 420 |
assert g.geometry.__geo_interface__ == { |
| 421 |
"type": "LineString", |
| 422 |
"bbox": (0.0, 0.0, 1.0, 1.0), |
| 423 |
"coordinates": ((0.0, 0.0), (1.0, 1.0)), |
| 424 |
} |
| 425 |
assert "LineString>" in g.to_string() |
| 426 |
assert "coordinates>0.000000,0.000000 1.000000,1.000000</" in g.to_string( |
| 427 |
precision=6, |
| 428 |
) |
| 429 |
|
| 430 |
def test_create_kml_geometry_linearring(self) -> None: |
| 431 |
"""Test the create_kml_geometry function.""" |
| 432 |
g = create_kml_geometry(geo.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)])) |
| 433 |
|
| 434 |
assert isinstance(g, LinearRing) |
| 435 |
assert g.geometry |
| 436 |
assert g.geometry.__geo_interface__ == { |
| 437 |
"type": "LinearRing", |
| 438 |
"bbox": (0.0, 0.0, 1.0, 1.0), |
| 439 |
"coordinates": ((0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)), |
| 440 |
} |
| 441 |
assert "LinearRing>" in g.to_string() |
| 442 |
assert ( |
| 443 |
"coordinates>0.000000,0.000000 1.000000,1.000000 1.000000,0.000000 " |
| 444 |
"0.000000,0.000000</" |
| 445 |
) in g.to_string(precision=6) |
| 446 |
|
| 447 |
def test_create_kml_geometry_polygon(self) -> None: |
| 448 |
"""Test the create_kml_geometry function.""" |
| 449 |
g = create_kml_geometry(geo.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)])) |
| 450 |
|
| 451 |
assert isinstance(g, Polygon) |
| 452 |
assert g.geometry |
| 453 |
assert g.geometry.__geo_interface__ == { |
| 454 |
"type": "Polygon", |
| 455 |
"bbox": (0.0, 0.0, 1.0, 1.0), |
| 456 |
"coordinates": (((0.0, 0.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),), |
| 457 |
} |
| 458 |
assert "Polygon>" in g.to_string() |
| 459 |
assert ( |
| 460 |
"coordinates>0.000000,0.000000 1.000000,1.000000 1.000000,0.000000 " |
| 461 |
"0.000000,0.000000</" |
| 462 |
) in g.to_string(precision=6) |
| 463 |
|
| 464 |
def test_create_kml_geometry_multipoint(self) -> None: |
| 465 |
"""Test the create_kml_geometry function.""" |
| 466 |
g = create_kml_geometry(geo.MultiPoint([(0, 0), (1, 1), (1, 0), (2, 2)])) |
| 467 |
|
| 468 |
assert isinstance(g, MultiGeometry) |
| 469 |
assert g.geometry |
| 470 |
assert len(g.geometry) == 4 |
| 471 |
xml = g.to_string(precision=6) |
| 472 |
assert "MultiGeometry>" in xml |
| 473 |
assert "Point>" in xml |
| 474 |
assert "coordinates>0.000000,0.000000</" in xml |
| 475 |
assert "coordinates>1.000000,1.000000</" in xml |
| 476 |
assert "coordinates>1.000000,0.000000</" in xml |
| 477 |
assert "coordinates>2.000000,2.000000</" in xml |
| 478 |
|
| 479 |
def test_create_kml_geometry_multilinestring(self) -> None: |
| 480 |
"""Test the create_kml_geometry function.""" |
| 481 |
g = create_kml_geometry( |
| 482 |
geo.MultiLineString([[(0, 0), (1, 1)], [(0, 0), (1, 1)]]), |
| 483 |
) |
| 484 |
|
| 485 |
assert isinstance(g, MultiGeometry) |
| 486 |
assert g.geometry |
| 487 |
assert len(g.geometry) == 2 |
| 488 |
xml = g.to_string(precision=6) |
| 489 |
assert "MultiGeometry>" in xml |
| 490 |
assert "LineString>" in xml |
| 491 |
assert "coordinates>0.000000,0.000000 1.000000,1.000000</" in xml |
| 492 |
assert "coordinates>0.000000,0.000000 1.000000,1.000000</" in xml |
| 493 |
|
| 494 |
def test_create_kml_geometry_multipolygon(self) -> None: |
| 495 |
"""Test the create_kml_geometry function.""" |
| 496 |
g = create_kml_geometry( |
| 497 |
geo.MultiPolygon( |
| 498 |
( |
| 499 |
( |
| 500 |
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)), |
| 501 |
[((0.1, 0.1), (0.1, 0.2), (0.2, 0.2), (0.2, 0.1))], |
| 502 |
), |
| 503 |
(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),), |
| 504 |
), |
| 505 |
), |
| 506 |
) |
| 507 |
|
| 508 |
assert isinstance(g, MultiGeometry) |
| 509 |
assert g.geometry |
| 510 |
assert len(g.geometry) == 2 |
| 511 |
xml = g.to_string(precision=6) |
| 512 |
assert "MultiGeometry>" in xml |
| 513 |
assert "Polygon>" in xml |
| 514 |
assert ( |
| 515 |
"coordinates>0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 " |
| 516 |
"1.000000,0.000000 0.000000,0.000000</" |
| 517 |
) in xml |
| 518 |
assert ( |
| 519 |
"coordinates>0.100000,0.100000 0.100000,0.200000 0.200000,0.200000 " |
| 520 |
"0.200000,0.100000 0.100000,0.100000</" |
| 521 |
) in xml |
| 522 |
assert ( |
| 523 |
"coordinates>0.000000,0.000000 0.000000,1.000000 1.000000,1.000000 " |
| 524 |
"1.000000,0.000000 0.000000,0.000000</" |
| 525 |
) in xml |
| 526 |
|
| 527 |
def test_create_kml_geometry_geometrycollection(self) -> None: |
| 528 |
multipoint = geo.MultiPoint([(0, 0), (1, 1), (1, 2), (2, 2)]) |
| 529 |
gc1 = geo.GeometryCollection([geo.Point(0, 0), multipoint]) |
| 530 |
line1 = geo.LineString([(0, 0), (3, 1)]) |
| 531 |
gc2 = geo.GeometryCollection([gc1, line1]) |
| 532 |
poly1 = geo.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)]) |
| 533 |
e = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)] |
| 534 |
i = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)] |
| 535 |
poly2 = geo.Polygon(e, [i]) |
| 536 |
p0 = geo.Point(0, 0) |
| 537 |
p1 = geo.Point(-1, -1) |
| 538 |
ring = geo.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)]) |
| 539 |
line = geo.LineString([(0, 0), (1, 1)]) |
| 540 |
gc = geo.GeometryCollection([gc2, poly1, poly2, p0, p1, ring, line]) |
| 541 |
|
| 542 |
g = create_kml_geometry(gc) |
| 543 |
|
| 544 |
assert isinstance(g, MultiGeometry) |
| 545 |
assert g.geometry |
| 546 |
assert len(g.geometry) == 7 |
| 547 |
xml = g.to_string(precision=6) |
| 548 |
assert "MultiGeometry>" in xml |
| 549 |
assert "LineString>" in xml |
| 550 |
assert "LinearRing>" in xml |
| 551 |
assert "Polygon>" in xml |
| 552 |
assert "outerBoundaryIs>" in xml |
| 553 |
assert "innerBoundaryIs>" in xml |
| 554 |
assert "Point>" in xml |
| 555 |
assert "coordinates>0.000000,0.000000</" in xml |
| 556 |
assert g.geometry == gc |
| 557 |
|
| 558 |
def test_create_kml_geometry_geometrycollection_roundtrip(self) -> None: |
| 559 |
multipoint = geo.MultiPoint([(0, 0), (1, 1), (1, 2), (2, 2)]) |
| 560 |
gc1 = geo.GeometryCollection([geo.Point(0, 0), multipoint]) |
| 561 |
line1 = geo.LineString([(0, 0), (3, 1)]) |
| 562 |
gc2 = geo.GeometryCollection([line1, gc1]) |
| 563 |
poly1 = geo.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)]) |
| 564 |
e = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)] |
| 565 |
i = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)] |
| 566 |
poly2 = geo.Polygon(e, [i]) |
| 567 |
p0 = geo.Point(0, 0) |
| 568 |
p1 = geo.Point(-1, -1) |
| 569 |
ring = geo.LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)]) |
| 570 |
line = geo.LineString([(0, 0), (1, 1)]) |
| 571 |
gc = geo.GeometryCollection( |
| 572 |
[ |
| 573 |
p0, |
| 574 |
p1, |
| 575 |
line, |
| 576 |
poly1, |
| 577 |
poly2, |
| 578 |
ring, |
| 579 |
gc2, |
| 580 |
], |
| 581 |
) |
| 582 |
g = create_kml_geometry(gc) |
| 583 |
|
| 584 |
mg = MultiGeometry.from_string(g.to_string()) |
| 585 |
|
| 586 |
assert mg.geometry == gc |
| 587 |
|
| 588 |
def test_create_kml_geometry_non_geometry(self) -> None: |
| 589 |
"""Test the create_kml_geometry function.""" |
| 590 |
with pytest.raises( |
| 591 |
AttributeError, |
| 592 |
match=r"^'str' object has no attribute '__geo_interface__'$", |
| 593 |
): |
| 594 |
create_kml_geometry("not a geometry") |
| 595 |
|
| 596 |
|
| 597 |
class TestGetGeometryLxml(Lxml, TestGetGeometry): |
| 598 |
"""Test with lxml.""" |
| 599 |
|
| 600 |
|
| 601 |
class TestGeometryLxml(Lxml, TestGeometry): |
| 602 |
"""Test with lxml.""" |
| 603 |
|
| 604 |
|
| 605 |
class TestCreateKmlGeometryLxml(Lxml, TestCreateKmlGeometry): |
| 606 |
"""Test with lxml.""" |