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"
17 default_local_builtin = "palette-4.png"
18 default_foreign_builtin = "palette-3.png"
22 def __init__ (self,outputname,options):
23 self.outputname=outputname
27 self.output = open(self.outputname,"w")
34 def write(self,string):
35 self.output.write(string.encode("UTF-8"))
39 def site_compare (s1,s2):
41 if s1['peer_id']: p1=s1['peer_id']
42 if s2['peer_id']: p2=s2['peer_id']
50 all_sites = GetSites({'enabled':True,'is_public':True})
51 all_sites.sort(KmlMap.site_compare)
52 for site in all_sites:
53 self.write_site(site,peers)
57 # initial placement is for europe - dunno how to tune that yet
58 def write_header (self):
59 self.write("""<?xml version="1.0" encoding="UTF-8"?>
60 <kml xmlns="http://earth.google.com/kml/2.2">
62 <name> PlanetLab Sites </name>
64 <longitude>9.180821112577378</longitude>
65 <latitude>44.43275321178062</latitude>
66 <altitude>0</altitude>
67 <range>5782133.196489797</range>
69 <heading>-7.767386340832667</heading>
71 <description> All the sites known to the PlanetLab testbed. </description>
74 def write_footer (self):
75 self.write("""</Document></kml>
78 def peer_info (self,site, peers):
79 if not site['peer_id']:
80 return (api.config.PLC_NAME, "http://%s/"%api.config.PLC_API_HOST,)
82 if peer['peer_id'] == site['peer_id']:
83 return (peer['peername'],peer['peer_url'].replace("PLCAPI/",""),)
84 return "Unknown peer_name"
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']
105 # the size for google icons
106 if not self.options.use_custom_icons:
109 iconfile=default_local_builtin
110 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
113 iconfile=default_foreign_builtin
114 xyspec="<x>160</x><y>0</y><w>32</w><h>32</h>"
115 iconurl="root://icons/%(iconfile)s"%locals()
116 # the size for our own brew of icons
119 iconfile=self.options.local_icon
121 iconfile=self.options.foreign_icon
122 iconurl="%(baseurl)s/%(iconfile)s"%locals()
125 iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
128 description = "<br/><table style='border: 1px solid black; padding: 5px;' width='500px'>"
131 description += "<tr><td></td><td> </td><td></td></tr>"
132 description += "<tr><td align='center'>"
133 description += "<b>Testbed</b>"
134 description += "</td><td>"
135 (peername,peerurl) = self.peer_info (site,peers)
136 description += "<a style='text-decoration: none;' href='%(peerurl)s'> %(peername)s </a>"%locals()
137 #description += "<a style='text-decoration: none;' href='%(apiurl)s/db/peers/index.php?id=%(peer_id)d'>[description]</a>"%locals()
138 description += "</td><td>"
139 description += "<img src='%(iconurl)s'/>"%locals()
140 description += "</td></tr>"
145 description += "<tr><td align='center'>"
146 description += "<b>Website</b>"
147 description += "</td><td>"
148 description += "<a style='text-decoration: none;' href='%(site_url)s'> %(site_url)s </a>"%locals()
149 description += "</td><td></td></tr>"
151 description += "<tr><td></td><td> </td><td></td></tr>"
152 description += "<tr><td align='center'>Usage details:</td><td></td><td></td></tr>"
153 description += "<tr><td></td><td> </td><td></td></tr>"
156 description += "<tr><td align='center'>"
157 description += "<img src='%(apiurl)s/sites/node.png'/>"%locals()
158 description += "</td><td>"
160 description += "<a style='text-decoration: none;' href='%(apiurl)s/db/nodes/index.php?site_id=%(site_id)d'>%(nb_nodes)d node(s)</a>"%locals()
161 #description += "<a style='text-decoration: none;' href='%(apiurl)s/db/nodes/comon.php?site_id=%(site_id)d'> (in Comon)</a>"%locals()
163 description += "<i>No node</i>"
164 description += "</td><td></td></tr>"
167 description += "<tr><td align='center'>"
168 description += "<img src='%(apiurl)s/sites/slice.png'/>"%locals()
169 description += "</td><td>"
171 description += "<a style='text-decoration: none;' href='%(apiurl)s/db/slices/index.php?site_id=%(site_id)d'>%(nb_slices)d slice(s)</a>"%locals()
173 description += "<i>No slice</i>"
174 description += "</td><td></td></tr>"
176 description += "<tr><td></td><td> </td><td></td></tr>"
179 description += "</table>"
181 if not self.options.labels:
185 # set the camera 50km high
186 template="""<Placemark>
187 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
188 <name><![CDATA[%(name)s]]></name>
190 <latitude>%(latitude)f</latitude>
191 <longitude>%(longitude)f</longitude>
192 <altitude>0</altitude>
193 <altitudeMode>relativeToGround</altitudeMode>
194 <range>50000.</range>
196 <description><![CDATA[%(description)s]]></description>
197 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
200 self.write(template%locals())
203 from optparse import OptionParser
204 usage = "Usage %prog [plcsh-options] [ -- options ]"
205 parser = OptionParser (usage=usage)
207 parser.add_option("-o","--output",action="store",dest="output",
208 default=default_output,
209 help="output file - default is %s"%default_output)
210 parser.add_option("-n","--no-label",action="store_false",dest="labels",
212 help="outputs only geographic positions, no labels")
213 # default - for private depls. - is to use google-provided icons like palette-3
214 parser.add_option("-c","--custom",action="store_true",dest="use_custom_icons",
216 help="use locally customized icons rather than the %s and %s defaults"%(default_local_builtin,default_foreign_builtin))
217 parser.add_option("-l","--local",action="store",dest="local_icon",
218 default=default_local_icon,
219 help="set icon url to use for local sites marker -- requires -c -- default is %s"%default_local_icon)
220 parser.add_option("-f","--foreign",action="store",dest="foreign_icon",
221 default=default_foreign_icon,
222 help="set icon url to use for foreign sites marker -- requires -c -- default is %s"%default_foreign_icon)
224 (options, args) = parser.parse_args()
228 KmlMap(options.output,options).refresh()
231 if __name__ == "__main__":