center initial view
authorTero Marttila <terom@fixme.fi>
Thu, 07 Jan 2010 22:24:30 +0200
changeset 43 fcd818eb5a71
parent 40 5454d2e2f633
child 44 1a93b5a6efd0
center initial view
pngtile/wsgi.py
static/tiles2.js
--- a/pngtile/wsgi.py	Wed Jan 06 17:45:29 2010 +0200
+++ b/pngtile/wsgi.py	Thu Jan 07 22:24:30 2010 +0200
@@ -15,6 +15,9 @@
 TILE_WIDTH = 256
 TILE_HEIGHT = 256
 
+# max. output resolution to allow
+MAX_PIXELS = 1920 * 1200
+
 def dir_view (req, name, path) :
     prefix = os.path.dirname(req.script_root).rstrip('/')
     name = name.rstrip('/')
@@ -48,6 +51,8 @@
 def image_view (req, image_path, image) :
     image_name = os.path.basename(image_path)
 
+    img_width, img_height = image.info()
+
     return """\
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
     <head>
@@ -72,7 +77,7 @@
         </div>
 
         <script type="text/javascript">
-            var tile_source = new Source("%(tile_url)s", %(tile_width)d, %(tile_height)d, -4, 0);
+            var tile_source = new Source("%(tile_url)s", %(tile_width)d, %(tile_height)d, -4, 0, %(img_width)d, %(img_height)d);
             var main = new Viewport(tile_source, "viewport");
         </script>
     </body>
@@ -83,6 +88,9 @@
 
         tile_width      = TILE_WIDTH,
         tile_height     = TILE_HEIGHT,
+
+        img_width       = img_width,
+        img_height      = img_height,
     )
 
 def scale_by_zoom (val, zoom) :
@@ -106,11 +114,15 @@
     x = scale_by_zoom(cx - width / 2, -zoom)
     y = scale_by_zoom(cy - height / 2, -zoom)
 
+    # safely limit
+    if width * height > MAX_PIXELS :
+        raise exceptions.Forbidden("Image too large: %d * %d > %d" % (width, height, MAX_PIXELS))
+
     return image.tile_mem(
         width, height,
         x, y,
         zoom
-   )
+    )
 
 def handle_main (req) :
     # path to image
--- a/static/tiles2.js	Wed Jan 06 17:45:29 2010 +0200
+++ b/static/tiles2.js	Thu Jan 07 22:24:30 2010 +0200
@@ -1,11 +1,13 @@
 // A source of tile images of a specific width/height, zoom level range, and some other attributes
 var Source = Class.create({
-    initialize: function (path, tile_width, tile_height, zoom_min, zoom_max) {
+    initialize: function (path, tile_width, tile_height, zoom_min, zoom_max, img_width, img_height) {
         this.path = path;
         this.tile_width = tile_width;
         this.tile_height = tile_height;
         this.zoom_min = zoom_min;
         this.zoom_max = zoom_max;
+        this.img_width = img_width;
+        this.img_height = img_height;
 
         this.refresh = false;
         this.opt_key = this.opt_value = null;
@@ -91,30 +93,33 @@
         
         // this comes after update_size, since it must be updated once we have the size and zoom layer...
         this.image_link = $("lnk-image");
+            
+        // initial location
+        var cx = 0, cy = 0, z = 0;
         
         // initial location?    
         if (document.location.hash) {
             // x:y:z tuple
             var pt = document.location.hash.substr(1).split(":");
             
-            // unpack
-            var cx = 0, cy = 0, z = 0;
-            
+            // unpack    
             if (pt.length) cx = parseInt(pt.shift());
             if (pt.length) cy = parseInt(pt.shift());
             if (pt.length) z = parseInt(pt.shift());
 
-            // initial view
-            this.zoom_scaled(
-                cx - this.center_offset_x, 
-                cy - this.center_offset_y, 
-                z
-            );
+        } else {
+            // start in the center
+            cx = this.source.img_width / 2;
+            cy = this.source.img_height / 2;
+            z = 0; // XXX: xy unscaled: (this.source.zoom_min + this.source.zoom_max) / 2;
+        }
 
-        } else {
-            // this sets the scroll offsets, zoom level, and loads the tiles
-            this.zoom_to(0, 0, 0);
-        }
+        // initial view
+        this.zoom_scaled(
+            cx - this.center_offset_x, 
+            cy - this.center_offset_y, 
+            z
+        );
     },
     
     /* event handlers */