UNPKG

@deck.gl/geo-layers

Version:

deck.gl layers supporting geospatial use cases and GIS formats

247 lines (202 loc) 7.62 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getURLFromTemplate = getURLFromTemplate; exports.osmTile2lngLat = osmTile2lngLat; exports.tileToBoundingBox = tileToBoundingBox; exports.getTileIndices = getTileIndices; exports.isURLTemplate = isURLTemplate; exports.urlType = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _tile2dTraversal = require("./tile-2d-traversal"); var TILE_SIZE = 512; var DEFAULT_EXTENT = [-Infinity, -Infinity, Infinity, Infinity]; var urlType = { type: 'url', value: null, validate: function validate(value, propType) { return propType.optional && value === null || typeof value === 'string' || Array.isArray(value) && value.every(function (url) { return typeof url === 'string'; }); }, equals: function equals(value1, value2) { if (value1 === value2) { return true; } if (!Array.isArray(value1) || !Array.isArray(value2)) { return false; } var len = value1.length; if (len !== value2.length) { return false; } for (var i = 0; i < len; i++) { if (value1[i] !== value2[i]) { return false; } } return true; } }; exports.urlType = urlType; function transformBox(bbox, modelMatrix) { var transformedCoords = [modelMatrix.transformAsPoint([bbox[0], bbox[1]]), modelMatrix.transformAsPoint([bbox[2], bbox[1]]), modelMatrix.transformAsPoint([bbox[0], bbox[3]]), modelMatrix.transformAsPoint([bbox[2], bbox[3]])]; var transformedBox = [Math.min.apply(Math, (0, _toConsumableArray2.default)(transformedCoords.map(function (i) { return i[0]; }))), Math.min.apply(Math, (0, _toConsumableArray2.default)(transformedCoords.map(function (i) { return i[1]; }))), Math.max.apply(Math, (0, _toConsumableArray2.default)(transformedCoords.map(function (i) { return i[0]; }))), Math.max.apply(Math, (0, _toConsumableArray2.default)(transformedCoords.map(function (i) { return i[1]; })))]; return transformedBox; } function getURLFromTemplate(template, properties) { if (!template || !template.length) { return null; } if (Array.isArray(template)) { var index = Math.abs(properties.x + properties.y) % template.length; template = template[index]; } var x = properties.x, y = properties.y, z = properties.z; return template.replace(/\{x\}/g, x).replace(/\{y\}/g, y).replace(/\{z\}/g, z).replace(/\{-y\}/g, Math.pow(2, z) - y - 1); } function getBoundingBox(viewport, zRange, extent) { var bounds; if (zRange && zRange.length === 2) { var _zRange = (0, _slicedToArray2.default)(zRange, 2), minZ = _zRange[0], maxZ = _zRange[1]; var bounds0 = viewport.getBounds({ z: minZ }); var bounds1 = viewport.getBounds({ z: maxZ }); bounds = [Math.min(bounds0[0], bounds1[0]), Math.min(bounds0[1], bounds1[1]), Math.max(bounds0[2], bounds1[2]), Math.max(bounds0[3], bounds1[3])]; } else { bounds = viewport.getBounds(); } if (!viewport.isGeospatial) { return [Math.max(Math.min(bounds[0], extent[2]), extent[0]), Math.max(Math.min(bounds[1], extent[3]), extent[1]), Math.min(Math.max(bounds[2], extent[0]), extent[2]), Math.min(Math.max(bounds[3], extent[1]), extent[3])]; } return [Math.max(bounds[0], extent[0]), Math.max(bounds[1], extent[1]), Math.min(bounds[2], extent[2]), Math.min(bounds[3], extent[3])]; } function getIndexingCoords(bbox, scale, modelMatrixInverse) { if (modelMatrixInverse) { var transformedTileIndex = transformBox(bbox, modelMatrixInverse).map(function (i) { return i * scale / TILE_SIZE; }); return transformedTileIndex; } return bbox.map(function (i) { return i * scale / TILE_SIZE; }); } function getScale(z, tileSize) { return Math.pow(2, z) * TILE_SIZE / tileSize; } function osmTile2lngLat(x, y, z) { var scale = getScale(z, TILE_SIZE); var lng = x / scale * 360 - 180; var n = Math.PI - 2 * Math.PI * y / scale; var lat = 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))); return [lng, lat]; } function tile2XY(x, y, z, tileSize) { var scale = getScale(z, tileSize); return [x / scale * TILE_SIZE, y / scale * TILE_SIZE]; } function tileToBoundingBox(viewport, x, y, z) { var tileSize = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : TILE_SIZE; if (viewport.isGeospatial) { var _osmTile2lngLat = osmTile2lngLat(x, y, z), _osmTile2lngLat2 = (0, _slicedToArray2.default)(_osmTile2lngLat, 2), west = _osmTile2lngLat2[0], north = _osmTile2lngLat2[1]; var _osmTile2lngLat3 = osmTile2lngLat(x + 1, y + 1, z), _osmTile2lngLat4 = (0, _slicedToArray2.default)(_osmTile2lngLat3, 2), east = _osmTile2lngLat4[0], south = _osmTile2lngLat4[1]; return { west: west, north: north, east: east, south: south }; } var _tile2XY = tile2XY(x, y, z, tileSize), _tile2XY2 = (0, _slicedToArray2.default)(_tile2XY, 2), left = _tile2XY2[0], top = _tile2XY2[1]; var _tile2XY3 = tile2XY(x + 1, y + 1, z, tileSize), _tile2XY4 = (0, _slicedToArray2.default)(_tile2XY3, 2), right = _tile2XY4[0], bottom = _tile2XY4[1]; return { left: left, top: top, right: right, bottom: bottom }; } function getIdentityTileIndices(viewport, z, tileSize, extent, modelMatrixInverse) { var bbox = getBoundingBox(viewport, null, extent); var scale = getScale(z, tileSize); var _getIndexingCoords = getIndexingCoords(bbox, scale, modelMatrixInverse), _getIndexingCoords2 = (0, _slicedToArray2.default)(_getIndexingCoords, 4), minX = _getIndexingCoords2[0], minY = _getIndexingCoords2[1], maxX = _getIndexingCoords2[2], maxY = _getIndexingCoords2[3]; var indices = []; for (var x = Math.floor(minX); x < maxX; x++) { for (var y = Math.floor(minY); y < maxY; y++) { indices.push({ x: x, y: y, z: z }); } } return indices; } function getTileIndices(_ref) { var viewport = _ref.viewport, maxZoom = _ref.maxZoom, minZoom = _ref.minZoom, zRange = _ref.zRange, extent = _ref.extent, _ref$tileSize = _ref.tileSize, tileSize = _ref$tileSize === void 0 ? TILE_SIZE : _ref$tileSize, modelMatrix = _ref.modelMatrix, modelMatrixInverse = _ref.modelMatrixInverse, _ref$zoomOffset = _ref.zoomOffset, zoomOffset = _ref$zoomOffset === void 0 ? 0 : _ref$zoomOffset; var z = viewport.isGeospatial ? Math.round(viewport.zoom + Math.log2(TILE_SIZE / tileSize)) + zoomOffset : Math.ceil(viewport.zoom) + zoomOffset; if (Number.isFinite(minZoom) && z < minZoom) { if (!extent) { return []; } z = minZoom; } if (Number.isFinite(maxZoom) && z > maxZoom) { z = maxZoom; } var transformedExtent = extent; if (modelMatrix && modelMatrixInverse && extent && !viewport.isGeospatial) { transformedExtent = transformBox(extent, modelMatrix); } return viewport.isGeospatial ? (0, _tile2dTraversal.getOSMTileIndices)(viewport, z, zRange, extent) : getIdentityTileIndices(viewport, z, tileSize, transformedExtent || DEFAULT_EXTENT, modelMatrixInverse); } function isURLTemplate(s) { return /(?=.*{z})(?=.*{x})(?=.*({y}|{-y}))/.test(s); } //# sourceMappingURL=utils.js.map