OpenLayers OpenLayers

Ticket #627: tilecache.2.patch

File tilecache.2.patch, 13.9 kB (added by crschmidt, 1 year ago)
  • tests/Layer/test_TileCache.html

    old new  
     1<html> 
     2<head> 
     3  <script src="../../lib/OpenLayers.js"></script> 
     4  <script type="text/javascript"><!-- 
     5    var isMozilla = (navigator.userAgent.indexOf("compatible") == -1); 
     6    var layer;  
     7 
     8    var name = 'Test Layer'; 
     9    var url = "http://labs.metacarta.com/wms-c/Basic.py/"; 
     10    var layername = "basic"; 
     11    var options = {'type':'png'};  
     12 
     13 
     14    function test_01_Layer_TileCache_constructor (t) { 
     15        t.plan( 1 ); 
     16                        
     17        layer = new OpenLayers.Layer.TileCache(name, url, layername, options); 
     18        t.ok( layer instanceof OpenLayers.Layer.TileCache, "returns OpenLayers.Layer.TileCache object" ); 
     19    } 
     20 
     21 
     22 
     23    function test_03_Layer_TileCache_clearTiles (t) { 
     24        t.plan( 1 ); 
     25        var map = new OpenLayers.Map('map'); 
     26        layer = new OpenLayers.Layer.TileCache(name, url, layername, options); 
     27        map.addLayer(layer); 
     28 
     29        map.setCenter(new OpenLayers.LonLat(0,0)); 
     30 
     31        //grab a reference to one of the tiles 
     32        var tile = layer.grid[0][0];         
     33 
     34        layer.clearGrid(); 
     35 
     36        t.ok( layer.grid != null, "layer.grid does not get nullified" ); 
     37    } 
     38 
     39 
     40    function test_04_Layer_TileCache_getTileCacheBounds(t) { 
     41        t.plan( 1 ); 
     42 
     43        layer = new OpenLayers.Layer.TileCache(name, url, layername, options); 
     44 
     45        var bl = { bounds: new OpenLayers.Bounds(1,2,0,0)}; 
     46        var tr = { bounds: new OpenLayers.Bounds(0,0,3,4)}; 
     47        layer.grid = [ [6, tr],  
     48                       [bl, 7]]; 
     49 
     50        var bounds = layer.getGridBounds(); 
     51     
     52        var testBounds = new OpenLayers.Bounds(1,2,3,4); 
     53         
     54        t.ok( bounds.equals(testBounds), "getTileCacheBounds() returns correct bounds") 
     55         
     56        layer.grid = null; 
     57    } 
     58 
     59    function test_05_Layer_TileCache_getResolution(t) { 
     60        t.plan( 1 ); 
     61 
     62        var map = new OpenLayers.Map('map'); 
     63        layer = new OpenLayers.Layer.TileCache(name, url, layername, options); 
     64        map.addLayer(layer); 
     65 
     66        map.zoom = 5; 
     67 
     68        t.eq( layer.getResolution(), 0.02197265625, "getResolution() returns correct value"); 
     69    } 
     70 
     71    function test_06_Layer_TileCache_getZoomForExtent(t) { 
     72        t.plan( 2 ); 
     73        var bounds, zoom; 
     74 
     75        var map = new OpenLayers.Map('map'); 
     76        layer = new OpenLayers.Layer.TileCache(name, url, layername, options); 
     77        map.addLayer(layer); 
     78 
     79        bounds = new OpenLayers.Bounds(10,10,12,12); 
     80        zoom = layer.getZoomForExtent(bounds); 
     81 
     82        t.eq( zoom, 7, "getZoomForExtent() returns correct value"); 
     83 
     84        bounds = new OpenLayers.Bounds(10,10,100,100); 
     85        zoom = layer.getZoomForExtent(bounds); 
     86 
     87        t.eq( zoom, 1, "getZoomForExtent() returns correct value"); 
     88    }    
     89 
     90 
     91    /** THIS WOULD BE WHERE THE TESTS WOULD GO FOR  
     92     *      
     93     *    -moveTo 
     94     *    -insertColumn 
     95     *    -insertRow 
     96     
     97    function 07_Layer_TileCache_moveTo(t) { 
     98    } 
     99 
     100    function 08_Layer_TileCache_insertColumn(t) { 
     101    } 
     102 
     103    function 09_Layer_TileCache_insertRow(t) { 
     104    } 
     105 
     106     *  
     107     */ 
     108    function test_10_Layer_TileCache_getURL(t) { 
     109 
     110        t.plan(2); 
     111         
     112        var map = new OpenLayers.Map('map', options); 
     113        var options = {'layername':'basic', 'type':'png'};  
     114        layer = new OpenLayers.Layer.TileCache(name, url, layername, options); 
     115        map.addLayer(layer); 
     116        map.setCenter(new OpenLayers.LonLat(0,0), 9); 
     117        var tileurl = layer.getURL(new OpenLayers.Bounds(3.515625,45,4.21875,45.703125)); 
     118        t.eq(tileurl, "http://labs.metacarta.com/wms-c/Basic.py/basic/09/000/000/522/000/000/384.png", "Tile URL is correct"); 
     119 
     120        layer.url = ["http://tilecache1/", "http://tilecache2/", "http://tilecache3/"]; 
     121        tileurl = layer.getURL(new OpenLayers.Bounds(3.515625,45,4.21875,45.703125)); 
     122        t.eq(tileurl, "http://tilecache3/basic/09/000/000/522/000/000/384.png", "Tile URL is deterministic"); 
     123    } 
     124 
     125    function test_11_Layer_TileCache_setMap(t) { 
     126 
     127        t.plan(3); 
     128         
     129        var map = new OpenLayers.Map('map', options); 
     130        layer = new OpenLayers.Layer.TileCache(name, url, layername, options); 
     131 
     132        t.eq(layer.tileOrigin, null, "Tile origin starts out null"); 
     133        layer.setMap(map); 
     134         
     135        t.eq(layer.tileOrigin.lat, -90, "lat is -90"); 
     136        t.eq(layer.tileOrigin.lon, -180, "lon is -180"); 
     137    } 
     138 
     139    function test_99_Layer_TileCache_destroy (t) { 
     140 
     141        t.plan( 3 ); 
     142 
     143        var map = new OpenLayers.Map('map'); 
     144        layer = new OpenLayers.Layer.TileCache(name, url, layername, options); 
     145        map.addLayer(layer); 
     146        layer.destroy(); 
     147        t.eq( layer.grid, null, "layer.grid is null after destroy" ); 
     148        t.eq( layer.tileSize, null, "layer.tileSize is null after destroy" ); 
     149 
     150 
     151    //test with tile creation 
     152        layer = new OpenLayers.Layer.TileCache(name, url, options); 
     153        map.addLayer(layer); 
     154        map.setCenter(new OpenLayers.LonLat(0,0), 5); 
     155        //grab a reference to one of the tiles 
     156        var tile = layer.grid[0][0];         
     157 
     158        layer.destroy(); 
     159 
     160        t.ok( layer.grid == null, "tiles appropriately destroyed"); 
     161    } 
     162     
     163       // --> 
     164  </script> 
     165</head> 
     166<body> 
     167<div id="map" style="width:500px;height:550px"></div> 
     168</body> 
     169</html> 
  • tests/list-tests.html

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

    old new  
    110110            "OpenLayers/Layer/GeoRSS.js", 
    111111            "OpenLayers/Layer/Boxes.js", 
    112112            "OpenLayers/Layer/TMS.js", 
     113            "OpenLayers/Layer/TileCache.js", 
    113114            "OpenLayers/Popup/Anchored.js", 
    114115            "OpenLayers/Popup/AnchoredBubble.js", 
    115116            "OpenLayers/Handler.js", 
  • 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://c0.labs.metacarta.com/wms-c/cache/", 
     28                     "http://c1.labs.metacarta.com/wms-c/cache/", 
     29                     "http://c2.labs.metacarta.com/wms-c/cache/", 
     30                     "http://c3.labs.metacarta.com/wms-c/cache/", 
     31                     "http://c4.labs.metacarta.com/wms-c/cache/"], 
     32                    "basic"); 
     33            map.addLayer(layer); 
     34            map.setCenter(new OpenLayers.LonLat(0, 0), 0); 
     35        } 
     36 
     37        OpenLayers.Util.onImageLoadError = function() { 
     38            /** 
     39             * For images that don't exist in the cache, you can display 
     40             * a default image - one that looks like water for example. 
     41             * To show nothing at all, leave the following lines commented out. 
     42             */ 
     43 
     44            //this.src = "../img/blank.gif"; 
     45            //this.style.display = ""; 
     46        }; 
     47 
     48        // --> 
     49    </script> 
     50  </head> 
     51  <body onload="init()"> 
     52    <div id="map"> 
     53    <div id="title"> 
     54        <b>OpenLayers (Read-Only) TileCache Example</b> 
     55        <br />from a web accessible disk-based cache only 
     56    </div> 
     57  </body> 
     58</html>