| 1 |
|
| 2 |
from fastkml import kml |
| 3 |
|
| 4 |
|
| 5 |
doc = """<?xml version="1.0" encoding="UTF-8"?> |
| 6 |
<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 7 |
<Document> |
| 8 |
<name>Document.kml</name> |
| 9 |
<open>1</open> |
| 10 |
<Style id="exampleStyleDocument"> |
| 11 |
<LabelStyle> |
| 12 |
<color>ff0000cc</color> |
| 13 |
</LabelStyle> |
| 14 |
</Style> |
| 15 |
<Placemark> |
| 16 |
<name>Document Feature 1</name> |
| 17 |
<styleUrl>#exampleStyleDocument</styleUrl> |
| 18 |
<Point> |
| 19 |
<coordinates>-122.371,37.816,0</coordinates> |
| 20 |
</Point> |
| 21 |
</Placemark> |
| 22 |
<Placemark> |
| 23 |
<name>Document Feature 2</name> |
| 24 |
<styleUrl>#exampleStyleDocument</styleUrl> |
| 25 |
<Point> |
| 26 |
<coordinates>-122.370,37.817,0</coordinates> |
| 27 |
</Point> |
| 28 |
</Placemark> |
| 29 |
</Document> |
| 30 |
</kml>""" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
k = kml.KML.from_string(doc.encode("utf-8")) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
features = k.features |
| 41 |
print(len(features)) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
print(features[0].features) |
| 46 |
|
| 47 |
f2 = features[0].features |
| 48 |
print(len(f2)) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
print(f2[0]) |
| 53 |
print(f2[0].description) |
| 54 |
print(f2[0].name) |
| 55 |
|
| 56 |
|
| 57 |
print(f2[1].name) |
| 58 |
f2[1].name = "ANOTHER NAME" |
| 59 |
print(f2[1].name) |
| 60 |
|
| 61 |
|
| 62 |
print(k.to_string(prettyprint=True)) |
| 63 |
|
| 64 |
expected = """<kml:kml xmlns:ns0="http://www.opengis.net/kml/2.2"> |
| 65 |
<kml:Document> |
| 66 |
<kml:name>Document.kml</kml:name> |
| 67 |
<kml:visibility>1</kml:visibility> |
| 68 |
<kml:open>1</kml:open> |
| 69 |
<kml:Style id="exampleStyleDocument"> |
| 70 |
<kml:LabelStyle> |
| 71 |
<kml:color>ff0000cc</kml:color> |
| 72 |
<kml:scale>1.0</kml:scale> |
| 73 |
</kml:LabelStyle> |
| 74 |
</kml:Style> |
| 75 |
<kml:Placemark> |
| 76 |
<kml:name>Document Feature 1</kml:name> |
| 77 |
<kml:visibility>1</kml:visibility> |
| 78 |
<kml:open>0</kml:open> |
| 79 |
<kml:Point> |
| 80 |
<kml:coordinates>-122.371000,37.816000,0.000000</kml:coordinates> |
| 81 |
</kml:Point> |
| 82 |
</kml:Placemark> |
| 83 |
<kml:Placemark> |
| 84 |
<kml:name>ANOTHER NAME</kml:name> |
| 85 |
<kml:visibility>1</kml:visibility> |
| 86 |
<kml:open>0</kml:open> |
| 87 |
<kml:Point> |
| 88 |
<kml:coordinates>-122.370000,37.817000,0.000000</kml:coordinates> |
| 89 |
</kml:Point> |
| 90 |
</kml:Placemark> |
| 91 |
</kml:Document> |
| 92 |
</kml:kml>""" |