8

Using JTS, how to find the nearest point on a polygon's boundary from an outside point?

user521044
  • 91
  • 1
  • 3

1 Answers1

11

Check out the DistanceOp. It returns an array of Coordinates:

Coordinate[] pts = DistanceOp.closestPoints(poly, outsidePoint);

pts[0] will be the point on the first geometry (in this case the polygon) and pts[1] will be on the second geometry (so, the same outside point you just passed in).

LEDfan
  • 175
  • 1
  • 2
  • 17
Eggplant Jeff
  • 1,749
  • 2
  • 15
  • 20
  • 2
    The solution that says: "Eggplant Jeff" is fantastic. But the newest versions of the [DistanceOP](http://www.jarvana.com/jarvana/view/com/vividsolutions/jts/1.11/jts-1.11-javadoc.jar!/com/vividsolutions/jts/operation/distance/DistanceOp.html) API has been marked the method "closestPoints" as "deprecated". You can use nearestPoints() instead! – Ignacio Rubio Jul 09 '13 at 11:14