diff --git a/src/ol/source/TileArcGISRest.js b/src/ol/source/TileArcGISRest.js index 6d4a0613e9..471042e227 100644 --- a/src/ol/source/TileArcGISRest.js +++ b/src/ol/source/TileArcGISRest.js @@ -76,6 +76,7 @@ class TileArcGISRest extends TileImage { reprojectionErrorThreshold: options.reprojectionErrorThreshold, tileGrid: options.tileGrid, tileLoadFunction: options.tileLoadFunction, + tileUrlFunction: tileUrlFunction, url: options.url, urls: options.urls, wrapX: options.wrapX !== undefined ? options.wrapX : true, @@ -169,41 +170,6 @@ class TileArcGISRest extends TileImage { return /** @type {number} */ (pixelRatio); } - /** - * @inheritDoc - */ - fixedTileUrlFunction(tileCoord, pixelRatio, projection) { - - let tileGrid = this.getTileGrid(); - if (!tileGrid) { - tileGrid = this.getTileGridForProjection(projection); - } - - if (tileGrid.getResolutions().length <= tileCoord[0]) { - return undefined; - } - - const tileExtent = tileGrid.getTileCoordExtent( - tileCoord, this.tmpExtent_); - let tileSize = toSize( - tileGrid.getTileSize(tileCoord[0]), this.tmpSize); - - if (pixelRatio != 1) { - tileSize = scaleSize(tileSize, pixelRatio, this.tmpSize); - } - - // Apply default params and override with user specified values. - const baseParams = { - 'F': 'image', - 'FORMAT': 'PNG32', - 'TRANSPARENT': true - }; - assign(baseParams, this.params_); - - return this.getRequestUrl_(tileCoord, tileSize, tileExtent, - pixelRatio, projection, baseParams); - } - /** * Update the user-provided params. * @param {Object} params Params. @@ -215,5 +181,44 @@ class TileArcGISRest extends TileImage { } } +/** + * @param {import("../tilecoord.js").TileCoord} tileCoord The tile coordinate + * @param {number} pixelRatio The pixel ratio + * @param {import("../proj/Projection.js").default} projection The projection + * @return {string|undefined} The tile URL + * @this {TileArcGISRest} + */ +function tileUrlFunction(tileCoord, pixelRatio, projection) { + + let tileGrid = this.getTileGrid(); + if (!tileGrid) { + tileGrid = this.getTileGridForProjection(projection); + } + + if (tileGrid.getResolutions().length <= tileCoord[0]) { + return undefined; + } + + const tileExtent = tileGrid.getTileCoordExtent( + tileCoord, this.tmpExtent_); + let tileSize = toSize( + tileGrid.getTileSize(tileCoord[0]), this.tmpSize); + + if (pixelRatio != 1) { + tileSize = scaleSize(tileSize, pixelRatio, this.tmpSize); + } + + // Apply default params and override with user specified values. + const baseParams = { + 'F': 'image', + 'FORMAT': 'PNG32', + 'TRANSPARENT': true + }; + assign(baseParams, this.params_); + + return this.getRequestUrl_(tileCoord, tileSize, tileExtent, + pixelRatio, projection, baseParams); +} + export default TileArcGISRest; diff --git a/src/ol/source/TileWMS.js b/src/ol/source/TileWMS.js index 4c6760b6a6..a69f5518e8 100644 --- a/src/ol/source/TileWMS.js +++ b/src/ol/source/TileWMS.js @@ -98,6 +98,7 @@ class TileWMS extends TileImage { tileClass: options.tileClass, tileGrid: options.tileGrid, tileLoadFunction: options.tileLoadFunction, + tileUrlFunction: tileUrlFunction, url: options.url, urls: options.urls, wrapX: options.wrapX !== undefined ? options.wrapX : true, @@ -321,52 +322,6 @@ class TileWMS extends TileImage { return res.join('/'); } - /** - * @inheritDoc - */ - fixedTileUrlFunction(tileCoord, pixelRatio, projection) { - - let tileGrid = this.getTileGrid(); - if (!tileGrid) { - tileGrid = this.getTileGridForProjection(projection); - } - - if (tileGrid.getResolutions().length <= tileCoord[0]) { - return undefined; - } - - if (pixelRatio != 1 && (!this.hidpi_ || this.serverType_ === undefined)) { - pixelRatio = 1; - } - - const tileResolution = tileGrid.getResolution(tileCoord[0]); - let tileExtent = tileGrid.getTileCoordExtent(tileCoord, this.tmpExtent_); - let tileSize = toSize( - tileGrid.getTileSize(tileCoord[0]), this.tmpSize); - - const gutter = this.gutter_; - if (gutter !== 0) { - tileSize = bufferSize(tileSize, gutter, this.tmpSize); - tileExtent = buffer(tileExtent, tileResolution * gutter, tileExtent); - } - - if (pixelRatio != 1) { - tileSize = scaleSize(tileSize, pixelRatio, this.tmpSize); - } - - const baseParams = { - 'SERVICE': 'WMS', - 'VERSION': DEFAULT_WMS_VERSION, - 'REQUEST': 'GetMap', - 'FORMAT': 'image/png', - 'TRANSPARENT': true - }; - assign(baseParams, this.params_); - - return this.getRequestUrl_(tileCoord, tileSize, tileExtent, - pixelRatio, projection, baseParams); - } - /** * Update the user-provided params. * @param {Object} params Params. @@ -387,5 +342,55 @@ class TileWMS extends TileImage { } } +/** + * @param {import("../tilecoord.js").TileCoord} tileCoord The tile coordinate + * @param {number} pixelRatio The pixel ratio + * @param {import("../proj/Projection.js").default} projection The projection + * @return {string|undefined} The tile URL + * @this {TileWMS} + */ +function tileUrlFunction(tileCoord, pixelRatio, projection) { + + let tileGrid = this.getTileGrid(); + if (!tileGrid) { + tileGrid = this.getTileGridForProjection(projection); + } + + if (tileGrid.getResolutions().length <= tileCoord[0]) { + return undefined; + } + + if (pixelRatio != 1 && (!this.hidpi_ || this.serverType_ === undefined)) { + pixelRatio = 1; + } + + const tileResolution = tileGrid.getResolution(tileCoord[0]); + let tileExtent = tileGrid.getTileCoordExtent(tileCoord, this.tmpExtent_); + let tileSize = toSize( + tileGrid.getTileSize(tileCoord[0]), this.tmpSize); + + const gutter = this.gutter_; + if (gutter !== 0) { + tileSize = bufferSize(tileSize, gutter, this.tmpSize); + tileExtent = buffer(tileExtent, tileResolution * gutter, tileExtent); + } + + if (pixelRatio != 1) { + tileSize = scaleSize(tileSize, pixelRatio, this.tmpSize); + } + + const baseParams = { + 'SERVICE': 'WMS', + 'VERSION': DEFAULT_WMS_VERSION, + 'REQUEST': 'GetMap', + 'FORMAT': 'image/png', + 'TRANSPARENT': true + }; + assign(baseParams, this.params_); + + return this.getRequestUrl_(tileCoord, tileSize, tileExtent, + pixelRatio, projection, baseParams); +} + export default TileWMS; diff --git a/src/ol/source/UrlTile.js b/src/ol/source/UrlTile.js index 548e30bddf..0a2c02d311 100644 --- a/src/ol/source/UrlTile.js +++ b/src/ol/source/UrlTile.js @@ -50,6 +50,12 @@ class UrlTile extends TileSource { transition: options.transition }); + /** + * @private + * @type {boolean} + */ + this.generateTileUrlFunction_ = !options.tileUrlFunction; + /** * @protected * @type {import("../Tile.js").LoadFunction} @@ -60,8 +66,7 @@ class UrlTile extends TileSource { * @protected * @type {import("../Tile.js").UrlFunction} */ - this.tileUrlFunction = this.fixedTileUrlFunction ? - this.fixedTileUrlFunction.bind(this) : nullTileUrlFunction; + this.tileUrlFunction = options.tileUrlFunction ? options.tileUrlFunction.bind(this) : nullTileUrlFunction; /** * @protected @@ -74,9 +79,6 @@ class UrlTile extends TileSource { } else if (options.url) { this.setUrl(options.url); } - if (options.tileUrlFunction) { - this.setTileUrlFunction(options.tileUrlFunction); - } /** * @private @@ -173,9 +175,7 @@ class UrlTile extends TileSource { */ setUrl(url) { const urls = this.urls = expandUrl(url); - this.setTileUrlFunction(this.fixedTileUrlFunction ? - this.fixedTileUrlFunction.bind(this) : - createFromTemplates(urls, this.tileGrid), url); + this.setUrls(urls); } /** @@ -186,9 +186,11 @@ class UrlTile extends TileSource { setUrls(urls) { this.urls = urls; const key = urls.join('\n'); - this.setTileUrlFunction(this.fixedTileUrlFunction ? - this.fixedTileUrlFunction.bind(this) : - createFromTemplates(urls, this.tileGrid), key); + if (this.generateTileUrlFunction_) { + this.setTileUrlFunction(createFromTemplates(urls, this.tileGrid), key); + } else { + this.setKey(key); + } } /** @@ -203,10 +205,4 @@ class UrlTile extends TileSource { } -/** - * @type {import("../Tile.js").UrlFunction|undefined} - * @protected - */ -UrlTile.prototype.fixedTileUrlFunction; - export default UrlTile; diff --git a/src/ol/source/WMTS.js b/src/ol/source/WMTS.js index ae957552e3..ef7615b708 100644 --- a/src/ol/source/WMTS.js +++ b/src/ol/source/WMTS.js @@ -159,9 +159,7 @@ class WMTS extends TileImage { setUrls(urls) { this.urls = urls; const key = urls.join('\n'); - this.setTileUrlFunction(this.fixedTileUrlFunction ? - this.fixedTileUrlFunction.bind(this) : - createFromTileUrlFunctions(urls.map(createFromWMTSTemplate.bind(this))), key); + this.setTileUrlFunction(createFromTileUrlFunctions(urls.map(createFromWMTSTemplate.bind(this))), key); } /** diff --git a/test/spec/ol/source/tilewms.test.js b/test/spec/ol/source/tilewms.test.js index a00fd9e67b..8a7563d17e 100644 --- a/test/spec/ol/source/tilewms.test.js +++ b/test/spec/ol/source/tilewms.test.js @@ -310,7 +310,7 @@ describe('ol.source.TileWMS', function() { }); describe('#setUrls()', function() { - it ('updates the source key', function() { + it('updates the source key', function() { const source = new TileWMS({ urls: ['u1', 'u2'] });