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