package.tileurlfunction.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ol Show documentation
Show all versions of ol Show documentation
OpenLayers mapping library
The newest version!
/**
* @module ol/tileurlfunction
*/
import {modulo} from './math.js';
import {renderXYZTemplate} from './uri.js';
import {hash as tileCoordHash} from './tilecoord.js';
/**
* @param {string} template Template.
* @param {import("./tilegrid/TileGrid.js").default|null} tileGrid Tile grid.
* @return {import("./Tile.js").UrlFunction} Tile URL function.
*/
export function createFromTemplate(template, tileGrid) {
return (
/**
* @param {import("./tilecoord.js").TileCoord} tileCoord Tile Coordinate.
* @param {number} pixelRatio Pixel ratio.
* @param {import("./proj/Projection.js").default} projection Projection.
* @return {string|undefined} Tile URL.
*/
function (tileCoord, pixelRatio, projection) {
if (!tileCoord) {
return undefined;
}
let maxY;
const z = tileCoord[0];
if (tileGrid) {
// The `{-y}` placeholder only works for sources that have a tile grid at construction
const range = tileGrid.getFullTileRange(z);
if (range) {
maxY = range.getHeight() - 1;
}
}
return renderXYZTemplate(template, z, tileCoord[1], tileCoord[2], maxY);
}
);
}
/**
* @param {Array} templates Templates.
* @param {import("./tilegrid/TileGrid.js").default} tileGrid Tile grid.
* @return {import("./Tile.js").UrlFunction} Tile URL function.
*/
export function createFromTemplates(templates, tileGrid) {
const len = templates.length;
const tileUrlFunctions = new Array(len);
for (let i = 0; i < len; ++i) {
tileUrlFunctions[i] = createFromTemplate(templates[i], tileGrid);
}
return createFromTileUrlFunctions(tileUrlFunctions);
}
/**
* @param {Array} tileUrlFunctions Tile URL Functions.
* @return {import("./Tile.js").UrlFunction} Tile URL function.
*/
export function createFromTileUrlFunctions(tileUrlFunctions) {
if (tileUrlFunctions.length === 1) {
return tileUrlFunctions[0];
}
return (
/**
* @param {import("./tilecoord.js").TileCoord} tileCoord Tile Coordinate.
* @param {number} pixelRatio Pixel ratio.
* @param {import("./proj/Projection.js").default} projection Projection.
* @return {string|undefined} Tile URL.
*/
function (tileCoord, pixelRatio, projection) {
if (!tileCoord) {
return undefined;
}
const h = tileCoordHash(tileCoord);
const index = modulo(h, tileUrlFunctions.length);
return tileUrlFunctions[index](tileCoord, pixelRatio, projection);
}
);
}
/**
* @param {import("./tilecoord.js").TileCoord} tileCoord Tile coordinate.
* @param {number} pixelRatio Pixel ratio.
* @param {import("./proj/Projection.js").default} projection Projection.
* @return {string|undefined} Tile URL.
*/
export function nullTileUrlFunction(tileCoord, pixelRatio, projection) {
return undefined;
}
/**
* TODO: Update ol-mapbox-style to import this from the uri.js module.
*/
export {expandUrl} from './uri.js';