@loaders.gl/gis 3.1.3 → 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 (54) hide show
  1. package/dist/bundle.js +2 -2
  2. package/dist/bundle.js.map +1 -0
  3. package/dist/index.js +6 -18
  4. package/dist/index.js.map +1 -0
  5. package/dist/lib/binary-to-geojson.js +242 -201
  6. package/dist/lib/binary-to-geojson.js.map +1 -0
  7. package/dist/lib/extract-geometry-info.js +102 -93
  8. package/dist/lib/extract-geometry-info.js.map +1 -0
  9. package/dist/lib/flat-geojson-to-binary-types.js +2 -2
  10. package/dist/{es5/lib → lib}/flat-geojson-to-binary-types.js.map +0 -0
  11. package/dist/lib/flat-geojson-to-binary.js +309 -345
  12. package/dist/lib/flat-geojson-to-binary.js.map +1 -0
  13. package/dist/lib/geojson-to-binary.js +20 -23
  14. package/dist/lib/geojson-to-binary.js.map +1 -0
  15. package/dist/lib/geojson-to-flat-geojson.js +104 -116
  16. package/dist/lib/geojson-to-flat-geojson.js.map +1 -0
  17. package/dist/lib/transform.js +42 -51
  18. package/dist/lib/transform.js.map +1 -0
  19. package/package.json +6 -6
  20. package/dist/es5/bundle.js +0 -7
  21. package/dist/es5/bundle.js.map +0 -1
  22. package/dist/es5/index.js +0 -64
  23. package/dist/es5/index.js.map +0 -1
  24. package/dist/es5/lib/binary-to-geojson.js +0 -330
  25. package/dist/es5/lib/binary-to-geojson.js.map +0 -1
  26. package/dist/es5/lib/extract-geometry-info.js +0 -204
  27. package/dist/es5/lib/extract-geometry-info.js.map +0 -1
  28. package/dist/es5/lib/flat-geojson-to-binary-types.js +0 -2
  29. package/dist/es5/lib/flat-geojson-to-binary.js +0 -397
  30. package/dist/es5/lib/flat-geojson-to-binary.js.map +0 -1
  31. package/dist/es5/lib/geojson-to-binary.js +0 -30
  32. package/dist/es5/lib/geojson-to-binary.js.map +0 -1
  33. package/dist/es5/lib/geojson-to-flat-geojson.js +0 -169
  34. package/dist/es5/lib/geojson-to-flat-geojson.js.map +0 -1
  35. package/dist/es5/lib/transform.js +0 -73
  36. package/dist/es5/lib/transform.js.map +0 -1
  37. package/dist/esm/bundle.js +0 -5
  38. package/dist/esm/bundle.js.map +0 -1
  39. package/dist/esm/index.js +0 -6
  40. package/dist/esm/index.js.map +0 -1
  41. package/dist/esm/lib/binary-to-geojson.js +0 -274
  42. package/dist/esm/lib/binary-to-geojson.js.map +0 -1
  43. package/dist/esm/lib/extract-geometry-info.js +0 -105
  44. package/dist/esm/lib/extract-geometry-info.js.map +0 -1
  45. package/dist/esm/lib/flat-geojson-to-binary-types.js +0 -2
  46. package/dist/esm/lib/flat-geojson-to-binary-types.js.map +0 -1
  47. package/dist/esm/lib/flat-geojson-to-binary.js +0 -339
  48. package/dist/esm/lib/flat-geojson-to-binary.js.map +0 -1
  49. package/dist/esm/lib/geojson-to-binary.js +0 -21
  50. package/dist/esm/lib/geojson-to-binary.js.map +0 -1
  51. package/dist/esm/lib/geojson-to-flat-geojson.js +0 -116
  52. package/dist/esm/lib/geojson-to-flat-geojson.js.map +0 -1
  53. package/dist/esm/lib/transform.js +0 -50
  54. package/dist/esm/lib/transform.js.map +0 -1
package/dist/bundle.js CHANGED
@@ -1,5 +1,5 @@
1
- "use strict";
2
- // @ts-nocheck
3
1
  const moduleExports = require('./index');
2
+
4
3
  globalThis.loaders = globalThis.loaders || {};
5
4
  module.exports = Object.assign(globalThis.loaders, moduleExports);
