Skip to content

Commit 8ef57d2

Browse files
committed
Implement WKT validation in data-type.ts
Add WKT validation function and regex for geometry types. Signed-off-by: bdjulbic <bdjulbic@foursquare.com>
1 parent 4bdf8f4 commit 8ef57d2

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

src/common-utils/src/data-type.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ import {h3IsValid} from './h3-utils';
1111

1212
const H3_ANALYZER_TYPE = 'H3';
1313

14+
// WKT (Well-Known Text) geometry prefixes.
15+
// Keep this intentionally lightweight: we only use it to prevent geometry strings
16+
// from being treated as generic text (e.g. for tooltip field auto-picking).
17+
const WKT_PREFIX_RE =
18+
/^(?:SRID=\d+\s*;\s*)?(?:POINT|LINESTRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)(?:\s+(?:Z|M|ZM))?\s*\(/i;
19+
20+
function isWkt(value: unknown): boolean {
21+
if (typeof value !== 'string') {
22+
return false;
23+
}
24+
25+
const s = value.trim();
26+
if (s.length < 10) {
27+
return false;
28+
}
29+
30+
// Quick structural checks to avoid regex work for typical strings.
31+
if (!s.includes('(') || !s.includes(')')) {
32+
return false;
33+
}
34+
35+
return WKT_PREFIX_RE.test(s);
36+
}
37+
1438
export const ACCEPTED_ANALYZER_TYPES = [
1539
AnalyzerDATA_TYPES.DATE,
1640
AnalyzerDATA_TYPES.TIME,
@@ -267,6 +291,11 @@ export function getFieldsFromData(data: RowData, fieldOrder: string[]): Field[]
267291
type = data.some(d => isHexWkb(d[name])) ? AnalyzerDATA_TYPES.GEOMETRY : type;
268292
}
269293

294+
// quick check if string is wkt
295+
if (type === AnalyzerDATA_TYPES.STRING) {
296+
type = data.some(d => isWkt(d[name])) ? AnalyzerDATA_TYPES.GEOMETRY_FROM_STRING : type;
297+
}
298+
270299
return {
271300
name,
272301
id: name,

0 commit comments

Comments
 (0)