Populate PostGIS geom column from GPX track data

Extract coordinates from parsed GPX tracks and store as PostGIS
LineString geometry via ST_GeomFromGeoJSON. Applied to:
- createRoute / updateRoute (routes.server.ts)
- createActivity / createRouteFromActivity (activities.server.ts)

Adds lineStringFromCoords() helper to the journal schema that
builds the SQL expression from [lon, lat] coordinate pairs.

Unblocks spatial queries (e.g. route discovery via ST_Intersects).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-04-02 17:05:02 +01:00
parent 47baa11e69
commit b4c3f97296
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9
3 changed files with 45 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import {
jsonb,
customType,
} from "drizzle-orm/pg-core";
import { sql, type SQL } from "drizzle-orm";
const bytea = customType<{ data: Buffer }>({
dataType() {
@ -14,12 +15,27 @@ const bytea = customType<{ data: Buffer }>({
},
});
const lineString = customType<{ data: string }>({
const lineString = customType<{ data: string | SQL }>({
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", {