PostGISDistance Relationships

ST_3DDWithin

What is ST_3DDWithin?

ST_3DDWithin returns true if two geometries are within a given 3D Euclidean distance of each other, using X, Y, and Z coordinates. It is the 3D counterpart of ST_DWithin and is index-accelerated by an N-D GiST index.

SQL
ST_3DDWithin(geometry g1, geometry g2, float distance)boolean

Distance is in the CRS units of the inputs — typically metres for projected 3D data. There is no geography overload.

When would you use ST_3DDWithin?

Use ST_3DDWithin for any thresholded 3D proximity test: finding trees within 5 m of power lines taking elevation into account, pipes within 2 m of foundations in a BIM model, or aircraft tracks within a vertical safety envelope of terrain. It avoids the pitfall of ST_DWithin reporting false positives for features that cross in plan view but are at different elevations.

To use the index, create an N-D GiST index on each input:

Code
undefined

FAQs

Which index should I create?

An N-dimensional GiST index: CREATE INDEX ON tbl USING GIST (geom gist_geometry_ops_nd). The default 2D GiST index does not accelerate 3D distance queries.

What units does the distance parameter use?

The CRS units of the inputs. For projected 3D data in metres (e.g. UTM + metres elevation), the threshold is in metres. There is no geography overload.

What if the inputs are 2D?

Missing Z is treated as 0. Mixed 2D/3D inputs produce misleading results — ensure all inputs are 3D before calling.

Is it symmetric?

Yes. ST_3DDWithin(a, b, d) = ST_3DDWithin(b, a, d).