2019-09-17 12:54:35 -04:00
|
|
|
import SphericalMercator from '@mapbox/sphericalmercator';
|
|
|
|
|
2019-11-07 02:48:51 -05:00
|
|
|
import { BoundingBox, TileParams } from './types';
|
2019-09-17 12:54:35 -04:00
|
|
|
|
|
|
|
const TILE_SIZE = 256;
|
|
|
|
|
|
|
|
const mercator = new SphericalMercator({
|
|
|
|
size: TILE_SIZE
|
|
|
|
});
|
|
|
|
|
|
|
|
function getBbox(z, x, y) {
|
|
|
|
return mercator.bbox(x, y, z, false, '900913');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getXYZ(bbox, z) {
|
2019-11-07 03:13:30 -05:00
|
|
|
return mercator.xyz(bbox, z, false, '900913');
|
2019-09-17 12:54:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatParams({ tileset, z, x, y, scale }: TileParams): string {
|
|
|
|
return `${tileset}/${z}/${x}/${y}@${scale}x`;
|
|
|
|
}
|
|
|
|
|
2019-10-07 08:34:22 -04:00
|
|
|
function isOutsideExtent({ x, y, z }: TileParams, bbox: BoundingBox) {
|
|
|
|
const xy = getXYZ(bbox, z);
|
|
|
|
return xy.minY > y || xy.maxY < y || xy.minX > x || xy.maxX < x;
|
|
|
|
}
|
|
|
|
|
2019-09-17 12:54:35 -04:00
|
|
|
export {
|
|
|
|
TILE_SIZE,
|
|
|
|
getBbox,
|
|
|
|
getXYZ,
|
2019-10-07 08:34:22 -04:00
|
|
|
formatParams,
|
|
|
|
isOutsideExtent
|
2019-09-17 12:54:35 -04:00
|
|
|
};
|