@loaders.gl/gis 3.1.0-alpha.2 → 3.1.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.
Files changed (42) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +4 -6
  4. package/dist/es5/bundle.js +7 -0
  5. package/dist/es5/bundle.js.map +1 -0
  6. package/dist/es5/index.js +48 -0
  7. package/dist/es5/index.js.map +1 -0
  8. package/dist/es5/lib/binary-to-geojson.js +284 -0
  9. package/dist/es5/lib/binary-to-geojson.js.map +1 -0
  10. package/dist/es5/lib/geojson-to-binary.js +417 -0
  11. package/dist/es5/lib/geojson-to-binary.js.map +1 -0
  12. package/dist/es5/lib/transform.js +58 -0
  13. package/dist/es5/lib/transform.js.map +1 -0
  14. package/dist/esm/bundle.js +5 -0
  15. package/dist/esm/bundle.js.map +1 -0
  16. package/dist/esm/index.js +4 -0
  17. package/dist/esm/index.js.map +1 -0
  18. package/dist/esm/lib/binary-to-geojson.js +274 -0
  19. package/dist/esm/lib/binary-to-geojson.js.map +1 -0
  20. package/dist/esm/lib/geojson-to-binary.js +407 -0
  21. package/dist/esm/lib/geojson-to-binary.js.map +1 -0
  22. package/dist/esm/lib/transform.js +50 -0
  23. package/dist/esm/lib/transform.js.map +1 -0
  24. package/dist/index.d.ts +4 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +14 -4
  27. package/dist/lib/binary-to-geojson.d.ts +21 -0
  28. package/dist/lib/binary-to-geojson.d.ts.map +1 -0
  29. package/dist/lib/binary-to-geojson.js +201 -242
  30. package/dist/lib/geojson-to-binary.d.ts +39 -0
  31. package/dist/lib/geojson-to-binary.d.ts.map +1 -0
  32. package/dist/lib/geojson-to-binary.js +324 -369
  33. package/dist/lib/transform.d.ts +19 -0
  34. package/dist/lib/transform.d.ts.map +1 -0
  35. package/dist/lib/transform.js +51 -42
  36. package/package.json +7 -7
  37. package/src/bundle.ts +2 -3
  38. package/dist/bundle.js.map +0 -1
  39. package/dist/index.js.map +0 -1
  40. package/dist/lib/binary-to-geojson.js.map +0 -1
  41. package/dist/lib/geojson-to-binary.js.map +0 -1
  42. package/dist/lib/transform.js.map +0 -1
