vector-tile-esm
Version:
Parses vector tiles
69 lines (53 loc) • 1.92 kB
JavaScript
import { VectorTileFeature } from "./vectortilefeature.js";
export function VectorTileLayer(pbf, end) {
// Public
this.version = 1;
this.name = null;
this.extent = 4096;
this.length = 0;
// Private
this._pbf = pbf;
this._keys = [];
this._values = [];
this._features = [];
pbf.readFields(readLayer, this, end);
this.length = this._features.length;
}
function readLayer(tag, layer, pbf) {
if (tag === 15) layer.version = pbf.readVarint();
else if (tag === 1) layer.name = pbf.readString();
else if (tag === 5) layer.extent = pbf.readVarint();
else if (tag === 2) layer._features.push(pbf.pos);
else if (tag === 3) layer._keys.push(pbf.readString());
else if (tag === 4) layer._values.push(readValueMessage(pbf));
}
function readValueMessage(pbf) {
let value = null;
const end = pbf.readVarint() + pbf.pos;
while (pbf.pos < end) {
const tag = pbf.readVarint() >> 3;
value = tag === 1 ? pbf.readString() :
tag === 2 ? pbf.readFloat() :
tag === 3 ? pbf.readDouble() :
tag === 4 ? pbf.readVarint64() :
tag === 5 ? pbf.readVarint() :
tag === 6 ? pbf.readSVarint() :
tag === 7 ? pbf.readBoolean() : null;
}
return value;
}
// return feature 'i' from this layer as a 'VectorTileFeature'
VectorTileLayer.prototype.feature = function(i) {
const { _features, extent, _pbf, _keys, _values } = this;
const lastFeature = _features.length - 1;
if (i < 0 || i > lastFeature) throw Error("feature index out of bounds");
_pbf.pos = _features[i];
const end = _pbf.readVarint() + _pbf.pos;
return new VectorTileFeature(_pbf, end, extent, _keys, _values);
};
VectorTileLayer.prototype.toGeoJSON = function(size, sx, sy) {
const features = Array.from(Array(this._features.length), (v, i) => {
return this.feature(i).toGeoJSON(size, sx, sy);
});
return { type: "FeatureCollection", features, extent: this.extent };
};