| | 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 | */ |
|---|
| | 11 | OpenLayers.Layer.TileCache = OpenLayers.Class.create(); |
|---|
| | 12 | OpenLayers.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 | }); |