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
10 # kml reference can be found at
11 # http://code.google.com/apis/kml/documentation/kmlreference.html
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 default_local_builtin = "palette-4.png"
20 default_foreign_builtin = "palette-3.png"
24 def __init__ (self,outputname,options):
25 self.outputname=outputname
29 self.output = open(self.outputname,"w")
36 def write(self,string):
37 self.output.write(string.encode("UTF-8"))
41 def site_compare (s1,s2):
45 if s1['peer_id']: p1=s1['peer_id']
47 if s2['peer_id']: p2=s2['peer_id']
54 peers = GetPeers({},['peer_id','peername'])
55 all_sites = GetSites({'enabled':True,'is_public':True})
56 all_sites.sort(KmlMap.site_compare)
57 for site in all_sites:
58 self.write_site(site,peers)
62 # initial placement is for europe - dunno how to tune that yet
63 def write_header (self):
64 self.write("""<?xml version="1.0" encoding="UTF-8"?>
65 <kml xmlns="http://earth.google.com/kml/2.2">
67 <name> PlanetLab Sites </name>
69 <longitude>9.180821112577378</longitude>
70 <latitude>44.43275321178062</latitude>
71 <altitude>0</altitude>
72 <range>5782133.196489797</range>
74 <heading>-7.767386340832667</heading>
76 <description> All the sites known to the PlanetLab testbed. </description>
79 def write_footer (self):
80 self.write("""</Document></kml>
83 def peer_name (self,site, peers):
84 if not site['peer_id']:
87 if peer['peer_id'] == site['peer_id']:
88 return peer['peername']
90 def write_site (self, site, peers):
91 # discard sites with missing lat or lon
92 if not site['latitude'] or not site['longitude']:
94 # discard sites with no nodes
95 if len(site['node_ids']) == 0:
98 site_id=site['site_id']
100 nb_nodes=len(site['node_ids'])
101 nb_slices=len(site['slice_ids'])
102 latitude=site['latitude']
103 longitude=site['longitude']
104 apiurl='https://%s:443'%api.config.PLC_WWW_HOST
105 baseurl='http://%s'%api.config.PLC_WWW_HOST
106 peer_id=site['peer_id']
111 description += '<li>'
112 description += '<a href="%(apiurl)s/db/sites/index.php?id=%(site_id)d"> Site page </a>'%locals()
115 description += ' -- <a href="%(site_url)s"> %(site_url)s </a>'%locals()
116 description += '</li>'
119 description += '<li>'
120 description += '<a href="%(apiurl)s/db/nodes/index.php?site_id=%(site_id)d">%(nb_nodes)d node(s)</a>'%locals()
121 description += '<a href="%(apiurl)s/db/nodes/comon.php?site_id=%(site_id)d"> (in Comon)</a>'%locals()
122 description += '</li>'
124 description += '<li>No node</li>'
127 description += '<li><a href="%(apiurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slice(s)</a></li>'%locals()
129 description += '<li>No slice</li>'
132 peername = self.peer_name(site,peers)
133 description += '<li>'
134 description += '<a href="%(apiurl)s/db/peers/index.php?id=%(peer_id)d">At peer %(peername)s</a>'%locals()
135 description += '</li>'
137 description +='</ul>'
139 if not self.options.labels:
144 if self.options.use_google_icons:
147 iconfile=default_local_builtin
148 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
151 iconfile=default_foreign_builtin
152 xyspec="<x>160</x><y>0</y><w>32</w><h>32</h>"
153 iconurl="root://icons/%(iconfile)s"%locals()
156 iconfile=self.options.local_icon
158 iconfile=self.options.foreign_icon
159 iconurl="%(baseurl)s/%(iconfile)s"%locals()
162 iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
164 # set the camera 50km high
165 template="""<Placemark>
166 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
167 <name><![CDATA[%(name)s]]></name>
169 <latitude>%(latitude)f</latitude>
170 <longitude>%(longitude)f</longitude>
171 <altitude>0</altitude>
172 <altitudeMode>relativeToGround</altitudeMode>
173 <range>50000.</range>
175 <description><![CDATA[%(description)s]]></description>
176 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
179 self.write(template%locals())
182 from optparse import OptionParser
183 usage = "Usage %prog [plcsh-options] [ -- options ]"
184 parser = OptionParser (usage=usage)
186 parser.add_option("-o","--output",action="store",dest="output",
187 default=default_output,
188 help="output file - default is %s"%default_output)
189 parser.add_option("-n","--no-label",action="store_false",dest="labels",
191 help="outputs only geographic positions, no labels")
192 parser.add_option("-c","--custom",action="store_false",dest="use_google_icons",
194 help="use locally customized icons rather than the google-provided defaults")
195 parser.add_option("-l","--local",action="store",dest="local_icon",
196 default=default_local_icon,
197 help="set icon url to use for local sites marker -- default is %s"%default_local_icon)
198 parser.add_option("-f","--foreign",action="store",dest="foreign_icon",
199 default=default_foreign_icon,
200 help="set icon url to use for foreign sites marker -- default is %s"%default_foreign_icon)
202 (options, args) = parser.parse_args()
206 KmlMap(options.output,options).refresh()
209 if __name__ == "__main__":