@@ -1,407 +1,362 @@
1
- export function geojsonToBinary(features, options = {}) {
2
- const firstPassData = firstPass(features);
3
- return secondPass(features, firstPassData, {
4
- coordLength: options.coordLength || firstPassData.coordLength,
5
- numericPropKeys: options.numericPropKeys || firstPassData.numericPropKeys,
6
- PositionDataType: options.PositionDataType || Float32Array
7
- });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TEST_EXPORTS = exports.geojsonToBinary = void 0;
4
+ /** Convert GeoJSON features to flat binary arrays */
5
+ function geojsonToBinary(features, options = {}) {
6
+ const firstPassData = firstPass(features);
7
+ return secondPass(features, firstPassData, {
8
+ coordLength: options.coordLength || firstPassData.coordLength,
9
+ numericPropKeys: options.numericPropKeys || firstPassData.numericPropKeys,
10
+ PositionDataType: options.PositionDataType || Float32Array
11
+ });
8
12
  }
9
- export const TEST_EXPORTS = {
10
- firstPass,
11
- secondPass
13
+ exports.geojsonToBinary = geojsonToBinary;
14
+ exports.TEST_EXPORTS = {
15
+ firstPass,
16
+ secondPass
12
17
  };
13
-
18
+ /**
19
+ * Initial scan over GeoJSON features
20
+ * Counts number of coordinates of each geometry type and
21
+ * keeps track of the max coordinate dimensions
22
+ */
23
+ // eslint-disable-next-line complexity, max-statements
14
24
  function firstPass(features) {
15
- let pointPositionsCount = 0;
16
- let pointFeaturesCount = 0;
17
- let linePositionsCount = 0;
18
- let linePathsCount = 0;
19
- let lineFeaturesCount = 0;
20
- let polygonPositionsCount = 0;
21
- let polygonObjectsCount = 0;
22
- let polygonRingsCount = 0;
23
- let polygonFeaturesCount = 0;
24
- const coordLengths = new Set();
25
- const numericPropKeys = {};
26
-
27
- for (const feature of features) {
28
- const geometry = feature.geometry;
29
-
30
- switch (geometry.type) {
31
- case 'Point':
32
- pointFeaturesCount++;
33
- pointPositionsCount++;
34
- coordLengths.add(geometry.coordinates.length);
35
- break;
36
-
37
- case 'MultiPoint':
38
- pointFeaturesCount++;
39
- pointPositionsCount += geometry.coordinates.length;
40
-
41
- for (const point of geometry.coordinates) {
42
- coordLengths.add(point.length);
25
+ // Counts the number of _positions_, so [x, y, z] counts as one
26
+ let pointPositionsCount = 0;
27
+ let pointFeaturesCount = 0;
28
+ let linePositionsCount = 0;
29
+ let linePathsCount = 0;
30
+ let lineFeaturesCount = 0;
31
+ let polygonPositionsCount = 0;
32
+ let polygonObjectsCount = 0;
33
+ let polygonRingsCount = 0;
34
+ let polygonFeaturesCount = 0;
35
+ const coordLengths = new Set();
36
+ const numericPropKeys = {};
37
+ for (const feature of features) {
38
+ const geometry = feature.geometry;
39
+ switch (geometry.type) {
40
+ case 'Point':
41
+ pointFeaturesCount++;
42
+ pointPositionsCount++;
43
+ coordLengths.add(geometry.coordinates.length);
44
+ break;
45
+ case 'MultiPoint':
46
+ pointFeaturesCount++;
47
+ pointPositionsCount += geometry.coordinates.length;
48
+ for (const point of geometry.coordinates) {
49
+ coordLengths.add(point.length);
50
+ }
51
+ break;
52
+ case 'LineString':
53
+ lineFeaturesCount++;
54
+ linePositionsCount += geometry.coordinates.length;
55
+ linePathsCount++;
56
+ for (const coord of geometry.coordinates) {
57
+ coordLengths.add(coord.length);
58
+ }
59
+ break;
60
+ case 'MultiLineString':
61
+ lineFeaturesCount++;
62
+ for (const line of geometry.coordinates) {
63
+ linePositionsCount += line.length;
64
+ linePathsCount++;
65
+ // eslint-disable-next-line max-depth
66
+ for (const coord of line) {
67
+ coordLengths.add(coord.length);
68
+ }
69
+ }
70
+ break;
71
+ case 'Polygon':
72
+ polygonFeaturesCount++;
73
+ polygonObjectsCount++;
74
+ polygonRingsCount += geometry.coordinates.length;
75
+ polygonPositionsCount += flatten(geometry.coordinates).length;
76
+ for (const coord of flatten(geometry.coordinates)) {
77
+ coordLengths.add(coord.length);
78
+ }
79
+ break;
80
+ case 'MultiPolygon':
81
+ polygonFeaturesCount++;
82
+ for (const polygon of geometry.coordinates) {
83
+ polygonObjectsCount++;
84
+ polygonRingsCount += polygon.length;
85
+ polygonPositionsCount += flatten(polygon).length;
86
+ // eslint-disable-next-line max-depth
87
+ for (const coord of flatten(polygon)) {
88
+ coordLengths.add(coord.length);
89
+ }
90
+ }
91
+ break;
92
+ default:
93
+ throw new Error(`Unsupported geometry type: ${geometry.type}`);
43
94
  }
44
-
45
- break;
46
-
47
- case 'LineString':
48
- lineFeaturesCount++;
49
- linePositionsCount += geometry.coordinates.length;
50
- linePathsCount++;
51
-
52
- for (const coord of geometry.coordinates) {
53
- coordLengths.add(coord.length);
95
+ if (feature.properties) {
96
+ for (const key in feature.properties) {
97
+ const val = feature.properties[key];
98
+ // If property has not been seen before, or if property has been numeric
99
+ // in all previous features, check if numeric in this feature
100
+ // If not numeric, false is stored to prevent rechecking in the future
101
+ numericPropKeys[key] =
102
+ numericPropKeys[key] || numericPropKeys[key] === undefined
103
+ ? isNumeric(val)
104
+ : numericPropKeys[key];
105
+ }
54
106
  }
55
-
56
- break;
57
-
58
- case 'MultiLineString':
59
- lineFeaturesCount++;
60
-
61
- for (const line of geometry.coordinates) {
62
- linePositionsCount += line.length;
63
- linePathsCount++;
64
-
65
- for (const coord of line) {
66
- coordLengths.add(coord.length);
67
- }
68
- }
69
-
70
- break;
71
-
72
- case 'Polygon':
73
- polygonFeaturesCount++;
74
- polygonObjectsCount++;
75
- polygonRingsCount += geometry.coordinates.length;
76
- polygonPositionsCount += flatten(geometry.coordinates).length;
77
-
78
- for (const coord of flatten(geometry.coordinates)) {
79
- coordLengths.add(coord.length);
80
- }
81
-
82
- break;
83
-
84
- case 'MultiPolygon':
85
- polygonFeaturesCount++;
86
-
87
- for (const polygon of geometry.coordinates) {
88
- polygonObjectsCount++;
89
- polygonRingsCount += polygon.length;
90
- polygonPositionsCount += flatten(polygon).length;
91
-
92
- for (const coord of flatten(polygon)) {
93
- coordLengths.add(coord.length);
94
- }
95
- }
96
-
97
- break;
98
-
99
- default:
100
- throw new Error(`Unsupported geometry type: ${geometry.type}`);
101
- }
102
-
103
- if (feature.properties) {
104
- for (const key in feature.properties) {
105
- const val = feature.properties[key];
106
- numericPropKeys[key] = numericPropKeys[key] || numericPropKeys[key] === undefined ? isNumeric(val) : numericPropKeys[key];
107
- }
108
107
  }
109
- }
110
-
111
- return {
112
- coordLength: coordLengths.size > 0 ? Math.max(...coordLengths) : 2,
113
- pointPositionsCount,
114
- pointFeaturesCount,
115
- linePositionsCount,
116
- linePathsCount,
117
- lineFeaturesCount,
118
- polygonPositionsCount,
119
- polygonObjectsCount,
120
- polygonRingsCount,
121
- polygonFeaturesCount,
122
- numericPropKeys: Object.keys(numericPropKeys).filter(k => numericPropKeys[k])
123
- };
108
+ return {
109
+ coordLength: coordLengths.size > 0 ? Math.max(...coordLengths) : 2,
110
+ pointPositionsCount,
111
+ pointFeaturesCount,
112
+ linePositionsCount,
113
+ linePathsCount,
114
+ lineFeaturesCount,
115
+ polygonPositionsCount,
116
+ polygonObjectsCount,
117
+ polygonRingsCount,
118
+ polygonFeaturesCount,
119
+ // Array of keys whose values are always numeric
120
+ numericPropKeys: Object.keys(numericPropKeys).filter((k) => numericPropKeys[k])
121
+ };
124
122
  }
125
-
123
+ /**
124
+ * Second scan over GeoJSON features
125
+ * Fills coordinates into pre-allocated typed arrays
126
+ */
127
+ // eslint-disable-next-line complexity
126
128
  function secondPass(features, firstPassData, options) {
127
- const {
128
- pointPositionsCount,
129
- pointFeaturesCount,
130
- linePositionsCount,
131
- linePathsCount,
132
- lineFeaturesCount,
133
- polygonPositionsCount,
134
- polygonObjectsCount,
135
- polygonRingsCount,
136
- polygonFeaturesCount
137
- } = firstPassData;
138
- const {
139
- coordLength,
140
- numericPropKeys,
141
- PositionDataType = Float32Array
142
- } = options;
143
- const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
144
- const points = {
145
- positions: new PositionDataType(pointPositionsCount * coordLength),
146
- globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
147
- featureIds: pointFeaturesCount > 65535 ? new Uint32Array(pointPositionsCount) : new Uint16Array(pointPositionsCount),
148
- numericProps: {},
149
- properties: Array(),
150
- fields: Array()
151
- };
152
- const lines = {
153
- positions: new PositionDataType(linePositionsCount * coordLength),
154
- pathIndices: linePositionsCount > 65535 ? new Uint32Array(linePathsCount + 1) : new Uint16Array(linePathsCount + 1),
155
- globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
156
- featureIds: lineFeaturesCount > 65535 ? new Uint32Array(linePositionsCount) : new Uint16Array(linePositionsCount),
157
- numericProps: {},
158
- properties: Array(),
159
- fields: Array()
160
- };
161
- const polygons = {
162
- positions: new PositionDataType(polygonPositionsCount * coordLength),
163
- polygonIndices: polygonPositionsCount > 65535 ? new Uint32Array(polygonObjectsCount + 1) : new Uint16Array(polygonObjectsCount + 1),
164
- primitivePolygonIndices: polygonPositionsCount > 65535 ? new Uint32Array(polygonRingsCount + 1) : new Uint16Array(polygonRingsCount + 1),
165
- globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
166
- featureIds: polygonFeaturesCount > 65535 ? new Uint32Array(polygonPositionsCount) : new Uint16Array(polygonPositionsCount),
167
- numericProps: {},
168
- properties: Array(),
169
- fields: Array()
170
- };
171
-
172
- for (const object of [points, lines, polygons]) {
173
- for (const propName of numericPropKeys || []) {
174
- object.numericProps[propName] = new Float32Array(object.positions.length / coordLength);
129
+ const { pointPositionsCount, pointFeaturesCount, linePositionsCount, linePathsCount, lineFeaturesCount, polygonPositionsCount, polygonObjectsCount, polygonRingsCount, polygonFeaturesCount } = firstPassData;
130
+ const { coordLength, numericPropKeys, PositionDataType = Float32Array } = options;
131
+ const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
132
+ const points = {
133
+ // @ts-ignore Typescript doesn't like dynamic constructors
134
+ positions: new PositionDataType(pointPositionsCount * coordLength),
135
+ globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
136
+ featureIds: pointFeaturesCount > 65535
137
+ ? new Uint32Array(pointPositionsCount)
138
+ : new Uint16Array(pointPositionsCount),
139
+ numericProps: {},
140
+ properties: Array(),
141
+ fields: Array()
142
+ };
143
+ const lines = {
144
+ // @ts-ignore Typescript doesn't like dynamic constructors
145
+ positions: new PositionDataType(linePositionsCount * coordLength),
146
+ pathIndices: linePositionsCount > 65535
147
+ ? new Uint32Array(linePathsCount + 1)
148
+ : new Uint16Array(linePathsCount + 1),
149
+ globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
150
+ featureIds: lineFeaturesCount > 65535
151
+ ? new Uint32Array(linePositionsCount)
152
+ : new Uint16Array(linePositionsCount),
153
+ numericProps: {},
154
+ properties: Array(),
155
+ fields: Array()
156
+ };
157
+ const polygons = {
158
+ // @ts-ignore Typescript doesn't like dynamic constructors
159
+ positions: new PositionDataType(polygonPositionsCount * coordLength),
160
+ polygonIndices: polygonPositionsCount > 65535
161
+ ? new Uint32Array(polygonObjectsCount + 1)
162
+ : new Uint16Array(polygonObjectsCount + 1),
163
+ primitivePolygonIndices: polygonPositionsCount > 65535
164
+ ? new Uint32Array(polygonRingsCount + 1)
165
+ : new Uint16Array(polygonRingsCount + 1),
166
+ globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
167
+ featureIds: polygonFeaturesCount > 65535
168
+ ? new Uint32Array(polygonPositionsCount)
169
+ : new Uint16Array(polygonPositionsCount),
170
+ numericProps: {},
171
+ properties: Array(),
172
+ fields: Array()
173
+ };
174
+ // Instantiate numeric properties arrays; one value per vertex
175
+ for (const object of [points, lines, polygons]) {
176
+ for (const propName of numericPropKeys || []) {
177
+ // If property has been numeric in all previous features in which the property existed, check
178
+ // if numeric in this feature
179
+ object.numericProps[propName] = new Float32Array(object.positions.length / coordLength);
180
+ }
175
181
  }
176
- }
177
-
178
- lines.pathIndices[linePathsCount] = linePositionsCount;
179
- polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
180
- polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
181
- const indexMap = {
182
- pointPosition: 0,
183
- pointFeature: 0,
184
- linePosition: 0,
185
- linePath: 0,
186
- lineFeature: 0,
187
- polygonPosition: 0,
188
- polygonObject: 0,
189
- polygonRing: 0,
190
- polygonFeature: 0,
191
- feature: 0
192
- };
193
-
194
- for (const feature of features) {
195
- const geometry = feature.geometry;
196
- const properties = feature.properties || {};
197
-
198
- switch (geometry.type) {
199
- case 'Point':
200
- handlePoint(geometry.coordinates, points, indexMap, coordLength, properties);
201
- points.properties.push(keepStringProperties(properties, numericPropKeys));
202
- indexMap.pointFeature++;
203
- break;
204
-
205
- case 'MultiPoint':
206
- handleMultiPoint(geometry.coordinates, points, indexMap, coordLength, properties);
207
- points.properties.push(keepStringProperties(properties, numericPropKeys));
208
- indexMap.pointFeature++;
209
- break;
210
-
211
- case 'LineString':
212
- handleLineString(geometry.coordinates, lines, indexMap, coordLength, properties);
213
- lines.properties.push(keepStringProperties(properties, numericPropKeys));
214
- indexMap.lineFeature++;
215
- break;
216
-
217
- case 'MultiLineString':
218
- handleMultiLineString(geometry.coordinates, lines, indexMap, coordLength, properties);
219
- lines.properties.push(keepStringProperties(properties, numericPropKeys));
220
- indexMap.lineFeature++;
221
- break;
222
-
223
- case 'Polygon':
224
- handlePolygon(geometry.coordinates, polygons, indexMap, coordLength, properties);
225
- polygons.properties.push(keepStringProperties(properties, numericPropKeys));
226
- indexMap.polygonFeature++;
227
- break;
228
-
229
- case 'MultiPolygon':
230
- handleMultiPolygon(geometry.coordinates, polygons, indexMap, coordLength, properties);
231
- polygons.properties.push(keepStringProperties(properties, numericPropKeys));
232
- indexMap.polygonFeature++;
233
- break;
234
-
235
- default:
236
- throw new Error('Invalid geometry type');
182
+ // Set last element of path/polygon indices as positions length
183
+ lines.pathIndices[linePathsCount] = linePositionsCount;
184
+ polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
185
+ polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
186
+ const indexMap = {
187
+ pointPosition: 0,
188
+ pointFeature: 0,
189
+ linePosition: 0,
190
+ linePath: 0,
191
+ lineFeature: 0,
192
+ polygonPosition: 0,
193
+ polygonObject: 0,
194
+ polygonRing: 0,
195
+ polygonFeature: 0,
196
+ feature: 0
197
+ };
198
+ for (const feature of features) {
199
+ const geometry = feature.geometry;
200
+ const properties = feature.properties || {};
201
+ switch (geometry.type) {
202
+ case 'Point':
203
+ handlePoint(geometry.coordinates, points, indexMap, coordLength, properties);
204
+ points.properties.push(keepStringProperties(properties, numericPropKeys));
205
+ indexMap.pointFeature++;
206
+ break;
207
+ case 'MultiPoint':
208
+ handleMultiPoint(geometry.coordinates, points, indexMap, coordLength, properties);
209
+ points.properties.push(keepStringProperties(properties, numericPropKeys));
210
+ indexMap.pointFeature++;
211
+ break;
212
+ case 'LineString':
213
+ handleLineString(geometry.coordinates, lines, indexMap, coordLength, properties);
214
+ lines.properties.push(keepStringProperties(properties, numericPropKeys));
215
+ indexMap.lineFeature++;
216
+ break;
217
+ case 'MultiLineString':
218
+ handleMultiLineString(geometry.coordinates, lines, indexMap, coordLength, properties);
219
+ lines.properties.push(keepStringProperties(properties, numericPropKeys));
220
+ indexMap.lineFeature++;
221
+ break;
222
+ case 'Polygon':
223
+ handlePolygon(geometry.coordinates, polygons, indexMap, coordLength, properties);
224
+ polygons.properties.push(keepStringProperties(properties, numericPropKeys));
225
+ indexMap.polygonFeature++;
226
+ break;
227
+ case 'MultiPolygon':
228
+ handleMultiPolygon(geometry.coordinates, polygons, indexMap, coordLength, properties);
229
+ polygons.properties.push(keepStringProperties(properties, numericPropKeys));
230
+ indexMap.polygonFeature++;
231
+ break;
232
+ default:
233
+ throw new Error('Invalid geometry type');
234
+ }
235
+ indexMap.feature++;
237
236
  }
238
-
239
- indexMap.feature++;
240
- }
241
-
242
- return makeAccessorObjects(points, lines, polygons, coordLength);
237
+ // Wrap each array in an accessor object with value and size keys
238
+ return makeAccessorObjects(points, lines, polygons, coordLength);
243
239
  }
244
-
240
+ /** Fills Point coordinates into points object of arrays */
245
241
  function handlePoint(coords, points, indexMap, coordLength, properties) {
246
- points.positions.set(coords, indexMap.pointPosition * coordLength);
247
- points.globalFeatureIds[indexMap.pointPosition] = indexMap.feature;
248
- points.featureIds[indexMap.pointPosition] = indexMap.pointFeature;
249
- fillNumericProperties(points, properties, indexMap.pointPosition, 1);
250
- indexMap.pointPosition++;
242
+ points.positions.set(coords, indexMap.pointPosition * coordLength);
243
+ points.globalFeatureIds[indexMap.pointPosition] = indexMap.feature;
244
+ points.featureIds[indexMap.pointPosition] = indexMap.pointFeature;
245
+ fillNumericProperties(points, properties, indexMap.pointPosition, 1);
246
+ indexMap.pointPosition++;
251
247
  }
252
-
248
+ /** Fills MultiPoint coordinates into points object of arrays */
253
249
  function handleMultiPoint(coords, points, indexMap, coordLength, properties) {
254
- for (const point of coords) {
255
- handlePoint(point, points, indexMap, coordLength, properties);
256
- }
250
+ for (const point of coords) {
251
+ handlePoint(point, points, indexMap, coordLength, properties);
252
+ }
257
253
  }
258
-
254
+ /** Fills LineString coordinates into lines object of arrays */
259
255
  function handleLineString(coords, lines, indexMap, coordLength, properties) {
260
- lines.pathIndices[indexMap.linePath] = indexMap.linePosition;
261
- indexMap.linePath++;
262
- fillCoords(lines.positions, coords, indexMap.linePosition, coordLength);
263
- const nPositions = coords.length;
264
- fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
265
- lines.globalFeatureIds.set(new Uint32Array(nPositions).fill(indexMap.feature), indexMap.linePosition);
266
- lines.featureIds.set(new Uint32Array(nPositions).fill(indexMap.lineFeature), indexMap.linePosition);
267
- indexMap.linePosition += nPositions;
256
+ lines.pathIndices[indexMap.linePath] = indexMap.linePosition;
257
+ indexMap.linePath++;
258
+ fillCoords(lines.positions, coords, indexMap.linePosition, coordLength);
259
+ const nPositions = coords.length;
260
+ fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
261
+ lines.globalFeatureIds.set(new Uint32Array(nPositions).fill(indexMap.feature), indexMap.linePosition);
262
+ lines.featureIds.set(new Uint32Array(nPositions).fill(indexMap.lineFeature), indexMap.linePosition);
263
+ indexMap.linePosition += nPositions;
268
264
  }
269
-
265
+ /** Fills MultiLineString coordinates into lines object of arrays */
270
266
  function handleMultiLineString(coords, lines, indexMap, coordLength, properties) {
271
- for (const line of coords) {
272
- handleLineString(line, lines, indexMap, coordLength, properties);
273
- }
267
+ for (const line of coords) {
268
+ handleLineString(line, lines, indexMap, coordLength, properties);
269
+ }
274
270
  }
275
-
271
+ /** Fills Polygon coordinates into polygons object of arrays */
276
272
  function handlePolygon(coords, polygons, indexMap, coordLength, properties) {
277
- polygons.polygonIndices[indexMap.polygonObject] = indexMap.polygonPosition;
278
- indexMap.polygonObject++;
279
-
280
- for (const ring of coords) {
281
- polygons.primitivePolygonIndices[indexMap.polygonRing] = indexMap.polygonPosition;
282
- indexMap.polygonRing++;
283
- fillCoords(polygons.positions, ring, indexMap.polygonPosition, coordLength);
284
- const nPositions = ring.length;
285
- fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
286
- polygons.globalFeatureIds.set(new Uint32Array(nPositions).fill(indexMap.feature), indexMap.polygonPosition);
287
- polygons.featureIds.set(new Uint32Array(nPositions).fill(indexMap.polygonFeature), indexMap.polygonPosition);
288
- indexMap.polygonPosition += nPositions;
289
- }
273
+ polygons.polygonIndices[indexMap.polygonObject] = indexMap.polygonPosition;
274
+ indexMap.polygonObject++;
275
+ for (const ring of coords) {
276
+ polygons.primitivePolygonIndices[indexMap.polygonRing] = indexMap.polygonPosition;
277
+ indexMap.polygonRing++;
278
+ fillCoords(polygons.positions, ring, indexMap.polygonPosition, coordLength);
279
+ const nPositions = ring.length;
280
+ fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
281
+ polygons.globalFeatureIds.set(new Uint32Array(nPositions).fill(indexMap.feature), indexMap.polygonPosition);
282
+ polygons.featureIds.set(new Uint32Array(nPositions).fill(indexMap.polygonFeature), indexMap.polygonPosition);
283
+ indexMap.polygonPosition += nPositions;
284
+ }
290
285
  }
291
-
286
+ /** Fills MultiPolygon coordinates into polygons object of arrays */
292
287
  function handleMultiPolygon(coords, polygons, indexMap, coordLength, properties) {
293
- for (const polygon of coords) {
294
- handlePolygon(polygon, polygons, indexMap, coordLength, properties);
295
- }
288
+ for (const polygon of coords) {
289
+ handlePolygon(polygon, polygons, indexMap, coordLength, properties);
290
+ }
296
291
  }
297
-
292
+ /** Wrap each array in an accessor object with value and size keys */
298
293
  function makeAccessorObjects(points, lines, polygons, coordLength) {
299
- const returnObj = {
300
- points: { ...points,
301
- positions: {
302
- value: points.positions,
303
- size: coordLength
304
- },
305
- globalFeatureIds: {
306
- value: points.globalFeatureIds,
307
- size: 1
308
- },
309
- featureIds: {
310
- value: points.featureIds,
311
- size: 1
312
- },
313
- type: 'Point'
314
- },
315
- lines: { ...lines,
316
- pathIndices: {
317
- value: lines.pathIndices,
318
- size: 1
319
- },
320
- positions: {
321
- value: lines.positions,
322
- size: coordLength
323
- },
324
- globalFeatureIds: {
325
- value: lines.globalFeatureIds,
326
- size: 1
327
- },
328
- featureIds: {
329
- value: lines.featureIds,
330
- size: 1
331
- },
332
- type: 'LineString'
333
- },
334
- polygons: { ...polygons,
335
- polygonIndices: {
336
- value: polygons.polygonIndices,
337
- size: 1
338
- },
339
- primitivePolygonIndices: {
340
- value: polygons.primitivePolygonIndices,
341
- size: 1
342
- },
343
- positions: {
344
- value: polygons.positions,
345
- size: coordLength
346
- },
347
- globalFeatureIds: {
348
- value: polygons.globalFeatureIds,
349
- size: 1
350
- },
351
- featureIds: {
352
- value: polygons.featureIds,
353
- size: 1
354
- },
355
- type: 'Polygon'
356
- }
357
- };
358
-
359
- for (const geomType in returnObj) {
360
- for (const numericProp in returnObj[geomType].numericProps) {
361
- returnObj[geomType].numericProps[numericProp] = {
362
- value: returnObj[geomType].numericProps[numericProp],
363
- size: 1
364
- };
294
+ const returnObj = {
295
+ points: {
296
+ ...points,
297
+ positions: { value: points.positions, size: coordLength },
298
+ globalFeatureIds: { value: points.globalFeatureIds, size: 1 },
299
+ featureIds: { value: points.featureIds, size: 1 },
300
+ type: 'Point'
301
+ },
302
+ lines: {
303
+ ...lines,
304
+ pathIndices: { value: lines.pathIndices, size: 1 },
305
+ positions: { value: lines.positions, size: coordLength },
306
+ globalFeatureIds: { value: lines.globalFeatureIds, size: 1 },
307
+ featureIds: { value: lines.featureIds, size: 1 },
308
+ type: 'LineString'
309
+ },
310
+ polygons: {
311
+ ...polygons,
312
+ polygonIndices: { value: polygons.polygonIndices, size: 1 },
313
+ primitivePolygonIndices: { value: polygons.primitivePolygonIndices, size: 1 },
314
+ positions: { value: polygons.positions, size: coordLength },
315
+ globalFeatureIds: { value: polygons.globalFeatureIds, size: 1 },
316
+ featureIds: { value: polygons.featureIds, size: 1 },
317
+ type: 'Polygon'
318
+ }
319
+ };
320
+ for (const geomType in returnObj) {
321
+ for (const numericProp in returnObj[geomType].numericProps) {
322
+ returnObj[geomType].numericProps[numericProp] = {
323
+ value: returnObj[geomType].numericProps[numericProp],
324
+ size: 1
325
+ };
326
+ }
365
327
  }
366
- }
367
-
368
- return returnObj;
328
+ return returnObj;
369
329
  }
370
-
330
+ /** Add numeric properties to object */
371
331
  function fillNumericProperties(object, properties, index, length) {
372
- for (const numericPropName in object.numericProps) {
373
- if (numericPropName in properties) {
374
- object.numericProps[numericPropName].set(new Array(length).fill(properties[numericPropName]), index);
332
+ for (const numericPropName in object.numericProps) {
333
+ if (numericPropName in properties) {
334
+ object.numericProps[numericPropName].set(new Array(length).fill(properties[numericPropName]), index);
335
+ }
375
336
  }
376
- }
377
337
  }
378
-
338
+ /** Keep string properties in object */
379
339
  function keepStringProperties(properties, numericKeys) {
380
- const props = {};
381
-
382
- for (const key in properties) {
383
- if (!numericKeys.includes(key)) {
384
- props[key] = properties[key];
340
+ const props = {};
341
+ for (const key in properties) {
342
+ if (!numericKeys.includes(key)) {
343
+ props[key] = properties[key];
344
+ }
385
345
  }
386
- }
387
-
388
- return props;
346
+ return props;
389
347
  }
390
-
348
+ /** @param coords is expected to be a list of arrays, each with length 2-3 */
391
349
  function fillCoords(array, coords, startVertex, coordLength) {
392
- let index = startVertex * coordLength;
393
-
394
- for (const coord of coords) {
395
- array.set(coord, index);
396
- index += coordLength;
397
- }
350
+ let index = startVertex * coordLength;
351
+ for (const coord of coords) {
352
+ array.set(coord, index);
353
+ index += coordLength;
354
+ }
398
355
  }
399
-
356
+ // TODO - how does this work? Different `coordinates` have different nesting
400
357
  function flatten(arrays) {
401
- return [].concat(...arrays);
358
+ return [].concat(...arrays);
402
359
  }
403
-
404
360
  function isNumeric(x) {
405
- return Number.isFinite(x);
361
+ return Number.isFinite(x);
406
362
  }
407
- //# sourceMappingURL=geojson-to-binary.js.map