@loaders.gl/mvt 3.0.12 → 3.1.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dist.min.js +2 -2
- package/dist/dist.min.js.map +1 -1
- package/dist/es5/bundle.js +2 -4
- package/dist/es5/bundle.js.map +1 -1
- package/dist/es5/helpers/binary-util-functions.js +95 -0
- package/dist/es5/helpers/binary-util-functions.js.map +1 -0
- package/dist/es5/helpers/mapbox-util-functions.js +62 -0
- package/dist/es5/helpers/mapbox-util-functions.js.map +1 -0
- package/dist/es5/lib/binary-vector-tile/features-to-binary.js +4 -7
- package/dist/es5/lib/binary-vector-tile/features-to-binary.js.map +1 -1
- package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js +23 -94
- package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js +15 -3
- package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/es5/lib/binary-vector-tile/vector-tile.js +8 -3
- package/dist/es5/lib/binary-vector-tile/vector-tile.js.map +1 -1
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js +15 -55
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js +15 -3
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/es5/lib/mapbox-vector-tile/vector-tile.js +8 -3
- package/dist/es5/lib/mapbox-vector-tile/vector-tile.js.map +1 -1
- package/dist/es5/lib/parse-mvt.js +56 -53
- package/dist/es5/lib/parse-mvt.js.map +1 -1
- package/dist/es5/lib/types.js +2 -0
- package/dist/es5/lib/types.js.map +1 -0
- package/dist/es5/mvt-loader.js +1 -1
- package/dist/es5/mvt-loader.js.map +1 -1
- package/dist/es5/workers/mvt-worker.js +7 -3
- package/dist/es5/workers/mvt-worker.js.map +1 -0
- package/dist/esm/bundle.js +2 -4
- package/dist/esm/bundle.js.map +1 -1
- package/dist/esm/helpers/binary-util-functions.js +81 -0
- package/dist/esm/helpers/binary-util-functions.js.map +1 -0
- package/dist/esm/helpers/mapbox-util-functions.js +49 -0
- package/dist/esm/helpers/mapbox-util-functions.js.map +1 -0
- package/dist/esm/lib/binary-vector-tile/features-to-binary.js +4 -4
- package/dist/esm/lib/binary-vector-tile/features-to-binary.js.map +1 -1
- package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js +27 -92
- package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js +22 -3
- package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/esm/lib/binary-vector-tile/vector-tile.js +8 -3
- package/dist/esm/lib/binary-vector-tile/vector-tile.js.map +1 -1
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js +20 -54
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js +22 -3
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/esm/lib/mapbox-vector-tile/vector-tile.js +8 -3
- package/dist/esm/lib/mapbox-vector-tile/vector-tile.js.map +1 -1
- package/dist/esm/lib/parse-mvt.js +57 -52
- package/dist/esm/lib/parse-mvt.js.map +1 -1
- package/dist/esm/lib/types.js +2 -0
- package/dist/esm/lib/types.js.map +1 -0
- package/dist/esm/mvt-loader.js +1 -1
- package/dist/esm/mvt-loader.js.map +1 -1
- package/dist/esm/workers/mvt-worker.js +3 -3
- package/dist/esm/workers/mvt-worker.js.map +1 -0
- package/dist/mvt-worker.js +2 -2
- package/dist/mvt-worker.js.map +1 -1
- package/package.json +5 -5
- package/src/bundle.ts +2 -3
- package/src/helpers/binary-util-functions.ts +119 -0
- package/src/helpers/mapbox-util-functions.ts +78 -0
- package/src/lib/binary-vector-tile/{features-to-binary.js → features-to-binary.ts} +184 -29
- package/src/lib/binary-vector-tile/{vector-tile-feature.js → vector-tile-feature.ts} +41 -109
- package/src/lib/binary-vector-tile/vector-tile-layer.ts +109 -0
- package/src/lib/binary-vector-tile/vector-tile.ts +28 -0
- package/src/lib/mapbox-vector-tile/{vector-tile-feature.js → vector-tile-feature.ts} +31 -72
- package/src/lib/mapbox-vector-tile/{vector-tile-layer.js → vector-tile-layer.ts} +42 -14
- package/src/lib/mapbox-vector-tile/vector-tile.ts +28 -0
- package/src/lib/parse-mvt.ts +174 -0
- package/src/lib/types.ts +92 -0
- package/src/workers/{mvt-worker.js → mvt-worker.ts} +0 -0
- package/dist/dist.es5.min.js +0 -4
- package/dist/dist.es5.min.js.map +0 -1
- package/dist/es5/lib/binary-vector-tile/vector-tile-feature.d.ts +0 -39
- package/dist/es5/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -25
- package/dist/es5/lib/binary-vector-tile/vector-tile.d.ts +0 -16
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -19
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.d.ts +0 -14
- package/dist/es5/lib/mapbox-vector-tile/vector-tile.d.ts +0 -7
- package/dist/esm/lib/binary-vector-tile/vector-tile-feature.d.ts +0 -39
- package/dist/esm/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -25
- package/dist/esm/lib/binary-vector-tile/vector-tile.d.ts +0 -16
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -19
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.d.ts +0 -14
- package/dist/esm/lib/mapbox-vector-tile/vector-tile.d.ts +0 -7
- package/src/lib/binary-vector-tile/vector-tile-feature.d.ts +0 -39
- package/src/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -25
- package/src/lib/binary-vector-tile/vector-tile-layer.js +0 -79
- package/src/lib/binary-vector-tile/vector-tile.d.ts +0 -16
- package/src/lib/binary-vector-tile/vector-tile.js +0 -18
- package/src/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -19
- package/src/lib/mapbox-vector-tile/vector-tile-layer.d.ts +0 -14
- package/src/lib/mapbox-vector-tile/vector-tile.d.ts +0 -7
- package/src/lib/mapbox-vector-tile/vector-tile.js +0 -18
- package/src/lib/parse-mvt.js +0 -142
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/* eslint-disable indent */
|
|
2
|
+
// This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
|
|
3
|
+
|
|
4
|
+
import VectorTileFeature from './vector-tile-feature';
|
|
5
|
+
import Protobuf from 'pbf';
|
|
6
|
+
import {MvtFirstPassedData} from '../types';
|
|
7
|
+
|
|
8
|
+
export default class VectorTileLayer {
|
|
9
|
+
version: number;
|
|
10
|
+
name: string;
|
|
11
|
+
extent: number;
|
|
12
|
+
length: number;
|
|
13
|
+
_pbf: Protobuf;
|
|
14
|
+
_keys: string[];
|
|
15
|
+
_values: (string | number | boolean | null)[];
|
|
16
|
+
_features: number[];
|
|
17
|
+
constructor(pbf: Protobuf, end: number) {
|
|
18
|
+
// Public
|
|
19
|
+
this.version = 1;
|
|
20
|
+
this.name = '';
|
|
21
|
+
this.extent = 4096;
|
|
22
|
+
this.length = 0;
|
|
23
|
+
|
|
24
|
+
// Private
|
|
25
|
+
this._pbf = pbf;
|
|
26
|
+
this._keys = [];
|
|
27
|
+
this._values = [];
|
|
28
|
+
this._features = [];
|
|
29
|
+
|
|
30
|
+
pbf.readFields(readLayer, this, end);
|
|
31
|
+
|
|
32
|
+
this.length = this._features.length;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* return feature `i` from this layer as a `VectorTileFeature`
|
|
37
|
+
*
|
|
38
|
+
* @param index
|
|
39
|
+
* @param firstPassData
|
|
40
|
+
* @returns {VectorTileFeature}
|
|
41
|
+
*/
|
|
42
|
+
feature(i: number, firstPassData: MvtFirstPassedData): VectorTileFeature {
|
|
43
|
+
if (i < 0 || i >= this._features.length) {
|
|
44
|
+
throw new Error('feature index out of bounds');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
this._pbf.pos = this._features[i];
|
|
48
|
+
|
|
49
|
+
const end = this._pbf.readVarint() + this._pbf.pos;
|
|
50
|
+
return new VectorTileFeature(
|
|
51
|
+
this._pbf,
|
|
52
|
+
end,
|
|
53
|
+
this.extent,
|
|
54
|
+
this._keys,
|
|
55
|
+
this._values,
|
|
56
|
+
firstPassData
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
*
|
|
63
|
+
* @param tag
|
|
64
|
+
* @param layer
|
|
65
|
+
* @param pbf
|
|
66
|
+
*/
|
|
67
|
+
function readLayer(tag: number, layer?: VectorTileLayer, pbf?: Protobuf): void {
|
|
68
|
+
if (layer && pbf) {
|
|
69
|
+
if (tag === 15) layer.version = pbf.readVarint();
|
|
70
|
+
else if (tag === 1) layer.name = pbf.readString();
|
|
71
|
+
else if (tag === 5) layer.extent = pbf.readVarint();
|
|
72
|
+
else if (tag === 2) layer._features.push(pbf.pos);
|
|
73
|
+
else if (tag === 3) layer._keys.push(pbf.readString());
|
|
74
|
+
else if (tag === 4) layer._values.push(readValueMessage(pbf));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
*
|
|
80
|
+
* @param pbf
|
|
81
|
+
* @returns value
|
|
82
|
+
*/
|
|
83
|
+
function readValueMessage(pbf: Protobuf) {
|
|
84
|
+
let value: string | number | boolean | null = null;
|
|
85
|
+
const end = pbf.readVarint() + pbf.pos;
|
|
86
|
+
|
|
87
|
+
while (pbf.pos < end) {
|
|
88
|
+
const tag = pbf.readVarint() >> 3;
|
|
89
|
+
|
|
90
|
+
value =
|
|
91
|
+
tag === 1
|
|
92
|
+
? pbf.readString()
|
|
93
|
+
: tag === 2
|
|
94
|
+
? pbf.readFloat()
|
|
95
|
+
: tag === 3
|
|
96
|
+
? pbf.readDouble()
|
|
97
|
+
: tag === 4
|
|
98
|
+
? pbf.readVarint64()
|
|
99
|
+
: tag === 5
|
|
100
|
+
? pbf.readVarint()
|
|
101
|
+
: tag === 6
|
|
102
|
+
? pbf.readSVarint()
|
|
103
|
+
: tag === 7
|
|
104
|
+
? pbf.readBoolean()
|
|
105
|
+
: null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
|
|
2
|
+
|
|
3
|
+
import VectorTileLayer from './vector-tile-layer';
|
|
4
|
+
import Protobuf from 'pbf';
|
|
5
|
+
|
|
6
|
+
export default class VectorTile {
|
|
7
|
+
layers: {[x: string]: VectorTileLayer};
|
|
8
|
+
constructor(pbf: Protobuf, end?: number) {
|
|
9
|
+
this.layers = pbf.readFields(readTile, {}, end);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param tag
|
|
16
|
+
* @param layers
|
|
17
|
+
* @param pbf
|
|
18
|
+
*/
|
|
19
|
+
function readTile(tag: number, layers?: {[x: string]: VectorTileLayer}, pbf?: Protobuf): void {
|
|
20
|
+
if (tag === 3) {
|
|
21
|
+
if (pbf) {
|
|
22
|
+
const layer = new VectorTileLayer(pbf, pbf.readVarint() + pbf.pos);
|
|
23
|
+
if (layer.length && layers) {
|
|
24
|
+
layers[layer.name] = layer;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -1,12 +1,28 @@
|
|
|
1
1
|
// This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
|
|
2
|
-
|
|
2
|
+
import Protobuf from 'pbf';
|
|
3
|
+
import {MvtMapboxCoordinates, MvtMapboxGeometry} from '../types';
|
|
4
|
+
import {readFeature, classifyRings} from '../../helpers/mapbox-util-functions';
|
|
3
5
|
|
|
4
6
|
export default class VectorTileFeature {
|
|
7
|
+
properties: {[x: string]: string | number | boolean | null};
|
|
8
|
+
extent: any;
|
|
9
|
+
type: number;
|
|
10
|
+
id: number | null;
|
|
11
|
+
_pbf: Protobuf;
|
|
12
|
+
_geometry: number;
|
|
13
|
+
_keys: string[];
|
|
14
|
+
_values: (string | number | boolean | null)[];
|
|
5
15
|
static get types() {
|
|
6
16
|
return ['Unknown', 'Point', 'LineString', 'Polygon'];
|
|
7
17
|
}
|
|
8
18
|
|
|
9
|
-
constructor(
|
|
19
|
+
constructor(
|
|
20
|
+
pbf: Protobuf,
|
|
21
|
+
end: number,
|
|
22
|
+
extent: any,
|
|
23
|
+
keys: string[],
|
|
24
|
+
values: (string | number | boolean | null)[]
|
|
25
|
+
) {
|
|
10
26
|
// Public
|
|
11
27
|
this.properties = {};
|
|
12
28
|
this.extent = extent;
|
|
@@ -23,7 +39,7 @@ export default class VectorTileFeature {
|
|
|
23
39
|
}
|
|
24
40
|
|
|
25
41
|
// eslint-disable-next-line complexity, max-statements
|
|
26
|
-
loadGeometry() {
|
|
42
|
+
loadGeometry(): MvtMapboxGeometry {
|
|
27
43
|
const pbf = this._pbf;
|
|
28
44
|
pbf.pos = this._geometry;
|
|
29
45
|
|
|
@@ -32,8 +48,8 @@ export default class VectorTileFeature {
|
|
|
32
48
|
let length = 0;
|
|
33
49
|
let x = 0;
|
|
34
50
|
let y = 0;
|
|
35
|
-
const lines = [];
|
|
36
|
-
let line;
|
|
51
|
+
const lines: number[][][] = [];
|
|
52
|
+
let line: number[][] | undefined;
|
|
37
53
|
|
|
38
54
|
while (pbf.pos < end) {
|
|
39
55
|
if (length <= 0) {
|
|
@@ -53,8 +69,7 @@ export default class VectorTileFeature {
|
|
|
53
69
|
if (line) lines.push(line);
|
|
54
70
|
line = [];
|
|
55
71
|
}
|
|
56
|
-
|
|
57
|
-
line.push([x, y]);
|
|
72
|
+
if (line) line.push([x, y]);
|
|
58
73
|
} else if (cmd === 7) {
|
|
59
74
|
// Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
|
|
60
75
|
if (line) {
|
|
@@ -112,12 +127,13 @@ export default class VectorTileFeature {
|
|
|
112
127
|
_toGeoJSON(transform) {
|
|
113
128
|
let coords = this.loadGeometry();
|
|
114
129
|
let type = VectorTileFeature.types[this.type];
|
|
115
|
-
let i;
|
|
116
|
-
let j;
|
|
130
|
+
let i: number;
|
|
131
|
+
let j: number;
|
|
117
132
|
|
|
133
|
+
// eslint-disable-next-line default-case
|
|
118
134
|
switch (this.type) {
|
|
119
135
|
case 1:
|
|
120
|
-
|
|
136
|
+
const points: number[] = [];
|
|
121
137
|
for (i = 0; i < coords.length; i++) {
|
|
122
138
|
points[i] = coords[i][0];
|
|
123
139
|
}
|
|
@@ -147,7 +163,7 @@ export default class VectorTileFeature {
|
|
|
147
163
|
type = `Multi${type}`;
|
|
148
164
|
}
|
|
149
165
|
|
|
150
|
-
const result = {
|
|
166
|
+
const result: MvtMapboxCoordinates = {
|
|
151
167
|
type: 'Feature',
|
|
152
168
|
geometry: {
|
|
153
169
|
type,
|
|
@@ -163,7 +179,9 @@ export default class VectorTileFeature {
|
|
|
163
179
|
return result;
|
|
164
180
|
}
|
|
165
181
|
|
|
166
|
-
toGeoJSON(
|
|
182
|
+
toGeoJSON(
|
|
183
|
+
options: {x: number; y: number; z: number} | ((data: number[], feature: {extent: any}) => void)
|
|
184
|
+
): MvtMapboxCoordinates {
|
|
167
185
|
if (typeof options === 'function') {
|
|
168
186
|
return this._toGeoJSON(options);
|
|
169
187
|
}
|
|
@@ -172,7 +190,7 @@ export default class VectorTileFeature {
|
|
|
172
190
|
const x0 = this.extent * x;
|
|
173
191
|
const y0 = this.extent * y;
|
|
174
192
|
|
|
175
|
-
function project(line) {
|
|
193
|
+
function project(line: number[]) {
|
|
176
194
|
for (let j = 0; j < line.length; j++) {
|
|
177
195
|
const p = line[j];
|
|
178
196
|
p[0] = ((p[0] + x0) * 360) / size - 180;
|
|
@@ -183,62 +201,3 @@ export default class VectorTileFeature {
|
|
|
183
201
|
return this._toGeoJSON(project);
|
|
184
202
|
}
|
|
185
203
|
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Classifies an array of rings into polygons with outer rings and holes
|
|
189
|
-
*/
|
|
190
|
-
|
|
191
|
-
function classifyRings(rings) {
|
|
192
|
-
const len = rings.length;
|
|
193
|
-
|
|
194
|
-
if (len <= 1) return [rings];
|
|
195
|
-
|
|
196
|
-
const polygons = [];
|
|
197
|
-
let polygon;
|
|
198
|
-
let ccw;
|
|
199
|
-
|
|
200
|
-
for (let i = 0; i < len; i++) {
|
|
201
|
-
const area = signedArea(rings[i]);
|
|
202
|
-
if (area === 0) continue;
|
|
203
|
-
|
|
204
|
-
if (ccw === undefined) ccw = area < 0;
|
|
205
|
-
|
|
206
|
-
if (ccw === area < 0) {
|
|
207
|
-
if (polygon) polygons.push(polygon);
|
|
208
|
-
polygon = [rings[i]];
|
|
209
|
-
} else {
|
|
210
|
-
// @ts-ignore
|
|
211
|
-
polygon.push(rings[i]);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
if (polygon) polygons.push(polygon);
|
|
215
|
-
|
|
216
|
-
return polygons;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function signedArea(ring) {
|
|
220
|
-
let sum = 0;
|
|
221
|
-
for (let i = 0, j = ring.length - 1, p1, p2; i < ring.length; j = i++) {
|
|
222
|
-
p1 = ring[i];
|
|
223
|
-
p2 = ring[j];
|
|
224
|
-
sum += (p2[0] - p1[0]) * (p1[1] + p2[1]);
|
|
225
|
-
}
|
|
226
|
-
return sum;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function readFeature(tag, feature, pbf) {
|
|
230
|
-
if (tag === 1) feature.id = pbf.readVarint();
|
|
231
|
-
else if (tag === 2) readTag(pbf, feature);
|
|
232
|
-
else if (tag === 3) feature.type = pbf.readVarint();
|
|
233
|
-
else if (tag === 4) feature._geometry = pbf.pos;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function readTag(pbf, feature) {
|
|
237
|
-
const end = pbf.readVarint() + pbf.pos;
|
|
238
|
-
|
|
239
|
-
while (pbf.pos < end) {
|
|
240
|
-
const key = feature._keys[pbf.readVarint()];
|
|
241
|
-
const value = feature._values[pbf.readVarint()];
|
|
242
|
-
feature.properties[key] = value;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
@@ -1,12 +1,22 @@
|
|
|
1
|
+
/* eslint-disable indent */
|
|
1
2
|
// This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
|
|
2
3
|
|
|
3
|
-
import
|
|
4
|
+
import Protobuf from 'pbf';
|
|
5
|
+
import VectorTileFeature from './vector-tile-feature';
|
|
4
6
|
|
|
5
7
|
export default class VectorTileLayer {
|
|
6
|
-
|
|
8
|
+
version: number;
|
|
9
|
+
name: string;
|
|
10
|
+
extent: number;
|
|
11
|
+
length: number;
|
|
12
|
+
_pbf: Protobuf;
|
|
13
|
+
_keys: string[];
|
|
14
|
+
_values: (string | number | boolean | null)[];
|
|
15
|
+
_features: number[];
|
|
16
|
+
constructor(pbf: Protobuf, end: number) {
|
|
7
17
|
// Public
|
|
8
18
|
this.version = 1;
|
|
9
|
-
this.name =
|
|
19
|
+
this.name = '';
|
|
10
20
|
this.extent = 4096;
|
|
11
21
|
this.length = 0;
|
|
12
22
|
|
|
@@ -21,8 +31,13 @@ export default class VectorTileLayer {
|
|
|
21
31
|
this.length = this._features.length;
|
|
22
32
|
}
|
|
23
33
|
|
|
24
|
-
|
|
25
|
-
|
|
34
|
+
/**
|
|
35
|
+
* return feature `i` from this layer as a `VectorTileFeature`
|
|
36
|
+
* @param index
|
|
37
|
+
* @returns feature
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
feature(i: number): VectorTileFeature {
|
|
26
41
|
if (i < 0 || i >= this._features.length) {
|
|
27
42
|
throw new Error('feature index out of bounds');
|
|
28
43
|
}
|
|
@@ -34,17 +49,30 @@ export default class VectorTileLayer {
|
|
|
34
49
|
}
|
|
35
50
|
}
|
|
36
51
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
52
|
+
/**
|
|
53
|
+
*
|
|
54
|
+
* @param tag
|
|
55
|
+
* @param layer
|
|
56
|
+
* @param pbf
|
|
57
|
+
*/
|
|
58
|
+
function readLayer(tag: number, layer?: VectorTileLayer, pbf?: Protobuf): void {
|
|
59
|
+
if (layer && pbf) {
|
|
60
|
+
if (tag === 15) layer.version = pbf.readVarint();
|
|
61
|
+
else if (tag === 1) layer.name = pbf.readString();
|
|
62
|
+
else if (tag === 5) layer.extent = pbf.readVarint();
|
|
63
|
+
else if (tag === 2) layer._features.push(pbf.pos);
|
|
64
|
+
else if (tag === 3) layer._keys.push(pbf.readString());
|
|
65
|
+
else if (tag === 4) layer._values.push(readValueMessage(pbf));
|
|
66
|
+
}
|
|
44
67
|
}
|
|
45
68
|
|
|
46
|
-
|
|
47
|
-
|
|
69
|
+
/**
|
|
70
|
+
*
|
|
71
|
+
* @param pbf
|
|
72
|
+
* @returns value
|
|
73
|
+
*/
|
|
74
|
+
function readValueMessage(pbf: Protobuf) {
|
|
75
|
+
let value: string | number | boolean | null = null;
|
|
48
76
|
const end = pbf.readVarint() + pbf.pos;
|
|
49
77
|
|
|
50
78
|
while (pbf.pos < end) {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
|
|
2
|
+
|
|
3
|
+
import VectorTileLayer from './vector-tile-layer';
|
|
4
|
+
import Protobuf from 'pbf';
|
|
5
|
+
|
|
6
|
+
export default class VectorTile {
|
|
7
|
+
layers: {[x: string]: VectorTileLayer};
|
|
8
|
+
constructor(pbf: Protobuf, end?: number) {
|
|
9
|
+
this.layers = pbf.readFields(readTile, {}, end);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param tag
|
|
16
|
+
* @param layers
|
|
17
|
+
* @param pbf
|
|
18
|
+
*/
|
|
19
|
+
function readTile(tag: number, layers?: {[x: string]: VectorTileLayer}, pbf?: Protobuf): void {
|
|
20
|
+
if (tag === 3) {
|
|
21
|
+
if (pbf) {
|
|
22
|
+
const layer = new VectorTileLayer(pbf, pbf.readVarint() + pbf.pos);
|
|
23
|
+
if (layer.length && layers) {
|
|
24
|
+
layers[layer.name] = layer;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// import {VectorTile} from '@mapbox/vector-tile';
|
|
2
|
+
import VectorTile from './mapbox-vector-tile/vector-tile';
|
|
3
|
+
import BinaryVectorTile from './binary-vector-tile/vector-tile';
|
|
4
|
+
|
|
5
|
+
import {featuresToBinary} from './binary-vector-tile/features-to-binary';
|
|
6
|
+
import Protobuf from 'pbf';
|
|
7
|
+
import {MvtBinaryCoordinates, MvtMapboxCoordinates, MvtOptions} from '../lib/types';
|
|
8
|
+
import VectorTileFeatureBinary from './binary-vector-tile/vector-tile-feature';
|
|
9
|
+
import VectorTileFeatureMapBox from './mapbox-vector-tile/vector-tile-feature';
|
|
10
|
+
import {LoaderOptions} from '@loaders.gl/loader-utils/';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Parse MVT arrayBuffer and return GeoJSON.
|
|
14
|
+
*
|
|
15
|
+
* @param arrayBuffer A MVT arrayBuffer
|
|
16
|
+
* @param options
|
|
17
|
+
* @returns A GeoJSON geometry object or a binary representation
|
|
18
|
+
*/
|
|
19
|
+
export default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptions) {
|
|
20
|
+
options = normalizeOptions(options);
|
|
21
|
+
const features: (MvtBinaryCoordinates | MvtMapboxCoordinates)[] = [];
|
|
22
|
+
|
|
23
|
+
if (options) {
|
|
24
|
+
const binary = options.gis.format === 'binary';
|
|
25
|
+
const firstPassData = {
|
|
26
|
+
pointPositionsCount: 0,
|
|
27
|
+
pointFeaturesCount: 0,
|
|
28
|
+
linePositionsCount: 0,
|
|
29
|
+
linePathsCount: 0,
|
|
30
|
+
lineFeaturesCount: 0,
|
|
31
|
+
polygonPositionsCount: 0,
|
|
32
|
+
polygonObjectsCount: 0,
|
|
33
|
+
polygonRingsCount: 0,
|
|
34
|
+
polygonFeaturesCount: 0
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
if (arrayBuffer.byteLength > 0) {
|
|
38
|
+
const tile = binary
|
|
39
|
+
? new BinaryVectorTile(new Protobuf(arrayBuffer))
|
|
40
|
+
: new VectorTile(new Protobuf(arrayBuffer));
|
|
41
|
+
const loaderOptions = options.mvt;
|
|
42
|
+
|
|
43
|
+
const selectedLayers = Array.isArray(loaderOptions.layers)
|
|
44
|
+
? loaderOptions.layers
|
|
45
|
+
: Object.keys(tile.layers);
|
|
46
|
+
|
|
47
|
+
selectedLayers.forEach((layerName: string) => {
|
|
48
|
+
const vectorTileLayer = tile.layers[layerName];
|
|
49
|
+
const featureOptions = {...loaderOptions, layerName};
|
|
50
|
+
|
|
51
|
+
if (!vectorTileLayer) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
56
|
+
const vectorTileFeature = vectorTileLayer.feature(i, firstPassData);
|
|
57
|
+
|
|
58
|
+
const decodedFeature = binary
|
|
59
|
+
? getDecodedFeatureBinary(vectorTileFeature as VectorTileFeatureBinary, featureOptions)
|
|
60
|
+
: getDecodedFeature(vectorTileFeature as VectorTileFeatureMapBox, featureOptions);
|
|
61
|
+
features.push(decodedFeature);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (binary) {
|
|
67
|
+
const data = featuresToBinary(features as MvtBinaryCoordinates[], firstPassData);
|
|
68
|
+
// Add the original byteLength (as a reasonable approximation of the size of the binary data)
|
|
69
|
+
// TODO decide where to store extra fields like byteLength (header etc) and document
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
data.byteLength = arrayBuffer.byteLength;
|
|
72
|
+
return data;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return features;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param options
|
|
80
|
+
* @returns options
|
|
81
|
+
*/
|
|
82
|
+
function normalizeOptions(options: LoaderOptions | undefined) {
|
|
83
|
+
if (options) {
|
|
84
|
+
options = {
|
|
85
|
+
...options,
|
|
86
|
+
mvt: options.mvt || {},
|
|
87
|
+
gis: options.gis || {}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// Validate
|
|
91
|
+
const wgs84Coordinates = options.coordinates === 'wgs84';
|
|
92
|
+
const {tileIndex} = options;
|
|
93
|
+
const hasTileIndex =
|
|
94
|
+
tileIndex &&
|
|
95
|
+
Number.isFinite(tileIndex.x) &&
|
|
96
|
+
Number.isFinite(tileIndex.y) &&
|
|
97
|
+
Number.isFinite(tileIndex.z);
|
|
98
|
+
|
|
99
|
+
if (wgs84Coordinates && !hasTileIndex) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
'MVT Loader: WGS84 coordinates need tileIndex property. Check documentation.'
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return options;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @param feature
|
|
110
|
+
* @param options
|
|
111
|
+
* @returns decoded feature
|
|
112
|
+
*/
|
|
113
|
+
function getDecodedFeature(
|
|
114
|
+
feature: VectorTileFeatureMapBox,
|
|
115
|
+
options: MvtOptions
|
|
116
|
+
): MvtMapboxCoordinates {
|
|
117
|
+
const decodedFeature = feature.toGeoJSON(
|
|
118
|
+
options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// Add layer name to GeoJSON properties
|
|
122
|
+
if (options.layerProperty) {
|
|
123
|
+
decodedFeature.properties[options.layerProperty] = options.layerName;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return decodedFeature;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param feature
|
|
131
|
+
* @param options
|
|
132
|
+
* @returns decoded binary feature
|
|
133
|
+
*/
|
|
134
|
+
function getDecodedFeatureBinary(
|
|
135
|
+
feature: VectorTileFeatureBinary,
|
|
136
|
+
options: MvtOptions
|
|
137
|
+
): MvtBinaryCoordinates {
|
|
138
|
+
const decodedFeature = feature.toBinaryCoordinates(
|
|
139
|
+
options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// Add layer name to GeoJSON properties
|
|
143
|
+
if (options.layerProperty) {
|
|
144
|
+
decodedFeature.properties[options.layerProperty] = options.layerName;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return decodedFeature;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @param line
|
|
152
|
+
* @param feature
|
|
153
|
+
*/
|
|
154
|
+
function transformToLocalCoordinates(line: number[], feature: {extent: any}): void {
|
|
155
|
+
// This function transforms local coordinates in a
|
|
156
|
+
// [0 - bufferSize, this.extent + bufferSize] range to a
|
|
157
|
+
// [0 - (bufferSize / this.extent), 1 + (bufferSize / this.extent)] range.
|
|
158
|
+
// The resulting extent would be 1.
|
|
159
|
+
const {extent} = feature;
|
|
160
|
+
for (let i = 0; i < line.length; i++) {
|
|
161
|
+
const p = line[i];
|
|
162
|
+
p[0] /= extent;
|
|
163
|
+
p[1] /= extent;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function transformToLocalCoordinatesBinary(data: number[], feature: {extent: any}) {
|
|
168
|
+
// For the binary code path, the feature data is just
|
|
169
|
+
// one big flat array, so we just divide each value
|
|
170
|
+
const {extent} = feature;
|
|
171
|
+
for (let i = 0, il = data.length; i < il; ++i) {
|
|
172
|
+
data[i] /= extent;
|
|
173
|
+
}
|
|
174
|
+
}
|