UNPKG

open-vector-tile

Version:

This library reads/writes Open Vector Tiles

93 lines 3.1 kB
import VectorFeature from './vectorFeature.js'; /** * A MapboxVectorLayer is a storage structure for the vector tile. * It may contain either the old Mapbox layers or the new S2 layers. * Parses extent, keys, values, and features. Features will utilize the extent, keys, and values. */ export default class MapboxVectorLayer { version = 5; name = 'default'; extent = 4_096; length = 0; isS2; #pbf; #keys = []; #values = []; #featuresPos = []; #features = new Map(); /** * @param pbf - The Protobuf object to read from * @param end - The end position of the message in the buffer * @param isS2 - Whether the layer is an S2 layer or Mapbox layer */ constructor(pbf, end, isS2 = false) { this.#pbf = pbf; this.isS2 = isS2; pbf.readFields(this.#readLayer, this, end); this.length = this.#featuresPos.length; } /** * @param tag - The tag of the message * @param layer - The layer to mutate * @param pbf - The Protobuf object to read from */ #readLayer(tag, layer, pbf) { if (tag === 15) layer.version = pbf.readVarint(); else if (tag === 1) layer.name = pbf.readString(); else if (tag === 2) layer.#featuresPos.push(pbf.pos); else if (tag === 3) layer.#keys.push(pbf.readString()); else if (tag === 4) layer.#values.push(layer.#readValueMessage(pbf)); else if (tag === 5) layer.extent = pbf.readVarint(); } /** * @param i - The index of the feature * @returns - A feature at the given index */ feature(i) { if (i < 0 || i >= this.#featuresPos.length) throw new Error('feature index out of bounds'); let vtf = this.#features.get(i); if (vtf !== undefined) return vtf; this.#pbf.pos = this.#featuresPos[i]; const end = this.#pbf.readVarint() + this.#pbf.pos; vtf = new VectorFeature(this.#pbf, end, this.isS2, this.extent, this.version, this.#keys, this.#values); this.#features.set(i, vtf); return vtf; } /** * @param pbf - The Protobuffer object to read from * @returns - A parsed Value */ #readValueMessage(pbf) { let value = null; const end = pbf.readVarint() + pbf.pos; while (pbf.pos < end) { const tag = pbf.readVarint() >> 3; if (tag === 1) value = pbf.readString(); else if (tag === 2) value = pbf.readFloat(); else if (tag === 3) value = pbf.readDouble(); else if (tag === 4) value = pbf.readVarint64(); else if (tag === 5) value = pbf.readVarint(); else if (tag === 6) value = pbf.readSVarint(); else if (tag === 7) value = pbf.readBoolean(); else value = null; } return value; } } //# sourceMappingURL=vectorLayer.js.map