Commits

Dražen Odobašić committed 1b05dec

added simple, single layer WMS service, moved set_layer_opacity to utils.py

  • Participants
  • Parent commits 444dbec

Comments (0)

Files changed (4)

File Kartolog/renderer.py

 import mapnik
 import datetime
 
-from Kartolog.utils import Projection,Layer,Tile,tileHash
+from Kartolog.utils import Projection,Layer,Tile,tileHash,set_layer_opacity
 
-import StringIO, Image, ImageEnhance
+import StringIO, Image
 
 class Render():
     def __init__(self,db):
             img_type='png'
         else:
             data = im.tostring(img_type)
-
-        image = Image.open( StringIO.StringIO(data) )
             
         #set layer opacity if image is PNG
         if layer.opacity>=0 and layer.opacity<1 and self.tile.img_format.split('/')[1]=='png':
-            image=self.set_layer_opacity(image,layer.opacity)
-            
+            data=set_layer_opacity(data,layer.opacity)
+
+        image = Image.open( StringIO.StringIO(data) )
         metaHeight = metaRows * self.tile.size[1] + 2 * proj.metaBuffer[1]
 
         for i in range(metaCols):
             }
         self.db.tiles.update(tile_h,{'$set':tile_d},safe=True)
 
-    def set_layer_opacity(self, im, opacity):
-        #im = Image.open(StringIO.StringIO(img))
-        assert opacity >= 0 and opacity <= 1
-        
-        if im.mode != 'RGBA':
-            im = im.convert('RGBA')
-        else:
-            im = im.copy()
-        alpha = im.split()[3]
-        alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
-        im.putalpha(alpha)
-        img_buffer = StringIO.StringIO()
-        #hardcoded png format
-        im.save(img_buffer, 'png')
-        img_buffer.seek(0)
-        return im
 
 
         

File Kartolog/server.py

 from Kartolog.web.DemoHandler import DemoHandler
 from Kartolog.web.StatusHandler import StatusHandler
 
+import Kartolog.web.WMSHandler as WMSHandler
 import Kartolog.web.ProjectionHandler as ProjectionHandler
 import Kartolog.web.LayerHandler as LayerHandler
 import Kartolog.web.ProjectHandler as ProjectHandler
         handlers = [
             (r"/", IndexHandler),
             (r"/tiles", TileHandler),
+            (r"/wms", WMSHandler.WMSHandler),
+            
             (r"/demo", DemoHandler),
             (r"/status", StatusHandler),
             

File Kartolog/utils.py

     #encode to utf-8 because list items can be anything
     return base64.urlsafe_b64encode(hashlib.md5('|'.join([unicode(item) for item in hash_list]).encode('utf-8')).digest())[:4]
 
+import StringIO, ImageEnhance, Image
+
+def set_layer_opacity(im, opacity):
+    #im = Image.open(StringIO.StringIO(img))
+    assert opacity >= 0 and opacity <= 1
+
+    im = Image.open( StringIO.StringIO(im) )
+    if im.mode != 'RGBA':
+        im = im.convert('RGBA')
+    else:
+        im = im.copy()
+    alpha = im.split()[3]
+    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
+    im.putalpha(alpha)
+    img_buffer = StringIO.StringIO()
+    #hardcoded png format
+    im.save(img_buffer, 'png')
+    img_buffer.seek(0)
+    return img_buffer.read()

File Kartolog/web/WMSHandler.py

+from Kartolog.web.BaseHandler import BaseHandler
+from Kartolog.utils import Layer,set_layer_opacity
+
+import mapnik
+
+class WMSHandler(BaseHandler):
+    def get(self):
+        #WMS argument parsing
+        layers=self.get_argument('LAYERS').split(',')
+        bbox=[float(x) for x in self.get_argument('BBOX').split(',')]
+        #srs=self.get_argument('SRS')
+        img_format=self.get_argument('FORMAT')
+        size=(int(self.get_argument('WIDTH')),int(self.get_argument('HEIGHT')))
+
+        #render single layer
+        layer = Layer(self.db.layers.find_one({'hid':layers[0]}))
+
+        self.set_header("Content-Type", img_format)
+        result=self.mapnik_wms(size[0],size[1],img_format,layer,bbox)
+        self.write(result)
+
+    def mapnik_wms(self,width,height,img_format,layer,bbox):
+        self.map=mapnik.Map(width,height)
+        mapnik.load_map(self.map,layer.mapfile)
+        self.map.buffer_size = 128
+        self.map.zoom_to_box(mapnik.Envelope(*bbox))
+        
+        image = mapnik.Image(width, height)
+        mapnik.render(self.map,image)
+        image = image.tostring(str(img_format.split('/')[1]))
+
+        #set layer opacity if image is PNG
+        if layer.opacity>=0 and layer.opacity<1 and img_format.split('/')[1]=='png':
+            image=set_layer_opacity(image,layer.opacity)
+
+        return image