@loaders.gl/mvt 4.0.0-alpha.4 → 4.0.0-alpha.5

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.
Files changed (48) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/dist.min.js +2289 -0
  4. package/dist/helpers/binary-util-functions.d.ts +35 -0
  5. package/dist/helpers/binary-util-functions.d.ts.map +1 -0
  6. package/dist/helpers/binary-util-functions.js +8 -5
  7. package/dist/helpers/binary-util-functions.js.map +1 -1
  8. package/dist/helpers/mapbox-util-functions.d.ts +29 -0
  9. package/dist/helpers/mapbox-util-functions.d.ts.map +1 -0
  10. package/dist/index.d.ts +2 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +41 -0
  13. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +1 -0
  14. package/dist/lib/binary-vector-tile/vector-tile-feature.js +31 -30
  15. package/dist/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
  16. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +23 -0
  17. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +1 -0
  18. package/dist/lib/binary-vector-tile/vector-tile-layer.js +2 -2
  19. package/dist/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
  20. package/dist/lib/binary-vector-tile/vector-tile.d.ts +9 -0
  21. package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +1 -0
  22. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +27 -0
  23. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +1 -0
  24. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts +20 -0
  25. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +1 -0
  26. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts +9 -0
  27. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +1 -0
  28. package/dist/lib/parse-mvt.d.ts +17 -0
  29. package/dist/lib/parse-mvt.d.ts.map +1 -0
  30. package/dist/lib/parse-mvt.js +6 -5
  31. package/dist/lib/parse-mvt.js.map +1 -1
  32. package/dist/lib/types.d.ts +28 -0
  33. package/dist/lib/types.d.ts.map +1 -0
  34. package/dist/mvt-loader.d.ts +10 -0
  35. package/dist/mvt-loader.d.ts.map +1 -0
  36. package/dist/mvt-loader.js +1 -1
  37. package/dist/mvt-worker.js +103 -98
  38. package/dist/workers/mvt-worker.d.ts +2 -0
  39. package/dist/workers/mvt-worker.d.ts.map +1 -0
  40. package/package.json +8 -7
  41. package/src/helpers/binary-util-functions.ts +9 -7
  42. package/src/lib/binary-vector-tile/vector-tile-feature.ts +36 -44
  43. package/src/lib/binary-vector-tile/vector-tile-layer.ts +4 -4
  44. package/src/lib/parse-mvt.ts +11 -9
  45. package/src/lib/types.ts +0 -69
  46. package/dist/lib/binary-vector-tile/features-to-binary.js +0 -327
  47. package/dist/lib/binary-vector-tile/features-to-binary.js.map +0 -1
  48. package/src/lib/binary-vector-tile/features-to-binary.ts +0 -518
@@ -1,7 +1,7 @@
1
1
  // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
2
2
 
3
3
  import Protobuf from 'pbf';
4
- import {MvtBinaryCoordinates, MvtBinaryGeometry, MvtFirstPassedData} from '../types';
4
+ import {FlatFeature, FlatIndexedGeometry, GeojsonGeometryInfo} from '@loaders.gl/schema';
5
5
  import {classifyRings, project, readFeature} from '../../helpers/binary-util-functions';
6
6
 
7
7
  // Reduce GC by reusing variables
