888 lines
24 KiB
JavaScript
888 lines
24 KiB
JavaScript
/**
|
|
* Minified by jsDelivr using Terser v5.19.2.
|
|
* Original file: /npm/pbf@4.0.1/index.js
|
|
*
|
|
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
|
|
*/
|
|
const SHIFT_LEFT_32 = 4294967296, SHIFT_RIGHT_32 = 1 / 4294967296, TEXT_DECODER_MIN_LENGTH = 12,
|
|
utf8TextDecoder = "undefined" == typeof TextDecoder ? null : new TextDecoder("utf-8"), PBF_VARINT = 0,
|
|
PBF_FIXED64 = 1, PBF_BYTES = 2, PBF_FIXED32 = 5;
|
|
|
|
class Pbf {
|
|
constructor(t = new Uint8Array(16)) {
|
|
this.buf = ArrayBuffer.isView(t) ? t : new Uint8Array(t), this.dataView = new DataView(this.buf.buffer), this.pos = 0, this.type = 0, this.length = this.buf.length
|
|
}
|
|
|
|
readFields(t, e, i = this.length) {
|
|
for (; this.pos < i;) {
|
|
const i = this.readVarint(), s = i >> 3, r = this.pos;
|
|
this.type = 7 & i, t(s, e, this), this.pos === r && this.skip(i)
|
|
}
|
|
return e
|
|
}
|
|
|
|
readMessage(t, e) {
|
|
return this.readFields(t, e, this.readVarint() + this.pos)
|
|
}
|
|
|
|
readFixed32() {
|
|
const t = this.dataView.getUint32(this.pos, !0);
|
|
return this.pos += 4, t
|
|
}
|
|
|
|
readSFixed32() {
|
|
const t = this.dataView.getInt32(this.pos, !0);
|
|
return this.pos += 4, t
|
|
}
|
|
|
|
readFixed64() {
|
|
const t = this.dataView.getUint32(this.pos, !0) + 4294967296 * this.dataView.getUint32(this.pos + 4, !0);
|
|
return this.pos += 8, t
|
|
}
|
|
|
|
readSFixed64() {
|
|
const t = this.dataView.getUint32(this.pos, !0) + 4294967296 * this.dataView.getInt32(this.pos + 4, !0);
|
|
return this.pos += 8, t
|
|
}
|
|
|
|
readFloat() {
|
|
const t = this.dataView.getFloat32(this.pos, !0);
|
|
return this.pos += 4, t
|
|
}
|
|
|
|
readDouble() {
|
|
const t = this.dataView.getFloat64(this.pos, !0);
|
|
return this.pos += 8, t
|
|
}
|
|
|
|
readVarint(t) {
|
|
const e = this.buf;
|
|
let i, s;
|
|
return s = e[this.pos++], i = 127 & s, s < 128 ? i : (s = e[this.pos++], i |= (127 & s) << 7, s < 128 ? i : (s = e[this.pos++], i |= (127 & s) << 14, s < 128 ? i : (s = e[this.pos++], i |= (127 & s) << 21, s < 128 ? i : (s = e[this.pos], i |= (15 & s) << 28, readVarintRemainder(i, t, this)))))
|
|
}
|
|
|
|
readVarint64() {
|
|
return this.readVarint(!0)
|
|
}
|
|
|
|
readSVarint() {
|
|
const t = this.readVarint();
|
|
return t % 2 == 1 ? (t + 1) / -2 : t / 2
|
|
}
|
|
|
|
readBoolean() {
|
|
return Boolean(this.readVarint())
|
|
}
|
|
|
|
readString() {
|
|
const t = this.readVarint() + this.pos, e = this.pos;
|
|
return this.pos = t, t - e >= 12 && utf8TextDecoder ? utf8TextDecoder.decode(this.buf.subarray(e, t)) : readUtf8(this.buf, e, t)
|
|
}
|
|
|
|
readBytes() {
|
|
const t = this.readVarint() + this.pos, e = this.buf.subarray(this.pos, t);
|
|
return this.pos = t, e
|
|
}
|
|
|
|
readPackedVarint(t = [], e) {
|
|
const i = this.readPackedEnd();
|
|
for (; this.pos < i;) t.push(this.readVarint(e));
|
|
return t
|
|
}
|
|
|
|
readPackedSVarint(t = []) {
|
|
const e = this.readPackedEnd();
|
|
for (; this.pos < e;) t.push(this.readSVarint());
|
|
return t
|
|
}
|
|
|
|
readPackedBoolean(t = []) {
|
|
const e = this.readPackedEnd();
|
|
for (; this.pos < e;) t.push(this.readBoolean());
|
|
return t
|
|
}
|
|
|
|
readPackedFloat(t = []) {
|
|
const e = this.readPackedEnd();
|
|
for (; this.pos < e;) t.push(this.readFloat());
|
|
return t
|
|
}
|
|
|
|
readPackedDouble(t = []) {
|
|
const e = this.readPackedEnd();
|
|
for (; this.pos < e;) t.push(this.readDouble());
|
|
return t
|
|
}
|
|
|
|
readPackedFixed32(t = []) {
|
|
const e = this.readPackedEnd();
|
|
for (; this.pos < e;) t.push(this.readFixed32());
|
|
return t
|
|
}
|
|
|
|
readPackedSFixed32(t = []) {
|
|
const e = this.readPackedEnd();
|
|
for (; this.pos < e;) t.push(this.readSFixed32());
|
|
return t
|
|
}
|
|
|
|
readPackedFixed64(t = []) {
|
|
const e = this.readPackedEnd();
|
|
for (; this.pos < e;) t.push(this.readFixed64());
|
|
return t
|
|
}
|
|
|
|
readPackedSFixed64(t = []) {
|
|
const e = this.readPackedEnd();
|
|
for (; this.pos < e;) t.push(this.readSFixed64());
|
|
return t
|
|
}
|
|
|
|
readPackedEnd() {
|
|
return 2 === this.type ? this.readVarint() + this.pos : this.pos + 1
|
|
}
|
|
|
|
skip(t) {
|
|
const e = 7 & t;
|
|
if (0 === e) for (; this.buf[this.pos++] > 127;) ; else if (2 === e) this.pos = this.readVarint() + this.pos; else if (5 === e) this.pos += 4; else {
|
|
if (1 !== e) throw new Error(`Unimplemented type: ${e}`);
|
|
this.pos += 8
|
|
}
|
|
}
|
|
|
|
writeTag(t, e) {
|
|
this.writeVarint(t << 3 | e)
|
|
}
|
|
|
|
realloc(t) {
|
|
let e = this.length || 16;
|
|
for (; e < this.pos + t;) e *= 2;
|
|
if (e !== this.length) {
|
|
const t = new Uint8Array(e);
|
|
t.set(this.buf), this.buf = t, this.dataView = new DataView(t.buffer), this.length = e
|
|
}
|
|
}
|
|
|
|
finish() {
|
|
return this.length = this.pos, this.pos = 0, this.buf.subarray(0, this.length)
|
|
}
|
|
|
|
writeFixed32(t) {
|
|
this.realloc(4), this.dataView.setInt32(this.pos, t, !0), this.pos += 4
|
|
}
|
|
|
|
writeSFixed32(t) {
|
|
this.realloc(4), this.dataView.setInt32(this.pos, t, !0), this.pos += 4
|
|
}
|
|
|
|
writeFixed64(t) {
|
|
this.realloc(8), this.dataView.setInt32(this.pos, -1 & t, !0), this.dataView.setInt32(this.pos + 4, Math.floor(t * SHIFT_RIGHT_32), !0), this.pos += 8
|
|
}
|
|
|
|
writeSFixed64(t) {
|
|
this.realloc(8), this.dataView.setInt32(this.pos, -1 & t, !0), this.dataView.setInt32(this.pos + 4, Math.floor(t * SHIFT_RIGHT_32), !0), this.pos += 8
|
|
}
|
|
|
|
writeVarint(t) {
|
|
(t = +t || 0) > 268435455 || t < 0 ? writeBigVarint(t, this) : (this.realloc(4), this.buf[this.pos++] = 127 & t | (t > 127 ? 128 : 0), t <= 127 || (this.buf[this.pos++] = 127 & (t >>>= 7) | (t > 127 ? 128 : 0), t <= 127 || (this.buf[this.pos++] = 127 & (t >>>= 7) | (t > 127 ? 128 : 0), t <= 127 || (this.buf[this.pos++] = t >>> 7 & 127))))
|
|
}
|
|
|
|
writeSVarint(t) {
|
|
this.writeVarint(t < 0 ? 2 * -t - 1 : 2 * t)
|
|
}
|
|
|
|
writeBoolean(t) {
|
|
this.writeVarint(+t)
|
|
}
|
|
|
|
writeString(t) {
|
|
t = String(t), this.realloc(4 * t.length), this.pos++;
|
|
const e = this.pos;
|
|
this.pos = writeUtf8(this.buf, t, this.pos);
|
|
const i = this.pos - e;
|
|
i >= 128 && makeRoomForExtraLength(e, i, this), this.pos = e - 1, this.writeVarint(i), this.pos += i
|
|
}
|
|
|
|
writeFloat(t) {
|
|
this.realloc(4), this.dataView.setFloat32(this.pos, t, !0), this.pos += 4
|
|
}
|
|
|
|
writeDouble(t) {
|
|
this.realloc(8), this.dataView.setFloat64(this.pos, t, !0), this.pos += 8
|
|
}
|
|
|
|
writeBytes(t) {
|
|
const e = t.length;
|
|
this.writeVarint(e), this.realloc(e);
|
|
for (let i = 0; i < e; i++) this.buf[this.pos++] = t[i]
|
|
}
|
|
|
|
writeRawMessage(t, e) {
|
|
this.pos++;
|
|
const i = this.pos;
|
|
t(e, this);
|
|
const s = this.pos - i;
|
|
s >= 128 && makeRoomForExtraLength(i, s, this), this.pos = i - 1, this.writeVarint(s), this.pos += s
|
|
}
|
|
|
|
writeMessage(t, e, i) {
|
|
this.writeTag(t, 2), this.writeRawMessage(e, i)
|
|
}
|
|
|
|
writePackedVarint(t, e) {
|
|
e.length && this.writeMessage(t, writePackedVarint, e)
|
|
}
|
|
|
|
writePackedSVarint(t, e) {
|
|
e.length && this.writeMessage(t, writePackedSVarint, e)
|
|
}
|
|
|
|
writePackedBoolean(t, e) {
|
|
e.length && this.writeMessage(t, writePackedBoolean, e)
|
|
}
|
|
|
|
writePackedFloat(t, e) {
|
|
e.length && this.writeMessage(t, writePackedFloat, e)
|
|
}
|
|
|
|
writePackedDouble(t, e) {
|
|
e.length && this.writeMessage(t, writePackedDouble, e)
|
|
}
|
|
|
|
writePackedFixed32(t, e) {
|
|
e.length && this.writeMessage(t, writePackedFixed32, e)
|
|
}
|
|
|
|
writePackedSFixed32(t, e) {
|
|
e.length && this.writeMessage(t, writePackedSFixed32, e)
|
|
}
|
|
|
|
writePackedFixed64(t, e) {
|
|
e.length && this.writeMessage(t, writePackedFixed64, e)
|
|
}
|
|
|
|
writePackedSFixed64(t, e) {
|
|
e.length && this.writeMessage(t, writePackedSFixed64, e)
|
|
}
|
|
|
|
writeBytesField(t, e) {
|
|
this.writeTag(t, 2), this.writeBytes(e)
|
|
}
|
|
|
|
writeFixed32Field(t, e) {
|
|
this.writeTag(t, 5), this.writeFixed32(e)
|
|
}
|
|
|
|
writeSFixed32Field(t, e) {
|
|
this.writeTag(t, 5), this.writeSFixed32(e)
|
|
}
|
|
|
|
writeFixed64Field(t, e) {
|
|
this.writeTag(t, 1), this.writeFixed64(e)
|
|
}
|
|
|
|
writeSFixed64Field(t, e) {
|
|
this.writeTag(t, 1), this.writeSFixed64(e)
|
|
}
|
|
|
|
writeVarintField(t, e) {
|
|
this.writeTag(t, 0), this.writeVarint(e)
|
|
}
|
|
|
|
writeSVarintField(t, e) {
|
|
this.writeTag(t, 0), this.writeSVarint(e)
|
|
}
|
|
|
|
writeStringField(t, e) {
|
|
this.writeTag(t, 2), this.writeString(e)
|
|
}
|
|
|
|
writeFloatField(t, e) {
|
|
this.writeTag(t, 5), this.writeFloat(e)
|
|
}
|
|
|
|
writeDoubleField(t, e) {
|
|
this.writeTag(t, 1), this.writeDouble(e)
|
|
}
|
|
|
|
writeBooleanField(t, e) {
|
|
this.writeVarintField(t, +e)
|
|
}
|
|
}
|
|
|
|
// exports.Pbf = Pbf
|
|
|
|
function readVarintRemainder(t, e, i) {
|
|
const s = i.buf;
|
|
let r, a;
|
|
if (a = s[i.pos++], r = (112 & a) >> 4, a < 128) return toNum(t, r, e);
|
|
if (a = s[i.pos++], r |= (127 & a) << 3, a < 128) return toNum(t, r, e);
|
|
if (a = s[i.pos++], r |= (127 & a) << 10, a < 128) return toNum(t, r, e);
|
|
if (a = s[i.pos++], r |= (127 & a) << 17, a < 128) return toNum(t, r, e);
|
|
if (a = s[i.pos++], r |= (127 & a) << 24, a < 128) return toNum(t, r, e);
|
|
if (a = s[i.pos++], r |= (1 & a) << 31, a < 128) return toNum(t, r, e);
|
|
throw new Error("Expected varint not more than 10 bytes")
|
|
}
|
|
|
|
function toNum(t, e, i) {
|
|
return i ? 4294967296 * e + (t >>> 0) : 4294967296 * (e >>> 0) + (t >>> 0)
|
|
}
|
|
|
|
function writeBigVarint(t, e) {
|
|
let i, s;
|
|
if (t >= 0 ? (i = t % 4294967296 | 0, s = t / 4294967296 | 0) : (i = ~(-t % 4294967296), s = ~(-t / 4294967296), 4294967295 ^ i ? i = i + 1 | 0 : (i = 0, s = s + 1 | 0)), t >= 0x10000000000000000 || t < -0x10000000000000000) throw new Error("Given varint doesn't fit into 10 bytes");
|
|
e.realloc(10), writeBigVarintLow(i, s, e), writeBigVarintHigh(s, e)
|
|
}
|
|
|
|
function writeBigVarintLow(t, e, i) {
|
|
i.buf[i.pos++] = 127 & t | 128, t >>>= 7, i.buf[i.pos++] = 127 & t | 128, t >>>= 7, i.buf[i.pos++] = 127 & t | 128, t >>>= 7, i.buf[i.pos++] = 127 & t | 128, t >>>= 7, i.buf[i.pos] = 127 & t
|
|
}
|
|
|
|
function writeBigVarintHigh(t, e) {
|
|
const i = (7 & t) << 4;
|
|
e.buf[e.pos++] |= i | ((t >>>= 3) ? 128 : 0), t && (e.buf[e.pos++] = 127 & t | ((t >>>= 7) ? 128 : 0), t && (e.buf[e.pos++] = 127 & t | ((t >>>= 7) ? 128 : 0), t && (e.buf[e.pos++] = 127 & t | ((t >>>= 7) ? 128 : 0), t && (e.buf[e.pos++] = 127 & t | ((t >>>= 7) ? 128 : 0), t && (e.buf[e.pos++] = 127 & t)))))
|
|
}
|
|
|
|
function makeRoomForExtraLength(t, e, i) {
|
|
const s = e <= 16383 ? 1 : e <= 2097151 ? 2 : e <= 268435455 ? 3 : Math.floor(Math.log(e) / (7 * Math.LN2));
|
|
i.realloc(s);
|
|
for (let e = i.pos - 1; e >= t; e--) i.buf[e + s] = i.buf[e]
|
|
}
|
|
|
|
function writePackedVarint(t, e) {
|
|
for (let i = 0; i < t.length; i++) e.writeVarint(t[i])
|
|
}
|
|
|
|
function writePackedSVarint(t, e) {
|
|
for (let i = 0; i < t.length; i++) e.writeSVarint(t[i])
|
|
}
|
|
|
|
function writePackedFloat(t, e) {
|
|
for (let i = 0; i < t.length; i++) e.writeFloat(t[i])
|
|
}
|
|
|
|
function writePackedDouble(t, e) {
|
|
for (let i = 0; i < t.length; i++) e.writeDouble(t[i])
|
|
}
|
|
|
|
function writePackedBoolean(t, e) {
|
|
for (let i = 0; i < t.length; i++) e.writeBoolean(t[i])
|
|
}
|
|
|
|
function writePackedFixed32(t, e) {
|
|
for (let i = 0; i < t.length; i++) e.writeFixed32(t[i])
|
|
}
|
|
|
|
function writePackedSFixed32(t, e) {
|
|
for (let i = 0; i < t.length; i++) e.writeSFixed32(t[i])
|
|
}
|
|
|
|
function writePackedFixed64(t, e) {
|
|
for (let i = 0; i < t.length; i++) e.writeFixed64(t[i])
|
|
}
|
|
|
|
function writePackedSFixed64(t, e) {
|
|
for (let i = 0; i < t.length; i++) e.writeSFixed64(t[i])
|
|
}
|
|
|
|
function readUtf8(t, e, i) {
|
|
let s = "", r = e;
|
|
for (; r < i;) {
|
|
const e = t[r];
|
|
let a, o, h, n = null, d = e > 239 ? 4 : e > 223 ? 3 : e > 191 ? 2 : 1;
|
|
if (r + d > i) break;
|
|
1 === d ? e < 128 && (n = e) : 2 === d ? (a = t[r + 1], 128 == (192 & a) && (n = (31 & e) << 6 | 63 & a, n <= 127 && (n = null))) : 3 === d ? (a = t[r + 1], o = t[r + 2], 128 == (192 & a) && 128 == (192 & o) && (n = (15 & e) << 12 | (63 & a) << 6 | 63 & o, (n <= 2047 || n >= 55296 && n <= 57343) && (n = null))) : 4 === d && (a = t[r + 1], o = t[r + 2], h = t[r + 3], 128 == (192 & a) && 128 == (192 & o) && 128 == (192 & h) && (n = (15 & e) << 18 | (63 & a) << 12 | (63 & o) << 6 | 63 & h, (n <= 65535 || n >= 1114112) && (n = null))), null === n ? (n = 65533, d = 1) : n > 65535 && (n -= 65536, s += String.fromCharCode(n >>> 10 & 1023 | 55296), n = 56320 | 1023 & n), s += String.fromCharCode(n), r += d
|
|
}
|
|
return s
|
|
}
|
|
|
|
function writeUtf8(t, e, i) {
|
|
for (let s, r, a = 0; a < e.length; a++) {
|
|
if (s = e.charCodeAt(a), s > 55295 && s < 57344) {
|
|
if (!r) {
|
|
s > 56319 || a + 1 === e.length ? (t[i++] = 239, t[i++] = 191, t[i++] = 189) : r = s;
|
|
continue
|
|
}
|
|
if (s < 56320) {
|
|
t[i++] = 239, t[i++] = 191, t[i++] = 189, r = s;
|
|
continue
|
|
}
|
|
s = r - 55296 << 10 | s - 56320 | 65536, r = null
|
|
} else r && (t[i++] = 239, t[i++] = 191, t[i++] = 189, r = null);
|
|
s < 128 ? t[i++] = s : (s < 2048 ? t[i++] = s >> 6 | 192 : (s < 65536 ? t[i++] = s >> 12 | 224 : (t[i++] = s >> 18 | 240, t[i++] = s >> 12 & 63 | 128), t[i++] = s >> 6 & 63 | 128), t[i++] = 63 & s | 128)
|
|
}
|
|
return i
|
|
}
|
|
|
|
function Point(x, y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
Point.prototype = {
|
|
clone: function () {
|
|
return new Point(this.x, this.y);
|
|
},
|
|
|
|
add: function (p) {
|
|
return this.clone()._add(p);
|
|
},
|
|
sub: function (p) {
|
|
return this.clone()._sub(p);
|
|
},
|
|
mult: function (k) {
|
|
return this.clone()._mult(k);
|
|
},
|
|
div: function (k) {
|
|
return this.clone()._div(k);
|
|
},
|
|
rotate: function (a) {
|
|
return this.clone()._rotate(a);
|
|
},
|
|
matMult: function (m) {
|
|
return this.clone()._matMult(m);
|
|
},
|
|
unit: function () {
|
|
return this.clone()._unit();
|
|
},
|
|
perp: function () {
|
|
return this.clone()._perp();
|
|
},
|
|
round: function () {
|
|
return this.clone()._round();
|
|
},
|
|
|
|
mag: function () {
|
|
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
},
|
|
|
|
equals: function (p) {
|
|
return this.x === p.x &&
|
|
this.y === p.y;
|
|
},
|
|
|
|
dist: function (p) {
|
|
return Math.sqrt(this.distSqr(p));
|
|
},
|
|
|
|
distSqr: function (p) {
|
|
var dx = p.x - this.x,
|
|
dy = p.y - this.y;
|
|
return dx * dx + dy * dy;
|
|
},
|
|
|
|
angle: function () {
|
|
return Math.atan2(this.y, this.x);
|
|
},
|
|
|
|
angleTo: function (b) {
|
|
return Math.atan2(this.y - b.y, this.x - b.x);
|
|
},
|
|
|
|
angleWith: function (b) {
|
|
return this.angleWithSep(b.x, b.y);
|
|
},
|
|
|
|
// Find the angle of the two vectors, solving the formula for the cross product a x b = |a||b|sin(θ) for θ.
|
|
angleWithSep: function (x, y) {
|
|
return Math.atan2(
|
|
this.x * y - this.y * x,
|
|
this.x * x + this.y * y);
|
|
},
|
|
|
|
_matMult: function (m) {
|
|
var x = m[0] * this.x + m[1] * this.y,
|
|
y = m[2] * this.x + m[3] * this.y;
|
|
this.x = x;
|
|
this.y = y;
|
|
return this;
|
|
},
|
|
|
|
_add: function (p) {
|
|
this.x += p.x;
|
|
this.y += p.y;
|
|
return this;
|
|
},
|
|
|
|
_sub: function (p) {
|
|
this.x -= p.x;
|
|
this.y -= p.y;
|
|
return this;
|
|
},
|
|
|
|
_mult: function (k) {
|
|
this.x *= k;
|
|
this.y *= k;
|
|
return this;
|
|
},
|
|
|
|
_div: function (k) {
|
|
this.x /= k;
|
|
this.y /= k;
|
|
return this;
|
|
},
|
|
|
|
_unit: function () {
|
|
this._div(this.mag());
|
|
return this;
|
|
},
|
|
|
|
_perp: function () {
|
|
var y = this.y;
|
|
this.y = this.x;
|
|
this.x = -y;
|
|
return this;
|
|
},
|
|
|
|
_rotate: function (angle) {
|
|
var cos = Math.cos(angle),
|
|
sin = Math.sin(angle),
|
|
x = cos * this.x - sin * this.y,
|
|
y = sin * this.x + cos * this.y;
|
|
this.x = x;
|
|
this.y = y;
|
|
return this;
|
|
},
|
|
|
|
_round: function () {
|
|
this.x = Math.round(this.x);
|
|
this.y = Math.round(this.y);
|
|
return this;
|
|
}
|
|
};
|
|
|
|
// constructs Point from an array if necessary
|
|
Point.convert = function (a) {
|
|
if (a instanceof Point) {
|
|
return a;
|
|
}
|
|
if (Array.isArray(a)) {
|
|
return new Point(a[0], a[1]);
|
|
}
|
|
return a;
|
|
};
|
|
|
|
//# sourceMappingURL=/sm/91dc2634fd08444face2220488dd1b2296e475fe4a0f0781089e74342af5c955.map
|
|
class TileFeature {
|
|
constructor(pbf, end, extent, keys, values) {
|
|
// Public
|
|
this.properties = {};
|
|
this.extent = extent;
|
|
this.type = 0;
|
|
|
|
// Private
|
|
this._pbf = pbf;
|
|
this._geometry = -1;
|
|
this._keys = keys;
|
|
this._values = values;
|
|
pbf.readFields(readFeature, this, end);
|
|
}
|
|
|
|
types() {
|
|
return ['Unknown', 'Point', 'LineString', 'Polygon'];
|
|
}
|
|
|
|
loadGeometry() {
|
|
var pbf = this._pbf;
|
|
pbf.pos = this._geometry;
|
|
|
|
var end = pbf.readVarint() + pbf.pos,
|
|
cmd = 1,
|
|
length = 0,
|
|
x = 0,
|
|
y = 0,
|
|
lines = [],
|
|
line;
|
|
|
|
while (pbf.pos < end) {
|
|
if (!length) {
|
|
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(new Point(x, y));
|
|
|
|
} else if (cmd === 7) {
|
|
|
|
if (line) {
|
|
line.push(line[0].clone()); // closePolygon
|
|
|
|
}
|
|
|
|
} else {
|
|
throw new Error('unknown command ' + cmd);
|
|
}
|
|
}
|
|
if (line) lines.push(line);
|
|
|
|
return lines;
|
|
}
|
|
|
|
bbox() {
|
|
var pbf = this._pbf;
|
|
pbf.pos = this._geometry;
|
|
|
|
var end = pbf.readVarint() + pbf.pos,
|
|
cmd = 1,
|
|
length = 0,
|
|
x = 0,
|
|
y = 0,
|
|
x1 = Infinity,
|
|
x2 = -Infinity,
|
|
y1 = Infinity,
|
|
y2 = -Infinity;
|
|
|
|
while (pbf.pos < end) {
|
|
if (!length) {
|
|
var cmdLen = pbf.readVarint();
|
|
cmd = cmdLen & 0x7;
|
|
length = cmdLen >> 3;
|
|
}
|
|
|
|
length--;
|
|
|
|
if (cmd === 1 || cmd === 2) {
|
|
x += pbf.readSVarint();
|
|
y += pbf.readSVarint();
|
|
if (x < x1) x1 = x;
|
|
if (x > x2) x2 = x;
|
|
if (y < y1) y1 = y;
|
|
if (y > y2) y2 = y;
|
|
|
|
} else if (cmd !== 7) {
|
|
throw new Error('unknown command ' + cmd);
|
|
}
|
|
}
|
|
|
|
return [x1, y1, x2, y2];
|
|
}
|
|
|
|
toGeoJSON(x = 0, y = 0, z = 1) {
|
|
var size = this.extent * Math.pow(2, z),
|
|
x0 = this.extent * x,
|
|
y0 = this.extent * y,
|
|
coords = this.loadGeometry(),
|
|
type = this.types()[this.type],
|
|
i, j;
|
|
|
|
function project(line) {
|
|
for (var j = 0; j < line.length; j++) {
|
|
var p = line[j]
|
|
let y2 = 180 - (p.y + y0) * 360 / size;
|
|
line[j] = [
|
|
(p.x + x0) * 360 / size - 180,
|
|
360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90
|
|
];
|
|
|
|
}
|
|
}
|
|
|
|
switch (this.type) {
|
|
case 1:
|
|
var points = [];
|
|
for (i = 0; i < coords.length; i++) {
|
|
points[i] = coords[i][0];
|
|
}
|
|
coords = points;
|
|
project(coords);
|
|
break;
|
|
|
|
case 2:
|
|
for (i = 0; i < coords.length; i++) {
|
|
project(coords[i]);
|
|
}
|
|
break;
|
|
|
|
case 3:
|
|
coords = classifyRings(coords);
|
|
for (i = 0; i < coords.length; i++) {
|
|
for (j = 0; j < coords[i].length; j++) {
|
|
project(coords[i][j]);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
if (coords.length === 1) {
|
|
coords = coords[0];
|
|
} else {
|
|
type = 'Multi' + type;
|
|
}
|
|
|
|
var result = {
|
|
type: "Feature",
|
|
geometry: {
|
|
type: type,
|
|
coordinates: coords
|
|
},
|
|
properties: this.properties
|
|
};
|
|
|
|
if ('id' in this) {
|
|
result.id = this.id;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
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 classifyRings(rings) {
|
|
var len = rings.length;
|
|
|
|
if (len <= 1) return [rings];
|
|
|
|
var polygons = [],
|
|
polygon,
|
|
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.x - p1.x) * (p1.y + p2.y);
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
function readTag(pbf, feature) {
|
|
var end = pbf.readVarint() + pbf.pos;
|
|
|
|
while (pbf.pos < end) {
|
|
var key = feature._keys[pbf.readVarint()],
|
|
value = feature._values[pbf.readVarint()];
|
|
feature.properties[key] = value;
|
|
}
|
|
}
|
|
|
|
class TileLayer {
|
|
constructor(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;
|
|
}
|
|
|
|
feature(i) {
|
|
if (i < 0 || i >= this._features.length) throw new Error('feature index out of bounds');
|
|
|
|
this._pbf.pos = this._features[i];
|
|
|
|
var end = this._pbf.readVarint() + this._pbf.pos;
|
|
return new TileFeature(this._pbf, end, this.extent, this._keys, this._values);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
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) {
|
|
var value = null,
|
|
end = pbf.readVarint() + pbf.pos;
|
|
|
|
while (pbf.pos < end) {
|
|
var 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;
|
|
}
|
|
|
|
class VectorTile {
|
|
constructor(pbf_tile) {
|
|
this.pbf_tile = pbf_tile
|
|
this.layers = {}
|
|
}
|
|
|
|
features() {
|
|
let jsonArray = []
|
|
for (let layersKey in this.layers) {
|
|
for (let i = 0; i < tile.layers[layersKey]._features.length; i++) {
|
|
let json = this.layers[layersKey].feature(i).toGeoJSON()
|
|
jsonArray.push(json)
|
|
}
|
|
}
|
|
return jsonArray
|
|
}
|
|
|
|
async on() {
|
|
let res = await fetch(this.pbf_tile)
|
|
let buf = await res.arrayBuffer()
|
|
buf = new Uint8Array(buf)
|
|
let pbf = new Pbf(buf)
|
|
this.layers = pbf.readFields(this.readTile, {})
|
|
}
|
|
|
|
readTile(tag, layers, pbf) {
|
|
if (tag === 3) {
|
|
var layer = new TileLayer(pbf, pbf.readVarint() + pbf.pos);
|
|
if (layer.length) layers[layer.name] = layer;
|
|
}
|
|
}
|
|
}
|