Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
feat: Implement WKT validation in data-type.ts (#3298)
bdjulbiccbb3204cfc23
diff --git a/src/common-utils/src/data-type.ts b/src/common-utils/src/data-type.tsindex 4f94f36ff..0dc827917 100644--- a/src/common-utils/src/data-type.ts+++ b/src/common-utils/src/data-type.ts@@ -11,6 +11,27 @@ import {h3IsValid} from './h3-utils';const H3_ANALYZER_TYPE = 'H3';+// Returns true if the value is likely a WKT geometry string (heuristic check).+const WKT_PREFIX_RE =+ /^(?:SRID=\d+\s*;\s*)?(?:POINT|LINESTRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)(?:\s+(?:Z|M|ZM))?\s*\(/i;++export function isWkt(value: unknown): boolean {+ if (typeof value !== 'string') {+ return false;+ }++ const s = value.trim();+ if (s.length < 10) {+ return false;+ }++ if (!s.includes('(') || !s.includes(')')) {+ return false;+ }++ return WKT_PREFIX_RE.test(s);+}+export const ACCEPTED_ANALYZER_TYPES = [AnalyzerDATA_TYPES.DATE,AnalyzerDATA_TYPES.TIME,@@ -267,6 +288,11 @@ export function getFieldsFromData(data: RowData, fieldOrder: string[]): Field[]type = data.some(d => isHexWkb(d[name])) ? AnalyzerDATA_TYPES.GEOMETRY : type;}+ // quick check if string is wkt+ if (type === AnalyzerDATA_TYPES.STRING) {+ type = data.some(d => isWkt(d[name])) ? AnalyzerDATA_TYPES.GEOMETRY_FROM_STRING : type;+ }+return {name,id: name,diff --git a/test/node/utils/dataset-utils-test.js b/test/node/utils/dataset-utils-test.jsindex 1d7a18cd3..5cd62426f 100644--- a/test/node/utils/dataset-utils-test.js+++ b/test/node/utils/dataset-utils-test.js@@ -3,6 +3,7 @@import test from 'tape';import {findDefaultColorField, createNewDataEntry} from '@kepler.gl/utils';+import {isHexWkb, isWkt} from '@kepler.gl/common-utils';import {processCsvData} from '@kepler.gl/processors';@@ -88,4 +89,42 @@ test('datasetUtils.isHexWkb', t => {const validEWktNDR = '0020000001000013ff0000000000400000000000000040';t.ok(isHexWkb(validEWktNDR), 'A valid hex ewkb in NDR should be valid');+ t.end();+});++test('datasetUtils.isWkt', t => {+ // non-strings+ t.notOk(isWkt(null), 'null is not a valid WKT');+ t.notOk(isWkt(undefined), 'undefined is not a valid WKT');+ t.notOk(isWkt(123), 'number is not a valid WKT');+ t.notOk(isWkt({}), 'object is not a valid WKT');++ // regular strings / known non-WKT identifiers+ t.notOk(isWkt(''), 'empty string is not a valid WKT');+ t.notOk(isWkt('hello world'), 'regular string is not a valid WKT');+ t.notOk(isWkt('06075'), 'FIPS code should not be a valid WKT');+ t.notOk(isWkt('8a2a1072b59ffff'), 'H3 code should not be a valid WKT');++ // edge cases (missing coordinates / parentheses)+ t.notOk(isWkt('POINT'), 'POINT without coordinates should not be a valid WKT');+ t.notOk(isWkt('POINT 1 2'), 'POINT without parentheses should not be a valid WKT');+ t.notOk(isWkt('SRID=4326;POINT'), 'SRID prefix without geometry should not be a valid WKT');+ t.notOk(isWkt('POINT (1 2'), 'missing closing parenthesis should not be a valid WKT');+ t.notOk(isWkt('POINT ( )'), 'empty coordinates should not be a valid WKT');++ // valid WKT examples (heuristic)+ t.ok(isWkt('POINT (1 2)'), 'POINT should be recognized as WKT');+ t.ok(isWkt('POINT(1 2)'), 'POINT without space before parentheses should be recognized as WKT');+ t.ok(isWkt('LINESTRING (0 0, 1 1)'), 'LINESTRING should be recognized as WKT');+ t.ok(isWkt('POLYGON ((0 0, 1 0, 1 1, 0 0))'), 'POLYGON should be recognized as WKT');+ t.ok(+ isWkt('SRID=4326;POINT(1 2)'),+ 'WKT with SRID prefix should be recognized as WKT'+ );+ t.ok(isWkt('POINT Z (1 2 3)'), 'WKT with Z dimension should be recognized as WKT');++ // not WKT but contains parentheses+ t.notOk(isWkt('HELLO (1 2)'), 'non-WKT string with parentheses should not be valid WKT');++ t.end();});