Categories:

Geospatial Functions

ST_DWITHIN

Returns TRUE if the minimum geodesic distance between two points is within the specified distance. Otherwise, returns FALSE.

If the parameters are GEOGRAPHY values that are not points (e.g. lines or polygons), this returns TRUE or FALSE based on the minimum geodesic distance between the two closest points of the two values.

Syntax

ST_DWITHIN( <geography_expression_1> , <geography_expression_2> , <distance_in_meters> )

Arguments

geography_expression_1

The argument must be an expression of type GEOGRAPHY.

geography_expression_2

The argument must be an expression of type GEOGRAPHY.

distance_in_meters

The argument must be an expression of type REAL. The distance is in meters.

Returns

Returns a BOOLEAN.

Usage Notes

  • Returns NULL if any input is NULL.

  • The expression:

    ST_DWITHIN(g1, g2, distance_expression)
    

    is equivalent to:

    ST_DISTANCE(g1, g2) <= distance_expression
    

    but executes faster in some cases.

Examples

This returns TRUE because the distance in meters between two points 1 degree apart along the equator is less than 150,000 meters:

SELECT ST_DWITHIN (ST_MAKEPOINT(0, 0), ST_MAKEPOINT(1, 0), 150000);
+-------------------------------------------------------------+
| ST_DWITHIN (ST_MAKEPOINT(0, 0), ST_MAKEPOINT(1, 0), 150000) |
|-------------------------------------------------------------|
| True                                                        |
+-------------------------------------------------------------+