5
+ //# sourceMappingURL=bundle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/bundle.ts"],"names":["moduleExports","require","globalThis","loaders","module","exports","Object","assign"],"mappings":"AACA,MAAMA,aAAa,GAAGC,OAAO,CAAC,SAAD,CAA7B;;AACAC,UAAU,CAACC,OAAX,GAAqBD,UAAU,CAACC,OAAX,IAAsB,EAA3C;AACAC,MAAM,CAACC,OAAP,GAAiBC,MAAM,CAACC,MAAP,CAAcL,UAAU,CAACC,OAAzB,EAAkCH,aAAlC,CAAjB","sourcesContent":["// @ts-nocheck\nconst moduleExports = require('./index');\nglobalThis.loaders = globalThis.loaders || {};\nmodule.exports = Object.assign(globalThis.loaders, moduleExports);\n"],"file":"bundle.js"}
package/dist/index.js CHANGED
@@ -1,18 +1,6 @@
1
- "use strict";
2
- // Types from `@loaders.gl/schema`
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.transformGeoJsonCoords = exports.transformBinaryCoords = exports.binaryToGeometry = exports.binaryToGeoJson = exports.binaryToGeojson = exports.geojsonToFlatGeojson = exports.geojsonToBinary = exports.flatGeojsonToBinary = void 0;
5
- // Functions
6
- var flat_geojson_to_binary_1 = require("./lib/flat-geojson-to-binary");
7
- Object.defineProperty(exports, "flatGeojsonToBinary", { enumerable: true, get: function () { return flat_geojson_to_binary_1.flatGeojsonToBinary; } });
8
- var geojson_to_binary_1 = require("./lib/geojson-to-binary");
9
- Object.defineProperty(exports, "geojsonToBinary", { enumerable: true, get: function () { return geojson_to_binary_1.geojsonToBinary; } });
10
- var geojson_to_flat_geojson_1 = require("./lib/geojson-to-flat-geojson");
11
- Object.defineProperty(exports, "geojsonToFlatGeojson", { enumerable: true, get: function () { return geojson_to_flat_geojson_1.geojsonToFlatGeojson; } });
12
- var binary_to_geojson_1 = require("./lib/binary-to-geojson");
13
- Object.defineProperty(exports, "binaryToGeojson", { enumerable: true, get: function () { return binary_to_geojson_1.binaryToGeojson; } });
14
- Object.defineProperty(exports, "binaryToGeoJson", { enumerable: true, get: function () { return binary_to_geojson_1.binaryToGeoJson; } });
15
- Object.defineProperty(exports, "binaryToGeometry", { enumerable: true, get: function () { return binary_to_geojson_1.binaryToGeometry; } });
16
- var transform_1 = require("./lib/transform");
17
- Object.defineProperty(exports, "transformBinaryCoords", { enumerable: true, get: function () { return transform_1.transformBinaryCoords; } });
18
- Object.defineProperty(exports, "transformGeoJsonCoords", { enumerable: true, get: function () { return transform_1.transformGeoJsonCoords; } });
1
+ export { flatGeojsonToBinary } from './lib/flat-geojson-to-binary';
2
+ export { geojsonToBinary } from './lib/geojson-to-binary';
3
+ export { geojsonToFlatGeojson } from './lib/geojson-to-flat-geojson';
4
+ export { binaryToGeojson, binaryToGeoJson, binaryToGeometry } from './lib/binary-to-geojson';
5
+ export { transformBinaryCoords, transformGeoJsonCoords } from './lib/transform';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["flatGeojsonToBinary","geojsonToBinary","geojsonToFlatGeojson","binaryToGeojson","binaryToGeoJson","binaryToGeometry","transformBinaryCoords","transformGeoJsonCoords"],"mappings":"AAGA,SAAQA,mBAAR,QAAkC,8BAAlC;AACA,SAAQC,eAAR,QAA8B,yBAA9B;AACA,SAAQC,oBAAR,QAAmC,+BAAnC;AACA,SAAQC,eAAR,EAAyBC,eAAzB,EAA0CC,gBAA1C,QAAiE,yBAAjE;AACA,SAAQC,qBAAR,EAA+BC,sBAA/B,QAA4D,iBAA5D","sourcesContent":["// Types from `@loaders.gl/schema`\n\n// Functions\nexport {flatGeojsonToBinary} from './lib/flat-geojson-to-binary';\nexport {geojsonToBinary} from './lib/geojson-to-binary';\nexport {geojsonToFlatGeojson} from './lib/geojson-to-flat-geojson';\nexport {binaryToGeojson, binaryToGeoJson, binaryToGeometry} from './lib/binary-to-geojson';\nexport {transformBinaryCoords, transformGeoJsonCoords} from './lib/transform';\n"],"file":"index.js"}
@@ -1,233 +1,274 @@
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);
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);
18
9
  }
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
- }
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
+ }
30
21
  }
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
- */
22
+
37
23
  function getSingleFeature(data, globalFeatureId) {
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
- }
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);
58
47
  }
