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
8 # kml reference can be found at
9 # http://code.google.com/apis/kml/documentation/kmlreference.html
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"
20 def __init__ (self,outputname,options):
21 self.outputname=outputname
25 self.output = open(self.outputname,"w")
32 def write(self,string):
33 self.output.write(string.encode("UTF-8"))
36 def site_compare (s1,s2):
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)
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">
63 <name> PlanetLab Sites </name>
65 <longitude>9.180821112577378</longitude>
66 <latitude>44.43275321178062</latitude>
67 <altitude>0</altitude>
68 <range>5782133.196489797</range>
70 <heading>-7.767386340832667</heading>
72 <description> All the sites known to the PlanetLab testbed. </description>
75 def write_footer (self):
76 self.write("""</Document></kml>
79 def peer_name (self,site, peers):
80 if not site['peer_id']:
83 if peer['peer_id'] == site['peer_id']:
84 return peer['peername']
86 def write_site (self, site, peers):
87 # discard sites with missing lat or lon
88 if not site['latitude'] or not site['longitude']:
90 # discard sites with no nodes
91 if len(site['node_ids']) == 0:
94 site_id=site['site_id']
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']
107 description += '<li>'
108 description += '<a href="%(apiurl)s/db/sites/index.php?id=%(site_id)d"> Site page </a>'%locals()
111 description += ' -- <a href="%(site_url)s"> %(site_url)s </a>'%locals()
112 description += '</li>'
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>'
120 description += '<li>No node</li>'
123 description += '<li><a href="%(apiurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slice(s)</a></li>'%locals()
125 description += '<li>No slice</li>'
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>'
133 description +='</ul>'
136 if self.options.use_google_icons:
139 iconfile="palette-4.png"
140 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
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()
148 iconfile=self.options.local_icon
150 iconfile=self.options.foreign_icon
151 iconurl="%(baseurl)s/%(iconfile)s"%locals()
154 iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
156 # set the camera 50km high
157 template="""<Placemark>
158 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
159 <name><![CDATA[%(name)s]]></name>
161 <latitude>%(latitude)f</latitude>
162 <longitude>%(longitude)f</longitude>
163 <altitude>0</altitude>
164 <altitudeMode>relativeToGround</altitudeMode>
165 <range>50000.</range>
167 <description><![CDATA[%(description)s]]></description>
168 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
171 self.write(template%locals())
174 from optparse import OptionParser
175 usage = "Usage %prog [plcsh-options] [ -- options ]"
176 parser = OptionParser (usage=usage)
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",
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()
194 KmlMap(options.output,options).refresh()
197 if __name__ == "__main__":