@loaders.gl/gis 4.0.0-alpha.4 → 4.0.0-alpha.6

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 (75) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/es5/bundle.js +6 -0
  5. package/dist/es5/bundle.js.map +1 -0
  6. package/dist/es5/index.js +59 -0
  7. package/dist/es5/index.js.map +1 -0
  8. package/dist/es5/lib/binary-to-geojson.js +255 -0
  9. package/dist/es5/lib/binary-to-geojson.js.map +1 -0
  10. package/dist/es5/lib/extract-geometry-info.js +167 -0
  11. package/dist/es5/lib/extract-geometry-info.js.map +1 -0
  12. package/dist/es5/lib/flat-geojson-to-binary-types.js +2 -0
  13. package/dist/es5/lib/flat-geojson-to-binary-types.js.map +1 -0
  14. package/dist/es5/lib/flat-geojson-to-binary.js +344 -0
  15. package/dist/es5/lib/flat-geojson-to-binary.js.map +1 -0
  16. package/dist/es5/lib/geojson-to-binary.js +26 -0
  17. package/dist/es5/lib/geojson-to-binary.js.map +1 -0
  18. package/dist/es5/lib/geojson-to-flat-geojson.js +136 -0
  19. package/dist/es5/lib/geojson-to-flat-geojson.js.map +1 -0
  20. package/dist/es5/lib/transform.js +57 -0
  21. package/dist/es5/lib/transform.js.map +1 -0
  22. package/dist/esm/bundle.js +4 -0
  23. package/dist/esm/bundle.js.map +1 -0
  24. package/dist/esm/index.js +6 -0
  25. package/dist/esm/index.js.map +1 -0
  26. package/dist/esm/lib/binary-to-geojson.js +223 -0
  27. package/dist/esm/lib/binary-to-geojson.js.map +1 -0
  28. package/dist/esm/lib/extract-geometry-info.js +84 -0
  29. package/dist/esm/lib/extract-geometry-info.js.map +1 -0
  30. package/dist/esm/lib/flat-geojson-to-binary-types.js +2 -0
  31. package/dist/esm/lib/flat-geojson-to-binary-types.js.map +1 -0
  32. package/dist/esm/lib/flat-geojson-to-binary.js +305 -0
  33. package/dist/esm/lib/flat-geojson-to-binary.js.map +1 -0
  34. package/dist/esm/lib/geojson-to-binary.js +22 -0
  35. package/dist/esm/lib/geojson-to-binary.js.map +1 -0
  36. package/dist/esm/lib/geojson-to-flat-geojson.js +97 -0
  37. package/dist/esm/lib/geojson-to-flat-geojson.js.map +1 -0
  38. package/dist/esm/lib/transform.js +40 -0
  39. package/dist/esm/lib/transform.js.map +1 -0
  40. package/dist/index.d.ts +6 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +18 -4
  43. package/dist/lib/binary-to-geojson.d.ts +21 -0
  44. package/dist/lib/binary-to-geojson.d.ts.map +1 -0
  45. package/dist/lib/binary-to-geojson.js +201 -242
  46. package/dist/lib/extract-geometry-info.d.ts +8 -0
  47. package/dist/lib/extract-geometry-info.d.ts.map +1 -0
  48. package/dist/lib/extract-geometry-info.js +96 -0
  49. package/dist/lib/flat-geojson-to-binary-types.d.ts +58 -0
  50. package/dist/lib/flat-geojson-to-binary-types.d.ts.map +1 -0
  51. package/dist/lib/flat-geojson-to-binary-types.js +2 -0
  52. package/dist/lib/flat-geojson-to-binary.d.ts +37 -0
  53. package/dist/lib/flat-geojson-to-binary.d.ts.map +1 -0
  54. package/dist/lib/flat-geojson-to-binary.js +376 -0
  55. package/dist/lib/geojson-to-binary.d.ts +19 -0
  56. package/dist/lib/geojson-to-binary.d.ts.map +1 -0
  57. package/dist/lib/geojson-to-binary.js +24 -407
  58. package/dist/lib/geojson-to-flat-geojson.d.ts +17 -0
  59. package/dist/lib/geojson-to-flat-geojson.d.ts.map +1 -0
  60. package/dist/lib/geojson-to-flat-geojson.js +128 -0
  61. package/dist/lib/transform.d.ts +19 -0
  62. package/dist/lib/transform.d.ts.map +1 -0
  63. package/dist/lib/transform.js +51 -42
  64. package/package.json +8 -7
  65. package/src/index.ts +2 -0
  66. package/src/lib/extract-geometry-info.ts +101 -0
  67. package/src/lib/flat-geojson-to-binary-types.ts +58 -0
  68. package/src/lib/flat-geojson-to-binary.ts +565 -0
  69. package/src/lib/geojson-to-binary.ts +24 -450
  70. package/src/lib/geojson-to-flat-geojson.ts +171 -0
  71. package/dist/bundle.js.map +0 -1
  72. package/dist/index.js.map +0 -1
  73. package/dist/lib/binary-to-geojson.js.map +0 -1
  74. package/dist/lib/geojson-to-binary.js.map +0 -1
  75. package/dist/lib/transform.js.map +0 -1
