is there any convenient API in JTS that would help to

  • find two most distant points in line
  • find two most distant points in polygon

I think all of the JTS methods relating to distance are looking for the nearest or shortest. But providing you don't have too many vertices then you could do a brute force O(N*N) search:

GeometryFactory gf = new GeometryFactory();
double maxDist = 0;
Point start = null, end = null;
for (Coordinate c : geom.getCoordinates()) {
  Point p = gf.createPoint(c);
  for (Coordinate c2 : geom.getCoordinates()) {
    Point p2 = gf.createPoint(c2);
    double d = DistanceOp.distance(p, p2);
    if (d > maxDist) {
      maxDist = d;
      start = p;
      end = p2;

And if you need to consider points along the lines then add

geom = Densifier.densify(geom, .01);

before you start, though obviously this pushes N up.

enter image description here

  • Thanks @Ian! I had a hope I don't have to do it. – Sergey Oct 20 '18 at 15:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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