kepler.gl
Version:
kepler.gl is a webgl based application to visualize large scale location data in the browser
215 lines (174 loc) • 21.4 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getTileData = getTileData;
exports.decodeTile = decodeTile;
exports.vectorTileFeatureToProp = vectorTileFeatureToProp;
var _pbf = _interopRequireDefault(require("pbf"));
var _vectorTile = require("@mapbox/vector-tile");
var _viewportMercatorProject = require("viewport-mercator-project");
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/* global fetch */
var TILE_SIZE = 512;
var MAPBOX_HOST = 'https://a.tiles.mapbox.com';
var MAP_SOURCE = '/v4/mapbox.mapbox-streets-v7';
function getTileData(host, token, _ref) {
var x = _ref.x,
y = _ref.y,
z = _ref.z;
var mapSource = "".concat(host || MAPBOX_HOST).concat(MAP_SOURCE, "/").concat(z, "/").concat(x, "/").concat(y, ".vector.pbf?access_token=").concat(token);
return fetch(mapSource).then(function (response) {
return response.arrayBuffer();
}).then(function (buffer) {
return decodeTile(x, y, z, buffer);
});
}
function decodeTile(x, y, z, arrayBuffer) {
var tile = new _vectorTile.VectorTile(new _pbf["default"](arrayBuffer));
var result = [];
var xProj = x * TILE_SIZE;
var yProj = y * TILE_SIZE;
var scale = Math.pow(2, z);
var projectFunc = project.bind(null, xProj, yProj, scale);
/* eslint-disable guard-for-in */
var layerName = 'building';
var vectorTileLayer = tile.layers[layerName];
if (!vectorTileLayer) {
return [];
}
for (var i = 0; i < vectorTileLayer.length; i++) {
var vectorTileFeature = vectorTileLayer.feature(i);
var features = vectorTileFeatureToProp(vectorTileFeature, projectFunc);
features.forEach(function (f) {
f.properties.layer = layerName;
if (f.properties.height) {
result.push(f);
}
});
}
return result;
}
function project(x, y, scale, line, extent) {
var sizeToPixel = extent / TILE_SIZE;
for (var ii = 0; ii < line.length; ii++) {
var p = line[ii]; // LNGLAT
line[ii] = (0, _viewportMercatorProject.worldToLngLat)([x + p[0] / sizeToPixel, y + p[1] / sizeToPixel], scale);
}
}
/* adapted from @mapbox/vector-tile/lib/vectortilefeature.js for better perf */
/* eslint-disable */
function vectorTileFeatureToProp(vectorTileFeature, project) {
var coords = getCoordinates(vectorTileFeature);
var type = _vectorTile.VectorTileFeature.types[vectorTileFeature.type];
var extent = vectorTileFeature.extent;
var i;
var j;
coords = classifyRings(coords);
for (i = 0; i < coords.length; i++) {
for (j = 0; j < coords[i].length; j++) {
project(coords[i][j], extent);
}
}
return coords.map(function (coordinates) {
return {
coordinates: coordinates,
properties: vectorTileFeature.properties
};
});
}
function getCoordinates(vectorTileFeature) {
var pbf = vectorTileFeature._pbf;
pbf.pos = vectorTileFeature._geometry;
var end = pbf.readVarint() + pbf.pos;
var cmd = 1;
var length = 0;
var x = 0;
var y = 0;
var lines = [];
var line;
while (pbf.pos < end) {
if (length <= 0) {
var cmdLen = pbf.readVarint();
cmd = cmdLen & 0x7;
length = cmdLen >> 3;
}
length--;
if (cmd === 1 || cmd === 2) {
x += pbf.readSVarint();
y += pbf.readSVarint();
if (cmd === 1) {
// moveTo
if (line) lines.push(line);
line = [];
}
line.push([x, y]);
} else if (cmd === 7) {
// Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
if (line) {
line.push(line[0].slice()); // closePolygon
}
} else {
throw new Error("unknown command ".concat(cmd));
}
}
if (line) lines.push(line);
return lines;
} // classifies an array of rings into polygons with outer rings and holes
function classifyRings(rings) {
var len = rings.length;
if (len <= 1) return [rings];
var polygons = [];
var polygon;
var ccw;
for (var i = 0; i < len; i++) {
var area = signedArea(rings[i]);
if (area === 0) {
continue;
}
if (ccw === undefined) {
ccw = area < 0;
}
if (ccw === area < 0) {
if (polygon) {
polygons.push(polygon);
}
polygon = [rings[i]];
} else {
polygon.push(rings[i]);
}
}
if (polygon) {
polygons.push(polygon);
}
return polygons;
}
function signedArea(ring) {
var sum = 0;
for (var i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {
p1 = ring[i];
p2 = ring[j];
sum += (p2[0] - p1[0]) * (p1[1] + p2[1]);
}
return sum;
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9kZWNrZ2wtbGF5ZXJzLzNkLWJ1aWxkaW5nLWxheWVyLzNkLWJ1aWxkaW5nLXV0aWxzLmpzIl0sIm5hbWVzIjpbIlRJTEVfU0laRSIsIk1BUEJPWF9IT1NUIiwiTUFQX1NPVVJDRSIsImdldFRpbGVEYXRhIiwiaG9zdCIsInRva2VuIiwieCIsInkiLCJ6IiwibWFwU291cmNlIiwiZmV0Y2giLCJ0aGVuIiwicmVzcG9uc2UiLCJhcnJheUJ1ZmZlciIsImJ1ZmZlciIsImRlY29kZVRpbGUiLCJ0aWxlIiwiVmVjdG9yVGlsZSIsIlByb3RvYnVmIiwicmVzdWx0IiwieFByb2oiLCJ5UHJvaiIsInNjYWxlIiwiTWF0aCIsInBvdyIsInByb2plY3RGdW5jIi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