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"
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"))
38 def site_compare (s1,s2):
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)
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">
65 <name> PlanetLab Sites </name>
67 <longitude>9.180821112577378</longitude>
68 <latitude>44.43275321178062</latitude>
69 <altitude>0</altitude>
70 <range>5782133.196489797</range>
72 <heading>-7.767386340832667</heading>
74 <description> All the sites known to the PlanetLab testbed. </description>
77 def write_footer (self):
78 self.write("""</Document></kml>
81 def peer_name (self,site, peers):
82 if not site['peer_id']:
85 if peer['peer_id'] == site['peer_id']:
86 return peer['peername']
88 def write_site (self, site, peers):
89 # discard sites with missing lat or lon
90 if not site['latitude'] or not site['longitude']:
92 # discard sites with no nodes
93 if len(site['node_ids']) == 0:
96 site_id=site['site_id']
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']
109 description += '<li>'
110 description += '<a href="%(apiurl)s/db/sites/index.php?id=%(site_id)d"> Site page </a>'%locals()
113 description += ' -- <a href="%(site_url)s"> %(site_url)s </a>'%locals()
114 description += '</li>'
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>'
122 description += '<li>No node</li>'
125 description += '<li><a href="%(apiurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slice(s)</a></li>'%locals()
127 description += '<li>No slice</li>'
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>'
135 description +='</ul>'
137 if not self.options.labels:
142 if self.options.use_google_icons:
145 iconfile="palette-4.png"
146 xyspec="<x>128</x><y>0</y><w>32</w><h>32</h>"
149 iconfile="palette-3.png"
150 xyspec="<x>160</x><y>0</y><w>32</w><h>32</h>"
151 iconurl="root://icons/%(iconfile)s"%locals()
154 iconfile=self.options.local_icon
156 iconfile=self.options.foreign_icon
157 iconurl="%(baseurl)s/%(iconfile)s"%locals()
160 iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
162 # set the camera 50km high
163 template="""<Placemark>
164 <Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
165 <name><![CDATA[%(name)s]]></name>
167 <latitude>%(latitude)f</latitude>
168 <longitude>%(longitude)f</longitude>
169 <altitude>0</altitude>
170 <altitudeMode>relativeToGround</altitudeMode>
171 <range>50000.</range>
173 <description><![CDATA[%(description)s]]></description>
174 <Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
177 self.write(template%locals())
180 from optparse import OptionParser
181 usage = "Usage %prog [plcsh-options] [ -- options ]"
182 parser = OptionParser (usage=usage)
184 parser.add_option("-o","--output",action="store",dest="output",
185 default=default_output,
186 help="output file - default is %s"%default_output)
187 parser.add_option("-n","--no-label",action="store_false",dest="labels",
189 help="outputs only geographic positions, no labels")
190 parser.add_option("-c","--custom",action="store_false",dest="use_google_icons",
192 help="use locally customized icons rather than the google-provided defaults")
193 parser.add_option("-l","--local",action="store",dest="local_icon",
194 default=default_local_icon,
195 help="set icon url to use for local sites marker -- default is %s"%default_local_icon)
196 parser.add_option("-f","--foreign",action="store",dest="foreign_icon",
197 default=default_foreign_icon,
198 help="set icon url to use for foreign sites marker -- default is %s"%default_foreign_icon)
200 (options, args) = parser.parse_args()
204 KmlMap(options.output,options).refresh()
207 if __name__ == "__main__":