Two closest points on boundary of Postgis geometry Two closest points on boundary of Postgis geometry postgresql postgresql

Two closest points on boundary of Postgis geometry


Use ST_DumpPoints() to dump the points of the polygon, then select from that order by ST_Distance to A limit 2. ?

So it is something like

SELECT * from ST_DumpPoints(poly) order by ST_Distance(A,geom) asc limit 2;

(assumes that this is an inner select where poly is the polygon, A is the point to compare to and geom is the geom column of one of the points in the poly being compared)


There generally is no second closest point on the boundary polygon, if you include the lines. Just like there is no real number second closest to zero.Either you only wish to consider the points at the corners, like Markus suggests.Or you have only one closest point.


1) Kind of a left-field idea, but to find the second-closest point to your destination, why not find the closest point to the point you already found?

2) Or, more germaine to your specific question,

  • find the set of points within some reasonable range of the point,
  • find the intersection of that set with the set of points lying on the polygon border (which I am guessing may be another PostGIS function; haven't used postG in a while so I'm not sure)

3) Farther into left field, dump some of your dataset into Mongo and use the $near function... http://docs.mongodb.org/manual/reference/operator/near/