Fix PostGIS geom population: use parseGpxAsync + raw SQL
The initial approach (#150) didn't work because: 1. routes.server.ts used parseGpx (sync, needs browser DOMParser) instead of parseGpxAsync (uses linkedom on Node.js) — GPX parsing silently failed, so stats AND geom were never populated 2. Drizzle's customType doesn't pass SQL expressions through toDriver Fixes: - Switch to parseGpxAsync for all server-side GPX parsing - Use db.execute() with raw ST_GeomFromGeoJSON SQL after insert - Remove unused lineStringFromCoords helper from schema - Share setGeomFromGpx between routes and activities - Add unit tests for GPX→GeoJSON coordinate extraction Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
feeca58064
commit
6ed828ef9c
4 changed files with 104 additions and 52 deletions
|
|
@ -7,7 +7,6 @@ import {
|
|||
jsonb,
|
||||
customType,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { sql, type SQL } from "drizzle-orm";
|
||||
|
||||
const bytea = customType<{ data: Buffer }>({
|
||||
dataType() {
|
||||
|
|
@ -15,27 +14,12 @@ const bytea = customType<{ data: Buffer }>({
|
|||
},
|
||||
});
|
||||
|
||||
const lineString = customType<{ data: string | SQL }>({
|
||||
const lineString = customType<{ data: string }>({
|
||||
dataType() {
|
||||
return "geometry(LineString, 4326)";
|
||||
},
|
||||
toDriver(value) {
|
||||
return value;
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Build a SQL expression to create a PostGIS LineString from coordinate pairs.
|
||||
* Coordinates are [lon, lat] (GeoJSON order).
|
||||
*/
|
||||
export function lineStringFromCoords(coords: [number, number][]): SQL {
|
||||
const geojson = JSON.stringify({
|
||||
type: "LineString",
|
||||
coordinates: coords,
|
||||
});
|
||||
return sql`ST_GeomFromGeoJSON(${geojson})`;
|
||||
}
|
||||
|
||||
export const journalSchema = pgSchema("journal");
|
||||
|
||||
export const users = journalSchema.table("users", {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue