@loaders.gl/gis 3.1.0-beta.3 → 3.1.0

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.
@@ -25,9 +25,12 @@ export const TEST_EXPORTS = {
25
25
  secondPass
26
26
  };
27
27
 
28
+ type PropArrayConstructor = Float32ArrayConstructor | Float64ArrayConstructor | ArrayConstructor;
29
+
28
30
  type FirstPassData = {
29
31
  coordLength: number;
30
32
  numericPropKeys: string[];
33
+ propArrayTypes: {[key: string]: PropArrayConstructor};
31
34
 
32
35
  pointPositionsCount: number;
33
36
  pointFeaturesCount: number;
@@ -58,7 +61,7 @@ function firstPass(features: Feature[]): FirstPassData {
58
61
  let polygonRingsCount = 0;
59
62
  let polygonFeaturesCount = 0;
60
63
  const coordLengths = new Set<number>();
61
- const numericPropKeys = {};
64
+ const propArrayTypes = {};
62
65
 
63
66
  for (const feature of features) {
64
67
  const geometry = feature.geometry;
@@ -129,11 +132,9 @@ function firstPass(features: Feature[]): FirstPassData {
129
132
 
130
133
  // If property has not been seen before, or if property has been numeric
131
134
  // in all previous features, check if numeric in this feature
132
- // If not numeric, false is stored to prevent rechecking in the future
133
- numericPropKeys[key] =
134
- numericPropKeys[key] || numericPropKeys[key] === undefined
135
- ? isNumeric(val)
136
- : numericPropKeys[key];
135
+ // If not numeric, Array is stored to prevent rechecking in the future
136
+ // Additionally, detects if 64 bit precision is required
137
+ propArrayTypes[key] = deduceArrayType(val, propArrayTypes[key]);
137
138
  }
138
139
  }
139
140
  }
@@ -152,7 +153,8 @@ function firstPass(features: Feature[]): FirstPassData {
152
153
  polygonFeaturesCount,
153
154
 
154
155
  // Array of keys whose values are always numeric
155
- numericPropKeys: Object.keys(numericPropKeys).filter((k) => numericPropKeys[k])
156
+ numericPropKeys: Object.keys(propArrayTypes).filter((k) => propArrayTypes[k] !== Array),
157
+ propArrayTypes
156
158
  };
157
159
  }
158
160
 
@@ -175,6 +177,7 @@ function secondPass(
175
177
  polygonPositionsCount,
176
178
  polygonObjectsCount,
177
179
  polygonRingsCount,
180
+ propArrayTypes,
178
181
  polygonFeaturesCount
179
182
  } = firstPassData;
180
183
  const {coordLength, numericPropKeys, PositionDataType = Float32Array} = options;
@@ -233,7 +236,8 @@ function secondPass(
233
236
  for (const propName of numericPropKeys || []) {
234
237
  // If property has been numeric in all previous features in which the property existed, check
235
238
  // if numeric in this feature
236
- object.numericProps[propName] = new Float32Array(object.positions.length / coordLength);
239
+ const TypedArray = propArrayTypes[propName];
240
+ object.numericProps[propName] = new TypedArray(object.positions.length / coordLength);
237
241
  }
238
242
  }
239
243
 
@@ -457,6 +461,11 @@ function flatten(arrays): number[][] {
457
461
  return [].concat(...arrays);
458
462
  }
459
463
 
460
- function isNumeric(x: any): boolean {
461
- return Number.isFinite(x);
464
+ function deduceArrayType(x: any, constructor: PropArrayConstructor): PropArrayConstructor {
465
+ if (constructor === Array || !Number.isFinite(x)) {
466
+ return Array;
467
+ }
468
+
469
+ // If this or previous value required 64bits use Float64Array
470
+ return constructor === Float64Array || Math.fround(x) !== x ? Float64Array : Float32Array;
462
471
  }