OpenLayers OpenLayers

Ticket #627: tilecache.patch

File tilecache.patch, 7.3 kB (added by tschaub, 1 year ago)

read-only tilecache layer

  • examples/tilecache.html

    old new  
     1<html xmlns="http://www.w3.org/1999/xhtml"> 
     2  <head> 
     3    <style type="text/css"> 
     4        html, body { 
     5            height: 100%; 
     6            margin: 0; 
     7            padding: 0; 
     8        } 
     9        #map { 
     10            width: 100%; 
     11            height: 100%; 
     12        } 
     13        #title { 
     14            position: absolute; 
     15            left: 1em; 
     16            bottom: 1em; 
     17            z-index: 5000; 
     18        } 
     19    </style> 
     20    <script src="../lib/OpenLayers.js"></script> 
     21    <script type="text/javascript"> 
     22        <!-- 
     23        var map, layer; 
     24        function init(){ 
     25            map = new OpenLayers.Map( $('map')); 
     26            layer = new OpenLayers.Layer.TileCache("TileCache Layer",  
     27                    "http://labs.metacarta.com/wms-c/cache/", 
     28                    "basic"); 
     29            map.addLayer(layer); 
     30            map.setCenter(new OpenLayers.LonLat(0, 0), 0); 
     31        } 
     32 
     33        OpenLayers.Util.onImageLoadError = function() { 
     34            /** 
     35             * For images that don't exist in the cache, you can display 
     36             * a default image - one that looks like water for example. 
     37             * To show nothing at all, leave the following lines commented out. 
     38             */ 
     39 
     40            //this.src = "../img/blank.gif"; 
     41            //this.style.display = ""; 
     42        }; 
     43 
     44        // --> 
     45    </script> 
     46  </head> 
     47  <body onload="init()"> 
     48    <div id="map"> 
     49    <div id="title"> 
     50        <b>OpenLayers (Read-Only) TileCache Example</b> 
     51        <br />from a web accessible disk-based cache only 
     52    </div> 
     53  </body> 
     54</html> 
  • lib/OpenLayers.js

    old new  
    9191        "OpenLayers/Layer/Boxes.js", 
    9292        "OpenLayers/Layer/Canvas.js", 
    9393        "OpenLayers/Layer/TMS.js", 
     94        "OpenLayers/Layer/TileCache.js", 
    9495        "OpenLayers/Popup/Anchored.js", 
    9596        "OpenLayers/Popup/AnchoredBubble.js", 
    9697        "OpenLayers/Handler.js", 
  • lib/OpenLayers/Layer/TileCache.js

    old new  
     1/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD licence. 
     2 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt  
     3 * for the full text of the license. */ 
     4 
     5 
     6/** 
     7 * @class 
     8 *  
     9 * @requires OpenLayers/Layer/Grid.js 
     10 */ 
     11OpenLayers.Layer.TileCache = OpenLayers.Class.create(); 
     12OpenLayers.Layer.TileCache.prototype =  
     13  OpenLayers.Class.inherit( OpenLayers.Layer.Grid, { 
     14 
     15    /** @type Boolean **/ 
     16    reproject: false, 
     17     
     18    /** @type Boolean **/ 
     19    isBaseLayer: true, 
     20     
     21    /** @type OpenLayers.LonLat **/ 
     22    tileOrigin: null, 
     23     
     24    /** @type String **/ 
     25    format: 'image/png', 
     26 
     27    /** 
     28    * @constructor 
     29    * 
     30    * @param {String} name 
     31    * @param {String} url 
     32    * @param {String} layername 
     33    * @param {Object} options Hashtable of extra options to tag onto the layer 
     34    */ 
     35    initialize: function(name, url, layername, options) { 
     36        options = OpenLayers.Util.extend({maxResolution: 180/256}, options); 
     37        this.layername = layername; 
     38        OpenLayers.Layer.Grid.prototype.initialize.apply(this, 
     39                                                         [name, url, {}, options]); 
     40        this.extension = this.format.split('/')[1].toLowerCase(); 
     41        this.extension = (this.extension == 'jpeg') ? 'jpg' : this.extension; 
     42    },     
     43 
     44    /** 
     45     *  
     46     */ 
     47    destroy: function() { 
     48        // for now, nothing special to do here.  
     49        OpenLayers.Layer.Grid.prototype.destroy.apply(this, arguments);   
     50    }, 
     51 
     52     
     53    /** 
     54     * @param {Object} obj 
     55     *  
     56     * @returns An exact clone of this OpenLayers.Layer.TileCache 
     57     * @type OpenLayers.Layer.TileCache 
     58     */ 
     59    clone: function (obj) { 
     60         
     61        if (obj == null) { 
     62            obj = new OpenLayers.Layer.TileCache(this.name, 
     63                                           this.url, 
     64                                           this.options); 
     65        } 
     66 
     67        //get all additions from superclasses 
     68        obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]); 
     69 
     70        // copy/set any non-init, non-simple values here 
     71 
     72        return obj; 
     73    },     
     74     
     75    /** 
     76     * @param {OpenLayers.Bounds} bounds 
     77     *  
     78     * @returns A string with the layer's url and parameters and also the  
     79     *           passed-in bounds and appropriate tile size specified as  
     80     *           parameters 
     81     * @type String 
     82     */ 
     83    getURL: function(bounds) { 
     84        var res = this.map.getResolution(); 
     85        var bbox = this.maxExtent; 
     86        var size = this.tileSize; 
     87        var tileX = Math.floor((bounds.left - bbox.left) / (res * size.w)); 
     88        var tileY = Math.floor((bounds.bottom - bbox.bottom) / (res * size.h)); 
     89        var tileZ = this.map.zoom; 
     90        /** 
     91         * Zero-pad a positive integer. 
     92         * @param {Int} number 
     93         * @param {Int} length 
     94         * @type String 
     95         * @return A zero-padded string 
     96         */ 
     97        function zeroPad(number, length) { 
     98            number = String(number); 
     99            var zeros = []; 
     100            for(var i=0; i<length; ++i) { 
     101                zeros.push('0'); 
     102            } 
     103            return zeros.join('').substring(0, length - number.length) + number; 
     104        } 
     105        var components = [ 
     106            this.layername, 
     107            zeroPad(tileZ, 2), 
     108            zeroPad(parseInt(tileX / 1000000), 3), 
     109            zeroPad((parseInt(tileX / 1000) % 1000), 3), 
     110            zeroPad((parseInt(tileX) % 1000), 3), 
     111            zeroPad(parseInt(tileY / 1000000), 3), 
     112            zeroPad((parseInt(tileY / 1000) % 1000), 3), 
     113            zeroPad((parseInt(tileY) % 1000), 3) + '.' + this.extension 
     114        ]; 
     115        var path = components.join('/');  
     116        var url = this.url; 
     117        if (url instanceof Array) { 
     118            url = this.selectUrl(path, url); 
     119        } 
     120        url = (url[url.length - 1] == '/') ? url : url + '/'; 
     121        return url + path; 
     122    }, 
     123 
     124    /** 
     125    * addTile creates a tile, initializes it, and  
     126    * adds it to the layer div.  
     127    * 
     128    * @param {OpenLayers.Bounds} bounds 
     129    * 
     130    * @returns The added OpenLayers.Tile.Image 
     131    * @type OpenLayers.Tile.Image 
     132    */ 
     133    addTile:function(bounds, position) { 
     134        var url = this.getURL(bounds); 
     135        return new OpenLayers.Tile.Image(this, position, bounds,  
     136                                             url, this.tileSize); 
     137    }, 
     138 
     139    /** When the layer is added to a map, then we can fetch our origin  
     140     *  (if we don't have one.)  
     141     *  
     142     * @param {OpenLayers.Map} map 
     143     */ 
     144    setMap: function(map) { 
     145        OpenLayers.Layer.Grid.prototype.setMap.apply(this, arguments); 
     146        if (!this.tileOrigin) {  
     147            this.tileOrigin = new OpenLayers.LonLat(this.map.maxExtent.left, 
     148                                                    this.map.maxExtent.bottom); 
     149        } 
     150    }, 
     151 
     152     
     153    /** @final @type String */ 
     154    CLASS_NAME: "OpenLayers.Layer.TileCache" 
     155});