Simplify logic for fixed tile url functions

This commit is contained in:
ahocevar
2018-10-02 10:52:03 +02:00
parent 023ad3c531
commit 4aff3d0631
5 changed files with 106 additions and 102 deletions

View File

@@ -76,6 +76,7 @@ class TileArcGISRest extends TileImage {
reprojectionErrorThreshold: options.reprojectionErrorThreshold, reprojectionErrorThreshold: options.reprojectionErrorThreshold,
tileGrid: options.tileGrid, tileGrid: options.tileGrid,
tileLoadFunction: options.tileLoadFunction, tileLoadFunction: options.tileLoadFunction,
tileUrlFunction: tileUrlFunction,
url: options.url, url: options.url,
urls: options.urls, urls: options.urls,
wrapX: options.wrapX !== undefined ? options.wrapX : true, wrapX: options.wrapX !== undefined ? options.wrapX : true,
@@ -170,9 +171,24 @@ class TileArcGISRest extends TileImage {
} }
/** /**
* @inheritDoc * Update the user-provided params.
* @param {Object} params Params.
* @api
*/ */
fixedTileUrlFunction(tileCoord, pixelRatio, projection) { updateParams(params) {
assign(this.params_, params);
this.setKey(this.getKeyForParams_());
}
}
/**
* @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(); let tileGrid = this.getTileGrid();
if (!tileGrid) { if (!tileGrid) {
@@ -204,16 +220,5 @@ class TileArcGISRest extends TileImage {
pixelRatio, projection, baseParams); pixelRatio, projection, baseParams);
} }
/**
* Update the user-provided params.
* @param {Object} params Params.
* @api
*/
updateParams(params) {
assign(this.params_, params);
this.setKey(this.getKeyForParams_());
}
}
export default TileArcGISRest; export default TileArcGISRest;

View File

@@ -98,6 +98,7 @@ class TileWMS extends TileImage {
tileClass: options.tileClass, tileClass: options.tileClass,
tileGrid: options.tileGrid, tileGrid: options.tileGrid,
tileLoadFunction: options.tileLoadFunction, tileLoadFunction: options.tileLoadFunction,
tileUrlFunction: tileUrlFunction,
url: options.url, url: options.url,
urls: options.urls, urls: options.urls,
wrapX: options.wrapX !== undefined ? options.wrapX : true, wrapX: options.wrapX !== undefined ? options.wrapX : true,
@@ -322,9 +323,33 @@ class TileWMS extends TileImage {
} }
/** /**
* @inheritDoc * Update the user-provided params.
* @param {Object} params Params.
* @api
*/ */
fixedTileUrlFunction(tileCoord, pixelRatio, projection) { updateParams(params) {
assign(this.params_, params);
this.updateV13_();
this.setKey(this.getKeyForParams_());
}
/**
* @private
*/
updateV13_() {
const version = this.params_['VERSION'] || DEFAULT_WMS_VERSION;
this.v13_ = compareVersions(version, '1.3') >= 0;
}
}
/**
* @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(); let tileGrid = this.getTileGrid();
if (!tileGrid) { if (!tileGrid) {
@@ -367,25 +392,5 @@ class TileWMS extends TileImage {
pixelRatio, projection, baseParams); pixelRatio, projection, baseParams);
} }
/**
* Update the user-provided params.
* @param {Object} params Params.
* @api
*/
updateParams(params) {
assign(this.params_, params);
this.updateV13_();
this.setKey(this.getKeyForParams_());
}
/**
* @private
*/
updateV13_() {
const version = this.params_['VERSION'] || DEFAULT_WMS_VERSION;
this.v13_ = compareVersions(version, '1.3') >= 0;
}
}
export default TileWMS; export default TileWMS;

View File

@@ -50,6 +50,12 @@ class UrlTile extends TileSource {
transition: options.transition transition: options.transition
}); });
/**
* @private
* @type {boolean}
*/
this.generateTileUrlFunction_ = !options.tileUrlFunction;
/** /**
* @protected * @protected
* @type {import("../Tile.js").LoadFunction} * @type {import("../Tile.js").LoadFunction}
@@ -60,8 +66,7 @@ class UrlTile extends TileSource {
* @protected * @protected
* @type {import("../Tile.js").UrlFunction} * @type {import("../Tile.js").UrlFunction}
*/ */
this.tileUrlFunction = this.fixedTileUrlFunction ? this.tileUrlFunction = options.tileUrlFunction ? options.tileUrlFunction.bind(this) : nullTileUrlFunction;
this.fixedTileUrlFunction.bind(this) : nullTileUrlFunction;
/** /**
* @protected * @protected
@@ -74,9 +79,6 @@ class UrlTile extends TileSource {
} else if (options.url) { } else if (options.url) {
this.setUrl(options.url); this.setUrl(options.url);
} }
if (options.tileUrlFunction) {
this.setTileUrlFunction(options.tileUrlFunction);
}
/** /**
* @private * @private
@@ -173,9 +175,7 @@ class UrlTile extends TileSource {
*/ */
setUrl(url) { setUrl(url) {
const urls = this.urls = expandUrl(url); const urls = this.urls = expandUrl(url);
this.setTileUrlFunction(this.fixedTileUrlFunction ? this.setUrls(urls);
this.fixedTileUrlFunction.bind(this) :
createFromTemplates(urls, this.tileGrid), url);
} }
/** /**
@@ -186,9 +186,11 @@ class UrlTile extends TileSource {
setUrls(urls) { setUrls(urls) {
this.urls = urls; this.urls = urls;
const key = urls.join('\n'); const key = urls.join('\n');
this.setTileUrlFunction(this.fixedTileUrlFunction ? if (this.generateTileUrlFunction_) {
this.fixedTileUrlFunction.bind(this) : this.setTileUrlFunction(createFromTemplates(urls, this.tileGrid), key);
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; export default UrlTile;

View File

@@ -159,9 +159,7 @@ class WMTS extends TileImage {
setUrls(urls) { setUrls(urls) {
this.urls = urls; this.urls = urls;
const key = urls.join('\n'); const key = urls.join('\n');
this.setTileUrlFunction(this.fixedTileUrlFunction ? this.setTileUrlFunction(createFromTileUrlFunctions(urls.map(createFromWMTSTemplate.bind(this))), key);
this.fixedTileUrlFunction.bind(this) :
createFromTileUrlFunctions(urls.map(createFromWMTSTemplate.bind(this))), key);
} }
/** /**