<< All versions
Skill v1.0.0
currentAutomated scan100/100kendrick-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/geographycolumn - 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`:
| geometry | geography | ||
|---|---|---|---|
| Math | Planar (Cartesian) | Spheroidal (great-circle) | |
| Speed | Faster | Slower (more expensive math) | |
| Correct for | Regional/local data with an appropriate projected CRS | Global data, or when you want correct distances without manual reprojection | |
| Default recommendation | Use geometry + a suitable projected CRS for anything scoped to a country/region | Use 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 namedgeog - 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
- [ ]
GEOMETRYcolumn has an explicit SRID (geometry(Polygon, 4326), not baregeometry) - [ ] GiST index exists on every geometry/geography column used in a
WHERE/JOINpredicate - [ ] 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
geometrytype, 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 EXISTSon indexes) — critical if managed via TypeORM/Prisma migration files - [ ]
VACUUM ANALYZEafter 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/createQueryBuilderraw fragments for spatial predicates — plan for this rather than fighting the ORM. - N+1 spatial joins: looping application-side over rows to check
ST_Intersectsper-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 distanceSELECT id, name FROM sitesWHERE 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 DataSourceconst nearby = await dataSource.query(`SELECT id, name, ST_AsGeoJSON(geom) as geojson FROM sitesWHERE ST_DWithin(geom::geography, ST_SetSRID(ST_MakePoint($1,$2),4326)::geography, $3)`,[lon, lat, radiusMeters],);
Related skills
spatial-indexing-strategy— index type/strategy fundamentalscrs-and-reprojection— SRID and datum handlinggeospatial-api-design— exposing PostGIS query results through a REST/GraphQL API