@@ -26,10 +26,7 @@ export default class VectorTileFeature {
26
26
  _geometry: number;
27
27
  _keys: string[];
28
28
  _values: (string | number | boolean | null)[];
29
- _firstPassData: MvtFirstPassedData;
30
- static get types() {
31
- return ['Unknown', 'Point', 'LineString', 'Polygon'];
32
- }
29
+ _geometryInfo: GeojsonGeometryInfo;
33
30
 
34
31
  // eslint-disable-next-line max-params
35
32
  constructor(
@@ -38,7 +35,7 @@ export default class VectorTileFeature {
38
35
  extent: any,
39
36
  keys: string[],
40
37
  values: (string | number | boolean | null)[],
41
- firstPassData: MvtFirstPassedData
38
+ geometryInfo: GeojsonGeometryInfo
42
39
  ) {
43
40
  // Public
44
41
  this.properties = {};
@@ -51,13 +48,13 @@ export default class VectorTileFeature {
51
48
  this._geometry = -1;
52
49
  this._keys = keys;
53
50
  this._values = values;
54
- this._firstPassData = firstPassData;
51
+ this._geometryInfo = geometryInfo;
55
52
 
56
53
  pbf.readFields(readFeature, this, end);
57
54
  }
58
55
 
59
56
  // eslint-disable-next-line complexity, max-statements
60
- loadGeometry(): MvtBinaryGeometry {
57
+ loadGeometry(): FlatIndexedGeometry {
61
58
  const pbf = this._pbf;
62
59
  pbf.pos = this._geometry;
63
60
 
@@ -73,7 +70,7 @@ export default class VectorTileFeature {
73
70
  // `set()` and direct index access. Also, we cannot
74
71
  // know how large the buffer should be, so it would
75
72
  // increase memory usage
76
- const lines: number[] = []; // Indices where lines start
73
+ const indices: number[] = []; // Indices where geometries start
77
74
  const data: number[] = []; // Flat array of coordinate data
78
75
 
79
76
  while (pbf.pos < endPos) {
@@ -91,14 +88,14 @@ export default class VectorTileFeature {
91
88
 
92
89
  if (cmd === 1) {
93
90
  // New line
94
- lines.push(i);
91
+ indices.push(i);
95
92
  }
96
93
  data.push(x, y);
97
94
  i += 2;
98
95
  } else if (cmd === 7) {
99
96
  // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
100
97
  if (i > 0) {
101
- const start = lines[lines.length - 1]; // start index of polygon
98
+ const start = indices[indices.length - 1]; // start index of polygon
102
99
  data.push(data[start], data[start + 1]); // closePolygon
103
100
  i += 2;
104
101
  }
@@ -107,7 +104,7 @@ export default class VectorTileFeature {
107
104
  }
108
105
  }
109
106
 
110
- return {data, lines};
107
+ return {data, indices};
111
108
  }
112
109
 
113
110
  /**
@@ -116,27 +113,28 @@ export default class VectorTileFeature {
116
113
  * @returns result
117
114
  */
118
115
  _toBinaryCoordinates(transform) {
119
- // Expands the protobuf data to an intermediate `lines`
116
+ // Expands the protobuf data to an intermediate Flat GeoJSON
120
117
  // data format, which maps closely to the binary data buffers.
121
118
  // It is similar to GeoJSON, but rather than storing the coordinates
122
119
  // in multidimensional arrays, we have a 1D `data` with all the
123
- // coordinates, and then index into this using the `lines`
120
+ // coordinates, and then index into this using the `indices`
124
121
  // parameter, e.g.
125
122
  //
126
123
  // geometry: {
127
- // type: 'Point', data: [1,2], lines: [0]
124
+ // type: 'Point', data: [1,2], indices: [0]
128
125
  // }
129
126
  // geometry: {
130
- // type: 'LineString', data: [1,2,3,4,...], lines: [0]
127
+ // type: 'LineString', data: [1,2,3,4,...], indices: [0]
131
128
  // }
132
129
  // geometry: {
133
- // type: 'Polygon', data: [1,2,3,4,...], lines: [[0, 2]]
130
+ // type: 'Polygon', data: [1,2,3,4,...], indices: [[0, 2]]
134
131
  // }
135
- // Thus the lines member lets us look up the relevant range
132
+ // Thus the indices member lets us look up the relevant range
136
133
  // from the data array.
137
134
  // The Multi* versions of the above types share the same data
138
- // structure, just with multiple elements in the lines array
139
- let geom = this.loadGeometry();
135
+ // structure, just with multiple elements in the indices array
136
+ const geom = this.loadGeometry();
137
+ let geometry;
140
138
 
141
139
  // Apply the supplied transformation to data
142
140
  transform(geom.data, this);
@@ -146,43 +144,37 @@ export default class VectorTileFeature {
146
144
  // eslint-disable-next-line default-case
147
145
  switch (this.type) {
148
146
  case 1: // Point
149
- this._firstPassData.pointFeaturesCount++;
150
- this._firstPassData.pointPositionsCount += geom.lines.length;
147
+ this._geometryInfo.pointFeaturesCount++;
148
+ this._geometryInfo.pointPositionsCount += geom.indices.length;
149
+ geometry = {type: 'Point', ...geom};
151
150
  break;
152
151
 
153
152
  case 2: // LineString
154
- this._firstPassData.lineFeaturesCount++;
155
- this._firstPassData.linePathsCount += geom.lines.length;
156
- this._firstPassData.linePositionsCount += geom.data.length / coordLength;
153
+ this._geometryInfo.lineFeaturesCount++;
154
+ this._geometryInfo.linePathsCount += geom.indices.length;
155
+ this._geometryInfo.linePositionsCount += geom.data.length / coordLength;
156
+ geometry = {type: 'LineString', ...geom};
157
157
  break;
158
158
 
159
159
  case 3: // Polygon
160
- const classified = classifyRings(geom);
160
+ geometry = classifyRings(geom);
161
161
 
162
- // Unlike Point & LineString geom.lines is a 2D array, thanks
162
+ // Unlike Point & LineString geom.indices is a 2D array, thanks
163
163
  // to the classifyRings method
164
- this._firstPassData.polygonFeaturesCount++;
165
- this._firstPassData.polygonObjectsCount += classified.lines.length;
164
+ this._geometryInfo.polygonFeaturesCount++;
165
+ this._geometryInfo.polygonObjectsCount += geometry.indices.length;
166
166
 
167
- for (const lines of classified.lines) {
168
- this._firstPassData.polygonRingsCount += lines.length;
167
+ for (const indices of geometry.indices) {
168
+ this._geometryInfo.polygonRingsCount += indices.length;
169
169
  }
170
- this._firstPassData.polygonPositionsCount += classified.data.length / coordLength;
170
+ this._geometryInfo.polygonPositionsCount += geometry.data.length / coordLength;
171
171
 
172
- geom = classified;
173
172
  break;
173
+ default:
174
+ throw new Error(`Invalid geometry type: ${this.type}`);
174
175
  }
175
176
 
176
- geom.type = VectorTileFeature.types[this.type];
177
- if (geom.lines.length > 1) {
178
- geom.type = `Multi${geom.type}`;
179
- }
180
-
181
- const result: MvtBinaryCoordinates = {
182
- type: 'Feature',
183
- geometry: geom,
184
- properties: this.properties
185
- };
177
+ const result: FlatFeature = {type: 'Feature', geometry, properties: this.properties};
186
178
 
187
179
  if (this.id !== null) {
188
180
  result.id = this.id;
@@ -193,7 +185,7 @@ export default class VectorTileFeature {
193
185
 
194
186
  toBinaryCoordinates(
195
187
  options: {x: number; y: number; z: number} | ((data: number[], feature: {extent: any}) => void)
196
- ): MvtBinaryCoordinates {
188
+ ): FlatFeature {
197
189
  if (typeof options === 'function') {
198
190
  return this._toBinaryCoordinates(options);
199
191
  }
@@ -3,7 +3,7 @@
3
3
 
4
4
  import VectorTileFeature from './vector-tile-feature';
5
5
  import Protobuf from 'pbf';
6
- import {MvtFirstPassedData} from '../types';
6
+ import {GeojsonGeometryInfo} from '@loaders.gl/schema';
7
7
 
8
8
  export default class VectorTileLayer {
9
9
  version: number;
@@ -36,10 +36,10 @@ export default class VectorTileLayer {
36
36
  * return feature `i` from this layer as a `VectorTileFeature`
37
37
  *
38
38
  * @param index
39
- * @param firstPassData
39
+ * @param geometryInfo
40
40
  * @returns {VectorTileFeature}
41
41
  */
42
- feature(i: number, firstPassData: MvtFirstPassedData): VectorTileFeature {
42
+ feature(i: number, geometryInfo: GeojsonGeometryInfo): VectorTileFeature {
43
43
  if (i < 0 || i >= this._features.length) {
44
44
  throw new Error('feature index out of bounds');
45
45
  }
@@ -53,7 +53,7 @@ export default class VectorTileLayer {
53
53
  this.extent,
54
54
  this._keys,
55
55
  this._values,
56
- firstPassData
56
+ geometryInfo
57
57
  );
58
58
  }
59
59
  }
@@ -2,12 +2,13 @@
2
2
  import VectorTile from './mapbox-vector-tile/vector-tile';
3
3
  import BinaryVectorTile from './binary-vector-tile/vector-tile';
4
4
 
5
- import {featuresToBinary} from './binary-vector-tile/features-to-binary';
5
+ import {flatGeojsonToBinary} from '@loaders.gl/gis';
6
6
  import Protobuf from 'pbf';
7
- import {MvtBinaryCoordinates, MvtMapboxCoordinates, MvtOptions} from '../lib/types';
7
+ import type {FlatFeature} from '@loaders.gl/schema';
8
+ import type {MvtMapboxCoordinates, MvtOptions} from '../lib/types';
8
9
  import VectorTileFeatureBinary from './binary-vector-tile/vector-tile-feature';
9
10
  import VectorTileFeatureMapBox from './mapbox-vector-tile/vector-tile-feature';
10
- import {LoaderOptions} from '@loaders.gl/loader-utils/';
11
+ import {LoaderOptions} from '@loaders.gl/loader-utils';
11
12
 
12
13
  /**
13
14
  * Parse MVT arrayBuffer and return GeoJSON.
@@ -18,11 +19,12 @@ import {LoaderOptions} from '@loaders.gl/loader-utils/';
18
19
  */
19
20
  export default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptions) {
20
21
  options = normalizeOptions(options);
21
- const features: (MvtBinaryCoordinates | MvtMapboxCoordinates)[] = [];
22
+ const features: (FlatFeature | MvtMapboxCoordinates)[] = [];
22
23
 
23
24
  if (options) {
24
25
  const binary = options.gis.format === 'binary';
25
- const firstPassData = {
26
+ const geometryInfo = {
27
+ coordLength: 2,
26
28
  pointPositionsCount: 0,
27
29
  pointFeaturesCount: 0,
28
30
  linePositionsCount: 0,
@@ -53,7 +55,7 @@ export default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptio
53
55
  }
54
56
 
55
57
  for (let i = 0; i < vectorTileLayer.length; i++) {
56
- const vectorTileFeature = vectorTileLayer.feature(i, firstPassData);
58
+ const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
57
59
 
58
60
  const decodedFeature = binary
59
61
  ? getDecodedFeatureBinary(vectorTileFeature as VectorTileFeatureBinary, featureOptions)
@@ -64,7 +66,7 @@ export default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptio
64
66
  }
65
67
 
66
68
  if (binary) {
67
- const data = featuresToBinary(features as MvtBinaryCoordinates[], firstPassData);
69
+ const data = flatGeojsonToBinary(features as FlatFeature[], geometryInfo);
68
70
  // Add the original byteLength (as a reasonable approximation of the size of the binary data)
69
71
  // TODO decide where to store extra fields like byteLength (header etc) and document
70
72
  // @ts-ignore
@@ -134,13 +136,13 @@ function getDecodedFeature(
134
136
  function getDecodedFeatureBinary(
135
137
  feature: VectorTileFeatureBinary,
136
138
  options: MvtOptions
137
- ): MvtBinaryCoordinates {
139
+ ): FlatFeature {
138
140
  const decodedFeature = feature.toBinaryCoordinates(
139
141
  options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary
140
142
  );
141
143
 
142
144
  // Add layer name to GeoJSON properties
143
- if (options.layerProperty) {
145
+ if (options.layerProperty && decodedFeature.properties) {
144
146
  decodedFeature.properties[options.layerProperty] = options.layerName;
145
147
  }
146
148
 
package/src/lib/types.ts CHANGED
@@ -5,14 +5,6 @@ export type MvtOptions = {
5
5
  layerName: string;
6
6
  };
7
7
 
8
- export type MvtBinaryGeometry = {
9
- data: number[];
10
- lines: any[];
11
- areas?: number[];
12
- type?: string;
13
- id?: number;
14
- };
15
-
16
8
  export type MvtMapboxGeometry = {
17
9
  type?: string;
18
10
  id?: number;
@@ -20,13 +12,6 @@ export type MvtMapboxGeometry = {
20
12
  coordinates?: any[];
21
13
  };
22
14
 
23
- export type MvtBinaryCoordinates = {
24
- type: string;
25
- geometry: MvtBinaryGeometry;
26
- properties: {[x: string]: string | number | boolean | null};
27
- id?: number;
28
- };
29
-
30
15
  export type MvtMapboxCoordinates = {
31
16
  type: string;
32
17
  geometry: {
@@ -36,57 +21,3 @@ export type MvtMapboxCoordinates = {
36
21
  properties: {[x: string]: string | number | boolean | null};
37
22
  id?: number;
38
23
  };
39
-
40
- export type MvtBinaryOptions = {
41
- numericPropKeys: string[];
42
- PositionDataType: Float32ArrayConstructor;
43
- };
44
-
45
- export type MvtFirstPassedData = {
46
- pointPositionsCount: number;
47
- pointFeaturesCount: number;
48
- linePositionsCount: number;
49
- linePathsCount: number;
50
- lineFeaturesCount: number;
51
- polygonPositionsCount: number;
52
- polygonObjectsCount: number;
53
- polygonRingsCount: number;
54
- polygonFeaturesCount: number;
55
- };
56
-
57
- export type MvtPoints = {
58
- positions: Float32Array;
59
- globalFeatureIds: Uint16Array | Uint32Array;
60
- featureIds: Uint16Array | Uint32Array;
61
- numericProps: object;
62
- properties: {}[];
63
- fields: {
64
- id?: number;
65
- }[];
66
- };
67
-
68
- export type MvtLines = {
69
- pathIndices: Uint16Array | Uint32Array;
70
- positions: Float32Array;
71
- globalFeatureIds: Uint16Array | Uint32Array;
72
- featureIds: Uint16Array | Uint32Array;
73
- numericProps: object;
74
- properties: {}[];
75
- fields: {
76
- id?: number;
77
- }[];
78
- };
79
-
80
- export type MvtPolygons = {
81
- polygonIndices: Uint16Array | Uint32Array;
82
- primitivePolygonIndices: Uint16Array | Uint32Array;
83
- positions: Float32Array;
84
- triangles: number[];
85
- globalFeatureIds: Uint16Array | Uint32Array;
86
- featureIds: Uint16Array | Uint32Array;
87
- numericProps: object;
88
- properties: {}[];
89
- fields: {
90
- id?: number;
91
- }[];
92
- };
@@ -1,327 +0,0 @@
1
- import { earcut } from '@math.gl/polygon';
2
- export function featuresToBinary(features, firstPassData, options) {
3
- return fillArrays(features, firstPassData, {
4
- numericPropKeys: options ? options.numericPropKeys : extractNumericPropKeys(features),
5
- PositionDataType: options ? options.PositionDataType : Float32Array
6
- });
7
- }
8
- export const TEST_EXPORTS = {
9
- extractNumericPropKeys,
10
- fillArrays
11
- };
12
-
13
- function extractNumericPropKeys(features) {
14
- const numericPropKeys = {};
15
-
16
- for (const feature of features) {
17
- if (feature.properties) {
18
- for (const key in feature.properties) {
19
- const numericSoFar = numericPropKeys[key];
20
-
21
- if (numericSoFar || numericSoFar === undefined) {
22
- const val = feature.properties[key];
23
- numericPropKeys[key] = isNumeric(val);
24
- }
25
- }
26
- }
27
- }
28
-
29
- return Object.keys(numericPropKeys).filter(k => numericPropKeys[k]);
30
- }
31
-
32
- function fillArrays(features, firstPassData, options) {
33
- const {
34
- pointPositionsCount,
35
- pointFeaturesCount,
36
- linePositionsCount,
37
- linePathsCount,
38
- lineFeaturesCount,
39
- polygonPositionsCount,
40
- polygonObjectsCount,
41
- polygonRingsCount,
42
- polygonFeaturesCount
43
- } = firstPassData;
44
- const {
45
- numericPropKeys,
46
- PositionDataType = Float32Array
47
- } = options;
48
- const hasGlobalId = features[0] && 'id' in features[0];
49
- const coordLength = 2;
50
- const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
51
- const points = {
52
- positions: new PositionDataType(pointPositionsCount * coordLength),
53
- globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
54
- featureIds: pointFeaturesCount > 65535 ? new Uint32Array(pointPositionsCount) : new Uint16Array(pointPositionsCount),
55
- numericProps: {},
56
- properties: [],
57
- fields: []
58
- };
59
- const lines = {
60
- pathIndices: linePositionsCount > 65535 ? new Uint32Array(linePathsCount + 1) : new Uint16Array(linePathsCount + 1),
61
- positions: new PositionDataType(linePositionsCount * coordLength),
62
- globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
63
- featureIds: lineFeaturesCount > 65535 ? new Uint32Array(linePositionsCount) : new Uint16Array(linePositionsCount),
64
- numericProps: {},
65
- properties: [],
66
- fields: []
67
- };
68
- const polygons = {
69
- polygonIndices: polygonPositionsCount > 65535 ? new Uint32Array(polygonObjectsCount + 1) : new Uint16Array(polygonObjectsCount + 1),
70
- primitivePolygonIndices: polygonPositionsCount > 65535 ? new Uint32Array(polygonRingsCount + 1) : new Uint16Array(polygonRingsCount + 1),
71
- positions: new PositionDataType(polygonPositionsCount * coordLength),
72
- triangles: [],
73
- globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
74
- featureIds: polygonFeaturesCount > 65535 ? new Uint32Array(polygonPositionsCount) : new Uint16Array(polygonPositionsCount),
75
- numericProps: {},
76
- properties: [],
77
- fields: []
78
- };
79
-
80
- for (const object of [points, lines, polygons]) {
81
- for (const propName of numericPropKeys) {
82
- object.numericProps[propName] = new Float32Array(object.positions.length / coordLength);
83
- }
84
- }
85
-
86
- lines.pathIndices[linePathsCount] = linePositionsCount;
87
- polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
88
- polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
89
- const indexMap = {
90
- pointPosition: 0,
91
- pointFeature: 0,
92
- linePosition: 0,
93
- linePath: 0,
94
- lineFeature: 0,
95
- polygonPosition: 0,
96
- polygonObject: 0,
97
- polygonRing: 0,
98
- polygonFeature: 0,
99
- feature: 0
100
- };
101
-
102
- for (const feature of features) {
103
- const geometry = feature.geometry;
104
- const properties = feature.properties || {};
105
-
106
- switch (geometry.type) {
107
- case 'Point':
108
- case 'MultiPoint':
109
- handlePoint(geometry, points, indexMap, coordLength, properties);
110
- points.properties.push(keepStringProperties(properties, numericPropKeys));
111
-
112
- if (hasGlobalId) {
113
- points.fields.push({
114
- id: feature.id
115
- });
116
- }
117
-
118
- indexMap.pointFeature++;
119
- break;
120
-
121
- case 'LineString':
122
- case 'MultiLineString':
123
- handleLineString(geometry, lines, indexMap, coordLength, properties);
124
- lines.properties.push(keepStringProperties(properties, numericPropKeys));
125
-
126
- if (hasGlobalId) {
127
- lines.fields.push({
128
- id: feature.id
129
- });
130
- }
131
-
132
- indexMap.lineFeature++;
133
- break;
134
-
135
- case 'Polygon':
136
- case 'MultiPolygon':
137
- handlePolygon(geometry, polygons, indexMap, coordLength, properties);
138
- polygons.properties.push(keepStringProperties(properties, numericPropKeys));
139
-
140
- if (hasGlobalId) {
141
- polygons.fields.push({
142
- id: feature.id
143
- });
144
- }
145
-
146
- indexMap.polygonFeature++;
147
- break;
148
-
149
- default:
150
- throw new Error('Invalid geometry type');
151
- }
152
-
153
- indexMap.feature++;
154
- }
155
-
156
- return makeAccessorObjects(points, lines, polygons, coordLength);
157
- }
158
-
159
- function handlePoint(geometry, points, indexMap, coordLength, properties) {
160
- points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
161
- const nPositions = geometry.data.length / coordLength;
162
- fillNumericProperties(points, properties, indexMap.pointPosition, nPositions);
163
- points.globalFeatureIds.fill(indexMap.feature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
164
- points.featureIds.fill(indexMap.pointFeature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
165
- indexMap.pointPosition += nPositions;
166
- }
167
-
168
- function handleLineString(geometry, lines, indexMap, coordLength, properties) {
169
- lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
170
- const nPositions = geometry.data.length / coordLength;
171
- fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
172
- lines.globalFeatureIds.fill(indexMap.feature, indexMap.linePosition, indexMap.linePosition + nPositions);
173
- lines.featureIds.fill(indexMap.lineFeature, indexMap.linePosition, indexMap.linePosition + nPositions);
174
-
175
- for (let i = 0, il = geometry.lines.length; i < il; ++i) {
176
- const start = geometry.lines[i];
177
- const end = i === il - 1 ? geometry.data.length : geometry.lines[i + 1];
178
- lines.pathIndices[indexMap.linePath++] = indexMap.linePosition;
179
- indexMap.linePosition += (end - start) / coordLength;
180
- }
181
- }
182
-
183
- function handlePolygon(geometry, polygons, indexMap, coordLength, properties) {
184
- polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
185
- const nPositions = geometry.data.length / coordLength;
186
- fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
187
- polygons.globalFeatureIds.fill(indexMap.feature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
188
- polygons.featureIds.fill(indexMap.polygonFeature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
189
-
190
- for (let l = 0, ll = geometry.lines.length; l < ll; ++l) {
191
- const startPosition = indexMap.polygonPosition;
192
- polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
193
- const areas = geometry.areas[l];
194
- const lines = geometry.lines[l];
195
- const nextLines = geometry.lines[l + 1];
196
-
197
- for (let i = 0, il = lines.length; i < il; ++i) {
198
- const start = lines[i];
199
- const end = i === il - 1 ? nextLines === undefined ? geometry.data.length : nextLines[0] : lines[i + 1];
200
- polygons.primitivePolygonIndices[indexMap.polygonRing++] = indexMap.polygonPosition;
201
- indexMap.polygonPosition += (end - start) / coordLength;
202
- }
203
-
204
- const endPosition = indexMap.polygonPosition;
205
- triangulatePolygon(polygons, areas, lines, {
206
- startPosition,
207
- endPosition,
208
- coordLength
209
- });
210
- }
211
- }
212
-
213
- function triangulatePolygon(polygons, areas, lines, {
214
- startPosition,
215
- endPosition,
216
- coordLength
217
- }) {
218
- const start = startPosition * coordLength;
219
- const end = endPosition * coordLength;
220
- const polygonPositions = polygons.positions.subarray(start, end);
221
- const offset = lines[0];
222
- const holes = lines.slice(1).map(n => (n - offset) / coordLength);
223
- const indices = earcut(polygonPositions, holes, coordLength, areas);
224
-
225
- for (let t = 0, tl = indices.length; t < tl; ++t) {
226
- polygons.triangles.push(startPosition + indices[t]);
227
- }
228
- }
229
-
230
- function makeAccessorObjects(points, lines, polygons, coordLength) {
231
- const returnObj = {
232
- points: { ...points,
233
- positions: {
234
- value: points.positions,
235
- size: coordLength
236
- },
237
- globalFeatureIds: {
238
- value: points.globalFeatureIds,
239
- size: 1
240
- },
241
- featureIds: {
242
- value: points.featureIds,
243
- size: 1
244
- }
245
- },
246
- lines: { ...lines,
247
- pathIndices: {
248
- value: lines.pathIndices,
249
- size: 1
250
- },
251
- positions: {
252
- value: lines.positions,
253
- size: coordLength
254
- },
255
- globalFeatureIds: {
256
- value: lines.globalFeatureIds,
257
- size: 1
258
- },
259
- featureIds: {
260
- value: lines.featureIds,
261
- size: 1
262
- }
263
- },
264
- polygons: { ...polygons,
265
- polygonIndices: {
266
- value: polygons.polygonIndices,
267
- size: 1
268
- },
269
- primitivePolygonIndices: {
270
- value: polygons.primitivePolygonIndices,
271
- size: 1
272
- },
273
- positions: {
274
- value: polygons.positions,
275
- size: coordLength
276
- },
277
- triangles: {
278
- value: new Uint32Array(polygons.triangles),
279
- size: 1
280
- },
281
- globalFeatureIds: {
282
- value: polygons.globalFeatureIds,
283
- size: 1
284
- },
285
- featureIds: {
286
- value: polygons.featureIds,
287
- size: 1
288
- }
289
- }
290
- };
291
-
292
- for (const geomType in returnObj) {
293
- for (const numericProp in returnObj[geomType].numericProps) {
294
- returnObj[geomType].numericProps[numericProp] = {
295
- value: returnObj[geomType].numericProps[numericProp],
296
- size: 1
297
- };
298
- }
299
- }
300
-
301
- return returnObj;
302
- }
303
-
304
- function fillNumericProperties(object, properties, index, length) {
305
- for (const numericPropName in object.numericProps) {
306
- if (numericPropName in properties) {
307
- object.numericProps[numericPropName].fill(properties[numericPropName], index, index + length);
308
- }
309
- }
310
- }
311
-
312
- function keepStringProperties(properties, numericKeys) {
313
- const props = {};
314
-
315
- for (const key in properties) {
316
- if (!numericKeys.includes(key)) {
317
- props[key] = properties[key];
318
- }
319
- }
320
-
321
- return props;
322
- }
323
-
324
- function isNumeric(x) {
325
- return Number.isFinite(x);
326
- }
327
- //# sourceMappingURL=features-to-binary.js.map