postgis-nearest
Nearest Feature (KNN / <->)
Use this skill when you need the nearest feature to a point/geometry (e.g., nearest feeder, nearest segment, nearest hub).
PostGIS supports fast nearest-neighbor searches using the KNN operator <-> when a GiST index exists.
When to use
- “Find the nearest X to this point”
- “Rank candidates by proximity”
- “Pick the closest feature per input row”
Core rules
- KNN
<->works best with GiST indexes on geometry columns <->orders by distance in the geometry’s coordinate system (SRID units)- For meaningful distances, use a projected SRID or
geography(meters) - Keep queries index-friendly: avoid wrapping the indexed column in transforms inside ORDER BY when possible
More from mmbmf1/geospatial-skills
geojson-postgis
Serialize PostGIS geometry rows into a GeoJSON FeatureCollection for map display (4326, Feature + properties).
21geojson-points
Convert JSON rows with latitude/longitude fields into a GeoJSON FeatureCollection using raw PostGIS SQL.
16geojson-wkt
Convert JSON rows with WKT geometry strings into a GeoJSON FeatureCollection using raw PostGIS SQL.
13postgis-distance
Compute numeric distances safely with ST_Distance (geometry vs geography units) and pair with ST_DWithin for performance.
9postgis-dwithin
Distance-based spatial filtering with ST_DWithin, using index-friendly and unit-safe patterns.
8postgis-extract-xy
Extract longitude and latitude from PostGIS geometries using ST_X and ST_Y safely.
7