59
- throw new Error(`featureId:${globalFeatureId} not found`);
48
+ }
49
+
50
+ throw new Error("featureId:".concat(globalFeatureId, " not found"));
60
51
  }
52
+
61
53
  function parseFeatures(data, type) {
62
- const dataArray = normalizeInput(data, type);
63
- return parseFeatureCollection(dataArray);
54
+ const dataArray = normalizeInput(data, type);
55
+ return parseFeatureCollection(dataArray);
64
56
  }
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
- }
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
+ }
78
73
  }
79
- exports.binaryToGeometry = binaryToGeometry;
80
- // Normalize features
81
- // Return an array of data objects, each of which have a type key
74
+
82
75
  function normalizeInput(data, type) {
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;
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;
104
101
  }
105
- /** Parse input binary data and return an array of GeoJSON Features */
102
+
106
103
  function parseFeatureCollection(dataArray) {
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));
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;
128
124
  }
129
- return features;
125
+
126
+ features.push(parseFeature(data, lastIndex, data.featureIds.value.length));
127
+ }
128
+
129
+ return features;
130
130
  }
131
- /** Parse input binary data and return a single GeoJSON Feature */
131
+
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 { type: 'Feature', geometry, properties, ...fields };
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
+ };
137
142
  }
138
- /** Parse input binary data and return an object of fields */
143
+
139
144
  function parseFields(data, startIndex = 0, endIndex) {
140
- return data.fields && data.fields[data.featureIds.value[startIndex]];
145
+ return data.fields && data.fields[data.featureIds.value[startIndex]];
141
146
  }
142
- /** Parse input binary data and return an object of properties */
147
+
143
148
  function parseProperties(data, startIndex = 0, endIndex) {
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;
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;
149
156
  }
150
- /** Parse binary data of type Polygon */
157
+
151
158
  function polygonToGeoJson(data, startIndex = -Infinity, endIndex = Infinity) {
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
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) {
168
167
  const coordinates = [];
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);
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);
174
174
  }
175
- return { type: 'MultiPolygon', coordinates };
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
+ };
176
195
  }
177
- /** Parse binary data of type LineString */
196
+
178
197
  function lineStringToGeoJson(data, startIndex = -Infinity, endIndex = Infinity) {
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 };
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
+ };
192
223
  }
193
- /** Parse binary data of type Point */
224
+
194
225
  function pointToGeoJson(data, startIndex, endIndex) {
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] };
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
+ };
202
243
  }
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
- */
244
+
211
245
  function ringToGeoJson(positions, startIndex, endIndex) {
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);
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]));
221
255
  }
222
- return ringCoordinates;
256
+
257
+ ringCoordinates.push(coord);
258
+ }
259
+
260
+ return ringCoordinates;
223
261
  }
224
- // Deduce geometry type of data object
262
+
225
263
  function parseType(data) {
226
- if (data.pathIndices) {
227
- return 'LineString';
228
- }
229
- if (data.polygonIndices) {
230
- return 'Polygon';
231
- }
232
- return 'Point';
264
+ if (data.pathIndices) {
265
+ return 'LineString';
266
+ }
267
+
268
+ if (data.polygonIndices) {
269
+ return 'Polygon';
270
+ }
271
+
272
+ return 'Point';
233
273
  }
