<< All versions

Skill v1.0.0

currentAutomated scan100/100
kendrick-oppong/geoskills/postgis-geospatial
──Details
PublishedNovember 15, 2026 at 08:54 AM
Content Hashsha256:a57855212fbdaedc...
Git SHA77bd91343bcb
──Files
Files (1 file, 4.4 KB)
SKILL.md4.4 KBactive
SKILL.md · 82 lines · 4.4 KB

name: postgis-geospatial description: Use for geospatial SQL workflows in PostgreSQL/PostGIS — geometry vs geography choices, indexing, SRID handling, spatial joins, migrations, and query performance tuning. Relevant when the backend stack is NestJS/Node/TypeORM/Prisma over Postgres. metadata: version: "1.0.0"


PostGIS

When to use this skill

  • Designing or reviewing a schema with a geometry/geography column
  • Writing or optimizing spatial SQL (ST_Intersects, ST_DWithin, ST_Within, spatial joins)
  • Debugging slow spatial queries in a production Postgres database
  • Wiring PostGIS into a Node/NestJS backend (via pg, TypeORM, Prisma, or Kysely)

Core decisions

`geometry` vs `geography`:

geometrygeography
MathPlanar (Cartesian)Spheroidal (great-circle)
SpeedFasterSlower (more expensive math)
Correct forRegional/local data with an appropriate projected CRSGlobal data, or when you want correct distances without manual reprojection
Default recommendationUse geometry + a suitable projected CRS for anything scoped to a country/regionUse geography for global-scale "distance in meters between any two points on Earth" without projection juggling

Column & naming conventions (industry-standard, keep consistent across a codebase):

  • Geometry column named geom, geography column named geog
  • Always set an explicit SRID — never leave it at 0
  • Store one geometry type per table where feasible; use GEOMETRY (generic) only when genuinely mixed

Checklist

  • [ ] GEOMETRY column has an explicit SRID (geometry(Polygon, 4326), not bare geometry)
  • [ ] GiST index exists on every geometry/geography column used in a WHERE/JOIN predicate
  • [ ] Spatial joins use the sargable operators (&&, ST_Intersects, ST_DWithin) so the planner can use the index — avoid wrapping the geometry column in a transform inside the predicate (ST_Transform(geom, x) = ... defeats the index unless a matching expression index exists)
  • [ ] For "within X meters" queries on geometry type, reproject to a metric CRS or add a bbox pre-filter (&&) before the exact distance check
  • [ ] Migrations are idempotent (CREATE EXTENSION IF NOT EXISTS postgis, IF NOT EXISTS on indexes) — critical if managed via TypeORM/Prisma migration files
  • [ ] VACUUM ANALYZE after large bulk loads so the planner has fresh statistics for spatial columns
  • [ ] Use EXPLAIN (ANALYZE, BUFFERS) to confirm index usage, not just query correctness

Common pitfalls

  • Prisma/TypeORM don't natively model PostGIS types: geometry columns typically need raw SQL migrations and $queryRaw/createQueryBuilder raw fragments for spatial predicates — plan for this rather than fighting the ORM.
  • N+1 spatial joins: looping application-side over rows to check ST_Intersects per-row instead of a single indexed SQL join.
  • Slow `ST_DWithin` because of missing bbox pre-filter on non-indexed expressions: always let the index-friendly operator run first.
  • Storing lat/lon as separate float columns "for simplicity": loses every spatial indexing and query benefit — use a real geometry/geography column even for simple point data.
  • Reprojecting inside the hot query path repeatedly: reproject once at write time and store an additional column in the target CRS if a specific fast query pattern needs it.

Reference snippets

sql
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE TABLE sites (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
geom geometry(Point, 4326) NOT NULL
);
CREATE INDEX idx_sites_geom ON sites USING GIST (geom);
-- Nearby search within 500m, index-assisted bbox pre-filter + exact geography distance
SELECT id, name FROM sites
WHERE geom && ST_Expand(ST_SetSRID(ST_MakePoint(:lon, :lat), 4326), 0.01)
AND ST_DWithin(geom::geography, ST_SetSRID(ST_MakePoint(:lon, :lat), 4326)::geography, 500);
typescript
// NestJS + raw query via TypeORM DataSource
const nearby = await dataSource.query(
`SELECT id, name, ST_AsGeoJSON(geom) as geojson FROM sites
WHERE ST_DWithin(geom::geography, ST_SetSRID(ST_MakePoint($1,$2),4326)::geography, $3)`,
[lon, lat, radiusMeters],
);

Related skills

  • spatial-indexing-strategy — index type/strategy fundamentals
  • crs-and-reprojection — SRID and datum handling
  • geospatial-api-design — exposing PostGIS query results through a REST/GraphQL API
All versions