@loaders.gl/mvt 4.0.0-alpha.9 → 4.0.0-beta.1
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 +228 -24
- package/dist/es5/index.js +7 -0
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/lib/parse-mvt.js +8 -7
- package/dist/es5/lib/parse-mvt.js.map +1 -1
- package/dist/es5/lib/parse-tilejson.js +186 -0
- package/dist/es5/lib/parse-tilejson.js.map +1 -0
- package/dist/es5/lib/types.js.map +1 -1
- package/dist/es5/mvt-loader.js +10 -11
- package/dist/es5/mvt-loader.js.map +1 -1
- package/dist/es5/tilejson-loader.js +50 -0
- package/dist/es5/tilejson-loader.js.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lib/parse-mvt.js +8 -7
- package/dist/esm/lib/parse-mvt.js.map +1 -1
- package/dist/esm/lib/parse-tilejson.js +157 -0
- package/dist/esm/lib/parse-tilejson.js.map +1 -0
- package/dist/esm/lib/types.js.map +1 -1
- package/dist/esm/mvt-loader.js +10 -11
- package/dist/esm/mvt-loader.js.map +1 -1
- package/dist/esm/tilejson-loader.js +25 -0
- package/dist/esm/tilejson-loader.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/lib/parse-mvt.d.ts +3 -3
- package/dist/lib/parse-mvt.d.ts.map +1 -1
- package/dist/lib/parse-tilejson.d.ts +46 -0
- package/dist/lib/parse-tilejson.d.ts.map +1 -0
- package/dist/lib/types.d.ts +4 -4
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/mvt-loader.d.ts +5 -2
- package/dist/mvt-loader.d.ts.map +1 -1
- package/dist/mvt-worker.js +40 -26
- package/dist/tilejson-loader.d.ts +10 -0
- package/dist/tilejson-loader.d.ts.map +1 -0
- package/package.json +5 -5
- package/src/index.ts +5 -0
- package/src/lib/parse-mvt.ts +15 -11
- package/src/lib/parse-tilejson.ts +314 -0
- package/src/lib/types.ts +4 -4
- package/src/mvt-loader.ts +27 -13
- package/src/tilejson-loader.ts +39 -0
- package/dist/bundle.js +0 -5
- package/dist/helpers/binary-util-functions.js +0 -118
- package/dist/helpers/mapbox-util-functions.js +0 -82
- package/dist/index.js +0 -9
- package/dist/lib/binary-vector-tile/vector-tile-feature.js +0 -156
- package/dist/lib/binary-vector-tile/vector-tile-layer.js +0 -91
- package/dist/lib/binary-vector-tile/vector-tile.js +0 -29
- package/dist/lib/geojson-tiler/clip.js +0 -209
- package/dist/lib/geojson-tiler/convert.js +0 -134
- package/dist/lib/geojson-tiler/feature.js +0 -46
- package/dist/lib/geojson-tiler/geojson-tiler.js +0 -210
- package/dist/lib/geojson-tiler/simplify.js +0 -68
- package/dist/lib/geojson-tiler/tile.js +0 -125
- package/dist/lib/geojson-tiler/transform.js +0 -43
- package/dist/lib/geojson-tiler/wrap.js +0 -86
- package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +0 -170
- package/dist/lib/mapbox-vector-tile/vector-tile-layer.js +0 -89
- package/dist/lib/mapbox-vector-tile/vector-tile.js +0 -29
- package/dist/lib/parse-mvt.js +0 -167
- package/dist/lib/types.js +0 -2
- package/dist/mvt-loader.js +0 -47
- package/dist/workers/mvt-worker.js +0 -5
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loaders.gl/mvt",
|
|
3
3
|
"description": "Loader for Mapbox Vector Tiles",
|
|
4
|
-
"version": "4.0.0-
|
|
4
|
+
"version": "4.0.0-beta.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -32,14 +32,14 @@
|
|
|
32
32
|
"build-worker": "esbuild src/workers/mvt-worker.ts --bundle --outfile=dist/mvt-worker.js --define:__VERSION__=\\\"$npm_package_version\\\""
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@loaders.gl/gis": "4.0.0-
|
|
36
|
-
"@loaders.gl/loader-utils": "4.0.0-
|
|
37
|
-
"@loaders.gl/schema": "4.0.0-
|
|
35
|
+
"@loaders.gl/gis": "4.0.0-beta.1",
|
|
36
|
+
"@loaders.gl/loader-utils": "4.0.0-beta.1",
|
|
37
|
+
"@loaders.gl/schema": "4.0.0-beta.1",
|
|
38
38
|
"@math.gl/polygon": "^3.5.1",
|
|
39
39
|
"pbf": "^3.2.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/pbf": "^3.0.2"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "35c625e67132b0784e597d9ddabae8aefea29ff2"
|
|
45
45
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
// loaders.gl, MIT license
|
|
2
2
|
|
|
3
|
+
export type {MVTLoaderOptions} from './lib/types';
|
|
3
4
|
export {MVTLoader, MVTWorkerLoader} from './mvt-loader';
|
|
4
5
|
|
|
6
|
+
export type {TileJSON} from './lib/parse-tilejson';
|
|
7
|
+
export type {TileJSONLoaderOptions} from './tilejson-loader';
|
|
8
|
+
export {TileJSONLoader} from './tilejson-loader';
|
|
9
|
+
|
|
5
10
|
// GeoJSONTiler
|
|
6
11
|
|
|
7
12
|
export type {GeoJSONTilerOptions} from './lib/geojson-tiler/geojson-tiler';
|
package/src/lib/parse-mvt.ts
CHANGED
|
@@ -3,8 +3,8 @@ import type {
|
|
|
3
3
|
FlatFeature,
|
|
4
4
|
Feature,
|
|
5
5
|
GeojsonGeometryInfo,
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
BinaryFeatureCollection,
|
|
7
|
+
GeoJSONTable
|
|
8
8
|
} from '@loaders.gl/schema';
|
|
9
9
|
import Protobuf from 'pbf';
|
|
10
10
|
|
|
@@ -25,29 +25,31 @@ import VectorTileFeatureMapBox from './mapbox-vector-tile/vector-tile-feature';
|
|
|
25
25
|
export default function parseMVT(arrayBuffer: ArrayBuffer, options?: MVTLoaderOptions) {
|
|
26
26
|
const mvtOptions = normalizeOptions(options);
|
|
27
27
|
|
|
28
|
-
const shape
|
|
28
|
+
const shape: string | undefined =
|
|
29
|
+
options?.gis?.format || options?.mvt?.shape || (options?.shape as string);
|
|
29
30
|
switch (shape) {
|
|
30
31
|
case 'columnar-table': // binary + some JS arrays
|
|
31
32
|
return {shape: 'columnar-table', data: parseToBinary(arrayBuffer, mvtOptions)};
|
|
32
|
-
case 'geojson-
|
|
33
|
-
const table:
|
|
34
|
-
shape: 'geojson-
|
|
35
|
-
|
|
33
|
+
case 'geojson-table': {
|
|
34
|
+
const table: GeoJSONTable = {
|
|
35
|
+
shape: 'geojson-table',
|
|
36
|
+
type: 'FeatureCollection',
|
|
37
|
+
features: parseToGeojsonFeatures(arrayBuffer, mvtOptions)
|
|
36
38
|
};
|
|
37
39
|
return table;
|
|
38
40
|
}
|
|
39
41
|
case 'geojson':
|
|
40
|
-
return
|
|
42
|
+
return parseToGeojsonFeatures(arrayBuffer, mvtOptions);
|
|
41
43
|
case 'binary-geometry':
|
|
42
44
|
return parseToBinary(arrayBuffer, mvtOptions);
|
|
43
45
|
case 'binary':
|
|
44
46
|
return parseToBinary(arrayBuffer, mvtOptions);
|
|
45
47
|
default:
|
|
46
|
-
throw new Error(shape);
|
|
48
|
+
throw new Error(shape || 'undefined shape');
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
function parseToBinary(arrayBuffer: ArrayBuffer, options: MVTOptions):
|
|
52
|
+
function parseToBinary(arrayBuffer: ArrayBuffer, options: MVTOptions): BinaryFeatureCollection {
|
|
51
53
|
const [flatGeoJsonFeatures, geometryInfo] = parseToFlatGeoJson(arrayBuffer, options);
|
|
52
54
|
|
|
53
55
|
const binaryData = flatGeojsonToBinary(flatGeoJsonFeatures, geometryInfo);
|
|
@@ -101,7 +103,7 @@ function parseToFlatGeoJson(
|
|
|
101
103
|
return [features, geometryInfo];
|
|
102
104
|
}
|
|
103
105
|
|
|
104
|
-
function
|
|
106
|
+
function parseToGeojsonFeatures(arrayBuffer: ArrayBuffer, options: MVTOptions): Feature[] {
|
|
105
107
|
if (arrayBuffer.byteLength <= 0) {
|
|
106
108
|
return [];
|
|
107
109
|
}
|
|
@@ -159,6 +161,7 @@ function getDecodedFeature(
|
|
|
159
161
|
layerName: string
|
|
160
162
|
): MVTMapboxCoordinates {
|
|
161
163
|
const decodedFeature = feature.toGeoJSON(
|
|
164
|
+
// @ts-expect-error What is going on here?
|
|
162
165
|
options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates
|
|
163
166
|
);
|
|
164
167
|
|
|
@@ -181,6 +184,7 @@ function getDecodedFeatureBinary(
|
|
|
181
184
|
layerName: string
|
|
182
185
|
): FlatFeature {
|
|
183
186
|
const decodedFeature = feature.toBinaryCoordinates(
|
|
187
|
+
// @ts-expect-error What is going on here?
|
|
184
188
|
options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary
|
|
185
189
|
);
|
|
186
190
|
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
|
|
3
|
+
/** Parsed and typed TileJSON, merges Tilestats information if present */
|
|
4
|
+
export type TileJSON = {
|
|
5
|
+
name?: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
version?: string;
|
|
8
|
+
scheme?: 'xyz' | 'tms';
|
|
9
|
+
tiles?: string[];
|
|
10
|
+
/** `[[w, s], [e, n]]`, indicates the limits of the bounding box using the axis units and order of the specified CRS. */
|
|
11
|
+
boundingBox?: [min: [w: number, s: number], max: [e: number, n: number]];
|
|
12
|
+
center: number[] | null;
|
|
13
|
+
maxZoom: number | null;
|
|
14
|
+
minZoom: number | null;
|
|
15
|
+
htmlAttribution?: string;
|
|
16
|
+
htmlLegend?: string;
|
|
17
|
+
layers?: TileJSONLayer[];
|
|
18
|
+
metaJson?: any | null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type TileJSONLayer = {
|
|
22
|
+
/** The name (id) of this layer (tilejson.vector_layers[].id / tilestats.layers[].layer) */
|
|
23
|
+
name: string;
|
|
24
|
+
|
|
25
|
+
/** The description of this layer (tilejson.layer.description) */
|
|
26
|
+
description?: string;
|
|
27
|
+
|
|
28
|
+
// tilestats
|
|
29
|
+
|
|
30
|
+
/** The number of features in this layer (tilestats.layer.count) */
|
|
31
|
+
featureCount?: number;
|
|
32
|
+
/** The dominant geometry type in this layer (tilestats.layer.geometry) */
|
|
33
|
+
dominantGeometry?: string;
|
|
34
|
+
/** An array of details about the first 100 attributes in this layer */
|
|
35
|
+
|
|
36
|
+
/** */
|
|
37
|
+
minZoom?: number;
|
|
38
|
+
maxZoom?: number;
|
|
39
|
+
fields: TileJSONField[];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type TileJSONField = {
|
|
43
|
+
/** The name of this attribute */
|
|
44
|
+
name: string;
|
|
45
|
+
description?: string;
|
|
46
|
+
|
|
47
|
+
// tilestats
|
|
48
|
+
|
|
49
|
+
type: string;
|
|
50
|
+
/** min value (if there are *any* numbers in the values) */
|
|
51
|
+
min?: number;
|
|
52
|
+
/** max value (if there are *any* numbers in the values) */
|
|
53
|
+
max?: number;
|
|
54
|
+
/** An array of this attribute's first 100 unique values */
|
|
55
|
+
values?: unknown[];
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The raw/unparsed tilestats layer type
|
|
60
|
+
* @see https://github.com/mapbox/mapbox-geostats#output-the-stats
|
|
61
|
+
*/
|
|
62
|
+
type TilestatsLayer = {
|
|
63
|
+
/** The name of this layer */
|
|
64
|
+
layer: string;
|
|
65
|
+
/** The number of features in this layer */
|
|
66
|
+
count: number;
|
|
67
|
+
/** The dominant geometry type in this layer */
|
|
68
|
+
geometry: string;
|
|
69
|
+
/** The number of unique attributes in this layer (max. 1000) */
|
|
70
|
+
attributeCount: number;
|
|
71
|
+
/** Fields for this layer */
|
|
72
|
+
attributes?: TilestatsLayerAttribute[];
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The raw/unparsed tilestats attribute type
|
|
77
|
+
* @see https://github.com/mapbox/mapbox-geostats#output-the-stats
|
|
78
|
+
*/
|
|
79
|
+
type TilestatsLayerAttribute = {
|
|
80
|
+
/** The name of this layer */
|
|
81
|
+
attribute?: string;
|
|
82
|
+
/** Each attribute has one of the following types:
|
|
83
|
+
* - 'string' if all its values are strings (or null).
|
|
84
|
+
* - 'number' if all its values are numbers (or null).
|
|
85
|
+
* - 'boolean' if all its values are booleans (or null).
|
|
86
|
+
* - 'null' if its only value is null.
|
|
87
|
+
* - 'mixed' if it has values of multiple types.
|
|
88
|
+
* - Array and object values are coerced to strings.
|
|
89
|
+
*/
|
|
90
|
+
type?: string;
|
|
91
|
+
/** min value (if there are *any* numbers in the values) */
|
|
92
|
+
min?: number;
|
|
93
|
+
/** max value (if there are *any* numbers in the values) */
|
|
94
|
+
max?: number;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const isObject: (x: unknown) => boolean = (x) => x !== null && typeof x === 'object';
|
|
98
|
+
|
|
99
|
+
export function parseTileJSON(jsonMetadata: any): TileJSON | null {
|
|
100
|
+
if (!jsonMetadata || !isObject(jsonMetadata)) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const boundingBox = parseBounds(jsonMetadata.bounds);
|
|
105
|
+
const center = parseCenter(jsonMetadata.center);
|
|
106
|
+
const maxZoom = safeParseFloat(jsonMetadata.maxzoom);
|
|
107
|
+
const minZoom = safeParseFloat(jsonMetadata.minzoom);
|
|
108
|
+
|
|
109
|
+
let tileJSON: TileJSON = {
|
|
110
|
+
name: jsonMetadata.name || '',
|
|
111
|
+
description: jsonMetadata.description || '',
|
|
112
|
+
boundingBox,
|
|
113
|
+
center,
|
|
114
|
+
maxZoom,
|
|
115
|
+
minZoom,
|
|
116
|
+
layers: []
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// try to parse json
|
|
120
|
+
if (typeof jsonMetadata?.json === 'string') {
|
|
121
|
+
try {
|
|
122
|
+
tileJSON.metaJson = JSON.parse(jsonMetadata.json);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
// do nothing
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let layers = parseTilestatsLayers(tileJSON.metaJson?.tilestats);
|
|
129
|
+
// TODO - merge in description from tilejson
|
|
130
|
+
if (layers.length === 0) {
|
|
131
|
+
layers = parseTileJSONLayers(jsonMetadata.vector_layers); // eslint-disable-line camelcase
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
tileJSON = {
|
|
135
|
+
...tileJSON,
|
|
136
|
+
layers
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
return tileJSON;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function parseTileJSONLayers(layers: any[]): TileJSONLayer[] {
|
|
143
|
+
// Look for fields in vector_layers
|
|
144
|
+
if (!Array.isArray(layers)) {
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
return layers.map((layer) => ({
|
|
148
|
+
name: layer.id || '',
|
|
149
|
+
fields: Object.entries(layer.fields || []).map(([key, datatype]) => ({
|
|
150
|
+
name: key,
|
|
151
|
+
...attributeTypeToFieldType(String(datatype))
|
|
152
|
+
}))
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** parse Layers array from tilestats */
|
|
157
|
+
function parseTilestatsLayers(tilestats: any): TileJSONLayer[] {
|
|
158
|
+
if (isObject(tilestats) && Array.isArray(tilestats.layers)) {
|
|
159
|
+
// we are in luck!
|
|
160
|
+
return tilestats.layers.map((layer) => parseTilestatsForLayer(layer));
|
|
161
|
+
}
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function parseTilestatsForLayer(layer: TilestatsLayer): TileJSONLayer {
|
|
166
|
+
const fields: TileJSONField[] = [];
|
|
167
|
+
const indexedAttributes: {[key: string]: TilestatsLayerAttribute[]} = {};
|
|
168
|
+
|
|
169
|
+
const attributes = layer.attributes || [];
|
|
170
|
+
for (const attr of attributes) {
|
|
171
|
+
const name = attr.attribute;
|
|
172
|
+
if (typeof name === 'string') {
|
|
173
|
+
if (name.split('|').length > 1) {
|
|
174
|
+
// indexed field
|
|
175
|
+
const fname = name.split('|')[0];
|
|
176
|
+
indexedAttributes[fname] = indexedAttributes[fname] || [];
|
|
177
|
+
indexedAttributes[fname].push(attr);
|
|
178
|
+
} else if (!fields[name]) {
|
|
179
|
+
fields[name] = attributeToField(attr);
|
|
180
|
+
} else {
|
|
181
|
+
// return (fields[name], attr);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
name: layer.layer || '',
|
|
187
|
+
dominantGeometry: layer.geometry,
|
|
188
|
+
fields
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* bounds should be [minLng, minLat, maxLng, maxLat]
|
|
194
|
+
*`[[w, s], [e, n]]`, indicates the limits of the bounding box using the axis units and order of the specified CRS.
|
|
195
|
+
*/
|
|
196
|
+
function parseBounds(
|
|
197
|
+
bounds: string | number[]
|
|
198
|
+
): [[east: number, south: number], [west: number, north: number]] | undefined {
|
|
199
|
+
// supported formats
|
|
200
|
+
// string: "-96.657715,40.126127,-90.140061,43.516689",
|
|
201
|
+
// array: [ -180, -85.05112877980659, 180, 85.0511287798066 ]
|
|
202
|
+
const result = fromArrayOrString(bounds);
|
|
203
|
+
// validate bounds
|
|
204
|
+
if (
|
|
205
|
+
Array.isArray(result) &&
|
|
206
|
+
result.length === 4 &&
|
|
207
|
+
[result[0], result[2]].every(isLng) &&
|
|
208
|
+
[result[1], result[3]].every(isLat)
|
|
209
|
+
) {
|
|
210
|
+
return [
|
|
211
|
+
[result[0], result[1]],
|
|
212
|
+
[result[2], result[3]]
|
|
213
|
+
];
|
|
214
|
+
}
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function parseCenter(center: string | number[]): number[] | null {
|
|
219
|
+
// supported formats
|
|
220
|
+
// string: "-96.657715,40.126127,-90.140061,43.516689",
|
|
221
|
+
// array: [-91.505127,41.615442,14]
|
|
222
|
+
const result = fromArrayOrString(center);
|
|
223
|
+
if (
|
|
224
|
+
Array.isArray(result) &&
|
|
225
|
+
result.length === 3 &&
|
|
226
|
+
isLng(result[0]) &&
|
|
227
|
+
isLat(result[1]) &&
|
|
228
|
+
isZoom(result[2])
|
|
229
|
+
) {
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function safeParseFloat(input: unknown): number | null {
|
|
236
|
+
const result =
|
|
237
|
+
typeof input === 'string' ? parseFloat(input) : typeof input === 'number' ? input : null;
|
|
238
|
+
return result === null || isNaN(result) ? null : result;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// https://github.com/mapbox/tilejson-spec/tree/master/2.2.0
|
|
242
|
+
function isLat(num: any): boolean {
|
|
243
|
+
return Number.isFinite(num) && num <= 90 && num >= -90;
|
|
244
|
+
}
|
|
245
|
+
function isLng(num: any): boolean {
|
|
246
|
+
return Number.isFinite(num) && num <= 180 && num >= -180;
|
|
247
|
+
}
|
|
248
|
+
function isZoom(num: any): boolean {
|
|
249
|
+
return Number.isFinite(num) && num >= 0 && num <= 22;
|
|
250
|
+
}
|
|
251
|
+
function fromArrayOrString(data: string | number[]): number[] | null {
|
|
252
|
+
if (typeof data === 'string') {
|
|
253
|
+
return data.split(',').map(parseFloat);
|
|
254
|
+
} else if (Array.isArray(data)) {
|
|
255
|
+
return data;
|
|
256
|
+
}
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// possible types https://github.com/mapbox/tippecanoe#modifying-feature-attributes
|
|
261
|
+
const attrTypeMap = {
|
|
262
|
+
number: {
|
|
263
|
+
type: 'float32'
|
|
264
|
+
},
|
|
265
|
+
numeric: {
|
|
266
|
+
type: 'float32'
|
|
267
|
+
},
|
|
268
|
+
string: {
|
|
269
|
+
type: 'utf8'
|
|
270
|
+
},
|
|
271
|
+
vachar: {
|
|
272
|
+
type: 'utf8'
|
|
273
|
+
},
|
|
274
|
+
float: {
|
|
275
|
+
type: 'float32'
|
|
276
|
+
},
|
|
277
|
+
int: {
|
|
278
|
+
type: 'int32'
|
|
279
|
+
},
|
|
280
|
+
int4: {
|
|
281
|
+
type: 'int32'
|
|
282
|
+
},
|
|
283
|
+
boolean: {
|
|
284
|
+
type: 'boolean'
|
|
285
|
+
},
|
|
286
|
+
bool: {
|
|
287
|
+
type: 'boolean'
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
function attributeToField(attribute: TilestatsLayerAttribute = {}): TileJSONField {
|
|
292
|
+
// attribute: "_season_peaks_color"
|
|
293
|
+
// count: 1000
|
|
294
|
+
// max: 0.95
|
|
295
|
+
// min: 0.24375
|
|
296
|
+
// type: "number"
|
|
297
|
+
const fieldTypes = attributeTypeToFieldType(attribute.type!);
|
|
298
|
+
return {
|
|
299
|
+
name: attribute.attribute as string,
|
|
300
|
+
// what happens if attribute type is string...
|
|
301
|
+
// filterProps: getFilterProps(fieldTypes.type, attribute),
|
|
302
|
+
...fieldTypes
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function attributeTypeToFieldType(aType: string): {type: string} {
|
|
307
|
+
const type = aType.toLowerCase();
|
|
308
|
+
if (!type || !attrTypeMap[type]) {
|
|
309
|
+
// console.warn(
|
|
310
|
+
// `cannot convert attribute type ${type} to loaders.gl data type, use string by default`
|
|
311
|
+
// );
|
|
312
|
+
}
|
|
313
|
+
return attrTypeMap[type] || {type: 'string'};
|
|
314
|
+
}
|
package/src/lib/types.ts
CHANGED
|
@@ -14,13 +14,13 @@ type MVTWgs84CoordinatesOptions = {
|
|
|
14
14
|
/**
|
|
15
15
|
* When set to `wgs84`, the parser will return a flat array of GeoJSON objects with coordinates in longitude, latitude decoded from the provided tile index.
|
|
16
16
|
*/
|
|
17
|
-
coordinates
|
|
17
|
+
coordinates?: 'wgs84';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Mandatory with `wgs84` coordinates option. An object containing tile index values (`x`, `y`,
|
|
21
21
|
* `z`) to reproject features' coordinates into WGS84.
|
|
22
22
|
*/
|
|
23
|
-
tileIndex
|
|
23
|
+
tileIndex?: {x: number; y: number; z: number};
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
export type MVTOptions = (MVTLocalCoordinatesOptions | MVTWgs84CoordinatesOptions) & {
|
|
@@ -36,7 +36,7 @@ export type MVTOptions = (MVTLocalCoordinatesOptions | MVTWgs84CoordinatesOption
|
|
|
36
36
|
* be included in the output. If `null`, features from all layers are returned.
|
|
37
37
|
*/
|
|
38
38
|
layers?: string[];
|
|
39
|
-
shape?: 'geojson-
|
|
39
|
+
shape?: 'geojson-table' | 'columnar-table' | 'geojson' | 'binary' | 'binary-geometry';
|
|
40
40
|
};
|
|
41
41
|
|
|
42
42
|
export type MVTMapboxGeometry = {
|
|
@@ -64,6 +64,6 @@ export type MVTLoaderOptions = LoaderOptions & {
|
|
|
64
64
|
*/
|
|
65
65
|
binary?: boolean;
|
|
66
66
|
/** @deprecated. Use options.mvt.shape */
|
|
67
|
-
format?: 'geojson-
|
|
67
|
+
format?: 'geojson-table' | 'columnar-table' | 'geojson' | 'binary' | 'binary-geometry';
|
|
68
68
|
};
|
|
69
69
|
};
|
package/src/mvt-loader.ts
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
import type {Loader, LoaderWithParser} from '@loaders.gl/loader-utils';
|
|
2
2
|
import type {MVTLoaderOptions} from './lib/types';
|
|
3
|
+
// import type {
|
|
4
|
+
// Feature,
|
|
5
|
+
// BinaryFeatureCollection,
|
|
6
|
+
// GeoJSONTable,
|
|
7
|
+
// Geometry,
|
|
8
|
+
// GeoJsonProperties
|
|
9
|
+
// } from '@loaders.gl/schema';
|
|
3
10
|
import parseMVT from './lib/parse-mvt';
|
|
4
11
|
|
|
5
12
|
// __VERSION__ is injected by babel-plugin-version-inline
|
|
6
13
|
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
7
14
|
const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
|
|
8
15
|
|
|
9
|
-
const DEFAULT_MVT_LOADER_OPTIONS: MVTLoaderOptions = {
|
|
10
|
-
mvt: {
|
|
11
|
-
shape: 'geojson',
|
|
12
|
-
coordinates: 'local',
|
|
13
|
-
layerProperty: 'layerName',
|
|
14
|
-
layers: undefined,
|
|
15
|
-
tileIndex: null
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
|
|
19
16
|
/**
|
|
20
17
|
* Worker loader for the Mapbox Vector Tile format
|
|
21
18
|
*/
|
|
22
|
-
export const MVTWorkerLoader: Loader
|
|
19
|
+
export const MVTWorkerLoader: Loader<
|
|
20
|
+
any, // BinaryFeatureCollection | GeoJSONTable | Feature<Geometry, GeoJsonProperties>,
|
|
21
|
+
never,
|
|
22
|
+
MVTLoaderOptions
|
|
23
|
+
> = {
|
|
23
24
|
name: 'Mapbox Vector Tile',
|
|
24
25
|
id: 'mvt',
|
|
25
26
|
module: 'mvt',
|
|
@@ -27,19 +28,32 @@ export const MVTWorkerLoader: Loader = {
|
|
|
27
28
|
// Note: ArcGIS uses '.pbf' extension and 'application/octet-stream'
|
|
28
29
|
extensions: ['mvt', 'pbf'],
|
|
29
30
|
mimeTypes: [
|
|
31
|
+
// https://www.iana.org/assignments/media-types/application/vnd.mapbox-vector-tile
|
|
30
32
|
'application/vnd.mapbox-vector-tile',
|
|
31
33
|
'application/x-protobuf'
|
|
32
34
|
// 'application/octet-stream'
|
|
33
35
|
],
|
|
34
36
|
worker: true,
|
|
35
37
|
category: 'geometry',
|
|
36
|
-
options:
|
|
38
|
+
options: {
|
|
39
|
+
mvt: {
|
|
40
|
+
shape: 'geojson',
|
|
41
|
+
coordinates: 'local',
|
|
42
|
+
layerProperty: 'layerName',
|
|
43
|
+
layers: undefined,
|
|
44
|
+
tileIndex: null
|
|
45
|
+
}
|
|
46
|
+
}
|
|
37
47
|
};
|
|
38
48
|
|
|
39
49
|
/**
|
|
40
50
|
* Loader for the Mapbox Vector Tile format
|
|
41
51
|
*/
|
|
42
|
-
export const MVTLoader: LoaderWithParser
|
|
52
|
+
export const MVTLoader: LoaderWithParser<
|
|
53
|
+
any, // BinaryFeatureCollection | GeoJSONTable | Feature<Geometry, GeoJsonProperties>,
|
|
54
|
+
never,
|
|
55
|
+
MVTLoaderOptions
|
|
56
|
+
> = {
|
|
43
57
|
...MVTWorkerLoader,
|
|
44
58
|
parse: async (arrayBuffer, options?: MVTLoaderOptions) => parseMVT(arrayBuffer, options),
|
|
45
59
|
parseSync: parseMVT,
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
|
|
3
|
+
import type {LoaderWithParser, LoaderOptions} from '@loaders.gl/loader-utils';
|
|
4
|
+
import type {TileJSON} from './lib/parse-tilejson';
|
|
5
|
+
import {parseTileJSON} from './lib/parse-tilejson';
|
|
6
|
+
|
|
7
|
+
// __VERSION__ is injected by babel-plugin-version-inline
|
|
8
|
+
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
9
|
+
const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
|
|
10
|
+
|
|
11
|
+
export type TileJSONLoaderOptions = LoaderOptions & {
|
|
12
|
+
tilejson?: {};
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Loader for TileJSON metadata
|
|
17
|
+
*/
|
|
18
|
+
export const TileJSONLoader: LoaderWithParser<TileJSON, never, TileJSONLoaderOptions> = {
|
|
19
|
+
name: 'TileJSON',
|
|
20
|
+
id: 'tilejson',
|
|
21
|
+
module: 'pmtiles',
|
|
22
|
+
version: VERSION,
|
|
23
|
+
worker: true,
|
|
24
|
+
extensions: ['json'],
|
|
25
|
+
mimeTypes: ['application/json'],
|
|
26
|
+
text: true,
|
|
27
|
+
options: {
|
|
28
|
+
tilejson: {}
|
|
29
|
+
},
|
|
30
|
+
parse: async (arrayBuffer, options) => {
|
|
31
|
+
const jsonString = new TextDecoder().decode(arrayBuffer);
|
|
32
|
+
const json = JSON.parse(jsonString);
|
|
33
|
+
return parseTileJSON(json) as TileJSON;
|
|
34
|
+
},
|
|
35
|
+
parseTextSync: (text, options) => {
|
|
36
|
+
const json = JSON.parse(text);
|
|
37
|
+
return parseTileJSON(json) as TileJSON;
|
|
38
|
+
}
|
|
39
|
+
};
|