@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
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@loaders.gl/gis",
3
3
  "description": "Helpers for GIS category data",
4
- "version": "4.0.0-alpha.9",
4
+ "version": "4.0.0-beta.2",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -24,8 +24,8 @@
24
24
  "README.md"
25
25
  ],
26
26
  "dependencies": {
27
- "@loaders.gl/loader-utils": "4.0.0-alpha.9",
28
- "@loaders.gl/schema": "4.0.0-alpha.9",
27
+ "@loaders.gl/loader-utils": "4.0.0-beta.2",
28
+ "@loaders.gl/schema": "4.0.0-beta.2",
29
29
  "@mapbox/vector-tile": "^1.3.1",
30
30
  "@math.gl/polygon": "^3.5.1",
31
31
  "pbf": "^3.2.1"
@@ -33,5 +33,5 @@
33
33
  "devDependencies": {
34
34
  "@math.gl/proj4": "^3.5.1"
35
35
  },
36
- "gitHead": "03ff81ab468f20f3bddeec787aa88d477a7e1c72"
36
+ "gitHead": "79c2033f755e88e11bc30a04428e3666b177b8fc"
37
37
  }
package/src/index.ts CHANGED
@@ -4,5 +4,5 @@
4
4
  export {flatGeojsonToBinary} from './lib/flat-geojson-to-binary';
5
5
  export {geojsonToBinary} from './lib/geojson-to-binary';
6
6
  export {geojsonToFlatGeojson} from './lib/geojson-to-flat-geojson';
7
- export {binaryToGeojson, binaryToGeoJson, binaryToGeometry} from './lib/binary-to-geojson';
7
+ export {binaryToGeojson, binaryToGeometry} from './lib/binary-to-geojson';
8
8
  export {transformBinaryCoords, transformGeoJsonCoords} from './lib/transform';
@@ -1,10 +1,16 @@
1
+ // loaders.gl, MIT license
2
+
1
3
  import type {
2
4
  BinaryGeometry,
3
- BinaryFeatures,
4
5
  BinaryGeometryType,
5
- BinaryPointFeatures,
6
- BinaryLineFeatures,
7
- BinaryPolygonFeatures,
6
+ BinaryPointGeometry,
7
+ BinaryLineGeometry,
8
+ BinaryPolygonGeometry,
9
+ BinaryFeatureCollection,
10
+ BinaryFeature,
11
+ // BinaryPointFeature,
12
+ // BinaryLineFeature,
13
+ // BinaryPolygonFeature,
8
14
  BinaryAttribute
9
15
  } from '@loaders.gl/schema';
10
16
  import type {Feature, Geometry, Position, GeoJsonProperties} from '@loaders.gl/schema';