274
+ //# sourceMappingURL=binary-to-geojson.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/binary-to-geojson.ts"],"names":["binaryToGeojson","data","options","globalFeatureId","undefined","getSingleFeature","parseFeatures","type","binaryToGeoJson","format","binaryToGeometry","Error","dataArray","normalizeInput","lastIndex","lastValue","featureIds","value","i","length","currValue","globalFeatureIds","parseFeature","parseFeatureCollection","startIndex","endIndex","pointToGeoJson","lineStringToGeoJson","polygonToGeoJson","unexpectedInput","isHeterogeneousType","Boolean","points","lines","polygons","parseType","features","push","geometry","properties","parseProperties","fields","parseFields","Object","assign","key","numericProps","Infinity","positions","polygonIndices","filter","x","primitivePolygonIndices","multi","coordinates","startRingIndex","endRingIndex","ringCoordinates","ringToGeoJson","startPolygonIndex","endPolygonIndex","polygonCoordinates","pathIndices","size","j","coord","Array","k","Number"],"mappings":"AAmCA,OAAO,SAASA,eAAT,CACLC,IADK,EAELC,OAFK,EAGgB;AACrB,QAAMC,eAAe,GAAGD,OAAH,aAAGA,OAAH,uBAAGA,OAAO,CAAEC,eAAjC;;AACA,MAAIA,eAAe,KAAKC,SAAxB,EAAmC;AACjC,WAAOC,gBAAgB,CAACJ,IAAD,EAAOE,eAAP,CAAvB;AACD;;AACD,SAAOG,aAAa,CAACL,IAAD,EAAOC,OAAP,aAAOA,OAAP,uBAAOA,OAAO,CAAEK,IAAhB,CAApB;AACD;AAGD,OAAO,SAASC,eAAT,CACLP,IADK,EAELM,IAFK,EAGLE,MAA8B,GAAG,SAH5B,EAIiB;AACtB,UAAQA,MAAR;AACE,SAAK,SAAL;AACE,aAAOH,aAAa,CAACL,IAAD,EAAyBM,IAAzB,CAApB;;AACF,SAAK,UAAL;AACE,aAAOG,gBAAgB,CAACT,IAAD,CAAvB;;AACF;AACE,YAAM,IAAIU,KAAJ,CAAUF,MAAV,CAAN;AANJ;AAQD;;AAOD,SAASJ,gBAAT,CAA0BJ,IAA1B,EAAgDE,eAAhD,EAAkF;AAChF,QAAMS,SAAS,GAAGC,cAAc,CAACZ,IAAD,CAAhC;;A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B;AACpB,WAAO,YAAP;AACD;;AAED,MAAI7D,IAAI,CAACgD,cAAT,EAAyB;AACvB,WAAO,SAAP;AACD;;AAED,SAAO,OAAP;AACD","sourcesContent":["import type {\n BinaryGeometry,\n BinaryFeatures,\n BinaryGeometryType,\n BinaryPointFeatures,\n BinaryLineFeatures,\n BinaryPolygonFeatures,\n BinaryAttribute\n} from '@loaders.gl/schema';\nimport type {Feature, Geometry, Position, GeoJsonProperties} from '@loaders.gl/schema';\nimport type {\n Point,\n MultiPoint,\n LineString,\n MultiLineString,\n Polygon,\n MultiPolygon\n} from '@loaders.gl/schema';\n\n// Note:L We do not handle GeometryCollection, define a limited Geometry type that always has coordinates.\n// type FeatureGeometry = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon;\n\ntype BinaryToGeoJsonOptions = {\n type?: BinaryGeometryType;\n globalFeatureId?: number;\n};\n\n/**\n * Convert binary geometry representation to GeoJSON\n * @param data geometry data in binary representation\n * @param options\n * @param options.type Input data type: Point, LineString, or Polygon\n * @param options.featureId Global feature id. If specified, only a single feature is extracted\n * @return GeoJSON objects\n */\nexport function binaryToGeojson(\n data: BinaryFeatures,\n options?: BinaryToGeoJsonOptions\n): Feature[] | Feature {\n const globalFeatureId = options?.globalFeatureId;\n if (globalFeatureId !== undefined) {\n return getSingleFeature(data, globalFeatureId);\n }\n return parseFeatures(data, options?.type);\n}\n\n/** @deprecated use `binaryToGeojson` or `binaryToGeometry` instead */\nexport function binaryToGeoJson(\n data: BinaryGeometry | BinaryFeatures,\n type?: BinaryGeometryType,\n format: 'feature' | 'geometry' = 'feature'\n): Geometry | Feature[] {\n switch (format) {\n case 'feature':\n return parseFeatures(data as BinaryFeatures, type);\n case 'geometry':\n return binaryToGeometry(data as BinaryGeometry);\n default:\n throw new Error(format);\n }\n}\n\n/**\n * Return a single feature from a binary geometry representation as GeoJSON\n * @param data geometry data in binary representation\n * @return GeoJSON feature\n */\nfunction getSingleFeature(data: BinaryFeatures, globalFeatureId: number): Feature {\n const dataArray = normalizeInput(data);\n for (const data of dataArray) {\n let lastIndex = 0;\n let lastValue = data.featureIds.value[0];\n\n // Scan through data until we find matching feature\n for (let i = 0; i < data.featureIds.value.length; i++) {\n const currValue = data.featureIds.value[i];\n if (currValue === lastValue) {\n // eslint-disable-next-line no-continue\n continue;\n }\n if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {\n return parseFeature(data, lastIndex, i);\n }\n lastIndex = i;\n lastValue = currValue;\n }\n\n if (globalFeatureId === data.globalFeatureIds.value[lastIndex]) {\n return parseFeature(data, lastIndex, data.featureIds.value.length);\n }\n }\n\n throw new Error(`featureId:${globalFeatureId} not found`);\n}\n\nfunction parseFeatures(data: BinaryFeatures, type?: BinaryGeometryType): Feature[] {\n const dataArray = normalizeInput(data, type);\n return parseFeatureCollection(dataArray);\n}\n\n/** Parse input binary data and return a valid GeoJSON geometry object */\nexport function binaryToGeometry(\n data: BinaryGeometry,\n startIndex?: number,\n endIndex?: number\n): Geometry {\n switch (data.type) {\n case 'Point':\n return pointToGeoJson(data, startIndex, endIndex);\n case 'LineString':\n return lineStringToGeoJson(data, startIndex, endIndex);\n case 'Polygon':\n return polygonToGeoJson(data, startIndex, endIndex);\n default:\n const unexpectedInput: never = data;\n throw new Error(`Unsupported geometry type: ${(unexpectedInput as any)?.type}`);\n }\n}\n\ntype BinaryFeature = BinaryPointFeatures | BinaryLineFeatures | BinaryPolygonFeatures;\ntype BinaryFeaturesArray = BinaryFeature[];\n\n// Normalize features\n// Return an array of data objects, each of which have a type key\nfunction normalizeInput(data: BinaryFeatures, type?: BinaryGeometryType): BinaryFeaturesArray {\n const isHeterogeneousType = Boolean(data.points || data.lines || data.polygons);\n\n if (!isHeterogeneousType) {\n // @ts-expect-error This is a legacy check which allowed `data` to be an instance of the values\n // here. Aka the new data.points, data.lines, or data.polygons.\n data.type = type || parseType(data);\n return [data] as BinaryFeaturesArray;\n }\n\n const features: BinaryFeaturesArray = [];\n if (data.points) {\n data.points.type = 'Point';\n features.push(data.points);\n }\n if (data.lines) {\n data.lines.type = 'LineString';\n features.push(data.lines);\n }\n if (data.polygons) {\n data.polygons.type = 'Polygon';\n features.push(data.polygons);\n }\n\n return features;\n}\n\n/** Parse input binary data and return an array of GeoJSON Features */\nfunction parseFeatureCollection(dataArray): Feature[] {\n const features: Feature[] = [];\n for (const data of dataArray) {\n if (data.featureIds.value.length === 0) {\n // eslint-disable-next-line no-continue\n continue;\n }\n let lastIndex = 0;\n let lastValue = data.featureIds.value[0];\n\n // Need to deduce start, end indices of each feature\n for (let i = 0; i < data.featureIds.value.length; i++) {\n const currValue = data.featureIds.value[i];\n if (currValue === lastValue) {\n // eslint-disable-next-line no-continue\n continue;\n }\n\n features.push(parseFeature(data, lastIndex, i));\n lastIndex = i;\n lastValue = currValue;\n }\n\n // Last feature\n features.push(parseFeature(data, lastIndex, data.featureIds.value.length));\n }\n return features;\n}\n\n/** Parse input binary data and return a single GeoJSON Feature */\nfunction parseFeature(data, startIndex?: number, endIndex?: number): Feature {\n const geometry = binaryToGeometry(data, startIndex, endIndex);\n const properties = parseProperties(data, startIndex, endIndex);\n const fields = parseFields(data, startIndex, endIndex);\n return {type: 'Feature', geometry, properties, ...fields};\n}\n\n/** Parse input binary data and return an object of fields */\nfunction parseFields(data, startIndex: number = 0, endIndex?: number): GeoJsonProperties {\n return data.fields && data.fields[data.featureIds.value[startIndex]];\n}\n\n/** Parse input binary data and return an object of properties */\nfunction parseProperties(data, startIndex: number = 0, endIndex?: number): GeoJsonProperties {\n const properties = Object.assign({}, data.properties[data.featureIds.value[startIndex]]);\n for (const key in data.numericProps) {\n properties[key] = data.numericProps[key].value[startIndex];\n }\n return properties;\n}\n\n/** Parse binary data of type Polygon */\nfunction polygonToGeoJson(\n data,\n startIndex: number = -Infinity,\n endIndex: number = Infinity\n): Polygon | MultiPolygon {\n const {positions} = data;\n const polygonIndices = data.polygonIndices.value.filter((x) => x >= startIndex && x <= endIndex);\n const primitivePolygonIndices = data.primitivePolygonIndices.value.filter(\n (x) => x >= startIndex && x <= endIndex\n );\n const multi = polygonIndices.length > 2;\n\n // Polygon\n if (!multi) {\n const coordinates: Position[][] = [];\n for (let i = 0; i < primitivePolygonIndices.length - 1; i++) {\n const startRingIndex = primitivePolygonIndices[i];\n const endRingIndex = primitivePolygonIndices[i + 1];\n const ringCoordinates = ringToGeoJson(positions, startRingIndex, endRingIndex);\n coordinates.push(ringCoordinates);\n }\n\n return {type: 'Polygon', coordinates};\n }\n\n // MultiPolygon\n const coordinates: Position[][][] = [];\n for (let i = 0; i < polygonIndices.length - 1; i++) {\n const startPolygonIndex = polygonIndices[i];\n const endPolygonIndex = polygonIndices[i + 1];\n const polygonCoordinates = polygonToGeoJson(\n data,\n startPolygonIndex,\n endPolygonIndex\n ).coordinates;\n coordinates.push(polygonCoordinates as Position[][]);\n }\n\n return {type: 'MultiPolygon', coordinates};\n}\n\n/** Parse binary data of type LineString */\nfunction lineStringToGeoJson(\n data,\n startIndex: number = -Infinity,\n endIndex: number = Infinity\n): LineString | MultiLineString {\n const {positions} = data;\n const pathIndices = data.pathIndices.value.filter((x) => x >= startIndex && x <= endIndex);\n const multi = pathIndices.length > 2;\n\n if (!multi) {\n const coordinates = ringToGeoJson(positions, pathIndices[0], pathIndices[1]);\n return {type: 'LineString', coordinates};\n }\n\n const coordinates: Position[][] = [];\n for (let i = 0; i < pathIndices.length - 1; i++) {\n const ringCoordinates = ringToGeoJson(positions, pathIndices[i], pathIndices[i + 1]);\n coordinates.push(ringCoordinates);\n }\n\n return {type: 'MultiLineString', coordinates};\n}\n\n/** Parse binary data of type Point */\nfunction pointToGeoJson(data, startIndex, endIndex): Point | MultiPoint {\n const {positions} = data;\n const coordinates = ringToGeoJson(positions, startIndex, endIndex);\n const multi = coordinates.length > 1;\n\n if (multi) {\n return {type: 'MultiPoint', coordinates};\n }\n\n return {type: 'Point', coordinates: coordinates[0]};\n}\n\n/**\n * Parse a linear ring of positions to a GeoJSON linear ring\n *\n * @param positions Positions TypedArray\n * @param startIndex Start index to include in ring\n * @param endIndex End index to include in ring\n * @returns GeoJSON ring\n */\nfunction ringToGeoJson(\n positions: BinaryAttribute,\n startIndex?: number,\n endIndex?: number\n): Position[] {\n startIndex = startIndex || 0;\n endIndex = endIndex || positions.value.length / positions.size;\n\n const ringCoordinates: Position[] = [];\n for (let j = startIndex; j < endIndex; j++) {\n const coord = Array<number>();\n for (let k = j * positions.size; k < (j + 1) * positions.size; k++) {\n coord.push(Number(positions.value[k]));\n }\n ringCoordinates.push(coord);\n }\n return ringCoordinates;\n}\n\n// Deduce geometry type of data object\nfunction parseType(data) {\n if (data.pathIndices) {\n return 'LineString';\n }\n\n if (data.polygonIndices) {\n return 'Polygon';\n }\n\n return 'Point';\n}\n"],"file":"binary-to-geojson.js"}