fixing last identation problem
[JPSSData.git] / contour2kml.py
blob40d1f32f2ad0fa19bbdbcf0fc022e6fe740be73f
1 import json, sys, math
3 def contour2kml(data,kml_path):
4 with open(kml_path,'w') as kml:
5 name = data.get('name','No Name')
6 kml.write("""<?xml version="1.0" encoding="UTF-8"?>\n""")
7 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""")
8 kml.write("""<Document><name>%s</name>\n""" % name)
9 # write all styles first
10 for idx, c in enumerate(data['contours']):
11 kml.write("""<Style id="ColorStyle%s">""" % idx)
12 if 'text' in c and c['text'] is not None:
13 kml.write("""
14 <BalloonStyle>
15 <text>%s</text>
16 </BalloonStyle>""" % c['text'])
17 if 'LineStyle' in c and c['LineStyle'] is not None:
18 kml.write("""
19 <LineStyle>
20 <color>%s</color>
21 <width>%s</width>
22 </LineStyle>""" % (c['LineStyle']['color'],c['LineStyle'].get('width',3)))
23 if 'PolyStyle' in c and c['PolyStyle'] is not None:
24 kml.write("""
25 <PolyStyle>
26 <color>%s</color>
27 <colorMode>%s</colorMode>
28 </PolyStyle>""" % (c['PolyStyle']['color'],c['PolyStyle'].get('colorMode','random')))
29 kml.write("\n</Style>")
30 folder_name = data.get('folder_name',name)
31 kml.write("""
32 <Folder><name>%s</name>
33 <open>1</open>""" % folder_name)
34 for idx, c in enumerate(data['contours']):
35 time_begin = c['time_begin']
36 kml.write("""
37 <Folder id="layer 0">
38 <name>%s</name>
39 <Placemark>
40 <TimeSpan><begin>%s</begin></TimeSpan>
41 <name>%s</name>
42 <styleUrl>ColorStyle%s</styleUrl>
43 <MultiGeometry>""" % (
44 time_begin, time_begin, time_begin,idx))
45 for polygon in c['polygons']:
46 kml.write("""
47 <Polygon>
48 <outerBoundaryIs>
49 <LinearRing>
50 <coordinates>""")
51 for segment in polygon:
52 kml.write("\n%s,%s,0" % tuple(segment))
53 kml.write("""
54 </coordinates>
55 </LinearRing>
56 </outerBoundaryIs>
57 </Polygon>""")
58 kml.write("""
59 </MultiGeometry>
60 </Placemark>
61 </Folder>""")
62 kml.write("""
63 </Folder>
64 </Document>
65 </kml>
66 """ )
68 if __name__ == '__main__':
69 print 'Running a self-test case'
70 data={
71 'name':'selftest.kmz',
72 'folder_name':'Test Perimeters',
73 'contours': [
74 {'text':'2011-06-28T23:43:00-06:00',
75 'LineStyle':{
76 'color':'ff081388',
77 'width':'2.5',
79 'PolyStyle':{
80 'color':'66000086',
81 'colorMode':'random'
83 'time_begin':'2011-06-28T23:43:00-06:00',
84 'polygons':[
86 [-106.4,35.0],
87 [-106.4,35.9],
88 [-106.0,35.9],
89 [-106.0,35.0],
90 [-106.4,35.0]
91 ],[
92 [-105.4,35.0],
93 [-105.4,35.9],
94 [-105.0,35.9],
95 [-105.4,35.0]
99 {'text':'2011-06-29T23:43:00-06:00',
100 'LineStyle':{
101 'color':'ff051100',
102 'width':'2.5',
104 'PolyStyle':{
105 'color':'66000086',
106 'colorMode':'random'
108 'time_begin':'2011-06-29T23:43:00-06:00',
109 'polygons':[
111 [-106.3,35.0],
112 [-106.3,35.99],
113 [-106.0,35.99],
114 [-106.0,35.0],
115 [-106.4,35.0]
117 [-105.4,35.0],
118 [-105.4,35.9],
119 [-105.0,35.9],
120 [-105.4,35.0]
126 print ('data: ' + json.dumps(data,indent=4, separators=(',', ': ')))
127 contour2kml(data,'selftest.kml')
128 print 'open file selftest.kml in Google Earth'