@loaders.gl/gis 4.0.0-alpha.22 → 4.0.0-alpha.24

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 (41) 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.js +1 -1
  6. package/dist/es5/lib/flat-geojson-to-binary.js.map +1 -1
  7. package/dist/es5/lib/geojson-to-binary.js.map +1 -1
  8. package/dist/es5/lib/transform.js.map +1 -1
  9. package/dist/esm/index.js +1 -1
  10. package/dist/esm/index.js.map +1 -1
  11. package/dist/esm/lib/binary-to-geojson.js +0 -25
  12. package/dist/esm/lib/binary-to-geojson.js.map +1 -1
  13. package/dist/esm/lib/flat-geojson-to-binary.js +1 -1
  14. package/dist/esm/lib/flat-geojson-to-binary.js.map +1 -1
  15. package/dist/esm/lib/geojson-to-binary.js.map +1 -1
  16. package/dist/esm/lib/transform.js.map +1 -1
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/lib/binary-to-geojson.d.ts +2 -4
  20. package/dist/lib/binary-to-geojson.d.ts.map +1 -1
  21. package/dist/lib/flat-geojson-to-binary.d.ts +2 -2
  22. package/dist/lib/flat-geojson-to-binary.d.ts.map +1 -1
  23. package/dist/lib/geojson-to-binary.d.ts +2 -2
  24. package/dist/lib/geojson-to-binary.d.ts.map +1 -1
  25. package/dist/lib/transform.d.ts +2 -2
  26. package/dist/lib/transform.d.ts.map +1 -1
  27. package/package.json +4 -4
  28. package/src/index.ts +1 -1
  29. package/src/lib/binary-to-geojson.ts +25 -43
  30. package/src/lib/flat-geojson-to-binary.ts +6 -6
  31. package/src/lib/geojson-to-binary.ts +2 -2
  32. package/src/lib/transform.ts +3 -3
  33. package/dist/bundle.js +0 -5
  34. package/dist/index.js +0 -18
  35. package/dist/lib/binary-to-geojson.js +0 -233
  36. package/dist/lib/extract-geometry-info.js +0 -96
  37. package/dist/lib/flat-geojson-to-binary-types.js +0 -2
  38. package/dist/lib/flat-geojson-to-binary.js +0 -385
  39. package/dist/lib/geojson-to-binary.js +0 -25
  40. package/dist/lib/geojson-to-flat-geojson.js +0 -128
  41. package/dist/lib/transform.js +0 -59
@@ -2,8 +2,8 @@
2
2
  import {earcut} from '@math.gl/polygon';
