postgis-distance
1
总安装量
1
周安装量
#47018
全站排名
安装命令
npx skills add https://github.com/mmbmf1/geospatial-skills --skill postgis-distance
Agent 安装分布
amp
1
opencode
1
cursor
1
kimi-cli
1
codex
1
github-copilot
1
Skill 文档
ST_Distance (Numeric Distance)
Use this skill when you need the numeric distance value between geometries (for reporting, ranking display, metadata).
If you need a boolean âwithin X distanceâ filter, prefer ST_DWithin (faster). A common pattern is:
- use
ST_DWithinto filter candidates - use
ST_Distanceto compute the returned distance value
Core rules
- Units depend on type:
geometryâ SRID units (degrees/meters/feet depending on SRID)geographyâ meters
- Geometries must be in the same SRID for geometry math
- Do not compute meaningful distances in EPSG:4326 geometry (degrees) unless you intend degrees
Canonical patterns
Distance in meters (recommended): geography
Works well when data is stored in EPSG:4326 and you want meters.
SELECT
ST_Distance(a.geom::geography, b.geom::geography) AS distance_m
FROM a
JOIN b ON ...;
Distance in projected SRID units: geometry
Use a projected/client SRID where units are meaningful (feet/meters).
SELECT
ST_Distance(
ST_Transform(a.geom, client_srid),
ST_Transform(b.geom, client_srid)
) AS distance_units
FROM a
JOIN b ON ...;
Fast filter + distance output (recommended pattern)
Filter candidates with ST_DWithin, then compute distance.
WITH p AS (
SELECT ST_SetSRID(ST_MakePoint($1, $2), 4326) AS geom
)
SELECT
f.*,
ST_Distance(f.geom::geography, p.geom::geography) AS distance_m
FROM features f, p
WHERE ST_DWithin(f.geom::geography, p.geom::geography, 5000) -- 5km
ORDER BY f.geom <-> p.geom
LIMIT 50;
This is a common âmap / nearbyâ endpoint pattern:
ST_DWithinconstrains work<->ranks quicklyST_Distanceprovides the numeric value
Common mistakes
- Using
ST_Distance(geom, geom)on EPSG:4326 geometry and assuming meters - Using
ST_Distance(...) < xin WHERE instead ofST_DWithin(...) - Mixing SRIDs without transforming
- Computing distances on huge tables without bounding the search
Summary
- Use
geographyfor meters - Use projected SRIDs for planar geometry distances
- Use
ST_DWithinfor filtering,ST_Distancefor reporting