Simplify logic for fixed tile url functions
This commit is contained in:
@@ -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,
|
||||||
@@ -169,41 +170,6 @@ class TileArcGISRest extends TileImage {
|
|||||||
return /** @type {number} */ (pixelRatio);
|
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.
|
* Update the user-provided params.
|
||||||
* @param {Object} params 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;
|
export default TileArcGISRest;
|
||||||
|
|||||||
@@ -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,
|
||||||
@@ -321,52 +322,6 @@ class TileWMS extends TileImage {
|
|||||||
return res.join('/');
|
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.
|
* Update the user-provided params.
|
||||||
* @param {Object} params 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;
|
export default TileWMS;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -310,7 +310,7 @@ describe('ol.source.TileWMS', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('#setUrls()', function() {
|
describe('#setUrls()', function() {
|
||||||
it ('updates the source key', function() {
|
it('updates the source key', function() {
|
||||||
const source = new TileWMS({
|
const source = new TileWMS({
|
||||||
urls: ['u1', 'u2']
|
urls: ['u1', 'u2']
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user