adding requests to conda install docs
[JPSSData.git] / contour2kml.py
blobcff51388eb188c33734246cfb32066b6d40c9916
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 <LineString>
49 <coordinates>""")
50 for segment in polygon:
51 kml.write("\n%s,%s,0" % tuple(segment))
52 kml.write("""
53 </coordinates>
54 </LineString>""")
55 kml.write("""
56 </MultiGeometry>
57 </Placemark>
58 </Folder>""")
59 kml.write("""
60 </Folder>
61 </Document>
62 </kml>
63 """ )
65 if __name__ == '__main__':
66 print 'Running a self-test case'
67 data={
68 'name':'selftest.kmz',
69 'folder_name':'Test Perimeters',
70 'contours': [
71 {'text':'2011-06-28T23:43:00-06:00',
72 'LineStyle':{
73 'color':'ff081388',
74 'width':'2.5',
76 'PolyStyle':{
77 'color':'66000086',
78 'colorMode':'random'
80 'time_begin':'2011-06-28T23:43:00-06:00',
81 'polygons':[
83 [-106.4,35.0],
84 [-106.4,35.9],
85 [-106.0,35.9],
86 [-106.0,35.0],
87 [-106.4,35.0]
88 ],[
89 [-105.4,35.0],
90 [-105.4,35.9],
91 [-105.0,35.9],
92 [-105.4,35.0]
96 {'text':'2011-06-29T23:43:00-06:00',
97 'LineStyle':{
98 'color':'ff051100',
99 'width':'2.5',
101 'PolyStyle':{
102 'color':'66000086',
103 'colorMode':'random'
105 'time_begin':'2011-06-29T23:43:00-06:00',
106 'polygons':[
108 [-106.3,35.0],
109 [-106.3,35.99],
110 [-106.0,35.99],
111 [-106.0,35.0],
112 [-106.4,35.0]
114 [-105.4,35.0],
115 [-105.4,35.9],
116 [-105.0,35.9],
117 [-105.4,35.0]
123 print ('data: ' + json.dumps(data,indent=4, separators=(',', ': ')))
124 contour2kml(data,'selftest.kml')
125 print 'open file selftest.kml in Google Earth'