postgis-dwithin

ST_DWithin (Distance / Proximity Queries)

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "postgis-dwithin" with this command: npx skills add mmbmf1/geospatial-skills/mmbmf1-geospatial-skills-postgis-dwithin

ST_DWithin (Distance / Proximity Queries)

Use this skill when you need to find features within a given distance of another geometry:

  • “points within X meters of a line”

  • proximity searches around a point

  • replacing expensive buffer + intersects patterns

Core rules

  • ST_DWithin is faster than ST_Distance < x

  • Units depend on input type:

  • geometry → SRID units

  • geography → meters

  • Geometries must be in the same SRID

  • Prefer ST_DWithin over buffering when you only need a boolean test

Canonical patterns

Geometry (projected CRS, SRID units)

Use when your data is stored in a projected SRID (feet/meters).

SELECT * FROM points p JOIN lines l ON ST_DWithin(p.geom, l.geom, 5);

Distance 5 is in the SRID’s units.

Geography (meters, easiest default)

Use when geometry is stored in EPSG:4326 and you want meters.

SELECT * FROM points p JOIN lines l ON ST_DWithin( p.geom::geography, l.geom::geography, 5 );

Distance 5 is meters.

Transform once (projected math, better for large jobs)

When using a client/project SRID:

SELECT * FROM points p JOIN lines l ON ST_DWithin( ST_Transform(p.geom, client_srid), ST_Transform(l.geom, client_srid), 5 );

Transforming both sides is acceptable here because ST_DWithin can still use indexes in many cases, but prefer transforming the smaller side if possible.

Point near line (common validation pattern)

SELECT p.* FROM points p JOIN lines l ON ST_DWithin( p.geom::geography, l.geom::geography, 5 );

Use this instead of buffering lines unless you need the actual corridor geometry.

Index requirements

Ensure GiST indexes exist:

CREATE INDEX IF NOT EXISTS points_geom_gix ON points USING gist (geom); CREATE INDEX IF NOT EXISTS lines_geom_gix ON lines USING gist (geom);

ST_DWithin can leverage spatial indexes when used correctly.

Common mistakes

  • Using ST_Distance(...) < x instead of ST_DWithin

  • Forgetting geometry vs geography unit differences

  • Mixing SRIDs without transforming

  • Buffering geometries just to test proximity

Summary

  • Prefer ST_DWithin for distance tests

  • Use geography for meters when in 4326

  • Use projected SRIDs for heavy planar work

  • Index geometry columns

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

geojson-postgis

No summary provided by upstream source.

Repository SourceNeeds Review
General

geojson-points

No summary provided by upstream source.

Repository SourceNeeds Review
General

geojson-wkt

No summary provided by upstream source.

Repository SourceNeeds Review
General

postgis-distance

No summary provided by upstream source.

Repository SourceNeeds Review