UNPKG

@deck.gl/experimental-layers

Version:

Experimental layers for deck.gl

90 lines (72 loc) 3.82 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import { lngLatToWorld } from 'viewport-mercator-project'; const TILE_SIZE = 512; function getBoundingBox(viewport) { const corners = [viewport.unproject([0, 0]), viewport.unproject([viewport.width, 0]), viewport.unproject([0, viewport.height]), viewport.unproject([viewport.width, viewport.height])]; return [corners.reduce((minLng, p) => minLng < p[0] ? minLng : p[0], 180), corners.reduce((minLat, p) => minLat < p[1] ? minLat : p[1], 90), corners.reduce((maxLng, p) => maxLng > p[0] ? maxLng : p[0], -180), corners.reduce((maxLat, p) => maxLat > p[1] ? maxLat : p[1], -90)]; } function pixelsToTileIndex(a) { return Math.floor(a / TILE_SIZE); } /** * Returns all tile indices in the current viewport. If the current zoom level is smaller * than minZoom, return an empty array. If the current zoom level is greater than maxZoom, * return tiles that are on maxZoom. */ export function getTileIndices(viewport, maxZoom, minZoom) { const z = Math.floor(viewport.zoom); if (minZoom && z < minZoom) { return []; } viewport = new viewport.constructor(_objectSpread({}, viewport, { zoom: z })); const bbox = getBoundingBox(viewport); const _lngLatToWorld$map = lngLatToWorld([bbox[0], bbox[3]], viewport.scale).map(pixelsToTileIndex), _lngLatToWorld$map2 = _slicedToArray(_lngLatToWorld$map, 2), minX = _lngLatToWorld$map2[0], minY = _lngLatToWorld$map2[1]; const _lngLatToWorld$map3 = lngLatToWorld([bbox[2], bbox[1]], viewport.scale).map(pixelsToTileIndex), _lngLatToWorld$map4 = _slicedToArray(_lngLatToWorld$map3, 2), maxX = _lngLatToWorld$map4[0], maxY = _lngLatToWorld$map4[1]; const indices = []; for (let x = minX; x <= maxX; x++) { for (let y = minY; y <= maxY; y++) { if (maxZoom && z > maxZoom) { indices.push(getAdjustedTileIndex({ x, y, z }, maxZoom)); } else { indices.push({ x, y, z }); } } } return indices; } /** * Calculates and returns a new tile index {x, y, z}, with z being the given adjustedZ. */ function getAdjustedTileIndex(_ref, adjustedZ) { let x = _ref.x, y = _ref.y, z = _ref.z; const m = Math.pow(2, z - adjustedZ); return { x: Math.floor(x / m), y: Math.floor(y / m), z: adjustedZ }; } //# sourceMappingURL=viewport-util.js.map