pngtile.image: leaflet browser for tiles; coordinates are still wonky..
authorTero Marttila <terom@paivola.fi>
Sun, 14 Sep 2014 23:18:35 +0300
changeset 141 19a3ed063d18
parent 140 a3bbecbd31e7
child 142 9b316e83e9e3
pngtile.image: leaflet browser for tiles; coordinates are still wonky..
bin/image-server
pngtile/image.py
static/pngtile/image.css
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/image-server	Sun Sep 14 23:18:35 2014 +0300
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+
+"""
+    Development server for pngtile.image serving.
+"""
+
+import argparse
+import pngtile.image
+import werkzeug.serving
+
+def main ():
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('--listen',     metavar='ADDR', default='0.0.0.0',
+            help="Listen on address")
+    parser.add_argument('--port',       metavar='PORT', type=int, default=8080,
+            help="Listen on port")
+
+    parser.add_argument('--reload',     action='store_true',
+            help="Reload")
+    parser.add_argument('--debugger',   action='store_true',
+            help="Debugger")
+
+    parser.add_argument('--static',     metavar='PATH', default='./static',
+            help="Path to /static")
+    parser.add_argument('image_root',   metavar='PATH',
+            help="Path to images")
+
+    args = parser.parse_args()
+
+    application = pngtile.image.ImageApplication(
+        image_root  = args.image_root,
+    )
+
+    werkzeug.serving.run_simple(args.listen, args.port, application,
+            use_reloader    = args.reload,
+            use_debugger    = args.debugger,
+            static_files    = {
+                '/static':  args.static,
+            },
+    )
+
+if __name__ == '__main__':
+    main()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pngtile/image.py	Sun Sep 14 23:18:35 2014 +0300
@@ -0,0 +1,110 @@
+"""
+    Image handling.
+"""
+
+
+from werkzeug import Request, Response, exceptions
+from werkzeug.utils import html
+
+import pngtile.tile
+from pngtile.application import BaseApplication
+import pypngtile
+
+import json
+
+class ImageApplication (BaseApplication):
+
+    STYLESHEETS = (
+        'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css',
+        'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css',
+        'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css',
+        '/static/pngtile/image.css',
+    )
+
+    SCRIPTS = (
+        'https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js',
+        'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js',
+        'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js',
+    )
+
+    def __init__ (self, **opts):
+        BaseApplication.__init__(self, **opts)
+
+    def render_image (self, request, image, name):
+        """
+            request:    werkzeug.Request
+            image:      pypngtile.Image
+            name:       request path for .png image
+        """
+
+        image_info = image.info()
+
+        config = dict(
+            tile_url        = 'http://zovoweix.qmsk.net:8080/{name}?x={x}&y={y}&zoom={z}',
+            tile_name       = name,
+
+            tile_size       = pngtile.tile.TILE_SIZE,
+            tile_zoom       = pngtile.tile.MAX_ZOOM,
+            
+            image_width     = image_info['img_width'],
+            image_height    = image_info['img_height'],
+        )
+
+        return self.render_html(
+            title       = name,
+            body        = (
+                html.div(id='wrapper', *[
+                    html.div(id='map')
+                ]),
+            ),
+            end         = (
+                html.script("""\
+                    $(function() {{
+                        var config = {config};
+
+                        var bounds = [
+                            [ 0, config.image_height ],
+                            [ -config.image_width, 0 ]
+                        ];
+                        var center = [
+                            -256, 512
+                        ];
+                        var zoom = config.tile_zoom;
+                        
+                        var map = L.map('map', {{
+                            crs: L.CRS.Simple,
+                            center: center,
+                            zoom: zoom,
+                            maxBounds: bounds
+                        }});
+
+                        L.tileLayer(config.tile_url, {{
+                            name: config.tile_name,
+                            minZoom: 0,
+                            maxZoom: config.tile_zoom,
+                            tileSize: config.tile_size,
+                            continuousWorld: true,
+                            noWrap: true,
+                            zoomReverse: true,
+                            bounds: bounds
+                        }}).addTo(map);
+                    }});
+                """.format(config=json.dumps(config))),
+            ),
+        )
+
+    def handle (self, request):
+        """
+            Handle request for an image
+        """
+         
+        try:
+            image, name = self.get_image(request.path)
+        except pypngtile.Error as error:
+            raise exceptions.BadRequest(str(error))
+
+        html = self.render_image(request, image, name)
+
+        return Response(html, content_type="text/html")
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/static/pngtile/image.css	Sun Sep 14 23:18:35 2014 +0300
@@ -0,0 +1,9 @@
+#map {
+    overflow: hidden;
+    position: absolute;
+
+    top: 0px;
+    left: 0px;
+    width: 100%;
+    height: 100%;
+}