From e128bab6254fdbdd3af11a261e9f9725a6d2782e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 2 Apr 2013 11:00:39 +0200 Subject: [PATCH] Do not pass tile grid to getTile and tileUrlFunc getTile and the tileUrlFunc are functions of the source, so they do need to be passed the tile grid. The tile source knows its tile grid, and can get the projection's tile grid if it doesn't have a tile grid. --- .../canvas/canvastilelayerrenderer.js | 4 +-- src/ol/renderer/dom/domtilelayerrenderer.js | 4 +-- src/ol/renderer/layerrenderer.js | 7 ++-- .../renderer/webgl/webgltilelayerrenderer.js | 4 +-- src/ol/source/bingmapssource.js | 4 ++- src/ol/source/imagetilesource.js | 6 ++-- src/ol/source/tiledwmssource.js | 6 +++- src/ol/source/tilesource.js | 1 - src/ol/source/wmtssource.js | 4 ++- src/ol/tileurlfunction.js | 32 +++++++++++-------- test/spec/ol/source/wms.test.js | 25 +++++++-------- test/spec/ol/tileurlfunction.test.js | 5 +-- 12 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/ol/renderer/canvas/canvastilelayerrenderer.js b/src/ol/renderer/canvas/canvastilelayerrenderer.js index 64a1398344..34e71cc1b4 100644 --- a/src/ol/renderer/canvas/canvastilelayerrenderer.js +++ b/src/ol/renderer/canvas/canvastilelayerrenderer.js @@ -195,7 +195,7 @@ ol.renderer.canvas.TileLayer.prototype.renderFrame = var getTileIfLoaded = this.createGetTileIfLoadedFunction(function(tile) { return !goog.isNull(tile) && tile.getState() == ol.TileState.LOADED; - }, tileSource, tileGrid, projection); + }, tileSource, projection); var findLoadedTiles = goog.bind(tileSource.findLoadedTiles, tileSource, tilesToDrawByZ, getTileIfLoaded); @@ -204,7 +204,7 @@ ol.renderer.canvas.TileLayer.prototype.renderFrame = for (x = tileRange.minX; x <= tileRange.maxX; ++x) { for (y = tileRange.minY; y <= tileRange.maxY; ++y) { - tile = tileSource.getTile(z, x, y, tileGrid, projection); + tile = tileSource.getTile(z, x, y, projection); tileState = tile.getState(); if (tileState == ol.TileState.LOADED || tileState == ol.TileState.EMPTY) { tilesToDrawByZ[z][tile.tileCoord.toString()] = tile; diff --git a/src/ol/renderer/dom/domtilelayerrenderer.js b/src/ol/renderer/dom/domtilelayerrenderer.js index 61cb6a3908..1f6e70c7fd 100644 --- a/src/ol/renderer/dom/domtilelayerrenderer.js +++ b/src/ol/renderer/dom/domtilelayerrenderer.js @@ -107,7 +107,7 @@ ol.renderer.dom.TileLayer.prototype.renderFrame = var getTileIfLoaded = this.createGetTileIfLoadedFunction(function(tile) { return !goog.isNull(tile) && tile.getState() == ol.TileState.LOADED; - }, tileSource, tileGrid, projection); + }, tileSource, projection); var findLoadedTiles = goog.bind(tileSource.findLoadedTiles, tileSource, tilesToDrawByZ, getTileIfLoaded); @@ -116,7 +116,7 @@ ol.renderer.dom.TileLayer.prototype.renderFrame = for (x = tileRange.minX; x <= tileRange.maxX; ++x) { for (y = tileRange.minY; y <= tileRange.maxY; ++y) { - tile = tileSource.getTile(z, x, y, tileGrid, projection); + tile = tileSource.getTile(z, x, y, projection); tileState = tile.getState(); if (tileState == ol.TileState.LOADED) { tilesToDrawByZ[z][tile.tileCoord.toString()] = tile; diff --git a/src/ol/renderer/layerrenderer.js b/src/ol/renderer/layerrenderer.js index ec05d79c8f..6e0b6f16c6 100644 --- a/src/ol/renderer/layerrenderer.js +++ b/src/ol/renderer/layerrenderer.js @@ -249,15 +249,14 @@ ol.renderer.Layer.prototype.updateUsedTiles = * @param {function(ol.Tile): boolean} isLoadedFunction Function to * determine if the tile is loaded. * @param {ol.source.TileSource} tileSource Tile source. - * @param {ol.tilegrid.TileGrid} tileGrid Tile grid. * @param {ol.Projection} projection Projection. * @return {function(number, number, number): ol.Tile} Returns a tile if it is * loaded. */ ol.renderer.Layer.prototype.createGetTileIfLoadedFunction = - function(isLoadedFunction, tileSource, tileGrid, projection) { + function(isLoadedFunction, tileSource, projection) { return function(z, x, y) { - var tile = tileSource.getTile(z, x, y, tileGrid, projection); + var tile = tileSource.getTile(z, x, y, projection); return isLoadedFunction(tile) ? tile : null; }; }; @@ -314,7 +313,7 @@ ol.renderer.Layer.prototype.manageTilePyramid = function( for (x = tileRange.minX; x <= tileRange.maxX; ++x) { for (y = tileRange.minY; y <= tileRange.maxY; ++y) { if (currentZ - z <= preload) { - tile = tileSource.getTile(z, x, y, tileGrid, projection); + tile = tileSource.getTile(z, x, y, projection); if (tile.getState() == ol.TileState.IDLE) { wantedTiles[tile.tileCoord.toString()] = true; if (!tileQueue.isKeyQueued(tile.getKey())) { diff --git a/src/ol/renderer/webgl/webgltilelayerrenderer.js b/src/ol/renderer/webgl/webgltilelayerrenderer.js index 690dd6ffaa..74c321eabd 100644 --- a/src/ol/renderer/webgl/webgltilelayerrenderer.js +++ b/src/ol/renderer/webgl/webgltilelayerrenderer.js @@ -205,7 +205,7 @@ ol.renderer.webgl.TileLayer.prototype.renderFrame = var getTileIfLoaded = this.createGetTileIfLoadedFunction(function(tile) { return !goog.isNull(tile) && tile.getState() == ol.TileState.LOADED && mapRenderer.isTileTextureLoaded(tile); - }, tileSource, tileGrid, projection); + }, tileSource, projection); var findLoadedTiles = goog.bind(tileSource.findLoadedTiles, tileSource, tilesToDrawByZ, getTileIfLoaded); @@ -214,7 +214,7 @@ ol.renderer.webgl.TileLayer.prototype.renderFrame = for (x = tileRange.minX; x <= tileRange.maxX; ++x) { for (y = tileRange.minY; y <= tileRange.maxY; ++y) { - tile = tileSource.getTile(z, x, y, tileGrid, projection); + tile = tileSource.getTile(z, x, y, projection); tileState = tile.getState(); if (tileState == ol.TileState.LOADED) { if (mapRenderer.isTileTextureLoaded(tile)) { diff --git a/src/ol/source/bingmapssource.js b/src/ol/source/bingmapssource.js index d4dd57b24d..3e28a73bea 100644 --- a/src/ol/source/bingmapssource.js +++ b/src/ol/source/bingmapssource.js @@ -103,7 +103,9 @@ ol.source.BingMaps.prototype.handleImageryMetadataResponse = var imageUrl = resource.imageUrl .replace('{subdomain}', subdomain) .replace('{culture}', culture); - return function(tileCoord) { + return function(tileCoord, projection) { + goog.asserts.assert(ol.projection.equivalent( + projection, this.getProjection())); if (goog.isNull(tileCoord)) { return undefined; } else { diff --git a/src/ol/source/imagetilesource.js b/src/ol/source/imagetilesource.js index 11e0f96d37..9df28a9328 100644 --- a/src/ol/source/imagetilesource.js +++ b/src/ol/source/imagetilesource.js @@ -87,16 +87,14 @@ ol.source.ImageTileSource.prototype.expireCache = function(usedTiles) { /** * @inheritDoc */ -ol.source.ImageTileSource.prototype.getTile = - function(z, x, y, tileGrid, projection) { +ol.source.ImageTileSource.prototype.getTile = function(z, x, y, projection) { var tileCoordKey = ol.TileCoord.getKeyZXY(z, x, y); if (this.tileCache_.containsKey(tileCoordKey)) { return /** @type {!ol.Tile} */ (this.tileCache_.get(tileCoordKey)); } else { - goog.asserts.assert(tileGrid); goog.asserts.assert(projection); var tileCoord = new ol.TileCoord(z, x, y); - var tileUrl = this.tileUrlFunction(tileCoord, tileGrid, projection); + var tileUrl = this.tileUrlFunction(tileCoord, projection); var tile = new ol.ImageTile( tileCoord, goog.isDef(tileUrl) ? ol.TileState.IDLE : ol.TileState.EMPTY, diff --git a/src/ol/source/tiledwmssource.js b/src/ol/source/tiledwmssource.js index d719aab9a0..35a27ca3e3 100644 --- a/src/ol/source/tiledwmssource.js +++ b/src/ol/source/tiledwmssource.js @@ -42,7 +42,11 @@ ol.source.TiledWMS = function(tiledWMSOptions) { tiledWMSOptions.params['TRANSPARENT'] : true; var extent = tiledWMSOptions.extent; - var tileCoordTransform = function(tileCoord, tileGrid, projection) { + var tileCoordTransform = function(tileCoord, projection) { + var tileGrid = this.getTileGrid(); + if (goog.isNull(tileGrid)) { + tileGrid = ol.tilegrid.getForProjection(projection); + } if (tileGrid.getResolutions().length <= tileCoord.z) { return null; } diff --git a/src/ol/source/tilesource.js b/src/ol/source/tilesource.js index 7811ebe233..6ef41ebbdb 100644 --- a/src/ol/source/tilesource.js +++ b/src/ol/source/tilesource.js @@ -124,7 +124,6 @@ ol.source.TileSource.prototype.getResolutions = function() { * @param {number} z Tile coordinate z. * @param {number} x Tile coordinate x. * @param {number} y Tile coordinate y. - * @param {ol.tilegrid.TileGrid=} opt_tileGrid Tile grid. * @param {ol.Projection=} opt_projection Projection. * @return {!ol.Tile} Tile. */ diff --git a/src/ol/source/wmtssource.js b/src/ol/source/wmtssource.js index 65fc67523f..7b44040945 100644 --- a/src/ol/source/wmtssource.js +++ b/src/ol/source/wmtssource.js @@ -112,7 +112,9 @@ ol.source.WMTS = function(wmtsOptions) { } tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform( - function(tileCoord, tileGrid, projection) { + function(tileCoord, projection) { + var tileGrid = this.getTileGrid(); + goog.asserts.assert(!goog.isNull(tileGrid)); if (tileGrid.getResolutions().length <= tileCoord.z) { return null; } diff --git a/src/ol/tileurlfunction.js b/src/ol/tileurlfunction.js index a586fe9ca7..aafc989add 100644 --- a/src/ol/tileurlfunction.js +++ b/src/ol/tileurlfunction.js @@ -4,11 +4,10 @@ goog.provide('ol.TileUrlFunctionType'); goog.require('goog.array'); goog.require('goog.math'); goog.require('ol.TileCoord'); -goog.require('ol.tilegrid.TileGrid'); /** - * @typedef {function(this:ol.source.Source, ol.TileCoord, ol.tilegrid.TileGrid, + * @typedef {function(this:ol.source.ImageTileSource, ol.TileCoord, * ol.Projection): (string|undefined)} */ ol.TileUrlFunctionType; @@ -49,12 +48,12 @@ ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) { if (tileUrlFunctions.length === 1) { return tileUrlFunctions[0]; } - return function(tileCoord, tileGrid, projection) { + return function(tileCoord, projection) { if (goog.isNull(tileCoord)) { return undefined; } else { var index = goog.math.modulo(tileCoord.hash(), tileUrlFunctions.length); - return tileUrlFunctions[index](tileCoord, tileGrid, projection); + return tileUrlFunctions[index].call(this, tileCoord, projection); } }; }; @@ -63,20 +62,24 @@ ol.TileUrlFunction.createFromTileUrlFunctions = function(tileUrlFunctions) { /** * @param {string} baseUrl Base URL (may have query data). * @param {Object.} params to encode in the url. - * @param {function(string, Object., ol.Extent, ol.Size, - * ol.Projection)} paramsFunction params function. + * @param {function(this: ol.source.ImageTileSource, string, Object., + * ol.Extent, ol.Size, ol.Projection)} paramsFunction params function. * @return {ol.TileUrlFunctionType} Tile URL function. */ ol.TileUrlFunction.createFromParamsFunction = function(baseUrl, params, paramsFunction) { - return function(tileCoord, tileGrid, projection) { + return function(tileCoord, projection) { if (goog.isNull(tileCoord)) { return undefined; } else { + var tileGrid = this.getTileGrid(); + if (goog.isNull(tileGrid)) { + tileGrid = ol.tilegrid.getForProjection(projection); + } var size = tileGrid.getTileSize(tileCoord.z); var extent = tileGrid.getTileCoordExtent(tileCoord); - return paramsFunction( - baseUrl, params, extent, size, projection); + return paramsFunction.call(this, baseUrl, params, + extent, size, projection); } }; }; @@ -84,27 +87,28 @@ ol.TileUrlFunction.createFromParamsFunction = /** * @param {ol.TileCoord} tileCoord Tile coordinate. + * @param {ol.Projection} projection Projection. * @return {string|undefined} Tile URL. */ -ol.TileUrlFunction.nullTileUrlFunction = function(tileCoord) { +ol.TileUrlFunction.nullTileUrlFunction = function(tileCoord, projection) { return undefined; }; /** - * @param {function(ol.TileCoord, ol.tilegrid.TileGrid, ol.Projection): - * ol.TileCoord} transformFn Transform.function. + * @param {function(this:ol.source.ImageTileSource, ol.TileCoord, + * ol.Projection) : ol.TileCoord} transformFn Transform function. * @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL function. * @return {ol.TileUrlFunctionType} Tile URL function. */ ol.TileUrlFunction.withTileCoordTransform = function(transformFn, tileUrlFunction) { - return function(tileCoord, tileGrid, projection) { + return function(tileCoord, projection) { if (goog.isNull(tileCoord)) { return undefined; } else { return tileUrlFunction.call(this, - transformFn(tileCoord, tileGrid, projection), tileGrid, projection); + transformFn.call(this, tileCoord, projection), projection); } }; }; diff --git a/test/spec/ol/source/wms.test.js b/test/spec/ol/source/wms.test.js index 7d7be5d829..9c9f74d786 100644 --- a/test/spec/ol/source/wms.test.js +++ b/test/spec/ol/source/wms.test.js @@ -5,35 +5,32 @@ describe('ol.source.wms', function() { describe('ol.source.wms.getUrl', function() { it('creates expected URL', function() { var epsg3857 = ol.projection.get('EPSG:3857'); - var tileGrid = ol.tilegrid.getForProjection(epsg3857); - var tileUrlFunction = ol.TileUrlFunction.createFromParamsFunction( - 'http://wms', {'foo': 'bar'}, ol.source.wms.getUrl); - var tileCoord = new ol.TileCoord(1, 0, 0); - var tileUrl = tileUrlFunction(tileCoord, tileGrid, epsg3857); + var extent = new ol.Extent( + -20037508.342789244, -20037508.342789244, 0, 0); var expected = 'http://wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=' + 'GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&WIDTH=256&HEIGHT=256&' + 'foo=bar&STYLES=&CRS=EPSG%3A3857&BBOX=' + '-20037508.342789244%2C-20037508.342789244%2C0%2C0'; - expect(tileUrl).to.eql(expected); + var url = ol.source.wms.getUrl('http://wms', {'foo': 'bar'}, + extent, new ol.Size(256, 256), epsg3857); + expect(url).to.eql(expected); }); it('creates expected URL respecting axis orientation', function() { var epsg4326 = ol.projection.get('EPSG:4326'); - var tileGrid = ol.tilegrid.getForProjection(epsg4326); - var tileUrlFunction = ol.TileUrlFunction.createFromParamsFunction( - 'http://wms', {'foo': 'bar'}, ol.source.wms.getUrl); - var tileCoord = new ol.TileCoord(1, 0, 0); - var tileUrl = tileUrlFunction(tileCoord, tileGrid, epsg4326); + var extent = new ol.Extent(-180, -90, 0, 90); var expected = 'http://wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=' + 'GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&WIDTH=256&HEIGHT=256&' + 'foo=bar&STYLES=&CRS=EPSG%3A4326&BBOX=-90%2C-180%2C90%2C0'; - expect(tileUrl).to.eql(expected); + var url = ol.source.wms.getUrl('http://wms', {'foo': 'bar'}, + extent, new ol.Size(256, 256), epsg4326); + expect(url).to.eql(expected); }); }); }); -goog.require('ol.TileCoord'); -goog.require('ol.TileUrlFunction'); +goog.require('ol.Extent'); +goog.require('ol.Size'); goog.require('ol.projection'); goog.require('ol.source.wms'); diff --git a/test/spec/ol/tileurlfunction.test.js b/test/spec/ol/tileurlfunction.test.js index 6b5bd490d8..ae3e583a84 100644 --- a/test/spec/ol/tileurlfunction.test.js +++ b/test/spec/ol/tileurlfunction.test.js @@ -81,12 +81,13 @@ describe('ol.TileUrlFunction', function() { describe('createFromParamsFunction', function() { var paramsFunction = function(url, params) { return arguments; }; var projection = ol.projection.get('EPSG:3857'); + var fakeTileSource = {getTileGrid: function() {return null;}}; var params = {foo: 'bar'}; var tileUrlFunction = ol.TileUrlFunction.createFromParamsFunction( 'url', params, paramsFunction); it('calls the passed function with the correct arguments', function() { - var args = tileUrlFunction(new ol.TileCoord(0, 0, 0), - ol.tilegrid.getForProjection(projection), projection); + var args = tileUrlFunction.call(fakeTileSource, + new ol.TileCoord(0, 0, 0), projection); expect(args[0]).to.eql('url'); expect(args[1]).to.be(params); expect(args[2]).to.eql(projection.getExtent());