@kepler.gl/common-utils 3.3.0-alpha.2 → 3.3.0-alpha.21
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.
- package/dist/cjs/data-type.js +606 -0
- package/dist/cjs/data-type.js.map +1 -0
- package/dist/cjs/data.js +91 -0
- package/dist/cjs/data.js.map +1 -0
- package/dist/cjs/h3-utils.js +86 -0
- package/dist/cjs/h3-utils.js.map +1 -0
- package/dist/cjs/index.js +118 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/promise.js +20 -0
- package/dist/cjs/promise.js.map +1 -0
- package/dist/cjs/string.js +19 -0
- package/dist/cjs/string.js.map +1 -0
- package/dist/cjs/url.js +486 -0
- package/dist/cjs/url.js.map +1 -0
- package/dist/esm/data-type.js +596 -0
- package/dist/esm/data-type.js.map +1 -0
- package/dist/esm/data.js +81 -0
- package/dist/esm/data.js.map +1 -0
- package/dist/esm/h3-utils.js +42 -0
- package/dist/esm/h3-utils.js.map +1 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/promise.js +14 -0
- package/dist/esm/promise.js.map +1 -0
- package/dist/esm/string.js +13 -0
- package/dist/esm/string.js.map +1 -0
- package/dist/esm/url.js +469 -0
- package/dist/esm/url.js.map +1 -0
- package/dist/h3-utils.d.ts +6 -4
- package/dist/index.d.ts +1 -1
- package/dist/url.d.ts +45 -0
- package/package.json +19 -12
- package/dist/data-type.js +0 -302
- package/dist/data.js +0 -91
- package/dist/h3-utils.js +0 -63
- package/dist/index.js +0 -97
- package/dist/promise.js +0 -20
- package/dist/string.js +0 -19
- package/dist/url.js +0 -66
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
3
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
4
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
5
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
6
|
+
// SPDX-License-Identifier: MIT
|
|
7
|
+
// Copyright contributors to the kepler.gl project
|
|
8
|
+
|
|
9
|
+
import { Analyzer, DATA_TYPES as AnalyzerDATA_TYPES } from 'type-analyzer';
|
|
10
|
+
import { ALL_FIELD_TYPES } from '@kepler.gl/constants';
|
|
11
|
+
import { console as globalConsole } from 'global/window';
|
|
12
|
+
import { range } from 'd3-array';
|
|
13
|
+
import { isHexWkb, notNullorUndefined } from './data';
|
|
14
|
+
import { h3IsValid } from './h3-utils';
|
|
15
|
+
var H3_ANALYZER_TYPE = 'H3';
|
|
16
|
+
|
|
17
|
+
// a plain decimal number, optionally in scientific notation: 12, -0.5, .5, 1.5e+10, 2E-4
|
|
18
|
+
var DECIMAL_NUMBER_REGEX = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i;
|
|
19
|
+
var INTEGER_REGEX = /^[+-]?\d+$/;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* type-analyzer reports NUMBER for values that pass `!isNaN` but match neither its INT nor
|
|
23
|
+
* its FLOAT regex, which in practice is mostly scientific notation such as `1.5e+10`. Such a
|
|
24
|
+
* value is safe to read as a real number unless it is a non decimal literal (`0x1A`, also hex
|
|
25
|
+
* hashes and addresses) or an integer beyond the safe range (64-bit ids), both of which
|
|
26
|
+
* parseFloat would misread or round.
|
|
27
|
+
*/
|
|
28
|
+
function isRealNumberSample(value) {
|
|
29
|
+
if (!notNullorUndefined(value) || value === '') {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
if (typeof value === 'number') {
|
|
33
|
+
return Number.isFinite(value);
|
|
34
|
+
}
|
|
35
|
+
if (typeof value !== 'string' || !DECIMAL_NUMBER_REGEX.test(value)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
var asNumber = Number(value);
|
|
39
|
+
return Number.isFinite(asNumber) && (!INTEGER_REGEX.test(value) || Number.isSafeInteger(asNumber));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* type-analyzer uses `isNaN(value)`, which throws on BigInt
|
|
44
|
+
* ("Cannot convert a BigInt value to a number"). Arrow/Parquet Int64
|
|
45
|
+
* columns yield BigInt in JS, so stringify those samples first.
|
|
46
|
+
*/
|
|
47
|
+
function toTypeAnalyzerValue(value) {
|
|
48
|
+
if (typeof value === 'string') {
|
|
49
|
+
return value.trim();
|
|
50
|
+
}
|
|
51
|
+
if (typeof value === 'bigint') {
|
|
52
|
+
return value.toString();
|
|
53
|
+
}
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
function isGeoJsonGeometry(value) {
|
|
57
|
+
var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
58
|
+
if (!value || _typeof(value) !== 'object' || depth > 32) return false;
|
|
59
|
+
var geometry = value;
|
|
60
|
+
if (geometry.type === 'Feature') return isGeoJsonGeometry(geometry.geometry, depth + 1);
|
|
61
|
+
if (geometry.type === 'GeometryCollection') {
|
|
62
|
+
return Array.isArray(geometry.geometries) && geometry.geometries.every(function (child) {
|
|
63
|
+
return isGeoJsonGeometry(child, depth + 1);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
var position = function position(coordinates) {
|
|
67
|
+
return Array.isArray(coordinates) && coordinates.length >= 2 && coordinates.every(function (coordinate) {
|
|
68
|
+
return typeof coordinate === 'number' && Number.isFinite(coordinate);
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
var positions = function positions(coordinates, minimum) {
|
|
72
|
+
return Array.isArray(coordinates) && (coordinates.length === 0 || coordinates.length >= minimum) && coordinates.every(position);
|
|
73
|
+
};
|
|
74
|
+
var ring = function ring(coordinates) {
|
|
75
|
+
return positions(coordinates, 4) && (coordinates.length === 0 || coordinates[0].length === coordinates[coordinates.length - 1].length && coordinates[0].every(function (coordinate, index) {
|
|
76
|
+
return coordinate === coordinates[coordinates.length - 1][index];
|
|
77
|
+
}));
|
|
78
|
+
};
|
|
79
|
+
var polygon = function polygon(coordinates) {
|
|
80
|
+
return Array.isArray(coordinates) && coordinates.every(ring);
|
|
81
|
+
};
|
|
82
|
+
switch (geometry.type) {
|
|
83
|
+
case 'Point':
|
|
84
|
+
return position(geometry.coordinates);
|
|
85
|
+
case 'MultiPoint':
|
|
86
|
+
return positions(geometry.coordinates, 1);
|
|
87
|
+
case 'LineString':
|
|
88
|
+
return positions(geometry.coordinates, 2);
|
|
89
|
+
case 'MultiLineString':
|
|
90
|
+
return Array.isArray(geometry.coordinates) && geometry.coordinates.every(function (line) {
|
|
91
|
+
return positions(line, 2);
|
|
92
|
+
});
|
|
93
|
+
case 'Polygon':
|
|
94
|
+
return polygon(geometry.coordinates);
|
|
95
|
+
case 'MultiPolygon':
|
|
96
|
+
return Array.isArray(geometry.coordinates) && geometry.coordinates.every(polygon);
|
|
97
|
+
default:
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Validate the complete WKT collection before changing a text column's type. */
|
|
103
|
+
function isWktGeometryCollection(value) {
|
|
104
|
+
var cursor = 0;
|
|
105
|
+
var consume = function consume(pattern) {
|
|
106
|
+
var _match$;
|
|
107
|
+
var match = pattern.exec(value.slice(cursor));
|
|
108
|
+
if (!match) return undefined;
|
|
109
|
+
cursor += match[0].length;
|
|
110
|
+
// Captures exclude leading whitespace; punctuation only needs a truthy result.
|
|
111
|
+
return (_match$ = match[1]) !== null && _match$ !== void 0 ? _match$ : match[0];
|
|
112
|
+
};
|
|
113
|
+
var number = DECIMAL_NUMBER_REGEX.source.slice(1, -1);
|
|
114
|
+
var positionPattern = new RegExp("^\\s*(".concat(number, ")\\s+(").concat(number, ")((?:\\s+").concat(number, ")*)"), 'i');
|
|
115
|
+
var group = function group(read) {
|
|
116
|
+
if (!consume(/^\s*\(/)) return null;
|
|
117
|
+
var items = [];
|
|
118
|
+
do {
|
|
119
|
+
var item = read();
|
|
120
|
+
if (item == null) return null;
|
|
121
|
+
items.push(item);
|
|
122
|
+
} while (consume(/^\s*,/));
|
|
123
|
+
return consume(/^\s*\)/) ? items : null;
|
|
124
|
+
};
|
|
125
|
+
var _readGeometry = function readGeometry(depth, inheritedDimensions) {
|
|
126
|
+
var _consume, _consume2;
|
|
127
|
+
if (depth > 32) return null;
|
|
128
|
+
var type = (_consume = consume(/^\s*(GEOMETRYCOLLECTION|MULTIPOLYGON|MULTILINESTRING|MULTIPOINT|POLYGON|LINESTRING|POINT)\b/i)) === null || _consume === void 0 ? void 0 : _consume.toUpperCase();
|
|
129
|
+
if (!type) return null;
|
|
130
|
+
var dimension = (_consume2 = consume(/^\s+(ZM|Z|M)\b/i)) === null || _consume2 === void 0 ? void 0 : _consume2.toUpperCase();
|
|
131
|
+
var dimensions = dimension ? dimension === 'ZM' ? 4 : 3 : inheritedDimensions;
|
|
132
|
+
if (consume(/^\s*EMPTY\b/i)) return {};
|
|
133
|
+
if (type === 'GEOMETRYCOLLECTION') {
|
|
134
|
+
return group(function () {
|
|
135
|
+
return _readGeometry(depth + 1, dimensions);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
var _readCoordinates = function readCoordinates(level) {
|
|
139
|
+
if (level > 3) return null;
|
|
140
|
+
if (/^\s*\(/.test(value.slice(cursor))) return group(function () {
|
|
141
|
+
return _readCoordinates(level + 1);
|
|
142
|
+
});
|
|
143
|
+
var match = positionPattern.exec(value.slice(cursor));
|
|
144
|
+
if (!match) return null;
|
|
145
|
+
cursor += match[0].length;
|
|
146
|
+
// Capture common XY positions directly, without splitting/trimming each tuple.
|
|
147
|
+
var coordinates = [+match[1], +match[2]];
|
|
148
|
+
var _iterator = _createForOfIteratorHelper(match[3].match(/\S+/g) || []),
|
|
149
|
+
_step;
|
|
150
|
+
try {
|
|
151
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
152
|
+
var coordinate = _step.value;
|
|
153
|
+
coordinates.push(+coordinate);
|
|
154
|
+
}
|
|
155
|
+
} catch (err) {
|
|
156
|
+
_iterator.e(err);
|
|
157
|
+
} finally {
|
|
158
|
+
_iterator.f();
|
|
159
|
+
}
|
|
160
|
+
return coordinates.every(Number.isFinite) && (!dimensions || coordinates.length === dimensions) ? coordinates : null;
|
|
161
|
+
};
|
|
162
|
+
var coordinates = group(function () {
|
|
163
|
+
return _readCoordinates(0);
|
|
164
|
+
});
|
|
165
|
+
if (!coordinates) return null;
|
|
166
|
+
if (type === 'POINT') {
|
|
167
|
+
if (coordinates.length !== 1) return null;
|
|
168
|
+
coordinates = coordinates[0];
|
|
169
|
+
} else if (type === 'MULTIPOINT' && coordinates.every(function (point) {
|
|
170
|
+
return Array.isArray(point) && point.length === 1;
|
|
171
|
+
})) {
|
|
172
|
+
coordinates = coordinates.map(function (point) {
|
|
173
|
+
return point[0];
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
var types = {
|
|
177
|
+
POINT: 'Point',
|
|
178
|
+
MULTIPOINT: 'MultiPoint',
|
|
179
|
+
LINESTRING: 'LineString',
|
|
180
|
+
MULTILINESTRING: 'MultiLineString',
|
|
181
|
+
POLYGON: 'Polygon',
|
|
182
|
+
MULTIPOLYGON: 'MultiPolygon'
|
|
183
|
+
};
|
|
184
|
+
return isGeoJsonGeometry({
|
|
185
|
+
type: types[type],
|
|
186
|
+
coordinates: coordinates
|
|
187
|
+
}) ? {} : null;
|
|
188
|
+
};
|
|
189
|
+
return _readGeometry(0) != null && value.slice(cursor).trim() === '';
|
|
190
|
+
}
|
|
191
|
+
function isAdditionalGeometry(value) {
|
|
192
|
+
if (typeof value !== 'string') return isGeoJsonGeometry(value);
|
|
193
|
+
if (/^GEOMETRYCOLLECTION\b/i.test(value)) return isWktGeometryCollection(value);
|
|
194
|
+
if (!value.startsWith('{')) return false;
|
|
195
|
+
try {
|
|
196
|
+
return isGeoJsonGeometry(JSON.parse(value));
|
|
197
|
+
} catch (_unused) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Validate numeric-looking WKB before overriding NUMBER (e.g. account IDs). */
|
|
203
|
+
function isCompleteWkb(value) {
|
|
204
|
+
if (typeof value !== 'string' || !isHexWkb(value)) return false;
|
|
205
|
+
var length = value.length / 2;
|
|
206
|
+
var uint32 = function uint32(offset, littleEndian) {
|
|
207
|
+
if (offset + 4 > length) throw new Error('Truncated WKB');
|
|
208
|
+
var number = 0;
|
|
209
|
+
for (var i = 0; i < 4; i++) {
|
|
210
|
+
var _byte = parseInt(value.slice((offset + i) * 2, (offset + i + 1) * 2), 16);
|
|
211
|
+
number += _byte * Math.pow(256, littleEndian ? i : 3 - i);
|
|
212
|
+
}
|
|
213
|
+
return number;
|
|
214
|
+
};
|
|
215
|
+
var _endOfGeometry = function endOfGeometry(offset, depth) {
|
|
216
|
+
if (depth > 32 || offset + 5 > length) throw new Error('Invalid WKB');
|
|
217
|
+
var order = value.slice(offset * 2, offset * 2 + 2);
|
|
218
|
+
if (order !== '00' && order !== '01') throw new Error('Invalid WKB byte order');
|
|
219
|
+
var littleEndian = order === '01';
|
|
220
|
+
var rawType = uint32(offset + 1, littleEndian);
|
|
221
|
+
var type = rawType & 0x1fffffff;
|
|
222
|
+
var dimensions = 2 + Number(Boolean(rawType & 0x80000000) || [1, 3].includes(Math.floor(type / 1000))) + Number(Boolean(rawType & 0x40000000) || [2, 3].includes(Math.floor(type / 1000)));
|
|
223
|
+
var baseType = type % 1000;
|
|
224
|
+
if (baseType < 1 || baseType > 7 || type >= 4000) throw new Error('Invalid WKB type');
|
|
225
|
+
var cursor = offset + 5 + (rawType & 0x20000000 ? 4 : 0);
|
|
226
|
+
if (baseType === 1) return cursor + dimensions * 8;
|
|
227
|
+
var count = uint32(cursor, littleEndian);
|
|
228
|
+
cursor += 4;
|
|
229
|
+
if (baseType === 2) return cursor + count * dimensions * 8;
|
|
230
|
+
if (count > (length - cursor) / (baseType === 3 ? 4 : 5)) throw new Error('Truncated WKB');
|
|
231
|
+
for (var i = 0; i < count; i++) {
|
|
232
|
+
if (baseType === 3) {
|
|
233
|
+
var points = uint32(cursor, littleEndian);
|
|
234
|
+
cursor += 4 + points * dimensions * 8;
|
|
235
|
+
} else {
|
|
236
|
+
cursor = _endOfGeometry(cursor, depth + 1);
|
|
237
|
+
}
|
|
238
|
+
if (cursor > length) throw new Error('Truncated WKB');
|
|
239
|
+
}
|
|
240
|
+
return cursor;
|
|
241
|
+
};
|
|
242
|
+
try {
|
|
243
|
+
return _endOfGeometry(0, 0) === length;
|
|
244
|
+
} catch (_unused2) {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
export var ACCEPTED_ANALYZER_TYPES = [AnalyzerDATA_TYPES.DATE, AnalyzerDATA_TYPES.TIME, AnalyzerDATA_TYPES.DATETIME, AnalyzerDATA_TYPES.NUMBER, AnalyzerDATA_TYPES.INT, AnalyzerDATA_TYPES.FLOAT, AnalyzerDATA_TYPES.BOOLEAN, AnalyzerDATA_TYPES.STRING, AnalyzerDATA_TYPES.GEOMETRY, AnalyzerDATA_TYPES.GEOMETRY_FROM_STRING, AnalyzerDATA_TYPES.PAIR_GEOMETRY_FROM_STRING, AnalyzerDATA_TYPES.ZIPCODE, AnalyzerDATA_TYPES.ARRAY, AnalyzerDATA_TYPES.OBJECT, H3_ANALYZER_TYPE];
|
|
249
|
+
var IGNORE_DATA_TYPES = Object.keys(AnalyzerDATA_TYPES).filter(function (type) {
|
|
250
|
+
return !ACCEPTED_ANALYZER_TYPES.includes(type);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Getting sample data for analyzing field type.
|
|
255
|
+
*/
|
|
256
|
+
export function getSampleForTypeAnalyze(_ref) {
|
|
257
|
+
var fields = _ref.fields,
|
|
258
|
+
rows = _ref.rows,
|
|
259
|
+
_ref$sampleCount = _ref.sampleCount,
|
|
260
|
+
sampleCount = _ref$sampleCount === void 0 ? 50 : _ref$sampleCount;
|
|
261
|
+
var total = Math.min(sampleCount, rows.length);
|
|
262
|
+
// const fieldOrder = fields.map(f => f.name);
|
|
263
|
+
var sample = range(0, total, 1).map(function () {
|
|
264
|
+
return {};
|
|
265
|
+
});
|
|
266
|
+
if (rows.length < 1) {
|
|
267
|
+
return [];
|
|
268
|
+
}
|
|
269
|
+
var isRowObject = !Array.isArray(rows[0]);
|
|
270
|
+
|
|
271
|
+
// collect sample data for each field
|
|
272
|
+
fields.forEach(function (field, fieldIdx) {
|
|
273
|
+
// row counter
|
|
274
|
+
var i = 0;
|
|
275
|
+
// sample counter
|
|
276
|
+
var j = 0;
|
|
277
|
+
while (j < total) {
|
|
278
|
+
if (i >= rows.length) {
|
|
279
|
+
// if depleted data pool
|
|
280
|
+
sample[j][field] = null;
|
|
281
|
+
j++;
|
|
282
|
+
} else if (notNullorUndefined(rows[i][isRowObject ? field : fieldIdx])) {
|
|
283
|
+
var value = rows[i][isRowObject ? field : fieldIdx];
|
|
284
|
+
sample[j][field] = toTypeAnalyzerValue(value);
|
|
285
|
+
j++;
|
|
286
|
+
i++;
|
|
287
|
+
} else {
|
|
288
|
+
i++;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
return sample;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Getting sample data for analyzing field type for Arrow tables.
|
|
297
|
+
* @param table Arrow table or an array of vectors.
|
|
298
|
+
* @param fields Field names.
|
|
299
|
+
* @param sampleCount Number of sample rows to get.
|
|
300
|
+
* @returns Sample rows.
|
|
301
|
+
*/
|
|
302
|
+
export function getSampleForTypeAnalyzeArrow(table, fields) {
|
|
303
|
+
var sampleCount = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 50;
|
|
304
|
+
var isTable = !Array.isArray(table);
|
|
305
|
+
var numRows = isTable ? table.numRows : table[0].length;
|
|
306
|
+
var getVector = isTable ? function (index) {
|
|
307
|
+
return table.getChildAt(index);
|
|
308
|
+
} : function (index) {
|
|
309
|
+
return table[index];
|
|
310
|
+
};
|
|
311
|
+
var total = Math.min(sampleCount, numRows);
|
|
312
|
+
var sample = range(0, total, 1).map(function () {
|
|
313
|
+
return {};
|
|
314
|
+
});
|
|
315
|
+
if (numRows < 1) {
|
|
316
|
+
return [];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// collect sample data for each field
|
|
320
|
+
fields.forEach(function (field, fieldIdx) {
|
|
321
|
+
var rowIndex = 0;
|
|
322
|
+
var sampleIndex = 0;
|
|
323
|
+
while (sampleIndex < total) {
|
|
324
|
+
var _getVector;
|
|
325
|
+
if (rowIndex >= numRows) {
|
|
326
|
+
// if depleted data pool
|
|
327
|
+
sample[sampleIndex][field] = null;
|
|
328
|
+
sampleIndex++;
|
|
329
|
+
} else if (notNullorUndefined((_getVector = getVector(fieldIdx)) === null || _getVector === void 0 ? void 0 : _getVector.get(rowIndex))) {
|
|
330
|
+
var _getVector2;
|
|
331
|
+
var value = (_getVector2 = getVector(fieldIdx)) === null || _getVector2 === void 0 ? void 0 : _getVector2.get(rowIndex);
|
|
332
|
+
sample[sampleIndex][field] = toTypeAnalyzerValue(value);
|
|
333
|
+
sampleIndex++;
|
|
334
|
+
rowIndex++;
|
|
335
|
+
} else {
|
|
336
|
+
rowIndex++;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
return sample;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Convert type-analyzer output to kepler.gl field types
|
|
345
|
+
*
|
|
346
|
+
* @param aType
|
|
347
|
+
* @returns corresponding type in `ALL_FIELD_TYPES`
|
|
348
|
+
*/
|
|
349
|
+
/* eslint-disable complexity */
|
|
350
|
+
export function analyzerTypeToFieldType(aType) {
|
|
351
|
+
var DATE = AnalyzerDATA_TYPES.DATE,
|
|
352
|
+
TIME = AnalyzerDATA_TYPES.TIME,
|
|
353
|
+
DATETIME = AnalyzerDATA_TYPES.DATETIME,
|
|
354
|
+
NUMBER = AnalyzerDATA_TYPES.NUMBER,
|
|
355
|
+
INT = AnalyzerDATA_TYPES.INT,
|
|
356
|
+
FLOAT = AnalyzerDATA_TYPES.FLOAT,
|
|
357
|
+
BOOLEAN = AnalyzerDATA_TYPES.BOOLEAN,
|
|
358
|
+
STRING = AnalyzerDATA_TYPES.STRING,
|
|
359
|
+
GEOMETRY = AnalyzerDATA_TYPES.GEOMETRY,
|
|
360
|
+
GEOMETRY_FROM_STRING = AnalyzerDATA_TYPES.GEOMETRY_FROM_STRING,
|
|
361
|
+
PAIR_GEOMETRY_FROM_STRING = AnalyzerDATA_TYPES.PAIR_GEOMETRY_FROM_STRING,
|
|
362
|
+
ZIPCODE = AnalyzerDATA_TYPES.ZIPCODE,
|
|
363
|
+
ARRAY = AnalyzerDATA_TYPES.ARRAY,
|
|
364
|
+
OBJECT = AnalyzerDATA_TYPES.OBJECT;
|
|
365
|
+
|
|
366
|
+
// TODO: un recognized types
|
|
367
|
+
// CURRENCY PERCENT NONE
|
|
368
|
+
switch (aType) {
|
|
369
|
+
case DATE:
|
|
370
|
+
return ALL_FIELD_TYPES.date;
|
|
371
|
+
case TIME:
|
|
372
|
+
case DATETIME:
|
|
373
|
+
return ALL_FIELD_TYPES.timestamp;
|
|
374
|
+
case FLOAT:
|
|
375
|
+
return ALL_FIELD_TYPES.real;
|
|
376
|
+
case INT:
|
|
377
|
+
return ALL_FIELD_TYPES.integer;
|
|
378
|
+
case BOOLEAN:
|
|
379
|
+
return ALL_FIELD_TYPES["boolean"];
|
|
380
|
+
case GEOMETRY:
|
|
381
|
+
case GEOMETRY_FROM_STRING:
|
|
382
|
+
case PAIR_GEOMETRY_FROM_STRING:
|
|
383
|
+
return ALL_FIELD_TYPES.geojson;
|
|
384
|
+
case ARRAY:
|
|
385
|
+
return ALL_FIELD_TYPES.array;
|
|
386
|
+
case OBJECT:
|
|
387
|
+
return ALL_FIELD_TYPES.object;
|
|
388
|
+
case NUMBER:
|
|
389
|
+
case STRING:
|
|
390
|
+
case ZIPCODE:
|
|
391
|
+
return ALL_FIELD_TYPES.string;
|
|
392
|
+
case H3_ANALYZER_TYPE:
|
|
393
|
+
return ALL_FIELD_TYPES.h3;
|
|
394
|
+
default:
|
|
395
|
+
globalConsole.warn("Unsupported analyzer type: ".concat(aType));
|
|
396
|
+
return ALL_FIELD_TYPES.string;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Analyze field types from data in `string` format, e.g. uploaded csv.
|
|
402
|
+
* Assign `type`, `fieldIdx` and `format` (timestamp only) to each field
|
|
403
|
+
*
|
|
404
|
+
* @param data array of row object
|
|
405
|
+
* @param fieldOrder array of field names as string
|
|
406
|
+
* @returns formatted fields
|
|
407
|
+
* @public
|
|
408
|
+
* @example
|
|
409
|
+
*
|
|
410
|
+
* import {getFieldsFromData} from '@kepler.gl/common-utils';
|
|
411
|
+
* const data = [{
|
|
412
|
+
* time: '2016-09-17 00:09:55',
|
|
413
|
+
* value: '4',
|
|
414
|
+
* surge: '1.2',
|
|
415
|
+
* isTrip: 'true',
|
|
416
|
+
* zeroOnes: '0'
|
|
417
|
+
* }, {
|
|
418
|
+
* time: '2016-09-17 00:30:08',
|
|
419
|
+
* value: '3',
|
|
420
|
+
* surge: null,
|
|
421
|
+
* isTrip: 'false',
|
|
422
|
+
* zeroOnes: '1'
|
|
423
|
+
* }, {
|
|
424
|
+
* time: null,
|
|
425
|
+
* value: '2',
|
|
426
|
+
* surge: '1.3',
|
|
427
|
+
* isTrip: null,
|
|
428
|
+
* zeroOnes: '1'
|
|
429
|
+
* }];
|
|
430
|
+
*
|
|
431
|
+
* const fieldOrder = ['time', 'value', 'surge', 'isTrip', 'zeroOnes'];
|
|
432
|
+
* const fields = getFieldsFromData(data, fieldOrder);
|
|
433
|
+
* // fields = [
|
|
434
|
+
* // {name: 'time', format: 'YYYY-M-D H:m:s', fieldIdx: 1, type: 'timestamp'},
|
|
435
|
+
* // {name: 'value', format: '', fieldIdx: 4, type: 'integer'},
|
|
436
|
+
* // {name: 'surge', format: '', fieldIdx: 5, type: 'real'},
|
|
437
|
+
* // {name: 'isTrip', format: '', fieldIdx: 6, type: 'boolean'},
|
|
438
|
+
* // {name: 'zeroOnes', format: '', fieldIdx: 7, type: 'integer'}];
|
|
439
|
+
*
|
|
440
|
+
*/
|
|
441
|
+
export function getFieldsFromData(data, fieldOrder) {
|
|
442
|
+
// add a check for epoch timestamp
|
|
443
|
+
var analyzerData = data.map(function (row) {
|
|
444
|
+
var next = {};
|
|
445
|
+
var _iterator2 = _createForOfIteratorHelper(fieldOrder),
|
|
446
|
+
_step2;
|
|
447
|
+
try {
|
|
448
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
449
|
+
var key = _step2.value;
|
|
450
|
+
next[key] = toTypeAnalyzerValue(row[key]);
|
|
451
|
+
}
|
|
452
|
+
} catch (err) {
|
|
453
|
+
_iterator2.e(err);
|
|
454
|
+
} finally {
|
|
455
|
+
_iterator2.f();
|
|
456
|
+
}
|
|
457
|
+
return next;
|
|
458
|
+
});
|
|
459
|
+
var metadata = Analyzer.computeColMeta(analyzerData, [{
|
|
460
|
+
regex: /.*geojson|all_points/g,
|
|
461
|
+
dataType: 'GEOMETRY'
|
|
462
|
+
}, {
|
|
463
|
+
regex: /.*census/g,
|
|
464
|
+
dataType: 'STRING'
|
|
465
|
+
}], {
|
|
466
|
+
ignoredDataTypes: IGNORE_DATA_TYPES
|
|
467
|
+
});
|
|
468
|
+
var _renameDuplicateField = renameDuplicateFields(fieldOrder),
|
|
469
|
+
fieldByIndex = _renameDuplicateField.fieldByIndex;
|
|
470
|
+
var result = fieldOrder.map(function (field, index) {
|
|
471
|
+
var name = fieldByIndex[index];
|
|
472
|
+
var fieldMeta = metadata.find(function (m) {
|
|
473
|
+
return m.key === field;
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
// fieldMeta could be undefined if the field has no data and Analyzer.computeColMeta
|
|
477
|
+
// will ignore the field. In this case, we will simply assign the field type to STRING
|
|
478
|
+
// since dropping the column in the RowData could be expensive
|
|
479
|
+
var type = (fieldMeta === null || fieldMeta === void 0 ? void 0 : fieldMeta.type) || 'STRING';
|
|
480
|
+
var format = (fieldMeta === null || fieldMeta === void 0 ? void 0 : fieldMeta.format) || '';
|
|
481
|
+
|
|
482
|
+
// type-analyzer's unanchored year-first regex matches a prefix of slash dates
|
|
483
|
+
// (e.g. 09/17/20 in 09/17/2016). Correct only complete date samples. Keep the
|
|
484
|
+
// month-first default unless every sample is a valid day-first date and at
|
|
485
|
+
// least one day exceeds 12; ambiguous or conflicting samples cannot decide.
|
|
486
|
+
if (type === AnalyzerDATA_TYPES.DATE && ['YYYY/M/D', 'M/D/YYYY'].includes(format)) {
|
|
487
|
+
var samples = analyzerData.map(function (row) {
|
|
488
|
+
return row[field];
|
|
489
|
+
}).filter(notNullorUndefined);
|
|
490
|
+
if (samples.length && samples.every(function (value) {
|
|
491
|
+
return typeof value === 'string' && /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(value);
|
|
492
|
+
})) {
|
|
493
|
+
var hasDayFirstEvidence = false;
|
|
494
|
+
var allDayFirst = samples.every(function (value) {
|
|
495
|
+
var _String$split$map = String(value).split('/').map(Number),
|
|
496
|
+
_String$split$map2 = _slicedToArray(_String$split$map, 3),
|
|
497
|
+
day = _String$split$map2[0],
|
|
498
|
+
month = _String$split$map2[1],
|
|
499
|
+
year = _String$split$map2[2];
|
|
500
|
+
var date = new Date(0);
|
|
501
|
+
date.setUTCFullYear(year, month - 1, day);
|
|
502
|
+
hasDayFirstEvidence || (hasDayFirstEvidence = day > 12);
|
|
503
|
+
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
|
|
504
|
+
});
|
|
505
|
+
format = allDayFirst && hasDayFirstEvidence ? 'D/M/YYYY' : 'M/D/YYYY';
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// quick check if first valid string in column is H3
|
|
510
|
+
if (type === AnalyzerDATA_TYPES.STRING) {
|
|
511
|
+
for (var i = 0, n = analyzerData.length; i < n; ++i) {
|
|
512
|
+
var sampleValue = analyzerData[i][name];
|
|
513
|
+
if (typeof sampleValue === 'string') {
|
|
514
|
+
type = h3IsValid(sampleValue) ? H3_ANALYZER_TYPE : type;
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
if (notNullorUndefined(sampleValue)) {
|
|
518
|
+
break;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// quick check if string is hex wkb
|
|
524
|
+
if (type === AnalyzerDATA_TYPES.STRING) {
|
|
525
|
+
type = data.some(function (d) {
|
|
526
|
+
return isHexWkb(d[name]);
|
|
527
|
+
}) ? AnalyzerDATA_TYPES.GEOMETRY : type;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// The analyzer treats numeric-only WKB as NUMBER and GeoJSON strings as
|
|
531
|
+
// OBJECT. Only override these fallback types when every present sample
|
|
532
|
+
// describes geometry; unrelated objects, text and identifiers stay intact.
|
|
533
|
+
if ([AnalyzerDATA_TYPES.STRING, AnalyzerDATA_TYPES.NUMBER, AnalyzerDATA_TYPES.OBJECT].includes(type)) {
|
|
534
|
+
var _samples = analyzerData.map(function (row) {
|
|
535
|
+
return row[field];
|
|
536
|
+
}).filter(notNullorUndefined);
|
|
537
|
+
if (_samples.length && _samples.every(function (value) {
|
|
538
|
+
return isAdditionalGeometry(value) || type === AnalyzerDATA_TYPES.NUMBER && isCompleteWkb(value);
|
|
539
|
+
})) {
|
|
540
|
+
type = AnalyzerDATA_TYPES.GEOMETRY;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// read a column of numbers in scientific notation as real numbers instead of strings
|
|
545
|
+
if (type === AnalyzerDATA_TYPES.NUMBER && analyzerData.every(function (row) {
|
|
546
|
+
return isRealNumberSample(row[field]);
|
|
547
|
+
})) {
|
|
548
|
+
type = AnalyzerDATA_TYPES.FLOAT;
|
|
549
|
+
}
|
|
550
|
+
return {
|
|
551
|
+
name: name,
|
|
552
|
+
id: name,
|
|
553
|
+
displayName: name,
|
|
554
|
+
format: format,
|
|
555
|
+
fieldIdx: index,
|
|
556
|
+
type: analyzerTypeToFieldType(type),
|
|
557
|
+
analyzerType: type,
|
|
558
|
+
valueAccessor: function valueAccessor(dc) {
|
|
559
|
+
return function (d) {
|
|
560
|
+
return dc.valueAt(d.index, index);
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
});
|
|
565
|
+
return result;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* pass in an array of field names, rename duplicated one
|
|
570
|
+
* and return a map from old field index to new name
|
|
571
|
+
*
|
|
572
|
+
* @param fieldOrder
|
|
573
|
+
* @returns new field name by index
|
|
574
|
+
*/
|
|
575
|
+
export function renameDuplicateFields(fieldOrder) {
|
|
576
|
+
return fieldOrder.reduce(function (accu, field, i) {
|
|
577
|
+
var allNames = accu.allNames;
|
|
578
|
+
var fieldName = field;
|
|
579
|
+
|
|
580
|
+
// add a counter to duplicated names
|
|
581
|
+
if (allNames.includes(field)) {
|
|
582
|
+
var counter = 0;
|
|
583
|
+
while (allNames.includes("".concat(field, "-").concat(counter))) {
|
|
584
|
+
counter++;
|
|
585
|
+
}
|
|
586
|
+
fieldName = "".concat(field, "-").concat(counter);
|
|
587
|
+
}
|
|
588
|
+
accu.fieldByIndex[i] = fieldName;
|
|
589
|
+
accu.allNames.push(fieldName);
|
|
590
|
+
return accu;
|
|
591
|
+
}, {
|
|
592
|
+
allNames: [],
|
|
593
|
+
fieldByIndex: []
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
//# sourceMappingURL=data-type.js.map
|