[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * @requires OpenLayers/Layer/Grid.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.Layer.TileCache |
---|
| 13 | * A read only TileCache layer. Used to requests tiles cached by TileCache in |
---|
| 14 | * a web accessible cache. This means that you have to pre-populate your |
---|
| 15 | * cache before this layer can be used. It is meant only to read tiles |
---|
| 16 | * created by TileCache, and not to make calls to TileCache for tile |
---|
| 17 | * creation. Create a new instance with the |
---|
| 18 | * <OpenLayers.Layer.TileCache> constructor. |
---|
| 19 | * |
---|
| 20 | * Inherits from: |
---|
| 21 | * - <OpenLayers.Layer.Grid> |
---|
| 22 | */ |
---|
| 23 | OpenLayers.Layer.TileCache = OpenLayers.Class(OpenLayers.Layer.Grid, { |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * APIProperty: isBaseLayer |
---|
| 27 | * {Boolean} Treat this layer as a base layer. Default is true. |
---|
| 28 | */ |
---|
| 29 | isBaseLayer: true, |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * APIProperty: format |
---|
| 33 | * {String} Mime type of the images returned. Default is image/png. |
---|
| 34 | */ |
---|
| 35 | format: 'image/png', |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * APIProperty: serverResolutions |
---|
| 39 | * {Array} A list of all resolutions available on the server. Only set this |
---|
| 40 | * property if the map resolutions differs from the server. |
---|
| 41 | */ |
---|
| 42 | serverResolutions: null, |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * Constructor: OpenLayers.Layer.TileCache |
---|
| 46 | * Create a new read only TileCache layer. |
---|
| 47 | * |
---|
| 48 | * Parameters: |
---|
| 49 | * name - {String} Name of the layer displayed in the interface |
---|
| 50 | * url - {String} Location of the web accessible cache (not the location of |
---|
| 51 | * your tilecache script!) |
---|
| 52 | * layername - {String} Layer name as defined in the TileCache |
---|
| 53 | * configuration |
---|
| 54 | * options - {Object} Optional object with properties to be set on the |
---|
| 55 | * layer. Note that you should speficy your resolutions to match |
---|
| 56 | * your TileCache configuration. This can be done by setting |
---|
| 57 | * the resolutions array directly (here or on the map), by setting |
---|
| 58 | * maxResolution and numZoomLevels, or by using scale based properties. |
---|
| 59 | */ |
---|
| 60 | initialize: function(name, url, layername, options) { |
---|
| 61 | this.layername = layername; |
---|
| 62 | OpenLayers.Layer.Grid.prototype.initialize.apply(this, |
---|
| 63 | [name, url, {}, options]); |
---|
| 64 | this.extension = this.format.split('/')[1].toLowerCase(); |
---|
| 65 | this.extension = (this.extension == 'jpg') ? 'jpeg' : this.extension; |
---|
| 66 | }, |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * APIMethod: clone |
---|
| 70 | * obj - {Object} |
---|
| 71 | * |
---|
| 72 | * Returns: |
---|
| 73 | * {<OpenLayers.Layer.TileCache>} An exact clone of this |
---|
| 74 | * <OpenLayers.Layer.TileCache> |
---|
| 75 | */ |
---|
| 76 | clone: function (obj) { |
---|
| 77 | |
---|
| 78 | if (obj == null) { |
---|
| 79 | obj = new OpenLayers.Layer.TileCache(this.name, |
---|
| 80 | this.url, |
---|
| 81 | this.layername, |
---|
| 82 | this.getOptions()); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | //get all additions from superclasses |
---|
| 86 | obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]); |
---|
| 87 | |
---|
| 88 | // copy/set any non-init, non-simple values here |
---|
| 89 | |
---|
| 90 | return obj; |
---|
| 91 | }, |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Method: getURL |
---|
| 95 | * |
---|
| 96 | * Parameters: |
---|
| 97 | * bounds - {<OpenLayers.Bounds>} |
---|
| 98 | * |
---|
| 99 | * Returns: |
---|
| 100 | * {String} A string with the layer's url and parameters and also the |
---|
| 101 | * passed-in bounds and appropriate tile size specified as parameters. |
---|
| 102 | */ |
---|
| 103 | getURL: function(bounds) { |
---|
| 104 | var res = this.map.getResolution(); |
---|
| 105 | var bbox = this.maxExtent; |
---|
| 106 | var size = this.tileSize; |
---|
| 107 | var tileX = Math.round((bounds.left - bbox.left) / (res * size.w)); |
---|
| 108 | var tileY = Math.round((bounds.bottom - bbox.bottom) / (res * size.h)); |
---|
| 109 | var tileZ = this.serverResolutions != null ? |
---|
| 110 | OpenLayers.Util.indexOf(this.serverResolutions, res) : |
---|
| 111 | this.map.getZoom(); |
---|
| 112 | /** |
---|
| 113 | * Zero-pad a positive integer. |
---|
| 114 | * number - {Int} |
---|
| 115 | * length - {Int} |
---|
| 116 | * |
---|
| 117 | * Returns: |
---|
| 118 | * {String} A zero-padded string |
---|
| 119 | */ |
---|
| 120 | function zeroPad(number, length) { |
---|
| 121 | number = String(number); |
---|
| 122 | var zeros = []; |
---|
| 123 | for(var i=0; i<length; ++i) { |
---|
| 124 | zeros.push('0'); |
---|
| 125 | } |
---|
| 126 | return zeros.join('').substring(0, length - number.length) + number; |
---|
| 127 | } |
---|
| 128 | var components = [ |
---|
| 129 | this.layername, |
---|
| 130 | zeroPad(tileZ, 2), |
---|
| 131 | zeroPad(parseInt(tileX / 1000000), 3), |
---|
| 132 | zeroPad((parseInt(tileX / 1000) % 1000), 3), |
---|
| 133 | zeroPad((parseInt(tileX) % 1000), 3), |
---|
| 134 | zeroPad(parseInt(tileY / 1000000), 3), |
---|
| 135 | zeroPad((parseInt(tileY / 1000) % 1000), 3), |
---|
| 136 | zeroPad((parseInt(tileY) % 1000), 3) + '.' + this.extension |
---|
| 137 | ]; |
---|
| 138 | var path = components.join('/'); |
---|
| 139 | var url = this.url; |
---|
| 140 | if (url instanceof Array) { |
---|
| 141 | url = this.selectUrl(path, url); |
---|
| 142 | } |
---|
| 143 | url = (url.charAt(url.length - 1) == '/') ? url : url + '/'; |
---|
| 144 | return url + path; |
---|
| 145 | }, |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * Method: addTile |
---|
| 149 | * Create a tile, initialize it, and add it to the layer div. |
---|
| 150 | * |
---|
| 151 | * Parameters: |
---|
| 152 | * bounds - {<OpenLayers.Bounds>} |
---|
| 153 | * position - {<OpenLayers.Pixel>} |
---|
| 154 | * |
---|
| 155 | * Returns: |
---|
| 156 | * {<OpenLayers.Tile.Image>} The added <OpenLayers.Tile.Image> |
---|
| 157 | */ |
---|
| 158 | addTile:function(bounds, position) { |
---|
| 159 | var url = this.getURL(bounds); |
---|
| 160 | return new OpenLayers.Tile.Image(this, position, bounds, |
---|
| 161 | url, this.tileSize); |
---|
| 162 | }, |
---|
| 163 | |
---|
| 164 | CLASS_NAME: "OpenLayers.Layer.TileCache" |
---|
| 165 | }); |
---|