From aff4b0dc7a475d12fd4f4351075b84f7b7800116 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Tue, 11 Mar 2008 22:51:33 +0000 Subject: [PATCH] googlemap display via a kml file - see http://svn.planet-lab.org/wiki/GooglemapSetup --- plc-kml.py | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++ plc-map.py | 64 ++++--------------------- 2 files changed, 145 insertions(+), 56 deletions(-) create mode 100755 plc-kml.py diff --git a/plc-kml.py b/plc-kml.py new file mode 100755 index 0000000..0152217 --- /dev/null +++ b/plc-kml.py @@ -0,0 +1,137 @@ +#!/usr/bin/env plcsh + +# this script generates a kml file, located under the default location below +# you should crontab this job from your myplc image +# you can then use the googlemap.js javascript for creating your applet +# more on this at http://svn.planet-lab.org/wiki/GooglemapSetup + +import sys + +default_output="/var/www/html/sites/sites.kml" + +class KmlMap: + + def __init__ (self,outputname): + self.outputname=outputname + + def open (self): + self.output = open(self.outputname,"w") + + def close (self): + if self.output: + self.output.close() + self.output = None + + def refresh (self): + self.open() + self.write_header() + # cache peers + peers = GetPeers({},['peer_id','peername']) + for site in GetSites({'enabled':True,'is_public':True}): + self.write_site(site,peers) + self.write_footer() + self.close() + + def write(self,string): + self.output.write(string.encode("UTF-8")) + + def write_header (self): + self.write(""" + + + PlanetLab Sites + This map shows all sites knows to the PlanetLab testbed. +""") + + def write_footer (self): + self.write(""" +""") + + def peer_name (self,site, peers): + if not site['peer_id']: + return "local" + for peer in peers: + if peer['peer_id'] == site['peer_id']: + return peer['peername'] + + def write_site (self, site, peers): + # discard sites with missing lat or lon + if not site['latitude'] or not site['longitude']: + return + # discard sites with no nodes + if len(site['node_ids']) == 0: + return + + site_id=site['site_id'] + site_name=site['name'] + nb_nodes=len(site['node_ids']) + nb_slices=len(site['slice_ids']) + latitude=site['latitude'] + longitude=site['longitude'] + baseurl='https://%s:443'%api.config.PLC_WWW_HOST + # name + name = '

%(site_name)s

'%locals() + description='' + + +# iconfile="google-local.png" +# if site['peer_id']: +# iconfile="google-foreign.png" +# iconurl="http://%(base)s/misc/%(iconfile)s"%locals() +# xyspec="" + + if not site['peer_id']: + # local sites + iconurl="root://icons/palette-3.png" + xyspec="003232" + else: + # remote + iconurl="root://icons/palette-3.png" + xyspec="3203232" + + iconspec="%(iconurl)s%(xyspec)s"%locals() + + template=""" + + + + %(longitude)f,%(latitude)f,0 + +""" + self.write(template%locals()) + + +# print 'name',name +# print 'description',description +# print template +# print template%locals() + +if __name__ == "__main__": + if len(sys.argv) == 1: + out=default_output + elif len(sys.argv) == 2: + out=sys.argv[1] + else: + print "Usage: %s [output]"%sys.argv[0] + print "default output is %s"%default_output + sys.exit(1) + KmlMap(out).refresh() diff --git a/plc-map.py b/plc-map.py index 932afc5..2568d29 100755 --- a/plc-map.py +++ b/plc-map.py @@ -1,5 +1,13 @@ #!/usr/bin/env plcsh +# this is a very rustic script for generating png maps from a model +# the model is expected in /var/www/html/sites/map.png +# and the output is located in the same dir as livemap.png +# +# this has many drawbacks, as it needs a cylindric projection map +# (there's almost no such map out there) and manual calibration +# you want to use the kml-based googlemap applet instead + import Image, ImageDraw ####### first - rustic - linear positioning on a map @@ -62,64 +70,8 @@ def make_image(): image.save (live) -########## second - way simpler - export sites as a list to javascript for rendering with googlemap -js_prelude=""" -function Site (lat,lon,site_id,name,peer_id,peername,nb_nodes) { - this.lat=lat; - this.lon=lon; - this.site_id=site_id; - this.name=name; - this.peer_id=peer_id; - this.peername=peername; - this.nb_nodes=nb_nodes; -} -""" - -def locate_peer (peers,peer_id): - for peer in peers: - if peer['peer_id']==peer_id: - return peer - return {'peername':'Cannot locate peer'} - -def js_site (site,peers): - # some sites come with lat or lon being None - lat = site['latitude'] - if not lat: - lat=0 - lon = site['longitude'] - if not lon: - lon=0 - # build javascript text - jstext="new Site(" - jstext += str(lat) + "," + str(lon) + "," - jstext += str(site['site_id']) + "," - # needs html encoding for wierd chars - jstext += '"' + site['name'].encode("utf-8") + '"' + ',' - if not site['peer_id']: - jstext += '0,""' +',' - else: - peer=locate_peer(peers,site['peer_id']) - jstext += str(site['peer_id']) + ',"' + peer['peername'].encode("utf-8") + '"' + ',' - jstext += str(len(site['node_ids'])) - jstext += ')\n' - return jstext - -def make_javascript(): - outputname="/var/www/html/sites/plc-sites.js" - f=open(outputname,"w") - f.write(js_prelude) - columns=['latitude','longitude','site_id','name','peer_id','node_ids'] - f.write("allSites=new Array(\n") - # writes foreign sites first - foreign_sites=GetSites({'~peer_id':None},columns) - peers=GetPeers({}) - local_sites=GetSites({'peer_id':None},columns) - f.write(",".join([js_site(site,peers) for site in foreign_sites+local_sites])) - f.write(");") - def main (): make_image () - make_javascript () if __name__ == '__main__': main () -- 2.43.0