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):
43 if s1['peer_id']: p1=s1['peer_id']
44 if s2['peer_id']: p2=s2['peer_id']
51 peers = GetPeers({},['peer_id','peername'])
52 all_sites = GetSites({'enabled':True,'is_public':True})
53 all_sites.sort(KmlMap.site_compare)
54 for site in all_sites:
55 self.write_site(site,peers)
59 # initial placement is for europe - dunno how to tune that yet
60 def write_header (self):
61 self.write("""<?xml version="1.0" encoding="UTF-8"?>
62 <kml xmlns="http://earth.google.com/kml/2.2">
64 <name> PlanetLab Sites </name>
66 <longitude>9.180821112577378</longitude>
67 <latitude>44.43275321178062</latitude>
68 <altitude>0</altitude>
69 <range>5782133.196489797</range>
71 <heading>-7.767386340832667</heading>
73 <description> All the sites known to the PlanetLab testbed. </description>
76 def write_footer (self):
77 self.write("""</Document></kml>
80 def peer_name (self,site, peers):
81 if not site['peer_id']:
84 if peer['peer_id'] == site['peer_id']:
85 return peer['peername']
87 def write_site (self, site, peers):
88 # discard sites with missing lat or lon
89 if not site['latitude'] or not site['longitude']:
91 # discard sites with no nodes
92 if len(site['node_ids']) == 0:
95 site_id=site['site_id']
97 nb_nodes=len(site['node_ids'])
98 nb_slices=len(site['slice_ids'])
99 latitude=site['latitude']
100 longitude=site['longitude']
101 apiurl='https://%s:443'%api.config.PLC_WWW_HOST
102 baseurl='http://%s'%api.config.PLC_WWW_HOST
103 peer_id=site['peer_id']
108 description += '<li>'
109 description += '<a href="%(apiurl)s/db/sites/index.php?id=%(site_id)d"> Site page </a>'%locals()
112 description += ' -- <a href="%(site_url)s"> %(site_url)s </a>'%locals()
113 description += '</li>'
116 description += '<li>'
117 description += '<a href="%(apiurl)s/db/nodes/index.php?site_id=%(site_id)d">%(nb_nodes)d node(s)</a>'%locals()
118 description += '<a href="%(apiurl)s/db/nodes/comon.php?site_id=%(site_id)d"> (in Comon)</a>'%locals()
119 description += '</li>'
121 description += '<li>No node</li>'
124 description += '<li><a href="%(apiurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slice(s)</a></li>'%locals()
126 description += '<li>No slice</li>'
129 peername = self.peer_name(site,peers)
130 description += '<li>'
131 description += '<a href="%(apiurl)s/db/peers/index.php?id=%(peer_id)d">At peer %(peername)s</a>'%locals()
132 description += '</li>'
134 description +='</ul>'
136 if not self.options.labels:
141 if self.options.use_google_icons:
144 iconfile=default_local_builtin
145 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
148 iconfile=default_foreign_builtin
149 xyspec="<x>160</x><y>0</y><w>32</w><h>32</h>"
150 iconurl="root://icons/%(iconfile)s"%locals()
153 iconfile=self.options.local_icon
155 iconfile=self.options.foreign_icon
156 iconurl="%(baseurl)s/%(iconfile)s"%locals()
159 iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
161 # set the camera 50km high
162 template="""<Placemark>
163 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
164 <name><![CDATA[%(name)s]]></name>
166 <latitude>%(latitude)f</latitude>
167 <longitude>%(longitude)f</longitude>
168 <altitude>0</altitude>
169 <altitudeMode>relativeToGround</altitudeMode>
170 <range>50000.</range>
172 <description><![CDATA[%(description)s]]></description>
173 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
176 self.write(template%locals())
179 from optparse import OptionParser
180 usage = "Usage %prog [plcsh-options] [ -- options ]"
181 parser = OptionParser (usage=usage)
183 parser.add_option("-o","--output",action="store",dest="output",
184 default=default_output,
185 help="output file - default is %s"%default_output)
186 parser.add_option("-n","--no-label",action="store_false",dest="labels",
188 help="outputs only geographic positions, no labels")
189 parser.add_option("-c","--custom",action="store_false",dest="use_google_icons",
191 help="use locally customized icons rather than the google-provided defaults")
192 parser.add_option("-l","--local",action="store",dest="local_icon",
193 default=default_local_icon,
194 help="set icon url to use for local sites marker -- default is %s"%default_local_icon)
195 parser.add_option("-f","--foreign",action="store",dest="foreign_icon",
196 default=default_foreign_icon,
197 help="set icon url to use for foreign sites marker -- default is %s"%default_foreign_icon)
199 (options, args) = parser.parse_args()
203 KmlMap(options.output,options).refresh()
206 if __name__ == "__main__":