fancier kml output using 2.2 <LookAt>
[myplc.git] / plc-kml.py
1 #!/usr/bin/env plcsh
2
3 # this script generates a kml file, located under the default location below
4 # you should crontab this job from your myplc image
5 # you can then use the googlemap.js javascript for creating your applet
6 # more on this at http://svn.planet-lab.org/wiki/GooglemapSetup
7
8 # kml reference can be found at
9 # http://code.google.com/apis/kml/documentation/kmlreference.html
10 #
11
12 import sys
13
14 default_output       = "/var/www/html/sites/sites.kml"
15 default_local_icon   = "sites/google-local.png"
16 default_foreign_icon = "sites/google-foreign.png"
17
18 class KmlMap:
19
20     def __init__ (self,outputname,options):
21         self.outputname=outputname
22         self.options=options
23
24     def open (self):
25         self.output = open(self.outputname,"w")
26
27     def close (self):
28         if self.output:
29             self.output.close()
30         self.output = None
31
32     def write(self,string):
33         self.output.write(string.encode("UTF-8"))
34
35     @staticmethod
36     def site_compare (s1,s2):
37         n1=s1['name']
38         n2=s2['name']
39         if n1<n2:
40             return -1
41         elif n1>n2:
42             return 1
43         else:
44             return 0
45
46     def refresh (self):
47         self.open()
48         self.write_header()
49         # cache peers 
50         peers = GetPeers({},['peer_id','peername'])
51         all_sites = GetSites({'enabled':True,'is_public':True})
52         all_sites.sort(KmlMap.site_compare)
53         for site in all_sites:
54             self.write_site(site,peers)
55         self.write_footer()
56         self.close()
57
58 # initial placement is for europe - dunno how to tune that yet
59     def write_header (self):
60         self.write("""<?xml version="1.0" encoding="UTF-8"?>
61 <kml xmlns="http://earth.google.com/kml/2.2">
62 <Document>
63 <name> PlanetLab Sites </name>
64 <LookAt>
65 <longitude>9.180821112577378</longitude>
66 <latitude>44.43275321178062</latitude>
67 <altitude>0</altitude>
68 <range>5782133.196489797</range>
69 <tilt>0</tilt>
70 <heading>-7.767386340832667</heading>
71 </LookAt>
72 <description> All the sites known to the PlanetLab testbed. </description>
73 """)
74
75     def write_footer (self):
76         self.write("""</Document></kml>
77 """)
78
79     def peer_name (self,site, peers):
80         if not site['peer_id']:
81             return "local"
82         for peer in peers:
83             if peer['peer_id'] == site['peer_id']:
84                 return peer['peername']
85
86     def write_site (self, site, peers):
87         # discard sites with missing lat or lon
88         if not site['latitude'] or not site['longitude']:
89             return
90         # discard sites with no nodes 
91         if len(site['node_ids']) == 0:
92             return
93
94         site_id=site['site_id']
95         name=site['name']
96         nb_nodes=len(site['node_ids'])
97         nb_slices=len(site['slice_ids'])
98         latitude=site['latitude']
99         longitude=site['longitude']
100         apiurl='https://%s:443'%api.config.PLC_WWW_HOST
101         baseurl='http://%s'%api.config.PLC_WWW_HOST
102         peer_id=site['peer_id']
103
104         # open description
105         description='<ul>'
106         # Name and URL
107         description += '<li>'
108         description += '<a href="%(apiurl)s/db/sites/index.php?id=%(site_id)d"> Site page </a>'%locals()
109         if site['url']:
110             site_url=site['url']
111             description += ' -- <a href="%(site_url)s"> %(site_url)s </a>'%locals()
112         description += '</li>'
113         # NODES
114         if nb_nodes:
115             description += '<li>'
116             description += '<a href="%(apiurl)s/db/nodes/index.php?site_id=%(site_id)d">%(nb_nodes)d node(s)</a>'%locals()
117             description += '<a href="%(apiurl)s/db/nodes/comon.php?site_id=%(site_id)d"> (in Comon)</a>'%locals()
118             description += '</li>'
119         else:
120             description += '<li>No node</li>'
121         #SLICES
122         if nb_slices:
123             description += '<li><a href="%(apiurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slice(s)</a></li>'%locals()
124         else:
125             description += '<li>No slice</li>'
126         # PEER
127         if peer_id:
128             peername = self.peer_name(site,peers)
129             description += '<li>'
130             description += '<a href="%(apiurl)s/db/peers/index.php?id=%(peer_id)d">At peer %(peername)s</a>'%locals()
131             description += '</li>'
132         # close description
133         description +='</ul>'
134
135         # STYLE
136         if self.options.use_google_icons:
137             if not peer_id:
138                 # local sites
139                 iconfile="palette-4.png"
140                 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
141             else:
142                 # remote
143                 iconfile="palette-3.png"
144                 xyspec="<x>160</x><y>0</y><w>32</w><h>32</h>"
145             iconurl="root://icons/%(iconfile)s"%locals()
146         else:
147             if not peer_id:
148                 iconfile=self.options.local_icon
149             else:
150                 iconfile=self.options.foreign_icon
151             iconurl="%(baseurl)s/%(iconfile)s"%locals()
152             xyspec=""
153
154         iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
155
156         # set the camera 50km high
157         template="""<Placemark>
158 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
159 <name><![CDATA[%(name)s]]></name>
160 <LookAt>
161   <latitude>%(latitude)f</latitude>
162   <longitude>%(longitude)f</longitude>
163   <altitude>0</altitude>
164   <altitudeMode>relativeToGround</altitudeMode>              
165   <range>50000.</range> 
166 </LookAt>
167 <description><![CDATA[%(description)s]]></description>
168 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
169 </Placemark>
170 """
171         self.write(template%locals())
172
173 def main () :
174     from optparse import OptionParser
175     usage = "Usage %prog [plcsh-options] [ -- options ]"
176     parser = OptionParser (usage=usage)
177
178     parser.add_option("-o","--output",action="store",dest="output",
179                       default=default_output,
180                       help="output file - default is %s"%default_output)
181     parser.add_option("-c","--custom",action="store_false",dest="use_google_icons",
182                       default=True,
183                       help="use locally customized icons rather than the google-provided defaults")
184     parser.add_option("-l","--local",action="store",dest="local_icon",
185                       default=default_local_icon,
186                       help="set icon url to use for local sites marker -- default is %s"%default_local_icon)
187     parser.add_option("-f","--foreign",action="store",dest="foreign_icon",
188                       default=default_foreign_icon,
189                       help="set icon url to use for foreign sites marker -- default is %s"%default_foreign_icon)
190     (options, args) = parser.parse_args()
191     if len(args) != 0:
192         parser.print_help()
193         sys.exit(1)
194     KmlMap(options.output,options).refresh()
195
196 ####################
197 if __name__ == "__main__":
198     main()