3
3
  import type {
4
4
  BinaryAttribute,
5
- BinaryFeatures,
6
- BinaryPolygonFeatures,
5
+ BinaryFeatureCollection,
6
+ BinaryPolygonFeature,
7
7
  FlatFeature,
8
8
  FlatPoint,
9
9
  FlatLineString,
@@ -485,8 +485,8 @@ function makeAccessorObjects(
485
485
  lines: Lines,
486
486
  polygons: Polygons,
487
487
  coordLength: number
488
- ): BinaryFeatures {
489
- const binaryFeatures = {
488
+ ): BinaryFeatureCollection {
489
+ const binaryFeatures: BinaryFeatureCollection = {
490
490
  points: {
491
491
  ...points,
492
492
  positions: {value: points.positions, size: coordLength},
@@ -510,10 +510,10 @@ function makeAccessorObjects(
510
510
  globalFeatureIds: {value: polygons.globalFeatureIds, size: 1},
511
511
  featureIds: {value: polygons.featureIds, size: 1},
512
512
  numericProps: wrapProps(polygons.numericProps, 1)
513
- } as BinaryPolygonFeatures
513
+ } as BinaryPolygonFeature // triangles not expected
514
514
  };
515
515
 
516
- if (polygons.triangles) {
516
+ if (binaryFeatures.polygons && polygons.triangles) {
517
517
  binaryFeatures.polygons.triangles = {value: new Uint32Array(polygons.triangles), size: 1};
518
518
  }
519
519
 
@@ -1,5 +1,5 @@
1
1
  import type {Feature} from '@loaders.gl/schema';
2
- import type {BinaryFeatures} from '@loaders.gl/schema';
2
+ import type {BinaryFeatureCollection} from '@loaders.gl/schema';
3
3
 
4
4
  import {extractGeometryInfo} from './extract-geometry-info';
5
5
  import {geojsonToFlatGeojson} from './geojson-to-flat-geojson';
@@ -25,7 +25,7 @@ export type GeojsonToBinaryOptions = {
25
25
  export function geojsonToBinary(
26
26
  features: Feature[],
27
27
  options: GeojsonToBinaryOptions = {fixRingWinding: true, triangulate: true}
28
- ): BinaryFeatures {
28
+ ): BinaryFeatureCollection {
29
29
  const geometryInfo = extractGeometryInfo(features);
30
30
  const coordLength = geometryInfo.coordLength;
31
31
  const {fixRingWinding} = options;
@@ -1,4 +1,4 @@
1
- import type {BinaryFeatures, BinaryGeometry, Feature} from '@loaders.gl/schema';
1
+ import type {BinaryFeatureCollection, BinaryGeometry, Feature} from '@loaders.gl/schema';
2
2
 
3
3
  type TransformCoordinate = (coord: number[]) => number[];
4
4
 
@@ -9,9 +9,9 @@ type TransformCoordinate = (coord: number[]) => number[];
9
9
  * @return Transformed binary features
10
10
  */
11
11
  export function transformBinaryCoords(
12
- binaryFeatures: BinaryFeatures,
12
+ binaryFeatures: BinaryFeatureCollection,
13
13
  transformCoordinate: TransformCoordinate
14
- ): BinaryFeatures {
14
+ ): BinaryFeatureCollection {
15
15
  if (binaryFeatures.points) {
16
16
  transformBinaryGeometryPositions(binaryFeatures.points, transformCoordinate);
17
17
  }
package/dist/bundle.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- // @ts-nocheck
3
- const moduleExports = require('./index');
4
- globalThis.loaders = globalThis.loaders || {};
5
- module.exports = Object.assign(globalThis.loaders, moduleExports);
package/dist/index.js DELETED
@@ -1,18 +0,0 @@
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,233 +0,0 @@
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);
18
- }
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
- }
30
- }
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
- */
37
- 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
- }
58
- }
59
- throw new Error(`featureId:${globalFeatureId} not found`);
60
- }
61
- function parseFeatures(data, type) {
62
- const dataArray = normalizeInput(data, type);
63
- return parseFeatureCollection(dataArray);
64
- }
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
- }
78
- }
79
- exports.binaryToGeometry = binaryToGeometry;
80
- // Normalize features
81
- // Return an array of data objects, each of which have a type key
82
- 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;
104
- }
105
- /** Parse input binary data and return an array of GeoJSON Features */
106
- 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));
128
- }
129
- return features;
130
- }
131
- /** Parse input binary data and return a single GeoJSON Feature */
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 };
137
- }
138
- /** Parse input binary data and return an object of fields */
139
- function parseFields(data, startIndex = 0, endIndex) {
140
- return data.fields && data.fields[data.featureIds.value[startIndex]];
141
- }
142
- /** Parse input binary data and return an object of properties */
143
- 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
- }
150
- /** Parse binary data of type Polygon */
151
- 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
168
- 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);
174
- }
175
- return { type: 'MultiPolygon', coordinates };
176
- }
177
- /** Parse binary data of type LineString */
178
- 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 };
192
- }
193
- /** Parse binary data of type Point */
194
- 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] };
202
- }
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
- */
211
- 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);
221
- }
222
- return ringCoordinates;
223
- }
224
- // Deduce geometry type of data object
225
- function parseType(data) {
226
- if (data.pathIndices) {
227
- return 'LineString';
228
- }
229
- if (data.polygonIndices) {
230
- return 'Polygon';
231
- }
232
- return 'Point';
233
- }
@@ -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 });