-
Notifications
You must be signed in to change notification settings - Fork 1
/
kml_add_index.py
85 lines (73 loc) · 2.64 KB
/
kml_add_index.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Add "begin" timespan attribute to KML/KMZ polygons based on placemark name
Usage
python KmlTimeSpan.py --help
python KmlTimeSpan.py <kml_or_kmz_file>
"""
import argparse
import os
import xml.dom.minidom
import zipfile
def add_timespan(infile, suffix='_timespan'):
"""Read a KMZ file and add a timespan based on the placemark name"""
# read input kml/kmz data
# parse raw kml as document object model
rawkml = read_kmz(infile)
dom = xml.dom.minidom.parseString(rawkml)
# add begin timespan to each placemark element
for i, elem in enumerate(dom.getElementsByTagName('Placemark')):
# begin = elem.getElementsByTagName("name")[0].childNodes[0].data
elem.appendChild(create_index(dom, i))
# write output kmz file
write_kmz(dom, infile[:-4] + suffix + '.kml')
def create_index(dom, index):
name = dom.createElement('name')
name.appendChild(dom.createTextNode(str(index)))
return name
#
# def create_span(dom, begin=None, end=None):
# """ return a time span kml code block
# not specifying the begin/end will omit that attribute (and the file will work fine)
#
# <TimeSpan>
# <begin>
# 1984
# </begin>
# <end>
# 1996
# </end>
# </TimeSpan>
# """
# span = dom.createElement('TimeSpan')
# if begin:
# b = dom.createElement("begin")
# b.appendChild(dom.createTextNode(begin))
# span.appendChild(b)
#
# if end:
# e = dom.createElement("end")
# e.appendChild(dom.createTextNode(end))
# span.appendChild(e)
#
# return span
def read_kmz(infile):
"""read raw kml/kmz file"""
if infile.upper().endswith('.KML'):
with open(infile, "r") as f:
return f.read()
if infile.upper().endswith('.KMZ'):
with zipfile.ZipFile(infile, "r") as f:
return f.read(f.namelist()[0])
def write_kmz(dom, outkmz, arcname='doc.kml'):
"""write pretty xml to doc.kml then zip to kmz"""
kml_temp = os.path.join(os.path.dirname(outkmz), arcname)
with open(outkmz, "w") as f:
f.write(dom.toprettyxml())
# with zipfile.ZipFile(outkmz, "w") as f:
# f.write(kml_temp, arcname, zipfile.ZIP_STORED)
#
# os.remove(kml_temp)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description = "Add 'begin' timespan attribute to KML polygons based on numbers in the placemark name")
parser.add_argument('infile', help="Input kml or kmz file with placemark names as year (YYYY).")
parser.add_argument('suffix', help="suffix added to output file name", default='_timespan')
add_timespan(**vars(parser.parse_args()))