-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditKML.py
More file actions
78 lines (59 loc) · 2.02 KB
/
Copy patheditKML.py
File metadata and controls
78 lines (59 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from xml.etree import ElementTree
import sys
style_tidal = """
<Style id="tidal">
<LineStyle>
<color>800000ff</color>
</LineStyle>
<PolyStyle>
<color>800000ff</color>
<fill>1</fill>
</PolyStyle>
</Style>
"""
style_flood = """
<Style id="flood">
<LineStyle>
<color>80ff0000</color>
</LineStyle>
<PolyStyle>
<color>80ff0000</color>
<fill>1</fill>
</PolyStyle>
</Style>
"""
tree = ElementTree.parse(sys.argv[1])
root = tree.getroot()
document = root.findall('''.//*[@id='root_doc']''')[0]
ElementTree.register_namespace('kml', 'http://www.opengis.net/kml/2.2')
floodStyle = ElementTree.fromstring(style_flood)
tidalStyle = ElementTree.fromstring(style_tidal)
document.insert(0, floodStyle)
document.insert(0, tidalStyle)
namespaces = {'kml': 'http://www.opengis.net/kml/2.2'}
xpath = ".//kml:SimpleData[@name='DN']"
for placeMark in root.findall(".//kml:Placemark", namespaces):
simpleDataList = placeMark.findall(xpath, namespaces)
if len(simpleDataList) > 0:
simpleData = simpleDataList[0]
if simpleData.text == "1":
element = ElementTree.Element("styleUrl")
element.text = "#tidal"
placeMark.insert(0, element)
element = ElementTree.Element("name")
element.text = "Tide level"
placeMark.insert(0, element)
element = ElementTree.Element("description")
element.text = "Increased tide level from the given rise in sea level."
placeMark.insert(0, element)
elif simpleData.text == "255":
element = ElementTree.Element("styleUrl")
element.text = "#flood"
placeMark.insert(0, element)
element = ElementTree.Element("name")
element.text = "Flood level"
placeMark.insert(0, element)
element = ElementTree.Element("description")
element.text = "Area covered by rise in sea level."
placeMark.insert(0, element)
tree.write(sys.argv[1])