5

I have .shp file of polygons and single (point) location.

How can I retrieve the closest polygon to that single location?

enter image description here

  • 1
    Any illustration can be a plus – elmo Aug 21 '15 at 13:26
  • @elmo Please see my Edit – michael Aug 21 '15 at 13:29
  • 1
    this question is not really a duplicate of gis.stackexchange.com/questions/14456. That question is about finding closest Geometry in the same table. This question is about finding the closest geometry in a different table. – tinlyx Mar 23 '18 at 9:13
9

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.)

2

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
  • Thanks, but the result is the minimum distance, While I need the geom representing the closest polygon. – michael Aug 21 '15 at 13:59
  • 3
    seems like your question refer to this link gis.stackexchange.com/questions/14456/… – elmo Aug 21 '15 at 15:06
  • could also use ST_ClosestPoint(), and find any polygons which ST_Intersects() the closest point – Steven Kay Aug 21 '15 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.