@kibeo/loaders.gl-mvt
Version:
Loader for Mapbox Vector Tiles
260 lines (214 loc) • 8.22 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.TEST_EXPORTS = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _polygon = require("@math.gl/polygon");
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
var endPos, cmd, cmdLen, length, x, y, i;
var TEST_EXPORTS = {
classifyRings: classifyRings
};
exports.TEST_EXPORTS = TEST_EXPORTS;
var VectorTileFeature = function () {
function VectorTileFeature(pbf, end, extent, keys, values, firstPassData) {
(0, _classCallCheck2.default)(this, VectorTileFeature);
this.properties = {};
this.extent = extent;
this.type = 0;
this.id = null;
this._pbf = pbf;
this._geometry = -1;
this._keys = keys;
this._values = values;
this._firstPassData = firstPassData;
pbf.readFields(readFeature, this, end);
}
(0, _createClass2.default)(VectorTileFeature, [{
key: "loadGeometry",
value: function loadGeometry() {
var pbf = this._pbf;
pbf.pos = this._geometry;
endPos = pbf.readVarint() + pbf.pos;
cmd = 1;
length = 0;
x = 0;
y = 0;
i = 0;
var lines = [];
var data = [];
while (pbf.pos < endPos) {
if (length <= 0) {
cmdLen = pbf.readVarint();
cmd = cmdLen & 0x7;
length = cmdLen >> 3;
}
length--;
if (cmd === 1 || cmd === 2) {
x += pbf.readSVarint();
y += pbf.readSVarint();
if (cmd === 1) {
lines.push(i);
}
data.push(x, y);
i += 2;
} else if (cmd === 7) {
if (i > 0) {
var start = lines[lines.length - 1];
data.push(data[start], data[start + 1]);
i += 2;
}
} else {
throw new Error("unknown command ".concat(cmd));
}
}
return {
data: data,
lines: lines
};
}
}, {
key: "_toBinaryCoordinates",
value: function _toBinaryCoordinates(transform) {
var geom = this.loadGeometry();
transform(geom.data, this);
var coordLength = 2;
switch (this.type) {
case 1:
this._firstPassData.pointFeaturesCount++;
this._firstPassData.pointPositionsCount += geom.lines.length;
break;
case 2:
this._firstPassData.lineFeaturesCount++;
this._firstPassData.linePathsCount += geom.lines.length;
this._firstPassData.linePositionsCount += geom.data.length / coordLength;
break;
case 3:
var classified = classifyRings(geom);
this._firstPassData.polygonFeaturesCount++;
this._firstPassData.polygonObjectsCount += classified.lines.length;
var _iterator = _createForOfIteratorHelper(classified.lines),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var lines = _step.value;
this._firstPassData.polygonRingsCount += lines.length;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
this._firstPassData.polygonPositionsCount += classified.data.length / coordLength;
geom = classified;
break;
}
geom.type = VectorTileFeature.types[this.type];
if (geom.lines.length > 1) {
geom.type = "Multi".concat(geom.type);
}
var result = {
type: 'Feature',
geometry: geom,
properties: this.properties
};
if (this.id !== null) {
result.id = this.id;
}
return result;
}
}, {
key: "toBinaryCoordinates",
value: function toBinaryCoordinates(options) {
if (typeof options === 'function') {
return this._toBinaryCoordinates(options);
}
var x = options.x,
y = options.y,
z = options.z;
var size = this.extent * Math.pow(2, z);
var x0 = this.extent * x;
var y0 = this.extent * y;
function project(data) {
for (var j = 0, jl = data.length; j < jl; j += 2) {
data[j] = (data[j] + x0) * 360 / size - 180;
var y2 = 180 - (data[j + 1] + y0) * 360 / size;
data[j + 1] = 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
}
}
return this._toBinaryCoordinates(project);
}
}], [{
key: "types",
get: function get() {
return ['Unknown', 'Point', 'LineString', 'Polygon'];
}
}]);
return VectorTileFeature;
}();
exports.default = VectorTileFeature;
function classifyRings(geom) {
var len = geom.lines.length;
if (len <= 1) {
return {
data: geom.data,
areas: [[(0, _polygon.getPolygonSignedArea)(geom.data)]],
lines: [geom.lines]
};
}
var areas = [];
var polygons = [];
var ringAreas;
var polygon;
var ccw;
var offset = 0;
for (var _i = 0, startIndex, endIndex; _i < len; _i++) {
startIndex = geom.lines[_i] - offset;
endIndex = geom.lines[_i + 1] - offset || geom.data.length;
var shape = geom.data.slice(startIndex, endIndex);
var area = (0, _polygon.getPolygonSignedArea)(shape);
if (area === 0) {
var before = geom.data.slice(0, startIndex);
var after = geom.data.slice(endIndex);
geom.data = before.concat(after);
offset += endIndex - startIndex;
continue;
}
if (ccw === undefined) ccw = area < 0;
if (ccw === area < 0) {
if (polygon) {
areas.push(ringAreas);
polygons.push(polygon);
}
polygon = [startIndex];
ringAreas = [area];
} else {
ringAreas.push(area);
polygon.push(startIndex);
}
}
if (ringAreas) areas.push(ringAreas);
if (polygon) polygons.push(polygon);
return {
areas: areas,
lines: polygons,
data: geom.data
};
}
function readFeature(tag, feature, pbf) {
if (tag === 1) feature.id = pbf.readVarint();else if (tag === 2) readTag(pbf, feature);else if (tag === 3) feature.type = pbf.readVarint();else if (tag === 4) feature._geometry = pbf.pos;
}
function readTag(pbf, feature) {
var end = pbf.readVarint() + pbf.pos;
while (pbf.pos < end) {
var key = feature._keys[pbf.readVarint()];
var value = feature._values[pbf.readVarint()];
feature.properties[key] = value;
}
}
//# sourceMappingURL=vector-tile-feature.js.map