Skip to content

Commit 7673886

Browse files
committed
Add support for geoparquet geometries with WKT (well know text) #721
1 parent fee8e7a commit 7673886

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

web-client/src/utils/SrParquetUtils.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,41 @@ function parseWkbPoint(wkb: Uint8Array): { x: number; y: number; z?: number } |
499499
return { x, y, z };
500500
}
501501

502+
/**
503+
* Parse WKT Point geometry to extract x, y, z coordinates
504+
* WKT Point formats:
505+
* - "POINT (x y)"
506+
* - "POINT Z (x y z)"
507+
* - "POINT M (x y m)"
508+
* - "POINT ZM (x y z m)"
509+
*/
510+
function parseWktPoint(wkt: string): { x: number; y: number; z?: number } | null {
511+
if (!wkt || typeof wkt !== 'string') {
512+
return null;
513+
}
514+
515+
// Match POINT, POINT Z, POINT M, or POINT ZM
516+
// Coordinates are space-separated numbers inside parentheses
517+
const match = wkt.match(/POINT\s*(?:Z|M|ZM)?\s*\(\s*([-+]?[\d.]+(?:[eE][-+]?\d+)?)\s+([-+]?[\d.]+(?:[eE][-+]?\d+)?)(?:\s+([-+]?[\d.]+(?:[eE][-+]?\d+)?))?(?:\s+([-+]?[\d.]+(?:[eE][-+]?\d+)?))?\s*\)/i);
518+
519+
if (!match) {
520+
console.warn('Could not parse WKT Point:', wkt);
521+
return null;
522+
}
523+
524+
const x = parseFloat(match[1]);
525+
const y = parseFloat(match[2]);
526+
const z = match[3] ? parseFloat(match[3]) : undefined;
527+
// Note: match[4] would be M value if present, which we ignore
528+
529+
if (isNaN(x) || isNaN(y)) {
530+
console.warn('Invalid coordinates in WKT Point:', wkt);
531+
return null;
532+
}
533+
534+
return { x, y, z };
535+
}
536+
502537
interface RecordInfo {
503538
time?: string;
504539
as_geo?: boolean;
@@ -562,11 +597,21 @@ export async function exportCsvStreamed(fileName: string, headerCols: Ref<string
562597
const lines = rows.map(row => {
563598
let processedRow = row;
564599

565-
// If expanding geometry, parse WKB and add x,y,z columns
600+
// If expanding geometry, parse WKB or WKT and add x,y,z columns
566601
if (expandGeometry && geometryColumn && recordInfo) {
567-
const wkb = row[geometryColumn] as Uint8Array;
568-
if (wkb) {
569-
const coords = parseWkbPoint(wkb);
602+
const geomValue = row[geometryColumn];
603+
let coords: { x: number; y: number; z?: number } | null = null;
604+
605+
if (geomValue) {
606+
// Detect format: WKB (Uint8Array) or WKT (string)
607+
if (geomValue instanceof Uint8Array) {
608+
coords = parseWkbPoint(geomValue);
609+
} else if (typeof geomValue === 'string') {
610+
coords = parseWktPoint(geomValue);
611+
} else {
612+
console.warn('Unknown geometry format:', typeof geomValue);
613+
}
614+
570615
if (coords) {
571616
const xColName = recordInfo.x || 'lon';
572617
const yColName = recordInfo.y || 'lat';

0 commit comments

Comments
 (0)