| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Test the kml class.""" |
| 18 |
|
| 19 |
import io |
| 20 |
import pathlib |
| 21 |
import tempfile |
| 22 |
import zipfile |
| 23 |
|
| 24 |
import pygeoif as geo |
| 25 |
import pytest |
| 26 |
from pygeoif.geometry import Polygon |
| 27 |
|
| 28 |
from fastkml import containers |
| 29 |
from fastkml import features |
| 30 |
from fastkml import kml |
| 31 |
from fastkml.containers import Document |
| 32 |
from fastkml.features import Placemark |
| 33 |
from tests.base import Lxml |
| 34 |
from tests.base import StdLibrary |
| 35 |
|
| 36 |
BASEDIR = pathlib.Path(__file__).parent |
| 37 |
KMLFILEDIR = BASEDIR / "ogc_conformance" / "data" / "kml" |
| 38 |
|
| 39 |
|
| 40 |
class TestStdLibrary(StdLibrary): |
| 41 |
"""Test with the standard library.""" |
| 42 |
|
| 43 |
def test_linarring_placemark(self) -> None: |
| 44 |
doc = kml.KML.from_string( |
| 45 |
"""<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 46 |
<Placemark> |
| 47 |
<LinearRing> |
| 48 |
<coordinates>0.0,0.0 1.0,0.0 1.0,1.0 0.0,0.0</coordinates> |
| 49 |
</LinearRing> |
| 50 |
</Placemark> </kml>""", |
| 51 |
) |
| 52 |
doc2 = kml.KML.from_string(doc.to_string()) |
| 53 |
assert isinstance(doc.features[0].geometry, geo.LinearRing) |
| 54 |
assert doc.to_string() == doc2.to_string() |
| 55 |
|
| 56 |
def test_kml(self) -> None: |
| 57 |
"""Kml file without contents.""" |
| 58 |
k = kml.KML(ns="") |
| 59 |
assert k.features == [] |
| 60 |
assert ( |
| 61 |
k.to_string().strip().replace(" ", "") |
| 62 |
== '<kmlxmlns="http://www.opengis.net/kml/2.2"/>' |
| 63 |
) |
| 64 |
k2 = kml.KML.from_string(k.to_string(), ns="") |
| 65 |
assert k.to_string() == k2.to_string() |
| 66 |
|
| 67 |
def test_kml_with_document(self) -> None: |
| 68 |
"""Kml file with document/folder/placemark/polygon.""" |
| 69 |
doc = ( |
| 70 |
'<kml xmlns="http://www.opengis.net/kml/2.2">' |
| 71 |
'<Document id="root_doc">' |
| 72 |
"<Folder><name>test</name>" |
| 73 |
"<Placemark>" |
| 74 |
"<Polygon>" |
| 75 |
"<outerBoundaryIs>" |
| 76 |
"<LinearRing><coordinates>" |
| 77 |
"-93.7426720226024,57.4519411370713 -93.6051809086549,49.4316261567984 " |
| 78 |
"-80.8643376828499,49.5232868994301 -81.2309806533767,57.4519411370713 " |
| 79 |
"-81.2309806533767,57.4519411370713 -93.7426720226024,57.4519411370713" |
| 80 |
"</coordinates></LinearRing>" |
| 81 |
"</outerBoundaryIs>" |
| 82 |
"<innerBoundaryIs>" |
| 83 |
"<LinearRing><coordinates>" |
| 84 |
"-91.8663227028478,56.050879726904 -91.7704563496718,53.9897531336206 " |
| 85 |
"-90.1407283456804,54.0856194867966 -90.0927951690924,56.002946550316 " |
| 86 |
"-91.8663227028478,56.050879726904" |
| 87 |
"</coordinates></LinearRing>" |
| 88 |
"</innerBoundaryIs>" |
| 89 |
"<innerBoundaryIs>" |
| 90 |
"<LinearRing><coordinates>" |
| 91 |
"-85.4912102166459,55.90708019714 -85.4912102166459,54.0376863102086 " |
| 92 |
"-83.8135490360665,54.0856194867966 -83.9094153892425,55.90708019714 " |
| 93 |
"-85.4912102166459,55.90708019714</coordinates></LinearRing>" |
| 94 |
"</innerBoundaryIs>" |
| 95 |
"</Polygon>" |
| 96 |
"</Placemark>" |
| 97 |
"</Folder>" |
| 98 |
"</Document></kml>" |
| 99 |
) |
| 100 |
k = kml.KML.from_string(doc) |
| 101 |
assert len(k.features) == 1 |
| 102 |
assert isinstance(k.features[0], Document) |
| 103 |
assert len(k.features[0].features) == 1 |
| 104 |
assert isinstance(k.features[0].features[0], containers.Folder) |
| 105 |
assert len(k.features[0].features[0].features) == 1 |
| 106 |
assert isinstance(k.features[0].features[0].features[0], features.Placemark) |
| 107 |
assert isinstance(k.features[0].features[0].features[0].geometry, geo.Polygon) |
| 108 |
assert len(list(k.features[0].features[0].features[0].geometry.interiors)) == 2 |
| 109 |
|
| 110 |
|
| 111 |
class TestParseKML(StdLibrary): |
| 112 |
def test_parse_kml(self) -> None: |
| 113 |
empty_placemark = KMLFILEDIR / "emptyPlacemarkWithoutId.xml" |
| 114 |
|
| 115 |
doc = kml.KML.parse(empty_placemark) |
| 116 |
|
| 117 |
assert doc == kml.KML( |
| 118 |
ns="{http://www.opengis.net/kml/2.2}", |
| 119 |
features=[ |
| 120 |
Document( |
| 121 |
ns="{http://www.opengis.net/kml/2.2}", |
| 122 |
id="doc-001", |
| 123 |
target_id="", |
| 124 |
name="Vestibulum eleifend lobortis lorem.", |
| 125 |
features=[ |
| 126 |
Placemark( |
| 127 |
ns="{http://www.opengis.net/kml/2.2}", |
| 128 |
), |
| 129 |
], |
| 130 |
schemata=[], |
| 131 |
), |
| 132 |
], |
| 133 |
) |
| 134 |
|
| 135 |
def test_parse_kml_filename(self) -> None: |
| 136 |
empty_placemark = str(KMLFILEDIR / "emptyPlacemarkWithoutId.xml") |
| 137 |
|
| 138 |
doc = kml.KML.parse(empty_placemark) |
| 139 |
|
| 140 |
assert doc == kml.KML( |
| 141 |
ns="{http://www.opengis.net/kml/2.2}", |
| 142 |
features=[ |
| 143 |
Document( |
| 144 |
ns="{http://www.opengis.net/kml/2.2}", |
| 145 |
id="doc-001", |
| 146 |
target_id="", |
| 147 |
name="Vestibulum eleifend lobortis lorem.", |
| 148 |
features=[ |
| 149 |
Placemark( |
| 150 |
ns="{http://www.opengis.net/kml/2.2}", |
| 151 |
), |
| 152 |
], |
| 153 |
schemata=[], |
| 154 |
), |
| 155 |
], |
| 156 |
) |
| 157 |
|
| 158 |
def test_parse_kml_fileobject(self) -> None: |
| 159 |
empty_placemark = KMLFILEDIR / "emptyPlacemarkWithoutId.xml" |
| 160 |
with empty_placemark.open() as f: |
| 161 |
doc = kml.KML.parse(f) |
| 162 |
|
| 163 |
assert doc == kml.KML( |
| 164 |
ns="{http://www.opengis.net/kml/2.2}", |
| 165 |
features=[ |
| 166 |
Document( |
| 167 |
ns="{http://www.opengis.net/kml/2.2}", |
| 168 |
id="doc-001", |
| 169 |
target_id="", |
| 170 |
name="Vestibulum eleifend lobortis lorem.", |
| 171 |
features=[ |
| 172 |
Placemark( |
| 173 |
ns="{http://www.opengis.net/kml/2.2}", |
| 174 |
), |
| 175 |
], |
| 176 |
schemata=[], |
| 177 |
), |
| 178 |
], |
| 179 |
) |
| 180 |
|
| 181 |
|
| 182 |
class TestParseKMLNone(StdLibrary): |
| 183 |
def test_kml_parse(self) -> None: |
| 184 |
empty_placemark = KMLFILEDIR / "emptyPlacemarkWithoutId.xml" |
| 185 |
|
| 186 |
doc = kml.KML.parse(file=empty_placemark, ns="None") |
| 187 |
|
| 188 |
assert doc.ns == "None" |
| 189 |
|
| 190 |
|
| 191 |
class TestWriteKML(StdLibrary): |
| 192 |
def test_write_kml_file(self) -> None: |
| 193 |
doc = kml.KML( |
| 194 |
ns="{http://www.opengis.net/kml/2.2}", |
| 195 |
name="Vestibulum eleifend lobortis lorem.", |
| 196 |
features=[ |
| 197 |
Document( |
| 198 |
ns="{http://www.opengis.net/kml/2.2}", |
| 199 |
id="doc-001", |
| 200 |
target_id="", |
| 201 |
name="Vestibulum eleifend lobortis lorem.", |
| 202 |
features=[ |
| 203 |
Placemark( |
| 204 |
ns="{http://www.opengis.net/kml/2.2}", |
| 205 |
), |
| 206 |
], |
| 207 |
schemata=[], |
| 208 |
), |
| 209 |
], |
| 210 |
) |
| 211 |
with tempfile.TemporaryDirectory() as tmpdir_name: |
| 212 |
file_path = pathlib.Path(tmpdir_name) / "output.kml" |
| 213 |
|
| 214 |
doc.write(file_path=file_path, prettyprint=True) |
| 215 |
|
| 216 |
assert file_path.is_file(), "KML file was not created." |
| 217 |
parsed_doc = kml.KML.parse(file_path) |
| 218 |
assert parsed_doc.to_string() == doc.to_string() |
| 219 |
|
| 220 |
def test_write_kmz_file(self) -> None: |
| 221 |
doc = kml.KML( |
| 222 |
ns="{http://www.opengis.net/kml/2.2}", |
| 223 |
name="Vestibulum eleifend lobortis lorem.", |
| 224 |
features=[ |
| 225 |
Document( |
| 226 |
ns="{http://www.opengis.net/kml/2.2}", |
| 227 |
id="doc-001", |
| 228 |
target_id="", |
| 229 |
name="Vestibulum eleifend lobortis lorem.", |
| 230 |
features=[ |
| 231 |
Placemark( |
| 232 |
ns="{http://www.opengis.net/kml/2.2}", |
| 233 |
), |
| 234 |
], |
| 235 |
schemata=[], |
| 236 |
), |
| 237 |
], |
| 238 |
) |
| 239 |
with tempfile.TemporaryDirectory() as tmpdir_name: |
| 240 |
file_path = pathlib.Path(tmpdir_name) / "output.kmz" |
| 241 |
|
| 242 |
doc.write(file_path=file_path, prettyprint=True) |
| 243 |
|
| 244 |
assert file_path.is_file(), "KMZ file was not created." |
| 245 |
tree = doc.to_string() |
| 246 |
with zipfile.ZipFile(file_path, "r") as kmz: |
| 247 |
assert "doc.kml" in kmz.namelist(), "doc.kml not found in the KMZ file" |
| 248 |
with kmz.open("doc.kml") as doc_kml: |
| 249 |
kml_content = doc_kml.read().decode("utf-8") |
| 250 |
assert kml_content == tree, ( |
| 251 |
"KML content does not match expected content" |
| 252 |
) |
| 253 |
|
| 254 |
|
| 255 |
class TestKmlFromString(StdLibrary): |
| 256 |
def test_document(self) -> None: |
| 257 |
doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 258 |
<Document targetId="someTargetId"> |
| 259 |
<name>Document.kml</name> |
| 260 |
<open>1</open> |
| 261 |
<Style id="exampleStyleDocument"> |
| 262 |
<LabelStyle> |
| 263 |
<color>ff0000cc</color> |
| 264 |
</LabelStyle> |
| 265 |
</Style> |
| 266 |
<Placemark> |
| 267 |
<name>Document Feature 1</name> |
| 268 |
<styleUrl>#exampleStyleDocument</styleUrl> |
| 269 |
<Point> |
| 270 |
<coordinates>-122.371,37.816,0</coordinates> |
| 271 |
</Point> |
| 272 |
</Placemark> |
| 273 |
<Placemark targetId="someTargetId"> |
| 274 |
<name>Document Feature 2</name> |
| 275 |
<styleUrl>#exampleStyleDocument</styleUrl> |
| 276 |
<Point> |
| 277 |
<coordinates>-122.370,37.817,0</coordinates> |
| 278 |
</Point> |
| 279 |
</Placemark> |
| 280 |
</Document> |
| 281 |
</kml>""" |
| 282 |
|
| 283 |
k = kml.KML.from_string(doc) |
| 284 |
assert len(k.features) == 1 |
| 285 |
assert len(k.features[0].features) == 2 |
| 286 |
k2 = kml.KML.from_string(k.to_string()) |
| 287 |
assert k.to_string() == k2.to_string() |
| 288 |
|
| 289 |
def test_folders(self) -> None: |
| 290 |
doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 291 |
<Folder> |
| 292 |
<name>Folder.kml</name> |
| 293 |
<open>1</open> |
| 294 |
<description> |
| 295 |
A folder is a container that can hold multiple other objects |
| 296 |
</description> |
| 297 |
<Placemark> |
| 298 |
<name>Folder object 1 (Placemark)</name> |
| 299 |
<Point> |
| 300 |
<coordinates>-122.377588,37.830266,0</coordinates> |
| 301 |
</Point> |
| 302 |
</Placemark> |
| 303 |
<Placemark> |
| 304 |
<name>Folder object 2 (Polygon)</name> |
| 305 |
<Polygon> |
| 306 |
<outerBoundaryIs> |
| 307 |
<LinearRing> |
| 308 |
<coordinates> |
| 309 |
-122.377830,37.830445,0 |
| 310 |
-122.377576,37.830631,0 |
| 311 |
-122.377840,37.830642,0 |
| 312 |
-122.377830,37.830445,0 |
| 313 |
</coordinates> |
| 314 |
</LinearRing> |
| 315 |
</outerBoundaryIs> |
| 316 |
</Polygon> |
| 317 |
</Placemark> |
| 318 |
<Placemark> |
| 319 |
<name>Folder object 3 (Path)</name> |
| 320 |
<LineString> |
| 321 |
<tessellate>1</tessellate> |
| 322 |
<coordinates> |
| 323 |
-122.378009,37.830128,0 -122.377885,37.830379,0 |
| 324 |
</coordinates> |
| 325 |
</LineString> |
| 326 |
</Placemark> |
| 327 |
</Folder> |
| 328 |
</kml>""" |
| 329 |
|
| 330 |
k = kml.KML.from_string(doc) |
| 331 |
assert len(k.features) == 1 |
| 332 |
assert len(k.features[0].features) == 3 |
| 333 |
k2 = kml.KML.from_string(k.to_string()) |
| 334 |
assert k.to_string() == k2.to_string() |
| 335 |
|
| 336 |
def test_placemark(self) -> None: |
| 337 |
doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 338 |
<Placemark> |
| 339 |
<name>Simple placemark</name> |
| 340 |
<description>Attached to the ground. Intelligently places itself |
| 341 |
at the height of the underlying terrain.</description> |
| 342 |
<Point> |
| 343 |
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates> |
| 344 |
</Point> |
| 345 |
</Placemark> |
| 346 |
</kml>""" |
| 347 |
|
| 348 |
k = kml.KML.from_string(doc) |
| 349 |
assert len(k.features) == 1 |
| 350 |
assert k.features[0].name == "Simple placemark" |
| 351 |
k2 = kml.KML.from_string(k.to_string()) |
| 352 |
assert k.to_string() == k2.to_string() |
| 353 |
|
| 354 |
def test_polygon(self) -> None: |
| 355 |
doc = """ |
| 356 |
<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 357 |
<Placemark> |
| 358 |
<name>South Africa</name> |
| 359 |
<Polygon> |
| 360 |
<outerBoundaryIs> |
| 361 |
<LinearRing> |
| 362 |
<coordinates> |
| 363 |
31.521,-29.257,0 |
| 364 |
31.326,-29.402,0 |
| 365 |
30.902,-29.91,0 |
| 366 |
30.623,-30.424,0 |
| 367 |
30.056,-31.14,0 |
| 368 |
28.926,-32.172,0 |
| 369 |
28.22,-32.772,0 |
| 370 |
27.465,-33.227,0 |
| 371 |
26.419,-33.615,0 |
| 372 |
25.91,-33.667,0 |
| 373 |
25.781,-33.945,0 |
| 374 |
25.173,-33.797,0 |
| 375 |
24.678,-33.987,0 |
| 376 |
23.594,-33.794,0 |
| 377 |
22.988,-33.916,0 |
| 378 |
22.574,-33.864,0 |
| 379 |
21.543,-34.259,0 |
| 380 |
20.689,-34.417,0 |
| 381 |
20.071,-34.795,0 |
| 382 |
19.616,-34.819,0 |
| 383 |
19.193,-34.463,0 |
| 384 |
18.855,-34.444,0 |
| 385 |
18.425,-33.998,0 |
| 386 |
18.377,-34.137,0 |
| 387 |
18.244,-33.868,0 |
| 388 |
18.25,-33.281,0 |
| 389 |
17.925,-32.611,0 |
| 390 |
18.248,-32.429,0 |
| 391 |
18.222,-31.662,0 |
| 392 |
17.567,-30.726,0 |
| 393 |
17.064,-29.879,0 |
| 394 |
17.063,-29.876,0 |
| 395 |
16.345,-28.577,0 |
| 396 |
16.824,-28.082,0 |
| 397 |
17.219,-28.356,0 |
| 398 |
17.387,-28.784,0 |
| 399 |
17.836,-28.856,0 |
| 400 |
18.465,-29.045,0 |
| 401 |
19.002,-28.972,0 |
| 402 |
19.895,-28.461,0 |
| 403 |
19.896,-24.768,0 |
| 404 |
20.166,-24.918,0 |
| 405 |
20.759,-25.868,0 |
| 406 |
20.666,-26.477,0 |
| 407 |
20.89,-26.829,0 |
| 408 |
21.606,-26.727,0 |
| 409 |
22.106,-26.28,0 |
| 410 |
22.58,-25.979,0 |
| 411 |
22.824,-25.5,0 |
| 412 |
23.312,-25.269,0 |
| 413 |
23.734,-25.39,0 |
| 414 |
24.211,-25.67,0 |
| 415 |
25.025,-25.72,0 |
| 416 |
25.665,-25.487,0 |
| 417 |
25.766,-25.175,0 |
| 418 |
25.942,-24.696,0 |
| 419 |
26.486,-24.616,0 |
| 420 |
26.786,-24.241,0 |
| 421 |
27.119,-23.574,0 |
| 422 |
28.017,-22.828,0 |
| 423 |
29.432,-22.091,0 |
| 424 |
29.839,-22.102,0 |
| 425 |
30.323,-22.272,0 |
| 426 |
30.66,-22.152,0 |
| 427 |
31.191,-22.252,0 |
| 428 |
31.67,-23.659,0 |
| 429 |
31.931,-24.369,0 |
| 430 |
31.752,-25.484,0 |
| 431 |
31.838,-25.843,0 |
| 432 |
31.333,-25.66,0 |
| 433 |
31.044,-25.731,0 |
| 434 |
30.95,-26.023,0 |
| 435 |
30.677,-26.398,0 |
| 436 |
30.686,-26.744,0 |
| 437 |
31.283,-27.286,0 |
| 438 |
31.868,-27.178,0 |
| 439 |
32.072,-26.734,0 |
| 440 |
32.83,-26.742,0 |
| 441 |
32.58,-27.47,0 |
| 442 |
32.462,-28.301,0 |
| 443 |
32.203,-28.752,0 |
| 444 |
31.521,-29.257,0 |
| 445 |
</coordinates> |
| 446 |
</LinearRing> |
| 447 |
</outerBoundaryIs> |
| 448 |
<innerBoundaryIs> |
| 449 |
<LinearRing> |
| 450 |
<coordinates> |
| 451 |
28.978,-28.956,0 |
| 452 |
28.542,-28.648,0 |
| 453 |
28.074,-28.851,0 |
| 454 |
27.533,-29.243,0 |
| 455 |
26.999,-29.876,0 |
| 456 |
27.749,-30.645,0 |
| 457 |
28.107,-30.546,0 |
| 458 |
28.291,-30.226,0 |
| 459 |
28.848,-30.07,0 |
| 460 |
29.018,-29.744,0 |
| 461 |
29.325,-29.257,0 |
| 462 |
28.978,-28.956,0 |
| 463 |
</coordinates> |
| 464 |
</LinearRing> |
| 465 |
</innerBoundaryIs> |
| 466 |
</Polygon> |
| 467 |
</Placemark> |
| 468 |
</kml>""" |
| 469 |
|
| 470 |
k = kml.KML.from_string(doc) |
| 471 |
assert len(k.features) == 1 |
| 472 |
assert isinstance(k.features[0].geometry, Polygon) |
| 473 |
k2 = kml.KML.from_string(k.to_string()) |
| 474 |
assert k.to_string() == k2.to_string() |
| 475 |
|
| 476 |
def test_multipoints(self) -> None: |
| 477 |
doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 478 |
<Placemark id="feat_2"> |
| 479 |
<name>MultiPoint</name> |
| 480 |
<styleUrl>#stylesel_9</styleUrl> |
| 481 |
<MultiGeometry id="geom_0"> |
| 482 |
<Point id="geom_5"> |
| 483 |
<coordinates>16,-35,0.0</coordinates> |
| 484 |
</Point> |
| 485 |
<Point id="geom_6"> |
| 486 |
<coordinates>16,-33,0.0</coordinates> |
| 487 |
</Point> |
| 488 |
<Point id="geom_7"> |
| 489 |
<coordinates>16,-31,0.0</coordinates> |
| 490 |
</Point> |
| 491 |
<Point id="geom_8"> |
| 492 |
<coordinates>16,-29,0.0</coordinates> |
| 493 |
</Point> |
| 494 |
<Point id="geom_9"> |
| 495 |
<coordinates>16,-27,0.0</coordinates> |
| 496 |
</Point> |
| 497 |
<Point id="geom_10"> |
| 498 |
<coordinates>16,-25,0.0</coordinates> |
| 499 |
</Point> |
| 500 |
<Point id="geom_11"> |
| 501 |
<coordinates>16,-23,0.0</coordinates> |
| 502 |
</Point> |
| 503 |
<Point id="geom_12"> |
| 504 |
<coordinates>16,-21,0.0</coordinates> |
| 505 |
</Point> |
| 506 |
<Point id="geom_15"> |
| 507 |
<coordinates>18,-35,0.0</coordinates> |
| 508 |
</Point> |
| 509 |
<Point id="geom_16"> |
| 510 |
<coordinates>18,-33,0.0</coordinates> |
| 511 |
</Point> |
| 512 |
<Point id="geom_17"> |
| 513 |
<coordinates>18,-31,0.0</coordinates> |
| 514 |
</Point> |
| 515 |
<Point id="geom_18"> |
| 516 |
<coordinates>18,-29,0.0</coordinates> |
| 517 |
</Point> |
| 518 |
</MultiGeometry> |
| 519 |
</Placemark></kml>""" |
| 520 |
|
| 521 |
k = kml.KML.from_string(doc) |
| 522 |
assert len(k.features) == 1 |
| 523 |
assert isinstance(k.features[0].geometry, geo.MultiPoint) |
| 524 |
assert len(list(k.features[0].geometry.geoms)) == 12 |
| 525 |
k2 = kml.KML.from_string(k.to_string()) |
| 526 |
assert k.to_string() == k2.to_string() |
| 527 |
|
| 528 |
def test_snippet(self) -> None: |
| 529 |
doc = """<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 530 |
<Placemark> |
| 531 |
<Snippet maxLines="2" >Short Desc</Snippet> |
| 532 |
</Placemark> </kml>""" |
| 533 |
|
| 534 |
k = kml.KML.from_string(doc) |
| 535 |
assert k.features[0].snippet.text == "Short Desc" |
| 536 |
assert k.features[0].snippet.max_lines == 2 |
| 537 |
k.features[0].snippet = features.Snippet( |
| 538 |
text="Another Snippet", |
| 539 |
max_lines=3, |
| 540 |
) |
| 541 |
assert 'maxLines="3"' in k.to_string() |
| 542 |
k.features[0].snippet = features.Snippet(text="Another Snippet") |
| 543 |
assert "maxLines" not in k.to_string() |
| 544 |
assert "Another Snippet" in k.to_string() |
| 545 |
k.features[0].snippet = features.Snippet(text="Different Snippet") |
| 546 |
assert "maxLines" not in k.to_string() |
| 547 |
assert "Different Snippet" in k.to_string() |
| 548 |
k.features[0].snippet = features.Snippet(text="", max_lines=4) |
| 549 |
assert "Snippet" not in k.to_string() |
| 550 |
|
| 551 |
def test_address(self) -> None: |
| 552 |
doc = Document.from_string( |
| 553 |
""" |
| 554 |
<kml:Document xmlns:kml="http://www.opengis.net/kml/2.2" id="pm-id"> |
| 555 |
<kml:name>pm-name</kml:name> |
| 556 |
<kml:description>pm-description</kml:description> |
| 557 |
<kml:visibility>1</kml:visibility> |
| 558 |
<kml:address>1600 Amphitheatre Parkway,...</kml:address> |
| 559 |
</kml:Document> |
| 560 |
""", |
| 561 |
) |
| 562 |
|
| 563 |
doc2 = Document.from_string(doc.to_string()) |
| 564 |
assert doc.to_string() == doc2.to_string() |
| 565 |
|
| 566 |
def test_phone_number(self) -> None: |
| 567 |
doc = Document.from_string( |
| 568 |
""" |
| 569 |
<kml:Document xmlns:kml="http://www.opengis.net/kml/2.2" id="pm-id"> |
| 570 |
<kml:name>pm-name</kml:name> |
| 571 |
<kml:description>pm-description</kml:description> |
| 572 |
<kml:visibility>1</kml:visibility> |
| 573 |
<kml:phoneNumber>+1 234 567 8901</kml:phoneNumber> |
| 574 |
</kml:Document> |
| 575 |
""", |
| 576 |
) |
| 577 |
|
| 578 |
doc2 = Document.from_string(doc.to_string()) |
| 579 |
assert doc.to_string() == doc2.to_string() |
| 580 |
|
| 581 |
def test_groundoverlay(self) -> None: |
| 582 |
doc = kml.KML.from_string( |
| 583 |
""" |
| 584 |
<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 585 |
<Folder> |
| 586 |
<name>Ground Overlays</name> |
| 587 |
<description>Examples of ground overlays</description> |
| 588 |
<GroundOverlay> |
| 589 |
<name>Large-scale overlay on terrain</name> |
| 590 |
<description>Overlay shows Mount Etna erupting |
| 591 |
on July 13th, 2001.</description> |
| 592 |
<Icon> |
| 593 |
<href>http://developers.google.com/kml/etna.jpg</href> |
| 594 |
</Icon> |
| 595 |
<LatLonBox> |
| 596 |
<north>37.91904192681665</north> |
| 597 |
<south>37.46543388598137</south> |
| 598 |
<east>15.35832653742206</east> |
| 599 |
<west>14.60128369746704</west> |
| 600 |
<rotation>-0.1556640799496235</rotation> |
| 601 |
</LatLonBox> |
| 602 |
</GroundOverlay> |
| 603 |
</Folder> |
| 604 |
</kml> |
| 605 |
""", |
| 606 |
) |
| 607 |
|
| 608 |
doc2 = kml.KML.from_string(doc.to_string()) |
| 609 |
assert doc.to_string() == doc2.to_string() |
| 610 |
|
| 611 |
|
| 612 |
class TestLxml(Lxml, TestStdLibrary): |
| 613 |
"""Test with lxml.""" |
| 614 |
|
| 615 |
|
| 616 |
class TestLxmlParseKML(Lxml, TestParseKML): |
| 617 |
"""Test with Lxml.""" |
| 618 |
|
| 619 |
def test_from_string_with_unbound_prefix_strict(self) -> None: |
| 620 |
doc = io.StringIO( |
| 621 |
'<kml xmlns="http://www.opengis.net/kml/2.2">' |
| 622 |
"<Placemark><ExtendedData>" |
| 623 |
"<lc:attachment>image.png</lc:attachment>" |
| 624 |
"</ExtendedData>" |
| 625 |
"</Placemark> </kml>", |
| 626 |
) |
| 627 |
|
| 628 |
with pytest.raises( |
| 629 |
AssertionError, |
| 630 |
match=r"^Element 'lc:attachment': This element is not expected.", |
| 631 |
): |
| 632 |
kml.KML.parse(doc, ns="{http://www.opengis.net/kml/2.2}") |
| 633 |
|
| 634 |
def test_from_string_with_unbound_prefix_relaxed(self) -> None: |
| 635 |
doc = io.StringIO( |
| 636 |
'<kml xmlns="http://www.opengis.net/kml/2.2">' |
| 637 |
"<Placemark><ExtendedData>" |
| 638 |
"<lc:attachment>image.png</lc:attachment>" |
| 639 |
"</ExtendedData>" |
| 640 |
"</Placemark> </kml>", |
| 641 |
) |
| 642 |
k = kml.KML.parse(doc, strict=False) |
| 643 |
assert len(k.features) == 1 |
| 644 |
assert isinstance(k.features[0], features.Placemark) |
| 645 |
|
| 646 |
def test_from_string_with_unbound_prefix_strict_no_validate(self) -> None: |
| 647 |
doc = io.StringIO( |
| 648 |
'<kml xmlns="http://www.opengis.net/kml/2.2">' |
| 649 |
"<Placemark><ExtendedData>" |
| 650 |
"<lc:attachment>image.png</lc:attachment>" |
| 651 |
"</ExtendedData>" |
| 652 |
"</Placemark> </kml>", |
| 653 |
) |
| 654 |
k = kml.KML.parse(doc, ns="{http://www.opengis.net/kml/2.2}", validate=False) |
| 655 |
assert len(k.features) == 1 |
| 656 |
assert isinstance(k.features[0], features.Placemark) |
| 657 |
|
| 658 |
def test_from_string_no_namespace(self) -> None: |
| 659 |
doc = io.StringIO( |
| 660 |
"<kml><Placemark><ExtendedData></ExtendedData></Placemark></kml>", |
| 661 |
) |
| 662 |
|
| 663 |
k = kml.KML.parse(doc, ns="", strict=False) |
| 664 |
|
| 665 |
assert len(k.features) == 0 |
| 666 |
|
| 667 |
|
| 668 |
class TestWriteKMLLxmk(Lxml, TestWriteKML): |
| 669 |
"""Test with lxml.""" |
| 670 |
|
| 671 |
|
| 672 |
class TestKmlFromStringLxml(Lxml, TestKmlFromString): |
| 673 |
"""Test with lxml.""" |