@loaders.gl/gis 4.0.0-alpha.9 → 4.0.0-beta.2

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 (50) hide show
  1. package/dist/es5/index.js +0 -6
  2. package/dist/es5/index.js.map +1 -1
  3. package/dist/es5/lib/binary-to-geojson.js +23 -57
  4. package/dist/es5/lib/binary-to-geojson.js.map +1 -1
  5. package/dist/es5/lib/flat-geojson-to-binary-types.js.map +1 -1
  6. package/dist/es5/lib/flat-geojson-to-binary.js +19 -8
  7. package/dist/es5/lib/flat-geojson-to-binary.js.map +1 -1
  8. package/dist/es5/lib/geojson-to-binary.js +4 -2
  9. package/dist/es5/lib/geojson-to-binary.js.map +1 -1
  10. package/dist/es5/lib/transform.js +1 -1
  11. package/dist/es5/lib/transform.js.map +1 -1
  12. package/dist/esm/index.js +1 -1
  13. package/dist/esm/index.js.map +1 -1
  14. package/dist/esm/lib/binary-to-geojson.js +0 -25
  15. package/dist/esm/lib/binary-to-geojson.js.map +1 -1
  16. package/dist/esm/lib/flat-geojson-to-binary-types.js.map +1 -1
  17. package/dist/esm/lib/flat-geojson-to-binary.js +18 -8
  18. package/dist/esm/lib/flat-geojson-to-binary.js.map +1 -1
  19. package/dist/esm/lib/geojson-to-binary.js +4 -2
  20. package/dist/esm/lib/geojson-to-binary.js.map +1 -1
  21. package/dist/esm/lib/transform.js +1 -1
  22. package/dist/esm/lib/transform.js.map +1 -1
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/lib/binary-to-geojson.d.ts +2 -4
  26. package/dist/lib/binary-to-geojson.d.ts.map +1 -1
  27. package/dist/lib/flat-geojson-to-binary-types.d.ts +1 -1
  28. package/dist/lib/flat-geojson-to-binary-types.d.ts.map +1 -1
  29. package/dist/lib/flat-geojson-to-binary.d.ts +3 -2
  30. package/dist/lib/flat-geojson-to-binary.d.ts.map +1 -1
  31. package/dist/lib/geojson-to-binary.d.ts +3 -2
  32. package/dist/lib/geojson-to-binary.d.ts.map +1 -1
  33. package/dist/lib/transform.d.ts +3 -3
  34. package/dist/lib/transform.d.ts.map +1 -1
  35. package/package.json +4 -4
  36. package/src/index.ts +1 -1
  37. package/src/lib/binary-to-geojson.ts +25 -43
  38. package/src/lib/flat-geojson-to-binary-types.ts +1 -1
  39. package/src/lib/flat-geojson-to-binary.ts +24 -9
  40. package/src/lib/geojson-to-binary.ts +6 -4
  41. package/src/lib/transform.ts +10 -10
  42. package/dist/bundle.js +0 -5
  43. package/dist/index.js +0 -18
  44. package/dist/lib/binary-to-geojson.js +0 -233
  45. package/dist/lib/extract-geometry-info.js +0 -96
  46. package/dist/lib/flat-geojson-to-binary-types.js +0 -2
  47. package/dist/lib/flat-geojson-to-binary.js +0 -376
  48. package/dist/lib/geojson-to-binary.js +0 -24
  49. package/dist/lib/geojson-to-flat-geojson.js +0 -128
  50. package/dist/lib/transform.js +0 -59