@@ -1,274 +1,233 @@
1
- export function binaryToGeojson(data, options) {
2
- const globalFeatureId = options === null || options === void 0 ? void 0 : options.globalFeatureId;
3
-
4
- if (globalFeatureId !== undefined) {
5
- return getSingleFeature(data, globalFeatureId);
6
- }
7
-
8
- return parseFeatures(data, options === null || options === void 0 ? void 0 : options.type);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.binaryToGeometry = exports.binaryToGeoJson = exports.binaryToGeojson = void 0;
4
+ /**
5
+ * Convert binary geometry representation to GeoJSON
6
+ * @param data geometry data in binary representation
7
+ * @param options
8
+ * @param options.type Input data type: Point, LineString, or Polygon
9
+ * @param options.featureId Global feature id. If specified, only a single feature is extracted
10
+ * @return GeoJSON objects
11
+ */
12
+ function binaryToGeojson(data, options) {
13
+ const globalFeatureId = options?.globalFeatureId;
14
+ if (globalFeatureId !== undefined) {
15
+ return getSingleFeature(data, globalFeatureId);
16
+ }
17
+ return parseFeatures(data, options?.type);
9
18
  }
10
- export function binaryToGeoJson(data, type, format = 'feature') {
11
- switch (format) {
12
- case 'feature':
13
- return parseFeatures(data, type);
14
-
15
- case 'geometry':
16
- return binaryToGeometry(data);
17
-
18
- default:
19
- throw new Error(format);
20
- }
19
+ exports.binaryToGeojson = binaryToGeojson;
20
+ /** @deprecated use `binaryToGeojson` or `binaryToGeometry` instead */
21
+ function binaryToGeoJson(data, type, format = 'feature') {
22
+ switch (format) {
23
+ case 'feature':
24
+ return parseFeatures(data, type);
25
+ case 'geometry':
26
+ return binaryToGeometry(data);
27
+ default:
28
+ throw new Error(format);
29
+ }
21
30
  }
22
-
31
+ exports.binaryToGeoJson = binaryToGeoJson;
32
+ /**
33
+ * Return a single feature from a binary geometry representation as GeoJSON
34
+ * @param data geometry data in binary representation
35
+ * @return GeoJSON feature
36
+ */
23
37
  function getSingleFeature(data, globalFeatureId) {
24
- const dataArray = normalizeInput(data);
25
-
26
- for (const data of dataArray) {
27
- let lastIndex = 0;
28
- let lastValue = data.featureIds.value[0];
29
-
30
- for (let i = 0; i < data.featureIds.value.length; i++) {
31
- const currValue = data.featureIds.value[i];
32
-
33
- if (currValue === lastValue) {
34
- continue;
35
- }
36
-
37
- if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
38
- return parseFeature(data, lastIndex, i);
39
- }
40
-
41
- lastIndex = i;
42
- lastValue = currValue;
43
- }
44
-
45
- if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
46
- return parseFeature(data, lastIndex, data.featureIds.value.length);
38
+ const dataArray = normalizeInput(data);
39
+ for (const data of dataArray) {
40
+ let lastIndex = 0;
41
+ let lastValue = data.featureIds.value[0];
42
+ // Scan through data until we find matching feature
43
+ for (let i = 0; i < data.featureIds.value.length; i++) {
44
+ const currValue = data.featureIds.value[i];
45
+ if (currValue === lastValue) {
46
+ // eslint-disable-next-line no-continue
47
+ continue;
48
+ }
49
+ if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
50
+ return parseFeature(data, lastIndex, i);
51
+ }
52
+ lastIndex = i;
53
+ lastValue = currValue;
54
+ }
55
+ if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {
56
+ return parseFeature(data, lastIndex, data.featureIds.value.length);
57
+ }
47
58
  }
48
- }
49
-
50
- throw new Error("featureId:".concat(globalFeatureId, " not found"));
59
+ throw new Error(`featureId:${globalFeatureId} not found`);
51
60
  }
