fix typo
[JPSSData.git] / contour2kml.py
blobb8909262a439c6d8f86a932c23f43c75d6590188
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 <fill>0</fill>
29 </PolyStyle>""" % (c['PolyStyle']['color'],c['PolyStyle'].get('colorMode','random')))
30 kml.write("\n</Style>")
31 folder_name = data.get('folder_name',name)
32 kml.write("""
33 <Folder><name>%s</name>
34 <open>1</open>""" % folder_name)
35 for idx, c in enumerate(data['contours']):
36 time_begin = c['time_begin']
37 kml.write("""
38 <Folder id="layer 0">
39 <name>%s</name>
40 <Placemark>
41 <TimeSpan><begin>%s</begin></TimeSpan>
42 <name>%s</name>
43 <styleUrl>ColorStyle%s</styleUrl>
44 <MultiGeometry>""" % (
45 time_begin, time_begin, time_begin,idx))
46 for polygon in c['polygons']:
47 kml.write("""
48 <Polygon>
49 <outerBoundaryIs>
50 <LinearRing>
51 <coordinates>""")
52 for segment in polygon:
53 kml.write("\n%s,%s,0" % tuple(segment))
54 kml.write("""
55 </coordinates>
56 </LinearRing>
57 </outerBoundaryIs>
58 </Polygon>""")
59 kml.write("""
60 </MultiGeometry>
61 </Placemark>
62 </Folder>""")
63 kml.write("""
64 </Folder>
65 </Document>
66 </kml>
67 """ )
69 if __name__ == '__main__':
70 print 'Running a self-test case'
71 data={
72 'name':'selftest.kmz',
73 'folder_name':'Test Perimeters',
74 'contours': [
75 {'text':'2011-06-28T23:43:00-06:00',
76 'LineStyle':{
77 'color':'ff081388',
78 'width':'2.5',
80 'PolyStyle':{
81 'color':'66000086',
82 'colorMode':'random'
84 'time_begin':'2011-06-28T23:43:00-06:00',
85 'polygons':[
87 [-106.4,35.0],
88 [-106.4,35.9],
89 [-106.0,35.9],
90 [-106.0,35.0],
91 [-106.4,35.0]
92 ],[
93 [-105.4,35.0],
94 [-105.4,35.9],
95 [-105.0,35.9],
96 [-105.4,35.0]
100 {'text':'2011-06-29T23:43:00-06:00',
101 'LineStyle':{
102 'color':'ff051100',
103 'width':'2.5',
105 'PolyStyle':{
106 'color':'66000086',
107 'colorMode':'random'
109 'time_begin':'2011-06-29T23:43:00-06:00',
110 'polygons':[
112 [-106.3,35.0],
113 [-106.3,35.99],
114 [-106.0,35.99],
115 [-106.0,35.0],
116 [-106.4,35.0]
118 [-105.4,35.0],
119 [-105.4,35.9],
120 [-105.0,35.9],
121 [-105.4,35.0]
127 print ('data: ' + json.dumps(data,indent=4, separators=(',', ': ')))
128 contour2kml(data,'selftest.kml')
129 print 'open file selftest.kml in Google Earth'