@@ -1,96 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extractGeometryInfo = void 0;
4
- /**
5
- * Initial scan over GeoJSON features
6
- * Counts number of coordinates of each geometry type and
7
- * keeps track of the max coordinate dimensions
8
- */
9
- // eslint-disable-next-line complexity, max-statements
10
- function extractGeometryInfo(features) {
11
- // Counts the number of _positions_, so [x, y, z] counts as one
12
- let pointPositionsCount = 0;
13
- let pointFeaturesCount = 0;
14
- let linePositionsCount = 0;
15
- let linePathsCount = 0;
16
- let lineFeaturesCount = 0;
17
- let polygonPositionsCount = 0;
18
- let polygonObjectsCount = 0;
19
- let polygonRingsCount = 0;
20
- let polygonFeaturesCount = 0;
21
- const coordLengths = new Set();
22
- for (const feature of features) {
23
- const geometry = feature.geometry;
24
- switch (geometry.type) {
25
- case 'Point':
26
- pointFeaturesCount++;
27
- pointPositionsCount++;
28
- coordLengths.add(geometry.coordinates.length);
29
- break;
30
- case 'MultiPoint':
31
- pointFeaturesCount++;
32
- pointPositionsCount += geometry.coordinates.length;
33
- for (const point of geometry.coordinates) {
34
- coordLengths.add(point.length);
35
- }
36
- break;
37
- case 'LineString':
38
- lineFeaturesCount++;
39
- linePositionsCount += geometry.coordinates.length;
40
- linePathsCount++;
41
- for (const coord of geometry.coordinates) {
42
- coordLengths.add(coord.length);
43
- }
44
- break;
45
- case 'MultiLineString':
46
- lineFeaturesCount++;
47
- for (const line of geometry.coordinates) {
48
- linePositionsCount += line.length;
49
- linePathsCount++;
50
- // eslint-disable-next-line max-depth
51
- for (const coord of line) {
52
- coordLengths.add(coord.length);
53
- }
54
- }
55
- break;
56
- case 'Polygon':
57
- polygonFeaturesCount++;
58
- polygonObjectsCount++;
59
- polygonRingsCount += geometry.coordinates.length;
60
- const flattened = geometry.coordinates.flat();
61
- polygonPositionsCount += flattened.length;
62
- for (const coord of flattened) {
63
- coordLengths.add(coord.length);
64
- }
65
- break;
66
- case 'MultiPolygon':
67
- polygonFeaturesCount++;
68
- for (const polygon of geometry.coordinates) {
69
- polygonObjectsCount++;
70
- polygonRingsCount += polygon.length;
71
- const flattened = polygon.flat();
72
- polygonPositionsCount += flattened.length;
73
- // eslint-disable-next-line max-depth
74
- for (const coord of flattened) {
75
- coordLengths.add(coord.length);
76
- }
77
- }
78
- break;
79
- default:
80
- throw new Error(`Unsupported geometry type: ${geometry.type}`);
81
- }
82
- }
83
- return {
84
- coordLength: coordLengths.size > 0 ? Math.max(...coordLengths) : 2,
85
- pointPositionsCount,
86
- pointFeaturesCount,
87
- linePositionsCount,
88
- linePathsCount,
89
- lineFeaturesCount,
90
- polygonPositionsCount,
91
- polygonObjectsCount,
92
- polygonRingsCount,
93
- polygonFeaturesCount
94
- };
95
- }
96
- exports.extractGeometryInfo = extractGeometryInfo;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,376 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TEST_EXPORTS = exports.flatGeojsonToBinary = void 0;
4
- /* eslint-disable indent */
5
- const polygon_1 = require("@math.gl/polygon");
6
- /**
7
- * Convert binary features to flat binary arrays. Similar to
8
- * `geojsonToBinary` helper function, except that it expects
9
- * a binary representation of the feature data, which enables
10
- * 2X-3X speed increase in parse speed, compared to using
11
- * geoJSON. See `binary-vector-tile/VectorTileFeature` for
12
- * data format detais
13
- *
14
- * @param features
15
- * @param geometryInfo
16
- * @param options
17
- * @returns filled arrays
18
- */
19
- function flatGeojsonToBinary(features, geometryInfo, options) {
20
- const propArrayTypes = extractNumericPropTypes(features);
21
- const numericPropKeys = Object.keys(propArrayTypes).filter((k) => propArrayTypes[k] !== Array);
22
- return fillArrays(features, {
23
- propArrayTypes,
24
- ...geometryInfo
25
- }, {
26
- numericPropKeys: (options && options.numericPropKeys) || numericPropKeys,
27
- PositionDataType: options ? options.PositionDataType : Float32Array
28
- });
29
- }
30
- exports.flatGeojsonToBinary = flatGeojsonToBinary;
31
- exports.TEST_EXPORTS = {
32
- extractNumericPropTypes
33
- };
34
- /**
35
- * Extracts properties that are always numeric
36
- *
37
- * @param features
38
- * @returns object with numeric types
39
- */
40
- function extractNumericPropTypes(features) {
41
- const propArrayTypes = {};
42
- for (const feature of features) {
43
- if (feature.properties) {
44
- for (const key in feature.properties) {
45
- // If property has not been seen before, or if property has been numeric
46
- // in all previous features, check if numeric in this feature
47
- // If not numeric, Array is stored to prevent rechecking in the future
48
- // Additionally, detects if 64 bit precision is required
49
- const val = feature.properties[key];
50
- propArrayTypes[key] = deduceArrayType(val, propArrayTypes[key]);
51
- }
52
- }
53
- }
54
- return propArrayTypes;
55
- }
56
- /**
57
- * Fills coordinates into pre-allocated typed arrays
58
- *
59
- * @param features
60
- * @param geometryInfo
61
- * @param options
62
- * @returns an accessor object with value and size keys
63
- */
64
- // eslint-disable-next-line complexity
65
- function fillArrays(features, geometryInfo, options) {
66
- const { pointPositionsCount, pointFeaturesCount, linePositionsCount, linePathsCount, lineFeaturesCount, polygonPositionsCount, polygonObjectsCount, polygonRingsCount, polygonFeaturesCount, propArrayTypes, coordLength } = geometryInfo;
67
- const { numericPropKeys = [], PositionDataType = Float32Array } = options;
68
- const hasGlobalId = features[0] && 'id' in features[0];
69
- const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
70
- const points = {
71
- type: 'Point',
72
- positions: new PositionDataType(pointPositionsCount * coordLength),
73
- globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
74
- featureIds: pointFeaturesCount > 65535
75
- ? new Uint32Array(pointPositionsCount)
76
- : new Uint16Array(pointPositionsCount),
77
- numericProps: {},
78
- properties: [],
79
- fields: []
80
- };
81
- const lines = {
82
- type: 'LineString',
83
- pathIndices: linePositionsCount > 65535
84
- ? new Uint32Array(linePathsCount + 1)
85
- : new Uint16Array(linePathsCount + 1),
86
- positions: new PositionDataType(linePositionsCount * coordLength),
87
- globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
88
- featureIds: lineFeaturesCount > 65535
89
- ? new Uint32Array(linePositionsCount)
90
- : new Uint16Array(linePositionsCount),
91
- numericProps: {},
92
- properties: [],
93
- fields: []
94
- };
95
- const polygons = {
96
- type: 'Polygon',
97
- polygonIndices: polygonPositionsCount > 65535
98
- ? new Uint32Array(polygonObjectsCount + 1)
99
- : new Uint16Array(polygonObjectsCount + 1),
100
- primitivePolygonIndices: polygonPositionsCount > 65535
101
- ? new Uint32Array(polygonRingsCount + 1)
102
- : new Uint16Array(polygonRingsCount + 1),
103
- positions: new PositionDataType(polygonPositionsCount * coordLength),
104
- triangles: [],
105
- globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
106
- featureIds: polygonFeaturesCount > 65535
107
- ? new Uint32Array(polygonPositionsCount)
108
- : new Uint16Array(polygonPositionsCount),
109
- numericProps: {},
110
- properties: [],
111
- fields: []
112
- };
113
- // Instantiate numeric properties arrays; one value per vertex
114
- for (const object of [points, lines, polygons]) {
115
- for (const propName of numericPropKeys) {
116
- // If property has been numeric in all previous features in which the property existed, check
117
- // if numeric in this feature
118
- const T = propArrayTypes[propName];
119
- object.numericProps[propName] = new T(object.positions.length / coordLength);
120
- }
121
- }
122
- // Set last element of path/polygon indices as positions length
123
- lines.pathIndices[linePathsCount] = linePositionsCount;
124
- polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
125
- polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
126
- const indexMap = {
127
- pointPosition: 0,
128
- pointFeature: 0,
129
- linePosition: 0,
130
- linePath: 0,
131
- lineFeature: 0,
132
- polygonPosition: 0,
133
- polygonObject: 0,
134
- polygonRing: 0,
135
- polygonFeature: 0,
136
- feature: 0
137
- };
138
- for (const feature of features) {
139
- const geometry = feature.geometry;
140
- const properties = feature.properties || {};
141
- switch (geometry.type) {
142
- case 'Point':
143
- handlePoint(geometry, points, indexMap, coordLength, properties);
144
- points.properties.push(keepStringProperties(properties, numericPropKeys));
145
- if (hasGlobalId) {
146
- points.fields.push({ id: feature.id });
147
- }
148
- indexMap.pointFeature++;
149
- break;
150
- case 'LineString':
151
- handleLineString(geometry, lines, indexMap, coordLength, properties);
152
- lines.properties.push(keepStringProperties(properties, numericPropKeys));
153
- if (hasGlobalId) {
154
- lines.fields.push({ id: feature.id });
155
- }
156
- indexMap.lineFeature++;
157
- break;
158
- case 'Polygon':
159
- handlePolygon(geometry, polygons, indexMap, coordLength, properties);
160
- polygons.properties.push(keepStringProperties(properties, numericPropKeys));
161
- if (hasGlobalId) {
162
- polygons.fields.push({ id: feature.id });
163
- }
164
- indexMap.polygonFeature++;
165
- break;
166
- default:
167
- throw new Error('Invalid geometry type');
168
- }
169
- indexMap.feature++;
170
- }
171
- // Wrap each array in an accessor object with value and size keys
172
- return makeAccessorObjects(points, lines, polygons, coordLength);
173
- }
174
- /**
175
- * Fills (Multi)Point coordinates into points object of arrays
176
- *
177
- * @param geometry
178
- * @param points
179
- * @param indexMap
180
- * @param coordLength
181
- * @param properties
182
- */
183
- function handlePoint(geometry, points, indexMap, coordLength, properties) {
184
- points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
185
- const nPositions = geometry.data.length / coordLength;
186
- fillNumericProperties(points, properties, indexMap.pointPosition, nPositions);
187
- points.globalFeatureIds.fill(indexMap.feature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
188
- points.featureIds.fill(indexMap.pointFeature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
189
- indexMap.pointPosition += nPositions;
190
- }
191
- /**
192
- * Fills (Multi)LineString coordinates into lines object of arrays
193
- *
194
- * @param geometry
195
- * @param lines
196
- * @param indexMap
197
- * @param coordLength
198
- * @param properties
199
- */
200
- function handleLineString(geometry, lines, indexMap, coordLength, properties) {
201
- lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
202
- const nPositions = geometry.data.length / coordLength;
203
- fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
204
- lines.globalFeatureIds.fill(indexMap.feature, indexMap.linePosition, indexMap.linePosition + nPositions);
205
- lines.featureIds.fill(indexMap.lineFeature, indexMap.linePosition, indexMap.linePosition + nPositions);
206
- for (let i = 0, il = geometry.indices.length; i < il; ++i) {
207
- // Extract range of data we are working with, defined by start
208
- // and end indices (these index into the geometry.data array)
209
- const start = geometry.indices[i];
210
- const end = i === il - 1
211
- ? geometry.data.length // last line, so read to end of data
212
- : geometry.indices[i + 1]; // start index for next line
213
- lines.pathIndices[indexMap.linePath++] = indexMap.linePosition;
214
- indexMap.linePosition += (end - start) / coordLength;
215
- }
216
- }
217
- /**
218
- * Fills (Multi)Polygon coordinates into polygons object of arrays
219
- *
220
- * @param geometry
221
- * @param polygons
222
- * @param indexMap
223
- * @param coordLength
224
- * @param properties
225
- */
226
- function handlePolygon(geometry, polygons, indexMap, coordLength, properties) {
227
- polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
228
- const nPositions = geometry.data.length / coordLength;
229
- fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
230
- polygons.globalFeatureIds.fill(indexMap.feature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
231
- polygons.featureIds.fill(indexMap.polygonFeature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
232
- // Unlike Point & LineString geometry.indices is a 2D array
233
- for (let l = 0, ll = geometry.indices.length; l < ll; ++l) {
234
- const startPosition = indexMap.polygonPosition;
235
- polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
236
- const areas = geometry.areas[l];
237
- const indices = geometry.indices[l];
238
- const nextIndices = geometry.indices[l + 1];
239
- for (let i = 0, il = indices.length; i < il; ++i) {
240
- const start = indices[i];
241
- const end = i === il - 1
242
- ? // last line, so either read to:
243
- nextIndices === undefined
244
- ? geometry.data.length // end of data (no next indices)
245
- : nextIndices[0] // start of first line in nextIndices
246
- : indices[i + 1]; // start index for next line
247
- polygons.primitivePolygonIndices[indexMap.polygonRing++] = indexMap.polygonPosition;
248
- indexMap.polygonPosition += (end - start) / coordLength;
249
- }
250
- const endPosition = indexMap.polygonPosition;
251
- triangulatePolygon(polygons, areas, indices, { startPosition, endPosition, coordLength });
252
- }
253
- }
254
- /**
255
- * Triangulate polygon using earcut
256
- *
257
- * @param polygons
258
- * @param areas
259
- * @param indices
260
- * @param param3
261
- */
262
- function triangulatePolygon(polygons, areas, indices, { startPosition, endPosition, coordLength }) {
263
- const start = startPosition * coordLength;
264
- const end = endPosition * coordLength;
265
- // Extract positions and holes for just this polygon
266
- const polygonPositions = polygons.positions.subarray(start, end);
267
- // Holes are referenced relative to outer polygon
268
- const offset = indices[0];
269
- const holes = indices.slice(1).map((n) => (n - offset) / coordLength);
270
- // Compute triangulation
271
- // @ts-expect-error TODO can earcut handle binary arrays? Add tests?
272
- const triangles = (0, polygon_1.earcut)(polygonPositions, holes, coordLength, areas);
273
- // Indices returned by triangulation are relative to start
274
- // of polygon, so we need to offset
275
- for (let t = 0, tl = triangles.length; t < tl; ++t) {
276
- polygons.triangles.push(startPosition + triangles[t]);
277
- }
278
- }
279
- /**
280
- * Wraps an object containing array into accessors
281
- *
282
- * @param obj
283
- * @param size
284
- */
285
- function wrapProps(obj, size) {
286
- const returnObj = {};
287
- for (const key in obj) {
288
- returnObj[key] = { value: obj[key], size };
289
- }
290
- return returnObj;
291
- }
292
- /**
293
- * Wrap each array in an accessor object with value and size keys
294
- *
295
- * @param points
296
- * @param lines
297
- * @param polygons
298
- * @param coordLength
299
- * @returns object
300
- */
301
- function makeAccessorObjects(points, lines, polygons, coordLength) {
302
- return {
303
- points: {
304
- ...points,
305
- positions: { value: points.positions, size: coordLength },
306
- globalFeatureIds: { value: points.globalFeatureIds, size: 1 },
307
- featureIds: { value: points.featureIds, size: 1 },
308
- numericProps: wrapProps(points.numericProps, 1)
309
- },
310
- lines: {
311
- ...lines,
312
- positions: { value: lines.positions, size: coordLength },
313
- pathIndices: { value: lines.pathIndices, size: 1 },
314
- globalFeatureIds: { value: lines.globalFeatureIds, size: 1 },
315
- featureIds: { value: lines.featureIds, size: 1 },
316
- numericProps: wrapProps(lines.numericProps, 1)
317
- },
318
- polygons: {
319
- ...polygons,
320
- positions: { value: polygons.positions, size: coordLength },
321
- polygonIndices: { value: polygons.polygonIndices, size: 1 },
322
- primitivePolygonIndices: { value: polygons.primitivePolygonIndices, size: 1 },
323
- triangles: { value: new Uint32Array(polygons.triangles), size: 1 },
324
- globalFeatureIds: { value: polygons.globalFeatureIds, size: 1 },
325
- featureIds: { value: polygons.featureIds, size: 1 },
326
- numericProps: wrapProps(polygons.numericProps, 1)
327
- }
328
- };
329
- }
330
- /**
331
- * Add numeric properties to object
332
- *
333
- * @param object
334
- * @param properties
335
- * @param index
336
- * @param length
337
- */
338
- function fillNumericProperties(object, properties, index, length) {
339
- for (const numericPropName in object.numericProps) {
340
- if (numericPropName in properties) {
341
- const value = properties[numericPropName];
342
- object.numericProps[numericPropName].fill(value, index, index + length);
343
- }
344
- }
345
- }
346
- /**
347
- * Keep string properties in object
348
- *
349
- * @param properties
350
- * @param numericKeys
351
- * @returns object
352
- */
353
- function keepStringProperties(properties, numericKeys) {
354
- const props = {};
355
- for (const key in properties) {
356
- if (!numericKeys.includes(key)) {
357
- props[key] = properties[key];
358
- }
359
- }
360
- return props;
361
- }
362
- /**
363
- *
364
- * Deduce correct array constructor to use for a given value
365
- *
366
- * @param x value to test
367
- * @param constructor previous constructor deduced
368
- * @returns PropArrayConstructor
369
- */
370
- function deduceArrayType(x, constructor) {
371
- if (constructor === Array || !Number.isFinite(x)) {
372
- return Array;
373
- }
374
- // If this or previous value required 64bits use Float64Array
375
- return constructor === Float64Array || Math.fround(x) !== x ? Float64Array : Float32Array;
376
- }
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.geojsonToBinary = void 0;
4
- const extract_geometry_info_1 = require("./extract-geometry-info");
5
- const geojson_to_flat_geojson_1 = require("./geojson-to-flat-geojson");
6
- const flat_geojson_to_binary_1 = require("./flat-geojson-to-binary");
7
- /**
8
- * Convert GeoJSON features to flat binary arrays
9
- *
10
- * @param features
11
- * @param options
12
- * @returns features in binary format, grouped by geometry type
13
- */
14
- function geojsonToBinary(features, options = { fixRingWinding: true }) {
15
- const geometryInfo = (0, extract_geometry_info_1.extractGeometryInfo)(features);
16
- const coordLength = geometryInfo.coordLength;
17
- const { fixRingWinding } = options;
18
- const flatFeatures = (0, geojson_to_flat_geojson_1.geojsonToFlatGeojson)(features, { coordLength, fixRingWinding });
19
- return (0, flat_geojson_to_binary_1.flatGeojsonToBinary)(flatFeatures, geometryInfo, {
20
- numericPropKeys: options.numericPropKeys,
21
- PositionDataType: options.PositionDataType || Float32Array
22
- });
23
- }
24
- exports.geojsonToBinary = geojsonToBinary;
@@ -1,128 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.geojsonToFlatGeojson = void 0;
4
- const polygon_1 = require("@math.gl/polygon");
5
- /**
6
- * Convert GeoJSON features to Flat GeoJSON features
7
- *
8
- * @param features
9
- * @param options
10
- * @returns an Array of Flat GeoJSON features
11
- */
12
- function geojsonToFlatGeojson(features, options = { coordLength: 2, fixRingWinding: true }) {
13
- return features.map((feature) => flattenFeature(feature, options));
14
- }
15
- exports.geojsonToFlatGeojson = geojsonToFlatGeojson;
16
- /**
17
- * Helper function to copy Point values from `coordinates` into `data` & `indices`
18
- *
19
- * @param coordinates
20
- * @param data
21
- * @param indices
22
- * @param options
23
- */
24
- function flattenPoint(coordinates, data, indices, options) {
25
- indices.push(data.length);
26
- data.push(...coordinates);
27
- // Pad up to coordLength
28
- for (let i = coordinates.length; i < options.coordLength; i++) {
29
- data.push(0);
30
- }
31
- }
32
- /**
33
- * Helper function to copy LineString values from `coordinates` into `data` & `indices`
34
- *
35
- * @param coordinates
36
- * @param data
37
- * @param indices
38
- * @param options
39
- */
40
- function flattenLineString(coordinates, data, indices, options) {
41
- indices.push(data.length);
42
- for (const c of coordinates) {
43
- data.push(...c);
44
- // Pad up to coordLength
45
- for (let i = c.length; i < options.coordLength; i++) {
46
- data.push(0);
47
- }
48
- }
49
- }
50
- /**
51
- * Helper function to copy Polygon values from `coordinates` into `data` & `indices` & `areas`
52
- *
53
- * @param coordinates
54
- * @param data
55
- * @param indices
56
- * @param areas
57
- * @param options
58
- */
59
- function flattenPolygon(coordinates, data, indices, areas, options) {
60
- let count = 0;
61
- const ringAreas = [];
62
- const polygons = [];
63
- for (const lineString of coordinates) {
64
- const lineString2d = lineString.map((p) => p.slice(0, 2));
65
- let area = (0, polygon_1.getPolygonSignedArea)(lineString2d.flat());
66
- const ccw = area < 0;
67
- // Exterior ring must be CCW and interior rings CW
68
- if (options.fixRingWinding && ((count === 0 && !ccw) || (count > 0 && ccw))) {
69
- lineString.reverse();
70
- area = -area;
71
- }
72
- ringAreas.push(area);
73
- flattenLineString(lineString, data, polygons, options);
74
- count++;
75
- }
76
- if (count > 0) {
77
- areas.push(ringAreas);
78
- indices.push(polygons);
79
- }
80
- }
81
- /**
82
- * Flatten single GeoJSON feature into Flat GeoJSON
83
- *
84
- * @param feature
85
- * @param options
86
- * @returns A Flat GeoJSON feature
87
- */
88
- function flattenFeature(feature, options) {
89
- const { geometry } = feature;
90
- if (geometry.type === 'GeometryCollection') {
91
- throw new Error('GeometryCollection type not supported');
92
- }
93
- const data = [];
94
- const indices = [];
95
- let areas;
96
- let type;
97
- switch (geometry.type) {
98
- case 'Point':
99
- type = 'Point';
100
- flattenPoint(geometry.coordinates, data, indices, options);
101
- break;
102
- case 'MultiPoint':
103
- type = 'Point';
104
- geometry.coordinates.map((c) => flattenPoint(c, data, indices, options));
105
- break;
106
- case 'LineString':
107
- type = 'LineString';
108
- flattenLineString(geometry.coordinates, data, indices, options);
109
- break;
110
- case 'MultiLineString':
111
- type = 'LineString';
112
- geometry.coordinates.map((c) => flattenLineString(c, data, indices, options));
113
- break;
114
- case 'Polygon':
115
- type = 'Polygon';
116
- areas = [];
117
- flattenPolygon(geometry.coordinates, data, indices, areas, options);
118
- break;
119
- case 'MultiPolygon':
120
- type = 'Polygon';
121
- areas = [];
122
- geometry.coordinates.map((c) => flattenPolygon(c, data, indices, areas, options));
123
- break;
124
- default:
125
- throw new Error(`Unknown type: ${type}`);
126
- }
127
- return { ...feature, geometry: { type, indices, data, areas } };
128
- }
@@ -1,59 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.transformGeoJsonCoords = exports.transformBinaryCoords = void 0;
4
- /**
5
- * Apply transformation to every coordinate of binary features
6
- * @param binaryFeatures binary features
7
- * @param transformCoordinate Function to call on each coordinate
8
- * @return Transformed binary features
9
- */
10
- function transformBinaryCoords(binaryFeatures, transformCoordinate) {
11
- if (binaryFeatures.points) {
12
- transformBinaryGeometryPositions(binaryFeatures.points, transformCoordinate);
13
- }
14
- if (binaryFeatures.lines) {
15
- transformBinaryGeometryPositions(binaryFeatures.lines, transformCoordinate);
16
- }
17
- if (binaryFeatures.polygons) {
18
- transformBinaryGeometryPositions(binaryFeatures.polygons, transformCoordinate);
19
- }
20
- return binaryFeatures;
21
- }
22
- exports.transformBinaryCoords = transformBinaryCoords;
23
- /** Transform one binary geometry */
24
- function transformBinaryGeometryPositions(binaryGeometry, fn) {
25
- const { positions } = binaryGeometry;
26
- for (let i = 0; i < positions.value.length; i += positions.size) {
27
- // @ts-ignore inclusion of bigint causes problems
28
- const coord = Array.from(positions.value.subarray(i, i + positions.size));
29
- const transformedCoord = fn(coord);
30
- // @ts-ignore typescript typing for .set seems to require bigint?
31
- positions.value.set(transformedCoord, i);
32
- }
33
- }
34
- /**
35
- * Apply transformation to every coordinate of GeoJSON features
36
- *
37
- * @param features Array of GeoJSON features
38
- * @param fn Function to call on each coordinate
39
- * @return Transformed GeoJSON features
40
- */
41
- function transformGeoJsonCoords(features, fn) {
42
- for (const feature of features) {
43
- // @ts-ignore
44
- feature.geometry.coordinates = coordMap(feature.geometry.coordinates, fn);
45
- }
46
- return features;
47
- }
48
- exports.transformGeoJsonCoords = transformGeoJsonCoords;
49
- function coordMap(array, fn) {
50
- if (isCoord(array)) {
51
- return fn(array);
52
- }
53
- return array.map((item) => {
54
- return coordMap(item, fn);
55
- });
56
- }
57
- function isCoord(array) {
58
- return Number.isFinite(array[0]) && Number.isFinite(array[1]);
59
- }