pngtile.image: use --tile-server as url, fix render_dir href's, support legacy urls
authorTero Marttila <terom@qmsk.net>
Fri, 03 Oct 2014 21:08:38 +0300
changeset 153 55f3d3bc95d3
parent 152 7bd8d6062c9e
child 154 e9eef49f4d57
pngtile.image: use --tile-server as url, fix render_dir href's, support legacy urls
bin/image-server
pngtile/image.py
static/pngtile/map.js
--- a/bin/image-server	Fri Oct 03 21:07:59 2014 +0300
+++ b/bin/image-server	Fri Oct 03 21:08:38 2014 +0300
@@ -19,6 +19,9 @@
             help="Reload")
     parser.add_argument('--debugger',   action='store_true',
             help="Debugger")
+    
+    parser.add_argument('--tile-server',    metavar='URL', required=True,
+            help="http://.../ URL to tileserver root")
 
     parser.add_argument('--static',     metavar='PATH', default='./static',
             help="Path to /static")
@@ -29,6 +32,8 @@
 
     application = pngtile.image.ImageApplication(
         image_root  = args.image_root,
+
+        tile_server = args.tile_server,
     )
 
     werkzeug.serving.run_simple(args.listen, args.port, application,
--- a/pngtile/image.py	Fri Oct 03 21:07:59 2014 +0300
+++ b/pngtile/image.py	Fri Oct 03 21:08:38 2014 +0300
@@ -4,29 +4,15 @@
 
 
 from werkzeug import Request, Response, exceptions
-from werkzeug.utils import html
+from werkzeug.utils import html, redirect
 
 import pngtile.tile
-from pngtile.application import BaseApplication
+from pngtile.application import url, BaseApplication
 import pypngtile
 
 import json
 import os, os.path
 
-def dir_url (prefix, name, item):
-    """
-        Join together an absolute URL prefix, an optional directory name, and a directory item
-    """
-
-    url = prefix
-
-    if name:
-        url += '/' + name
-    
-    url += '/' + item
-
-    return url
-
 def dir_list (root):
     """
         Yield a series of directory items to show for the given dir
@@ -41,7 +27,7 @@
         
         # show dirs
         if os.path.isdir(path):
-            yield name
+            yield name + '/'
 
         # examine ext
         if '.' in name:
@@ -70,8 +56,14 @@
         '/static/pngtile/map.js',
     )
 
-    def __init__ (self, **opts):
+    def __init__ (self, tile_server=None, **opts):
+        """
+            http://.../ path to tileserver root
+        """
+
         BaseApplication.__init__(self, **opts)
+                
+        self.tile_server = tile_server
 
     def render_dir_breadcrumb (self, name):
         path = []
@@ -82,7 +74,7 @@
             for part in name.split('/'):
                 path.append(part)
 
-                yield html.li(html.a(href='/'.join(path), *[part]))
+                yield html.li(html.a(href=url('/', *path), *[part]))
 
     def render_dir (self, request, name, items):
         """
@@ -106,7 +98,7 @@
                     ]),
                     html.div(class_='list', *[
                         html.ul(class_='list-group', *[html.li(class_='list-group-item', *[
-                                html.a(href=dir_url('', name, item), *[item])
+                                html.a(href=item, *[item])
                             ]) for item in items]
                         ),
                     ]),
@@ -118,19 +110,21 @@
         """
             request:    werkzeug.Request
             image:      pypngtile.Image
-            name:       request path for .png image
+            name:       request path for .../.../*.png image
         """
 
         image_info = image.info()
 
         map_config = dict(
-            tile_url        = 'http://zovoweix.qmsk.net:8080/{name}?x={x}&y={y}&zoom={z}',
+            tile_url        = '{server}/{name}?x={x}&y={y}&zoom={z}',
+            tile_server     = self.tile_server.rstrip('/'),
             tile_name       = name,
 
             tile_size       = pngtile.tile.TILE_SIZE,
             tile_zoom       = pngtile.tile.MAX_ZOOM,
             
-            image_url       = 'http://zovoweix.qmsk.net:8080/{name}?w={w}&h={h}&x={x}&y={y}&zoom={z}',
+            image_url       = '{server}/{name}?w={w}&h={h}&x={x}&y={y}&zoom={z}',
+            image_server    = self.tile_server.rstrip('/'),
             image_name      = name,
             image_width     = image_info['img_width'],
             image_height    = image_info['img_height'],
@@ -153,6 +147,10 @@
             Generate response for directory listing.
         """
 
+        if not request.path.endswith('/'):
+            # we generate HTML with relative links
+            return redirect(request.path + '/')
+
         items = sorted(dir_list(path))
 
         html = self.render_dir(request, name, items)
@@ -163,7 +161,17 @@
         """
             Generate Response for image request.
         """
-        
+
+        # backwards-compat redirect from frontend -> tile-server
+        if all(attr in request.args for attr in ('cx', 'cy', 'w', 'h', 'zl')):
+            return redirect(url(self.tile_server, name,
+                w       = request.args['w'],
+                h       = request.args['h'],
+                x       = request.args['cx'],
+                y       = request.args['cy'],
+                zoom    = request.args['zl'],
+            ))
+
         try:
             image, name = self.get_image(request.path)
         except pypngtile.Error as error:
--- a/static/pngtile/map.js	Fri Oct 03 21:07:59 2014 +0300
+++ b/static/pngtile/map.js	Fri Oct 03 21:08:38 2014 +0300
@@ -73,6 +73,7 @@
     map.on('move', map_move);
 
     L.tileLayer(map_config.tile_url, {
+        server:             map_config.tile_server,
         name:               map_config.tile_name,
         minZoom:            0,
         maxZoom:            map_config.tile_zoom,
@@ -86,6 +87,7 @@
     // controls
     L.control.link({
         url:        map_config.image_url,
+        server:     map_config.image_server,
         name:       map_config.image_name,
     }).addTo(map);