maplibre-gl
Version:
BSD licensed community fork of mapbox-gl, a WebGL interactive maps library
49 lines (45 loc) • 1.85 kB
text/typescript
import {MAX_TILE_ZOOM, MIN_TILE_ZOOM} from './util.ts';
import type {LngLat} from '../geo/lng_lat.ts';
import type {WorldCoordinateHelper} from '../geo/transform_interface.ts';
/**
* Returns true if a given tile zoom (Z), X, and Y are in the bounds of the world.
* Zoom bounds are the minimum zoom (inclusive) through the maximum zoom (inclusive).
* X and Y bounds are 0 (inclusive) to their respective zoom-dependent maxima (exclusive).
*
* @param zoom - the tile zoom (Z)
* @param x - the tile X
* @param y - the tile Y
* @returns `true` if a given tile zoom, X, and Y are in the bounds of the world.
*/
export function isInBoundsForTileZoomXY(zoom: number, x: number, y: number): boolean {
return !(
zoom < MIN_TILE_ZOOM ||
zoom > MAX_TILE_ZOOM ||
y < 0 ||
y >= Math.pow(2, zoom) ||
x < 0 ||
x >= Math.pow(2, zoom)
);
}
/**
* Returns true if a given zoom and `LngLat` are in the bounds of the world.
* Does not wrap `LngLat` when checking if in bounds.
* Zoom bounds are the minimum zoom (inclusive) through the maximum zoom (inclusive).
* `LngLat` bounds are the world's north-west corner (inclusive) to its south-east corner (exclusive).
*
* @param zoom - the tile zoom (Z)
* @param LngLat - the `LngLat` object containing the longitude and latitude
* @param worldCoordinateHelper - the lng/lat to world mapping to use
* @returns `true` if a given zoom and `LngLat` are in the bounds of the world.
*/
export function isInBoundsForZoomLngLat(zoom: number, lnglat: LngLat, worldCoordinateHelper: WorldCoordinateHelper): boolean {
const {x, y} = worldCoordinateHelper.worldFromLngLat(lnglat.lng, lnglat.lat);
return !(
zoom < MIN_TILE_ZOOM ||
zoom > MAX_TILE_ZOOM ||
y < 0 ||
y >= 1 ||
x < 0 ||
x >= 1
);
}