[GIS] PostGIS query of point feature ~ Find nearest point to query location

postgissql

I am querying a point feature using a PostGIS request fired in JavaScript. The decoded URL looks like this and returns great results for polygon features.

http://test.tablelocation.com/api/v1/sql/?q=select '+tempCoi+' from '+tempTableName+' where ST_Intersects(the_geom, ST_GeomFromText('POINT('+clickLng+' '+clickLat+')',4326))&format=json&diagnostics=true&_maxage=86400&callback=?

As you can see, there are a few dynamic Javascript variables that are injected into each URL request. Two of these variables (clickLng and clickLat) specify the lat/lng of my query. They are generated based on where the user clicks on a google map. I am returning one attribute from the point layer referenced via the tempCoi variable. When I try to query a point feature, no data is returned.

I am guessing that this is because the location of my query is too specific (i.e. 6+ decimal points).

To make a long question shorter, is there a way to fuzz my data request.

For example, if I click at 45.123456N and -113.654987W how could I look for the closest point to that query?

Hope this is clear…

Best Answer

Passing SQL in your URLs directly looks risky to me.

Otherwise, the method you are looking for is https://postgis.net/docs/ST_DWithin.html

Related Question