I have .shp file of polygons and single (point) location.
How can I retrieve the closest polygon to that single location?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
Several ways to do this. One is to find the distance and order ascending.
SELECT geom FROM polys, points WHERE points.gid=1 AND ST_DWithin(poly.geom, points.geom, 1000) ORDER BY ST_Distance(a.geom, b.geom) LIMIT 1;
I added the ST_DWithin call to show how you might limit the number of candidates (it will make use of the index.)
I use ST_ClosestPoint to return the closest point from each polygone, then I calculate the min distance
SELECT foo.* from (SELECT min(st_distance(a.geom,ST_ClosestPoint(b.geom,a.geom))) from polyg a,point b) foo