geom is geometry(Point,4326) (planar degrees); add geog = geom::geography (STORED generated) + GiST index so ST_Distance/ST_DWithin/KNN work in real metres for nearest-vehicle and radius queries. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
18 lines
1.3 KiB
SQL
18 lines
1.3 KiB
SQL
-- 05_inc_geography.sql — fleettickets · add a geography column for routing/distance
|
|
-- ─────────────────────────────────────────────────────────────────────────────
|
|
-- `geom` is geometry(Point,4326) — great for display, but distance on it is in
|
|
-- planar degrees. For real-world routing ("nearest vehicle", radius search) we want
|
|
-- geography, whose ST_Distance / ST_DWithin work in metres on the spheroid.
|
|
--
|
|
-- `geog` is a STORED generated column = geom::geography (the cast is IMMUTABLE), so
|
|
-- it tracks geom automatically (incl. after geocode passes). GiST index supports
|
|
-- ST_DWithin(geog, vehicle::geography, metres) and KNN nearest-neighbour ordering.
|
|
-- Idempotent.
|
|
-- ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
SET search_path = tickets, public;
|
|
|
|
ALTER TABLE tickets.inc
|
|
ADD COLUMN IF NOT EXISTS geog geography(Point, 4326) GENERATED ALWAYS AS (geom::geography) STORED;
|
|
|
|
CREATE INDEX IF NOT EXISTS ix_inc_geog ON tickets.inc USING gist (geog);
|