-
Notifications
You must be signed in to change notification settings - Fork 1
/
contour2kml.py
128 lines (126 loc) · 4.85 KB
/
contour2kml.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
import json, sys, math
def contour2kml(data,kml_path):
with open(kml_path,'w') as kml:
name = data.get('name','No Name')
kml.write("""<?xml version="1.0" encoding="UTF-8"?>\n""")
kml.write("""<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">\n""")
kml.write("""<Document><name>%s</name>\n""" % name)
# write all styles first
for idx, c in enumerate(data['contours']):
kml.write("""<Style id="ColorStyle%s">""" % idx)
if 'text' in c and c['text'] is not None:
kml.write("""
<BalloonStyle>
<text>%s</text>
</BalloonStyle>""" % c['text'])
if 'LineStyle' in c and c['LineStyle'] is not None:
kml.write("""
<LineStyle>
<color>%s</color>
<width>%s</width>
</LineStyle>""" % (c['LineStyle']['color'],c['LineStyle'].get('width',3)))
if 'PolyStyle' in c and c['PolyStyle'] is not None:
kml.write("""
<PolyStyle>
<color>%s</color>
<colorMode>%s</colorMode>
</PolyStyle>""" % (c['PolyStyle']['color'],c['PolyStyle'].get('colorMode','random')))
kml.write("\n</Style>")
folder_name = data.get('folder_name',name)
kml.write("""
<Folder><name>%s</name>
<open>1</open>""" % folder_name)
for idx, c in enumerate(data['contours']):
time_begin = c['time_begin']
kml.write("""
<Folder id="layer 0">
<name>%s</name>
<Placemark>
<TimeSpan><begin>%s</begin></TimeSpan>
<name>%s</name>
<styleUrl>ColorStyle%s</styleUrl>
<MultiGeometry>""" % (
time_begin, time_begin, time_begin,idx))
for polygon in c['polygons']:
kml.write("""
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>""")
for segment in polygon:
kml.write("\n%s,%s,0" % tuple(segment))
kml.write("""
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>""")
kml.write("""
</MultiGeometry>
</Placemark>
</Folder>""")
kml.write("""
</Folder>
</Document>
</kml>
""" )
if __name__ == '__main__':
print 'Running a self-test case'
data={
'name':'selftest.kmz',
'folder_name':'Test Perimeters',
'contours': [
{'text':'2011-06-28T23:43:00-06:00',
'LineStyle':{
'color':'ff081388',
'width':'2.5',
},
'PolyStyle':{
'color':'66000086',
'colorMode':'random'
},
'time_begin':'2011-06-28T23:43:00-06:00',
'polygons':[
[
[-106.4,35.0],
[-106.4,35.9],
[-106.0,35.9],
[-106.0,35.0],
[-106.4,35.0]
],[
[-105.4,35.0],
[-105.4,35.9],
[-105.0,35.9],
[-105.4,35.0]
]
]
},
{'text':'2011-06-29T23:43:00-06:00',
'LineStyle':{
'color':'ff051100',
'width':'2.5',
},
'PolyStyle':{
'color':'66000086',
'colorMode':'random'
},
'time_begin':'2011-06-29T23:43:00-06:00',
'polygons':[
[
[-106.3,35.0],
[-106.3,35.99],
[-106.0,35.99],
[-106.0,35.0],
[-106.4,35.0]
],[
[-105.4,35.0],
[-105.4,35.9],
[-105.0,35.9],
[-105.4,35.0]
]
]
},
]
}
print ('data: ' + json.dumps(data,indent=4, separators=(',', ': ')))
contour2kml(data,'selftest.kml')
print 'open file selftest.kml in Google Earth'