I want to return all the houses for sale within 500 meters from my own house.
I have this query:
SELECT * FROM houses_for_sale WHERE postgis.ST_DWithin(coordinates, $1, 500);
It returns houses located anywhere -- not just within 500 meters. Both "coordinates" and $1 are PostGIS geometry
types, using the SRID 4326
.
After spending a million hours sweating and swearing at the usual total lack of clarity from the manual and every other kind of resource I can find, it eventually turned out that my coordinates use something called "degrees" instead of meters:
UNIT["degree",0.0174532925199433,
I have no idea what a "degree" is or how I turn them into meters. I want meters -- not "degrees".
I find all of this to be total insanity. Why can't maps and coordinates be simple and consistent? Why must every single person who has ever looked at a map invent their own completely incompatible and weird format for coordinates? I swear, there are more "map systems" than there are people on this planet. The variations are absolutely endless. I've never found two map/coordinate sources that used the same or even slightly compatible systems. In practice, this means that they are all useless because it's so difficult to convert between them.
ST_DWithin
will compute spherical distance in meters when its first two parameters are of thegeography
type. While "coordinates" is an awful name for something as sophisticated as ageometry
object, all you need to do is cast it and the variable with::geography
and distance will be computed in meters.