@@ -34,7 +40,7 @@ type BinaryToGeoJsonOptions = {
34
40
  * @return GeoJSON objects
35
41
  */
36
42
  export function binaryToGeojson(
37
- data: BinaryFeatures,
43
+ data: BinaryFeatureCollection,
38
44
  options?: BinaryToGeoJsonOptions
39
45
  ): Feature[] | Feature {
40
46
  const globalFeatureId = options?.globalFeatureId;
@@ -44,28 +50,29 @@ export function binaryToGeojson(
44
50
  return parseFeatures(data, options?.type);
45
51
  }
46
52
 
47
- /** @deprecated use `binaryToGeojson` or `binaryToGeometry` instead */
53
+ /** @deprecated use `binaryToGeojson` or `binaryToGeometry` instead *
48
54
  export function binaryToGeoJson(
49
- data: BinaryGeometry | BinaryFeatures,
55
+ data: BinaryGeometry | BinaryFeatureCollection,
50
56
  type?: BinaryGeometryType,
51
57
  format: 'feature' | 'geometry' = 'feature'
52
58
  ): Geometry | Feature[] {
53
59
  switch (format) {
54
60
  case 'feature':
55
- return parseFeatures(data as BinaryFeatures, type);
61
+ return parseFeatures(data as BinaryFeatureCollection, type);
56
62
  case 'geometry':
57
- return binaryToGeometry(data as BinaryGeometry);
63
+ return binaryToGeometry(data as any);
58
64
  default:
59
65
  throw new Error(format);
60
66
  }
61
67
  }
68
+ */
62
69
 
63
70
  /**
64
71
  * Return a single feature from a binary geometry representation as GeoJSON
65
72
  * @param data geometry data in binary representation
66
73
  * @return GeoJSON feature
67
74
  */
68
- function getSingleFeature(data: BinaryFeatures, globalFeatureId: number): Feature {
75
+ function getSingleFeature(data: BinaryFeatureCollection, globalFeatureId: number): Feature {
69
76
  const dataArray = normalizeInput(data);
70
77
  for (const data of dataArray) {
71
78
  let lastIndex = 0;
@@ -93,7 +100,7 @@ function getSingleFeature(data: BinaryFeatures, globalFeatureId: number): Featur
93
100
  throw new Error(`featureId:${globalFeatureId} not found`);
94
101
  }
95
102
 
96
- function parseFeatures(data: BinaryFeatures, type?: BinaryGeometryType): Feature[] {
103
+ function parseFeatures(data: BinaryFeatureCollection, type?: BinaryGeometryType): Feature[] {
97
104
  const dataArray = normalizeInput(data, type);
98
105
  return parseFeatureCollection(dataArray);
99
106
  }
@@ -117,22 +124,10 @@ export function binaryToGeometry(
117
124
  }
118
125
  }
119
126
 
120
- type BinaryFeature = BinaryPointFeatures | BinaryLineFeatures | BinaryPolygonFeatures;
121
- type BinaryFeaturesArray = BinaryFeature[];
122
-
123
127
  // Normalize features
124
128
  // Return an array of data objects, each of which have a type key
125
- function normalizeInput(data: BinaryFeatures, type?: BinaryGeometryType): BinaryFeaturesArray {
126
- const isHeterogeneousType = Boolean(data.points || data.lines || data.polygons);
127
-
128
- if (!isHeterogeneousType) {
129
- // @ts-expect-error This is a legacy check which allowed `data` to be an instance of the values
130
- // here. Aka the new data.points, data.lines, or data.polygons.
131
- data.type = type || parseType(data);
132
- return [data] as BinaryFeaturesArray;
133
- }
134
-
135
- const features: BinaryFeaturesArray = [];
129
+ function normalizeInput(data: BinaryFeatureCollection, type?: BinaryGeometryType): BinaryFeature[] {
130
+ const features: BinaryFeature[] = [];
136
131
  if (data.points) {
137
132
  data.points.type = 'Point';
138
133
  features.push(data.points);
@@ -150,7 +145,7 @@ function normalizeInput(data: BinaryFeatures, type?: BinaryGeometryType): Binary
150
145
  }
151
146
 
152
147
  /** Parse input binary data and return an array of GeoJSON Features */
153
- function parseFeatureCollection(dataArray): Feature[] {
148
+ function parseFeatureCollection(dataArray: BinaryFeature[]): Feature[] {
154
149
  const features: Feature[] = [];
155
150
  for (const data of dataArray) {
156
151
  if (data.featureIds.value.length === 0) {
@@ -180,7 +175,7 @@ function parseFeatureCollection(dataArray): Feature[] {
180
175
  }
181
176
 
182
177
  /** Parse input binary data and return a single GeoJSON Feature */
183
- function parseFeature(data, startIndex?: number, endIndex?: number): Feature {
178
+ function parseFeature(data: BinaryFeature, startIndex?: number, endIndex?: number): Feature {
184
179
  const geometry = binaryToGeometry(data, startIndex, endIndex);
185
180
  const properties = parseProperties(data, startIndex, endIndex);
186
181
  const fields = parseFields(data, startIndex, endIndex);
@@ -203,7 +198,7 @@ function parseProperties(data, startIndex: number = 0, endIndex?: number): GeoJs
203
198
 
204
199
  /** Parse binary data of type Polygon */
205
200
  function polygonToGeoJson(
206
- data,
201
+ data: BinaryPolygonGeometry,
207
202
  startIndex: number = -Infinity,
208
203
  endIndex: number = Infinity
209
204
  ): Polygon | MultiPolygon {
@@ -245,7 +240,7 @@ function polygonToGeoJson(
245
240
 
246
241
  /** Parse binary data of type LineString */
247
242
  function lineStringToGeoJson(
248
- data,
243
+ data: BinaryLineGeometry,
249
244
  startIndex: number = -Infinity,
250
245
  endIndex: number = Infinity
251
246
  ): LineString | MultiLineString {
@@ -268,7 +263,7 @@ function lineStringToGeoJson(
268
263
  }
269
264
 
270
265
  /** Parse binary data of type Point */
271
- function pointToGeoJson(data, startIndex, endIndex): Point | MultiPoint {
266
+ function pointToGeoJson(data: BinaryPointGeometry, startIndex, endIndex): Point | MultiPoint {
272
267
  const {positions} = data;
273
268
  const coordinates = ringToGeoJson(positions, startIndex, endIndex);
274
269
  const multi = coordinates.length > 1;
@@ -306,16 +301,3 @@ function ringToGeoJson(
306
301
  }
307
302
  return ringCoordinates;
308
303
  }
309
-
310
- // Deduce geometry type of data object
311
- function parseType(data) {
312
- if (data.pathIndices) {
313
- return 'LineString';
314
- }
315
-
316
- if (data.polygonIndices) {
317
- return 'Polygon';
318
- }
319
-
320
- return 'Point';
321
- }
@@ -47,7 +47,7 @@ export type Polygons = {
47
47
  positions: Float32Array | Float64Array;
48
48
  polygonIndices: Uint16Array | Uint32Array;
49
49
  primitivePolygonIndices: Uint16Array | Uint32Array;
50
- triangles: number[];
50
+ triangles?: number[];
51
51
  globalFeatureIds: Uint16Array | Uint32Array;
52
52
  featureIds: Uint16Array | Uint32Array;
53
53
  numericProps: {[key: string]: TypedArray};
@@ -2,7 +2,8 @@
2
2
  import {earcut} from '@math.gl/polygon';
3
3
  import type {
4
4
  BinaryAttribute,
5
- BinaryFeatures,
5
+ BinaryFeatureCollection,
6
+ BinaryPolygonFeature,
6
7
  FlatFeature,
7
8
  FlatPoint,
8
9
  FlatLineString,
@@ -40,7 +41,8 @@ export function flatGeojsonToBinary(
40
41
  },
41
42
  {
42
43
  numericPropKeys: (options && options.numericPropKeys) || numericPropKeys,
43
- PositionDataType: options ? options.PositionDataType : Float32Array
44
+ PositionDataType: options ? options.PositionDataType : Float32Array,
45
+ triangulate: options ? options.triangulate : true
44
46
  }
45
47
  );
46
48
  }
@@ -51,6 +53,7 @@ export function flatGeojsonToBinary(
51
53
  export type FlatGeojsonToBinaryOptions = {
52
54
  numericPropKeys?: string[];
53
55
  PositionDataType?: Float32ArrayConstructor | Float64ArrayConstructor;
56
+ triangulate?: boolean;
54
57
  };
55
58
 
56
59
  export const TEST_EXPORTS = {
@@ -91,7 +94,7 @@ function extractNumericPropTypes(features: FlatFeature[]): {
91
94
  * @param options
92
95
  * @returns an accessor object with value and size keys
93
96
  */
94
- // eslint-disable-next-line complexity
97
+ // eslint-disable-next-line complexity, max-statements
95
98
  function fillArrays(
96
99
  features: FlatFeature[],
97
100
  geometryInfo: GeojsonGeometryInfo & {
@@ -112,7 +115,7 @@ function fillArrays(
112
115
  propArrayTypes,
113
116
  coordLength
114
117
  } = geometryInfo;
115
- const {numericPropKeys = [], PositionDataType = Float32Array} = options;
118
+ const {numericPropKeys = [], PositionDataType = Float32Array, triangulate = true} = options;
116
119
  const hasGlobalId = features[0] && 'id' in features[0];
117
120
  const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
118
121
  const points: Points = {
@@ -154,7 +157,6 @@ function fillArrays(
154
157
  ? new Uint32Array(polygonRingsCount + 1)
155
158
  : new Uint16Array(polygonRingsCount + 1),
156
159
  positions: new PositionDataType(polygonPositionsCount * coordLength),
157
- triangles: [],
158
160
  globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
159
161
  featureIds:
160
162
  polygonFeaturesCount > 65535
@@ -165,6 +167,10 @@ function fillArrays(
165
167
  fields: []
166
168
  };
167
169
 
170
+ if (triangulate) {
171
+ polygons.triangles = [];
172
+ }
173
+
168
174
  // Instantiate numeric properties arrays; one value per vertex
169
175
  for (const object of [points, lines, polygons]) {
170
176
  for (const propName of numericPropKeys) {
@@ -423,6 +429,10 @@ function triangulatePolygon(
423
429
  coordLength
424
430
  }: {startPosition: number; endPosition: number; coordLength: number}
425
431
  ): void {
432
+ if (!polygons.triangles) {
433
+ return;
434
+ }
435
+
426
436
  const start = startPosition * coordLength;
427
437
  const end = endPosition * coordLength;
428
438
 
@@ -475,8 +485,8 @@ function makeAccessorObjects(
475
485
  lines: Lines,
476
486
  polygons: Polygons,
477
487
  coordLength: number
478
- ): BinaryFeatures {
479
- return {
488
+ ): BinaryFeatureCollection {
489
+ const binaryFeatures: BinaryFeatureCollection = {
480
490
  points: {
481
491
  ...points,
482
492
  positions: {value: points.positions, size: coordLength},
@@ -497,12 +507,17 @@ function makeAccessorObjects(
497
507
  positions: {value: polygons.positions, size: coordLength},
498
508
  polygonIndices: {value: polygons.polygonIndices, size: 1},
499
509
  primitivePolygonIndices: {value: polygons.primitivePolygonIndices, size: 1},
500
- triangles: {value: new Uint32Array(polygons.triangles), size: 1},
501
510
  globalFeatureIds: {value: polygons.globalFeatureIds, size: 1},
502
511
  featureIds: {value: polygons.featureIds, size: 1},
503
512
  numericProps: wrapProps(polygons.numericProps, 1)
504
- }
513
+ } as BinaryPolygonFeature // triangles not expected
505
514
  };
515
+
516
+ if (binaryFeatures.polygons && polygons.triangles) {
517
+ binaryFeatures.polygons.triangles = {value: new Uint32Array(polygons.triangles), size: 1};
518
+ }
519
+
520
+ return binaryFeatures;
506
521
  }
507
522
 
508
523
  /**
@@ -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';
@@ -12,6 +12,7 @@ export type GeojsonToBinaryOptions = {
12
12
  fixRingWinding: boolean;
13
13
  numericPropKeys?: string[];
14
14
  PositionDataType?: Float32ArrayConstructor | Float64ArrayConstructor;
15
+ triangulate?: boolean;
15
16
  };
16
17
 
17
18
  /**
@@ -23,14 +24,15 @@ export type GeojsonToBinaryOptions = {
23
24
  */
24
25
  export function geojsonToBinary(
25
26
  features: Feature[],
26
- options: GeojsonToBinaryOptions = {fixRingWinding: true}
27
- ): BinaryFeatures {
27
+ options: GeojsonToBinaryOptions = {fixRingWinding: true, triangulate: true}
28
+ ): BinaryFeatureCollection {
28
29
  const geometryInfo = extractGeometryInfo(features);
29
30
  const coordLength = geometryInfo.coordLength;
30
31
  const {fixRingWinding} = options;
31
32
  const flatFeatures = geojsonToFlatGeojson(features, {coordLength, fixRingWinding});
32
33
  return flatGeojsonToBinary(flatFeatures, geometryInfo, {
33
34
  numericPropKeys: options.numericPropKeys,
34
- PositionDataType: options.PositionDataType || Float32Array
35
+ PositionDataType: options.PositionDataType || Float32Array,
36
+ triangulate: options.triangulate
35
37
  });
36
38
  }
@@ -1,4 +1,4 @@
1
- import type {BinaryFeatures, BinaryGeometry} 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
  }
@@ -44,9 +44,9 @@ function transformBinaryGeometryPositions(binaryGeometry: BinaryGeometry, fn: Tr
44
44
  * @return Transformed GeoJSON features
45
45
  */
46
46
  export function transformGeoJsonCoords(
47
- features: object[],
47
+ features: Feature[],
48
48
  fn: (coord: number[]) => number[]
49
- ): object[] {
49
+ ): Feature[] {
50
50
  for (const feature of features) {
51
51
  // @ts-ignore
52
52
  feature.geometry.coordinates = coordMap(feature.geometry.coordinates, fn);
@@ -54,16 +54,16 @@ export function transformGeoJsonCoords(
54
54
  return features;
55
55
  }
56
56
 
57
- function coordMap(array, fn) {
57
+ function coordMap(array: unknown, fn: (coord: number[]) => number[]): unknown[] {
58
58
  if (isCoord(array)) {
59
- return fn(array);
59
+ return fn(array as number[]);
60
60
  }
61
61
 
62
- return array.map((item) => {
62
+ return (array as unknown[]).map((item) => {
63
63
  return coordMap(item, fn);
64
64
  });
65
65
  }
66
66
 
67
- function isCoord(array) {
68
- return Number.isFinite(array[0]) && Number.isFinite(array[1]);
67
+ function isCoord(array: unknown) {
68
+ return Array.isArray(array) && Number.isFinite(array[0]) && Number.isFinite(array[1]);
69
69
  }
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
- }