kepler.gl
Version:
kepler.gl is a webgl based application to visualize large scale location data in the browser
83 lines (78 loc) • 10.1 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.MAPBOX_TILE_SIZE = void 0;
exports.getCenterAndZoomFromBounds = getCenterAndZoomFromBounds;
exports.normalizeEvent = normalizeEvent;
exports.validateBounds = validateBounds;
var _geoViewport = _interopRequireDefault(require("@mapbox/geo-viewport"));
var _console = _interopRequireDefault(require("global/console"));
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
var MAPBOX_TILE_SIZE = exports.MAPBOX_TILE_SIZE = 512;
function isLat(num) {
return Number.isFinite(num) && num <= 90 && num >= -90;
}
function isLng(num) {
return Number.isFinite(num) && num <= 180 && num >= -180;
}
/**
* bounds should be [minLng, minLat, maxLng, maxLat]
* @param {*} bounds
*/
function validateBounds(bounds) {
// array: [ -180, -85.05112877980659, 180, 85.0511287798066 ]
// validate bounds
if (Array.isArray(bounds) && bounds.length === 4 && [bounds[0], bounds[2]].every(isLng) && [bounds[1], bounds[3]].every(isLat)) {
return bounds;
}
return null;
}
function getCenterAndZoomFromBounds(bounds, _ref) {
var width = _ref.width,
height = _ref.height;
var validBounds = validateBounds(bounds);
if (!validBounds) {
_console["default"].warn('invalid map bounds provided');
return null;
}
// viewport(bounds, dimensions, minzoom, maxzoom, tileSize, allowFloat)
var _geoViewport$viewport = _geoViewport["default"].viewport(bounds, [width, height], undefined, undefined, MAPBOX_TILE_SIZE),
zoom = _geoViewport$viewport.zoom;
// center being calculated by geo-vieweport.viewport has a complex logic that
// projects and then unprojects the coordinates to determine the center
// Calculating a simple average instead as that is the expected behavior in most of cases
var center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2];
return {
zoom: zoom,
center: center
};
}
/**
* Add extra info about screen space position and world position to the event.
* @param {*} event Event to normalize.
* @param {*} viewport Current viewport.
* @returns Normalized event with extra information compatible with React-map-gl MapLayerMouseEvent
* https://visgl.github.io/react-map-gl/docs/api-reference/types#maplayermouseevent
*/
function normalizeEvent(event, viewport) {
var _event$target;
var bounds = (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.getBoundingClientRect();
if (!bounds) {
return event;
}
var x = event.clientX - bounds.left;
var y = event.clientY - bounds.top;
if (!Number.isFinite(x) || !Number.isFinite(y)) {
return event;
}
event.point = [x, y];
var location = viewport.unproject(event.point, {
targetZ: 0
});
event.lngLat = [location[0], location[1]];
return event;
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfZ2VvVmlld3BvcnQiLCJfaW50ZXJvcFJlcXVpcmVEZWZhdWx0IiwicmVxdWlyZSIsIl9jb25zb2xlIiwiTUFQQk9YX1RJTEVfU0laRSIsImV4cG9ydHMiLCJpc0xhdCIsIm51bSIsIk51bWJlciIsImlzRmluaXRlIiwiaXNMbmciLCJ2YWxpZGF0ZUJvdW5kcyIsImJvdW5kcyIsIkFycmF5IiwiaXNBcnJheSIsImxlbmd0aCIsImV2ZXJ5IiwiZ2V0Q2VudGVyQW5kWm9vbUZyb21Cb3VuZHMiLCJfcmVmIiwid2lkdGgiLCJoZWlnaHQiLCJ2YWxpZEJvdW5kcyIsIkNvbnNvbGUiLCJ3YXJuIiwiX2dlb1ZpZXdwb3J0JHZpZXdwb3J0IiwiZ2VvVmlld3BvcnQiLCJ2aWV3cG9ydCIsInVuZGVmaW5lZCIsInpvb20iLCJjZW50ZXIiLCJub3JtYWxpemVFdmVudCIsImV2ZW50IiwiX2V2ZW50JHRhcmdldCIsInRhcmdldCIsImdldEJvdW5kaW5nQ2xpZW50UmVjdCIsIngiLCJjbGllbnRYIiwibGVmdCIsInkiLCJjbGllbnRZIiwidG9wIiwicG9pbnQiLCJsb2NhdGlvbiIsInVucHJvamVjdCIsInRhcmdldFoiLCJsbmdMYXQiXSwic291cmNlcyI6WyIuLi9zcmMvcHJvamVjdGlv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