I am trying to get from a particular point more informations about the surroundings (which are also points). So I wrote this:
SELECT *
FROM osm_point
WHERE ST_DWithin(way,(ST_SetSRID(ST_MakePoint(12.2385578688105, 51.8435615931295), 4326, 1000);
But there is an error
ERROR: FEHLER: LINE 3: ...T_MakePoint(12.2385578688105, 51.8435615931295), 4326, 1000) ^ SQL state: 42601 Character: 133
What is wrong?
Answer
The SQL error 42601 is a syntax error (see here). Basically, you had an extra opening parenthesis before ST_SetSRID
and also a closing missing parenthesis for the same function.
If data type of osm_point.way
column is geometry
, one needs to cast it to geography
in order to ST_DWithin evaluate to geodetic distances (instead of planar distances) and in meters (if units of spatial reference aren't already in meters). The same cast to geography
is valid for ST_MakePoint
which output is of type geometry
and the units of applied CRS 4326 is degrees, but you want meters.
So, use the following query:
SELECT *
FROM osm_point
WHERE ST_DWithin(way::geography, ST_SetSRID(ST_MakePoint(12.2385578688105, 51.8435615931295), 4326)::geography, 1000);
Update: when using ST_DWithin
one needs to compare data which have the same SRID. So, if column osm_point.way
has a different SRID (you said EPSG:3857) from EPSG:4326 (the SRID you used to define the coordinates with ST_MakePoint
), one needs to transform coordinates first. For that use ST_Transform. See:
SELECT *
FROM osm_point
WHERE ST_DWithin(ST_Transform(way, 4326)::geography, ST_SetSRID(ST_MakePoint(12.2385578688105, 51.8435615931295), 4326)::geography, 1000);
About using indexes to speed up your queries, as suggested by Vince, take a look in:
How to properly set up indexes for PostGIS distance queries?.
No comments:
Post a Comment