googlemap display via a kml file - see http://svn.planet-lab.org/wiki/GooglemapSetup
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Tue, 11 Mar 2008 22:51:33 +0000 (22:51 +0000)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Tue, 11 Mar 2008 22:51:33 +0000 (22:51 +0000)
plc-kml.py [new file with mode: 0755]
plc-map.py

diff --git a/plc-kml.py b/plc-kml.py
new file mode 100755 (executable)
index 0000000..0152217
--- /dev/null
@@ -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("""<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://earth.google.com/kml/2.2">
+<Document>
+<name> PlanetLab Sites </name>
+<description> This map shows all sites knows to the PlanetLab testbed. </description>
+""")
+
+    def write_footer (self):
+        self.write("""</Document></kml>
+""")
+
+    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 = '<h3> <a href="%(baseurl)s/db/sites/index.php?id=%(site_id)d"> %(site_name)s </a></h3>'%locals()
+        description='<ul>'
+        # URL
+        if site['url']:
+            site_url=site['url']
+            description += '<li>URL:<a href="%(site_url)s"> %(site_url)s </a></li>'%locals()
+        # NODES
+        if nb_nodes:
+            description += '<li>'
+            description += '<a href="%(baseurl)s/db/nodes/index.php?site_id=%(site_id)d">%(nb_nodes)d nodes</a>'%locals()
+            description += '<a href="%(baseurl)s/db/nodes/comon.php?site_id=%(site_id)d"> (in Comon)</a>'%locals()
+            description += '</li>'
+        else:
+            description += '<li>No node</li>'
+        #SLICES
+        if nb_slices:
+            description += '<li><a href="%(baseurl)s/db/slices/index.php?site_id=%(site_id)d">%(nb_slices)d slices</a></li>'%locals()
+        else:
+            description += '<li>No slice</li>'
+
+        description +='</ul>'
+
+
+#        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="<x>0</x><y>0</y><w>32</w><h>32</h>"
+        else:
+            # remote
+            iconurl="root://icons/palette-3.png"
+            xyspec="<x>32</x><y>0</y><w>32</w><h>32</h>"
+            
+        iconspec="<href>%(iconurl)s</href>%(xyspec)s"%locals()
+
+        template="""<Placemark>
+<Style><IconStyle><Icon>%(iconspec)s</Icon></IconStyle></Style>
+<name><![CDATA[%(name)s]]></name>
+<description><![CDATA[%(description)s]]></description>
+<Point> <coordinates>%(longitude)f,%(latitude)f,0</coordinates> </Point>
+</Placemark>
+"""
+        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()
index 932afc5..2568d29 100755 (executable)
@@ -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 ()