The definition of SRID 4326 is in fact IN DEGREES. ALso as it says in the ST_DWithin page:
Returns true if the geometries are within the specified distance of one another. For geometry units are in those of spatial reference and For geography units are in meters and measurement is defaulted to use_spheroid=true (measure around spheroid),
Since you're are using the 4326 SRID, you can cast from geometry to geography and vice versa like this :
select st_distance(<geom_column>::geography) from myTable;
Most of the functions in postgis are overloaded, meaning, they return values and run a different procedure based on the input data type.
eg:
boolean ST_DWithin(geometry g1, geometry g2, double precision distance_of_srid);
boolean ST_DWithin(geography gg1, geography gg2, double precision distance_meters);
they have the same name, but the first function returns T/F based on units of the srid, and the second returns T/F based on meters.
If you plug into the function geog types, for its calculation, will take into account the curve of the earth so the algorithm 'costs' a bit more.
If you want to check how much more it costs, you can run for each query an EXPLAIN-ANALYSE
Always be aware of the axis order. PostGIS always uses the axis order: longitude (x), latitude (y).
-- Somewhere in Chicago, USA
SELECT ST_AsText(ST_Transform(ST_SetSRID(ST_Point(-87.642970, 41.937832), 4326), 3857));
-- POINT(-9756370.79201015 5151671.52336743)
-- Somewhere in Antarctica
SELECT ST_AsText(ST_Transform(ST_SetSRID(ST_Point(41.937832, -87.642970), 4326), 3857));
-- POINT(4668498.10321386 -24771604.6229288)
Best Answer
As an approximation suitable for medium-small radii, use the
cos()
of the latitude to scale up the true distance to a "distance" that makes sense for your latitude in web mercator, as described here.You only want to do this for small/medium radii because the scaling only makes sense at one latitude. The further you get from your reference latitude, the more wrong your radius gets. If you want a "perfect" result, you could pair this approach with a test on
ST_DistanceSpheroid()
to pare the results down to items exactly within the radius.