This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

find points that are a certain distance from another point

0

Hi I have a java program which I want to query my PostGIS databaes and return those nodes that are a certain distance from a given centre point.

I am not sure how to pass the centre point into the query. I have the following so far:

 public ArrayList<Node> getOsmPoiNodes(Point centrePoint) throws SQLException {

        /* 
         * Create a statement and execute a select query. 
         */ 
        Statement s = conn.createStatement(); 
        String sql = "select ST_Distance(" + centrePoint + ",way), ST_AsGeoJSON(ST_Transform(way,94326) from planet_osm_point;";

How could I pass the PostGis Point object in properly?

Also, when I am defining the distance from the centre point. Can I simply do a HAVING(ST_Distance(centrePoint, way) <= '100' where 100 would be in metres?

Thanks

asked 03 Dec '12, 16:30

srose's gravatar image

srose
161101016
accept rate: 0%


One Answer:

0

Use ST_GeomFromText:

Example: select st_geomfromtext('point(-23.4 -46.2)'::text, 93426); -- Lat/Lon/SRID

I would say that, unless some other optimizations are made, that this way of querying the database would - most likely - be VERY slow.

See: ST_Distance and ST_GeomFromText

answered 22 Dec '12, 22:13

MCPicoli's gravatar image

MCPicoli
2.2k133047
accept rate: 24%

Source code available on GitHub .