-
Notifications
You must be signed in to change notification settings - Fork 0
/
kml2csv.py
141 lines (124 loc) · 3.71 KB
/
kml2csv.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import xml.dom.minidom
import sys
def traverseTree(document, rows):
for child in document.childNodes:
if child.nodeType == xml.dom.Node.ELEMENT_NODE:
if child.tagName == 'Folder' :
traverseFolder1(child,rows)
def traverseFolder1(document,rows):
for child in document.childNodes:
if child.nodeType == xml.dom.Node.ELEMENT_NODE:
if child.tagName == 'Folder' :
traverseFolder2(child,rows)
def traverseFolder2(document,rows):
for child in document.childNodes:
if child.nodeType == xml.dom.Node.ELEMENT_NODE:
if child.tagName == 'Placemark' :
traversePlacemark(child,rows)
def traversePlacemark(document,rows):
dict = {}
for child in document.childNodes:
if child.nodeType == xml.dom.Node.ELEMENT_NODE:
if child.tagName == 'TimeSpan' :
traverseTimeSpan(child,dict)
if child.tagName == 'Point' :
traversePoint(child,dict)
if child.tagName == 'description' :
traverseDescription(child,dict)
#print 'dict' , dict
rows.append(dict)
def unescape(s):
s = s.replace("<", "<")
s = s.replace(">", ">")
# this has to be last:
s = s.replace("&", "&")
return s
def traverseDescription(description,dict):
desc = unescape(getText(description)) + '</td></tr></table>'
#print 'description', desc
descXml = xml.dom.minidom.parseString(desc)
traverseTable(descXml.documentElement,dict)
def traverseTable(table,dict):
#print 'table' , table.tagName
for child in table.childNodes:
if child.nodeType == xml.dom.Node.ELEMENT_NODE:
if child.tagName == 'tr' :
traverseTR(child,dict)
def traverseTR(tr,dict):
key=""
value=""
first=True
for child in tr.childNodes:
if child.nodeType == xml.dom.Node.ELEMENT_NODE:
if child.tagName == 'td' :
if first:
key = getText(child)
else:
value = getText(child)
first = False
if key != '' :
#print 'key', key, 'value', value
dict[key]=value
def traverseTimeSpan(document,dict):
for child in document.childNodes:
if child.nodeType == xml.dom.Node.ELEMENT_NODE:
if child.tagName == 'begin' :
#print 'begin:' , getText(child)
dict['begin']=getText(child)
def traversePoint(document,dict):
for child in document.childNodes:
if child.nodeType == xml.dom.Node.ELEMENT_NODE:
if child.tagName == 'coordinates' :
#print 'coordinates:' , getText(child)
dict['coordinates']=getText(child)
def getText(element):
rc = []
for node in element.childNodes:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc).strip()
def createCSV(rows,f):
headerExist = {}
for row in rows:
for key,value in row.items():
headerExist[key] = True
#print headers
headerExist["begin"] = False
headerExist["coordinates"] = False
headers = ["begin","coordinates"]
line = '"begin";"coordinates'
for key,value in headerExist.items():
if value:
line +='";"'
line += key
headers.append(key)
line += '"\n'
f.write(line)
for row in rows:
line = '"'
first = True
for header in headers:
value = row[header]
if value is None:
value = ''
if not first :
line+='";"'
first = False
line += value
line += '"\n'
f.write(line)
if __name__ == '__main__':
if len(sys.argv) == 1:
print "Please specify the input kml : kml2csv <input.kml> [<output.csv>]"
sys.exit(-1)
filename=sys.argv[1]
if len(sys.argv) > 2 :
outputfile = sys.argv[2]
else:
outputfile = sys.argv[1] + '.csv'
rows = []
dom = xml.dom.minidom.parse(filename)
traverseTree(dom.documentElement,rows)
f = open(outputfile,'w')
createCSV(rows,f)
f.close()