52
-
53
61
  function parseFeatures(data, type) {
54
- const dataArray = normalizeInput(data, type);
55
- return parseFeatureCollection(dataArray);
62
+ const dataArray = normalizeInput(data, type);
63
+ return parseFeatureCollection(dataArray);
56
64
  }
57
-
58
- export function binaryToGeometry(data, startIndex, endIndex) {
59
- switch (data.type) {
60
- case 'Point':
61
- return pointToGeoJson(data, startIndex, endIndex);
62
-
63
- case 'LineString':
64
- return lineStringToGeoJson(data, startIndex, endIndex);
65
-
66
- case 'Polygon':
67
- return polygonToGeoJson(data, startIndex, endIndex);
68
-
69
- default:
70
- const unexpectedInput = data;
71
- throw new Error("Unsupported geometry type: ".concat(unexpectedInput === null || unexpectedInput === void 0 ? void 0 : unexpectedInput.type));
72
- }
65
+ /** Parse input binary data and return a valid GeoJSON geometry object */
66
+ function binaryToGeometry(data, startIndex, endIndex) {
67
+ switch (data.type) {
68
+ case 'Point':
69
+ return pointToGeoJson(data, startIndex, endIndex);
70
+ case 'LineString':
71
+ return lineStringToGeoJson(data, startIndex, endIndex);
72
+ case 'Polygon':
73
+ return polygonToGeoJson(data, startIndex, endIndex);
74
+ default:
75
+ const unexpectedInput = data;
76
+ throw new Error(`Unsupported geometry type: ${unexpectedInput?.type}`);
77
+ }
73
78
  }
74
-
79
+ exports.binaryToGeometry = binaryToGeometry;
80
+ // Normalize features
81
+ // Return an array of data objects, each of which have a type key
75
82
  function normalizeInput(data, type) {
76
- const isHeterogeneousType = Boolean(data.points || data.lines || data.polygons);
77
-
78
- if (!isHeterogeneousType) {
79
- data.type = type || parseType(data);
80
- return [data];
81
- }
82
-
83
- const features = [];
84
-
85
- if (data.points) {
86
- data.points.type = 'Point';
87
- features.push(data.points);
88
- }
89
-
90
- if (data.lines) {
91
- data.lines.type = 'LineString';
92
- features.push(data.lines);
93
- }
94
-
95
- if (data.polygons) {
96
- data.polygons.type = 'Polygon';
97
- features.push(data.polygons);
98
- }
99
-
100
- return features;
83
+ const isHeterogeneousType = Boolean(data.points || data.lines || data.polygons);
84
+ if (!isHeterogeneousType) {
85
+ // @ts-expect-error This is a legacy check which allowed `data` to be an instance of the values
86
+ // here. Aka the new data.points, data.lines, or data.polygons.
87
+ data.type = type || parseType(data);
88
+ return [data];
89
+ }
90
+ const features = [];
91
+ if (data.points) {
92
+ data.points.type = 'Point';
93
+ features.push(data.points);
94
+ }
95
+ if (data.lines) {
96
+ data.lines.type = 'LineString';
97
+ features.push(data.lines);
98
+ }
99
+ if (data.polygons) {
100
+ data.polygons.type = 'Polygon';
101
+ features.push(data.polygons);
102
+ }
103
+ return features;
101
104
  }
102
-
105
+ /** Parse input binary data and return an array of GeoJSON Features */
103
106
  function parseFeatureCollection(dataArray) {
104
- const features = [];
105
-
106
- for (const data of dataArray) {
107
- if (data.featureIds.value.length === 0) {
108
- continue;
109
- }
110
-
111
- let lastIndex = 0;
112
- let lastValue = data.featureIds.value[0];
113
-
114
- for (let i = 0; i < data.featureIds.value.length; i++) {
115
- const currValue = data.featureIds.value[i];
116
-
117
- if (currValue === lastValue) {
118
- continue;
119
- }
120
-
121
- features.push(parseFeature(data, lastIndex, i));
122
- lastIndex = i;
123
- lastValue = currValue;
107
+ const features = [];
108
+ for (const data of dataArray) {
109
+ if (data.featureIds.value.length === 0) {
110
+ // eslint-disable-next-line no-continue
111
+ continue;
112
+ }
113
+ let lastIndex = 0;
114
+ let lastValue = data.featureIds.value[0];
115
+ // Need to deduce start, end indices of each feature
116
+ for (let i = 0; i < data.featureIds.value.length; i++) {
117
+ const currValue = data.featureIds.value[i];
118
+ if (currValue === lastValue) {
119
+ // eslint-disable-next-line no-continue
120
+ continue;
121
+ }
122
+ features.push(parseFeature(data, lastIndex, i));
123
+ lastIndex = i;
124
+ lastValue = currValue;
125
+ }
126
+ // Last feature
127
+ features.push(parseFeature(data, lastIndex, data.featureIds.value.length));
124
128
  }
125
-
126
- features.push(parseFeature(data, lastIndex, data.featureIds.value.length));
127
- }
128
-
129
- return features;
129
+ return features;
130
130
  }
131
-
131
+ /** Parse input binary data and return a single GeoJSON Feature */
132
132
  function parseFeature(data, startIndex, endIndex) {
133
- const geometry = binaryToGeometry(data, startIndex, endIndex);
134
- const properties = parseProperties(data, startIndex, endIndex);
135
- const fields = parseFields(data, startIndex, endIndex);
136
- return {
137
- type: 'Feature',
138
- geometry,
139
- properties,
140
- ...fields
141
- };
133
+ const geometry = binaryToGeometry(data, startIndex, endIndex);
134
+ const properties = parseProperties(data, startIndex, endIndex);
135
+ const fields = parseFields(data, startIndex, endIndex);
136
+ return { type: 'Feature', geometry, properties, ...fields };
142
137
  }
143
-
138
+ /** Parse input binary data and return an object of fields */
144
139
  function parseFields(data, startIndex = 0, endIndex) {
145
- return data.fields && data.fields[data.featureIds.value[startIndex]];
140
+ return data.fields && data.fields[data.featureIds.value[startIndex]];
146
141
  }
147
-
142
+ /** Parse input binary data and return an object of properties */
148
143
  function parseProperties(data, startIndex = 0, endIndex) {
149
- const properties = Object.assign({}, data.properties[data.featureIds.value[startIndex]]);
150
-
151
- for (const key in data.numericProps) {
152
- properties[key] = data.numericProps[key].value[startIndex];
153
- }
154
-
155
- return properties;
144
+ const properties = Object.assign({}, data.properties[data.featureIds.value[startIndex]]);
145
+ for (const key in data.numericProps) {
146
+ properties[key] = data.numericProps[key].value[startIndex];
147
+ }
148
+ return properties;
156
149
  }
157
-
150
+ /** Parse binary data of type Polygon */
158
151
  function polygonToGeoJson(data, startIndex = -Infinity, endIndex = Infinity) {
159
- const {
160
- positions
161
- } = data;
162
- const polygonIndices = data.polygonIndices.value.filter(x => x >= startIndex && x <= endIndex);
163
- const primitivePolygonIndices = data.primitivePolygonIndices.value.filter(x => x >= startIndex && x <= endIndex);
164
- const multi = polygonIndices.length > 2;
165
-
166
- if (!multi) {
152
+ const { positions } = data;
153
+ const polygonIndices = data.polygonIndices.value.filter((x) => x >= startIndex && x <= endIndex);
154
+ const primitivePolygonIndices = data.primitivePolygonIndices.value.filter((x) => x >= startIndex && x <= endIndex);
155
+ const multi = polygonIndices.length > 2;
156
+ // Polygon
157
+ if (!multi) {
158
+ const coordinates = [];
159
+ for (let i = 0; i < primitivePolygonIndices.length - 1; i++) {
160
+ const startRingIndex = primitivePolygonIndices[i];
161
+ const endRingIndex = primitivePolygonIndices[i + 1];
162
+ const ringCoordinates = ringToGeoJson(positions, startRingIndex, endRingIndex);
163
+ coordinates.push(ringCoordinates);
164
+ }
165
+ return { type: 'Polygon', coordinates };
166
+ }
167
+ // MultiPolygon
167
168
  const coordinates = [];
168
-
169
- for (let i = 0; i < primitivePolygonIndices.length - 1; i++) {
170
- const startRingIndex = primitivePolygonIndices[i];
171
- const endRingIndex = primitivePolygonIndices[i + 1];
172
- const ringCoordinates = ringToGeoJson(positions, startRingIndex, endRingIndex);
173
- coordinates.push(ringCoordinates);
169
+ for (let i = 0; i < polygonIndices.length - 1; i++) {
170
+ const startPolygonIndex = polygonIndices[i];
171
+ const endPolygonIndex = polygonIndices[i + 1];
172
+ const polygonCoordinates = polygonToGeoJson(data, startPolygonIndex, endPolygonIndex).coordinates;
173
+ coordinates.push(polygonCoordinates);
174
174
  }
175
-
176
- return {
177
- type: 'Polygon',
178
- coordinates
179
- };
180
- }
181
-
182
- const coordinates = [];
183
-
184
- for (let i = 0; i < polygonIndices.length - 1; i++) {
185
- const startPolygonIndex = polygonIndices[i];
186
- const endPolygonIndex = polygonIndices[i + 1];
187
- const polygonCoordinates = polygonToGeoJson(data, startPolygonIndex, endPolygonIndex).coordinates;
188
- coordinates.push(polygonCoordinates);
189
- }
190
-
191
- return {
192
- type: 'MultiPolygon',
193
- coordinates
194
- };
175
+ return { type: 'MultiPolygon', coordinates };
195
176
  }
196
-
177
+ /** Parse binary data of type LineString */
197
178
  function lineStringToGeoJson(data, startIndex = -Infinity, endIndex = Infinity) {
198
- const {
199
- positions
200
- } = data;
201
- const pathIndices = data.pathIndices.value.filter(x => x >= startIndex && x <= endIndex);
202
- const multi = pathIndices.length > 2;
203
-
204
- if (!multi) {
205
- const coordinates = ringToGeoJson(positions, pathIndices[0], pathIndices[1]);
206
- return {
207
- type: 'LineString',
208
- coordinates
209
- };
210
- }
211
-
212
- const coordinates = [];
213
-
214
- for (let i = 0; i < pathIndices.length - 1; i++) {
215
- const ringCoordinates = ringToGeoJson(positions, pathIndices[i], pathIndices[i + 1]);
216
- coordinates.push(ringCoordinates);
217
- }
218
-
219
- return {
220
- type: 'MultiLineString',
221
- coordinates
222
- };
179
+ const { positions } = data;
180
+ const pathIndices = data.pathIndices.value.filter((x) => x >= startIndex && x <= endIndex);
181
+ const multi = pathIndices.length > 2;
182
+ if (!multi) {
183
+ const coordinates = ringToGeoJson(positions, pathIndices[0], pathIndices[1]);
184
+ return { type: 'LineString', coordinates };
185
+ }
186
+ const coordinates = [];
187
+ for (let i = 0; i < pathIndices.length - 1; i++) {
188
+ const ringCoordinates = ringToGeoJson(positions, pathIndices[i], pathIndices[i + 1]);
189
+ coordinates.push(ringCoordinates);
190
+ }
191
+ return { type: 'MultiLineString', coordinates };
223
192
  }
224
-
193
+ /** Parse binary data of type Point */
225
194
  function pointToGeoJson(data, startIndex, endIndex) {
226
- const {
227
- positions
228
- } = data;
229
- const coordinates = ringToGeoJson(positions, startIndex, endIndex);
230
- const multi = coordinates.length > 1;
231
-
232
- if (multi) {
233
- return {
234
- type: 'MultiPoint',
235
- coordinates
236
- };
237
- }
238
-
239
- return {
240
- type: 'Point',
241
- coordinates: coordinates[0]
242
- };
195
+ const { positions } = data;
196
+ const coordinates = ringToGeoJson(positions, startIndex, endIndex);
197
+ const multi = coordinates.length > 1;
198
+ if (multi) {
199
+ return { type: 'MultiPoint', coordinates };
200
+ }
201
+ return { type: 'Point', coordinates: coordinates[0] };
243
202
  }
244
-
203
+ /**
204
+ * Parse a linear ring of positions to a GeoJSON linear ring
205
+ *
206
+ * @param positions Positions TypedArray
207
+ * @param startIndex Start index to include in ring
208
+ * @param endIndex End index to include in ring
209
+ * @returns GeoJSON ring
210
+ */
245
211
  function ringToGeoJson(positions, startIndex, endIndex) {
246
- startIndex = startIndex || 0;
247
- endIndex = endIndex || positions.value.length / positions.size;
248
- const ringCoordinates = [];
249
-
250
- for (let j = startIndex; j < endIndex; j++) {
251
- const coord = Array();
252
-
253
- for (let k = j * positions.size; k < (j + 1) * positions.size; k++) {
254
- coord.push(Number(positions.value[k]));
212
+ startIndex = startIndex || 0;
213
+ endIndex = endIndex || positions.value.length / positions.size;
214
+ const ringCoordinates = [];
215
+ for (let j = startIndex; j < endIndex; j++) {
216
+ const coord = Array();
217
+ for (let k = j * positions.size; k < (j + 1) * positions.size; k++) {
218
+ coord.push(Number(positions.value[k]));
219
+ }
220
+ ringCoordinates.push(coord);
255
221
  }
256
-
257
- ringCoordinates.push(coord);
258
- }
259
-
260
- return ringCoordinates;
222
+ return ringCoordinates;
261
223
  }
262
-
224
+ // Deduce geometry type of data object
263
225
  function parseType(data) {
264
- if (data.pathIndices) {
265
- return 'LineString';
266
- }
267
-
268
- if (data.polygonIndices) {
269
- return 'Polygon';
270
- }
271
-
272
- return 'Point';
226
+ if (data.pathIndices) {
227
+ return 'LineString';
228
+ }
229
+ if (data.polygonIndices) {
230
+ return 'Polygon';
231
+ }
232
+ return 'Point';
273
233
  }
274
- //# sourceMappingURL=binary-to-geojson.js.map
@@ -0,0 +1,8 @@
1
+ import { Feature, GeojsonGeometryInfo } from '@loaders.gl/schema';
2
+ /**
3
+ * Initial scan over GeoJSON features
4
+ * Counts number of coordinates of each geometry type and
5
+ * keeps track of the max coordinate dimensions
6
+ */
7
+ export declare function extractGeometryInfo(features: Feature[]): GeojsonGeometryInfo;
8
+ //# sourceMappingURL=extract-geometry-info.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract-geometry-info.d.ts","sourceRoot":"","sources":["../../src/lib/extract-geometry-info.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAE,mBAAmB,EAAC,MAAM,oBAAoB,CAAC;AAEhE;;;;GAIG;AAEH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,mBAAmB,CA4F5E"}
@@ -0,0 +1,96 @@
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;
@@ -0,0 +1,58 @@
1
+ import type { TypedArray } from '@loaders.gl/schema';
2
+ /**
3
+ * Permissable constructor for numeric props
4
+ */
5
+ export type PropArrayConstructor = Float32ArrayConstructor | Float64ArrayConstructor | ArrayConstructor;
6
+ /**
7
+ * Collection type for holding intermediate binary data before conversion to `BinaryPointGeometry`
8
+ */
9
+ export type Points = {
10
+ type: 'Point';
11
+ positions: Float32Array | Float64Array;
12
+ globalFeatureIds: Uint16Array | Uint32Array;
13
+ featureIds: Uint16Array | Uint32Array;
14
+ numericProps: {
15
+ [key: string]: TypedArray;
16
+ };
17
+ properties: {}[];
18
+ fields: {
19
+ id?: string | number;
20
+ }[];
21
+ };
22
+ /**
23
+ * Collection type for holding intermediate binary data before conversion to `BinaryLineStringGeometry`
24
+ */
25
+ export type Lines = {
26
+ type: 'LineString';
27
+ positions: Float32Array | Float64Array;
28
+ pathIndices: Uint16Array | Uint32Array;
29
+ globalFeatureIds: Uint16Array | Uint32Array;
30
+ featureIds: Uint16Array | Uint32Array;
31
+ numericProps: {
32
+ [key: string]: TypedArray;
33
+ };
34
+ properties: {}[];
35
+ fields: {
36
+ id?: string | number;
37
+ }[];
38
+ };
39
+ /**
40
+ * Collection type for holding intermediate binary data before conversion to `BinaryPolygonGeometry`
41
+ */
42
+ export type Polygons = {
43
+ type: 'Polygon';
44
+ positions: Float32Array | Float64Array;
45
+ polygonIndices: Uint16Array | Uint32Array;
46
+ primitivePolygonIndices: Uint16Array | Uint32Array;
47
+ triangles: number[];
48
+ globalFeatureIds: Uint16Array | Uint32Array;
49
+ featureIds: Uint16Array | Uint32Array;
50
+ numericProps: {
51
+ [key: string]: TypedArray;
52
+ };
53
+ properties: {}[];
54
+ fields: {
55
+ id?: string | number;
56
+ }[];
57
+ };
58
+ //# sourceMappingURL=flat-geojson-to-binary-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flat-geojson-to-binary-types.d.ts","sourceRoot":"","sources":["../../src/lib/flat-geojson-to-binary-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,oBAAoB,CAAC;AAEnD;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAC5B,uBAAuB,GACvB,uBAAuB,GACvB,gBAAgB,CAAC;AAErB;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,OAAO,CAAC;IACd,SAAS,EAAE,YAAY,GAAG,YAAY,CAAC;IACvC,gBAAgB,EAAE,WAAW,GAAG,WAAW,CAAC;IAC5C,UAAU,EAAE,WAAW,GAAG,WAAW,CAAC;IACtC,YAAY,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAA;KAAC,CAAC;IAC1C,UAAU,EAAE,EAAE,EAAE,CAAC;IACjB,MAAM,EAAE;QACN,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;KACtB,EAAE,CAAC;CACL,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,KAAK,GAAG;IAClB,IAAI,EAAE,YAAY,CAAC;IACnB,SAAS,EAAE,YAAY,GAAG,YAAY,CAAC;IACvC,WAAW,EAAE,WAAW,GAAG,WAAW,CAAC;IACvC,gBAAgB,EAAE,WAAW,GAAG,WAAW,CAAC;IAC5C,UAAU,EAAE,WAAW,GAAG,WAAW,CAAC;IACtC,YAAY,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAA;KAAC,CAAC;IAC1C,UAAU,EAAE,EAAE,EAAE,CAAC;IACjB,MAAM,EAAE;QACN,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;KACtB,EAAE,CAAC;CACL,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,SAAS,CAAC;IAChB,SAAS,EAAE,YAAY,GAAG,YAAY,CAAC;IACvC,cAAc,EAAE,WAAW,GAAG,WAAW,CAAC;IAC1C,uBAAuB,EAAE,WAAW,GAAG,WAAW,CAAC;IACnD,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,gBAAgB,EAAE,WAAW,GAAG,WAAW,CAAC;IAC5C,UAAU,EAAE,WAAW,GAAG,WAAW,CAAC;IACtC,YAAY,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAAA;KAAC,CAAC;IAC1C,UAAU,EAAE,EAAE,EAAE,CAAC;IACjB,MAAM,EAAE;QACN,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;KACtB,EAAE,CAAC;CACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });