Skill: spatial-data-quality
Invoked by:
geospatial-data-engineer(primary). Pairs withdesign-postgis-schema(the table the validated data lands in) andwrite-spatial-query(when a query crashes on invalid geometry).When to invoke: loading a Shapefile / GeoJSON / GeoPackage; "
ST_Area/ST_Unioncrashes on some rows"; "my points show up in the ocean"; hardening an ingest so bad geometry never lands.Output: the validity / SRID / coordinate-order / topology checks + a validate-at-load constraint or repair-on-ingest query, run at the boundary.
Bad geometry fails silently in some functions and loudly in others — gate it at the boundary.
The checks
- Validity:
ST_IsValid(geom)/ repair withST_MakeValid(geom). Self-intersections and bad ring order breakST_Area,ST_Intersection,ST_Union. - SRID present and correct:
ST_SRID(geom)is the declared SRID (not0); enforce with a typmodgeometry(Polygon, 4326). - Coordinate order / extent sanity: values plausibly within the CRS bounds (lon ∈ [-180,180], lat ∈ [-90,90] for 4326). A cluster off west Africa = swapped lon/lat.
- Topology (where it matters): no gaps / overlaps between adjacent polygons (e.g. admin boundaries) — check with
ST_Relate/ topology rules. - Empty / null geometry handled explicitly (
ST_IsEmpty).
Validate at load, not after
-- Reject invalid geometry on insert
ALTER TABLE parcels
ADD CONSTRAINT parcels_geom_valid CHECK (ST_IsValid(geom));
-- Or repair on the way in
INSERT INTO parcels (geom)
SELECT ST_MakeValid(ST_SetSRID(raw_geom, 4326)) FROM staging;
Loading checklist (ogr2ogr)
-s_srsset to the source CRS,-t_srsto the table CRS.-nlt PROMOTE_TO_MULTIwhere mixed single / multi geometry would otherwise fail.- Validate (
ST_IsValid) and reproject before the data is queryable.
Guardrails
- Trusting a file's claimed CRS without checking the extent is how lon/lat swaps land — sanity-check the extent (the GeoJSON-is-lon-lat trap).
- Never run area / union on un-validated polygons.
- Never let SRID
0(unknown) into a column — declare a typmod. See../../best-practices/validate-geometry-at-load.md,../../best-practices/geojson-is-lon-lat.md, and../../best-practices/always-store-an-srid.md.