GeometryUtils { public static double distanceTo(Geometry fromGeometry, Geometry toGeometry) { double distance = Double.MAX_VALUE; switch (fromGeometry.getGeometryType()) { case "Point": final GeoPoint fromGeoPoint = new GeoPoint((Point) fromGeometry); switch (toGeometry.getGeometryType()) { case "Point": distance = fromGeoPoint.distanceTo(new GeoPoint((Point) toGeometry)); break; case "LineString": case "LinearRing": for (int i = 0; i < toGeometry.getNumPoints(); i++) { double distanceFromPoint = fromGeoPoint.distanceTo(new GeoPoint(((LineString) toGeometry).getPointN(i))); if (distance > distanceFromPoint) { distance = distanceFromPoint; } } break; case "Polygon": distance = distanceTo(fromGeometry, ((Polygon) toGeometry).getExteriorRing()); break; default: Log.w(TAG, "distanceTo: no implementation found for geometry '" + toGeometry.getGeometryType() + "'"); break; } break; case "LineString": case "LinearRing": switch (toGeometry.getGeometryType()) { case "Point": distance = distanceTo(toGeometry, fromGeometry); break; case "LineString": case "LinearRing": for (int i = 0; i < fromGeometry.getNumPoints(); i++) { for (int j = 0; j < toGeometry.getNumPoints(); j++) { double distanceFromPoint = new GeoPoint(((LineString) fromGeometry).getPointN(i)).distanceTo(new GeoPoint(((LineString) toGeometry).getPointN(i))); if (distance > distanceFromPoint) { distance = distanceFromPoint; } } } break; case "Polygon": distance = distanceTo(fromGeometry, ((Polygon) toGeometry).getExteriorRing()); break; default: Log.w(TAG, "distanceTo: no implementation found for geometry '" + toGeometry.getGeometryType() + "'"); break; } break; case "Polygon": switch (toGeometry.getGeometryType()) { case "Point": distance = distanceTo(toGeometry, fromGeometry); break; case "LineString": case "LinearRing": distance = distanceTo(toGeometry, fromGeometry); break; case "Polygon": distance = distanceTo(((Polygon) toGeometry).getExteriorRing(), ((Polygon) fromGeometry).getExteriorRing()); break; default: Log.w(TAG, "distanceTo: no implementation found for geometry '" + toGeometry.getGeometryType() + "'"); break; } break; default: Log.w(TAG, "distanceTo: geometry " + fromGeometry.getGeometryType() + " not implemented"); } return distance; }  static double distanceTo(Geometry fromGeometry,
                                    Geometry toGeometry); static double getGeodesicLength(@NonNull final Geometry geometry); static double getGeodesicArea(@NonNull final LineString lineString); static double getGeodesicArea(@NonNull final Polygon polygon,
                                         boolean checkHoles);  }
@Test public void testDistanceBetweenTwoPoints() throws Exception { final double distance = distanceTo(createPoint(gf, 47.225782d, -1.554476d), createPoint(gf, 47.226468d, -1.554996d)); assertEquals(90d, distance, 1d); }
@Test public void testDistanceBetweenPointAndLineString() throws Exception { final double distance1 = distanceTo(createPoint(gf, 47.225782d, -1.554476d), createLineString(gf, createCoordinate(gf, 47.226468d, -1.554996d), createCoordinate(gf, 47.226600d, -1.554846d))); assertEquals(90d, distance1, 1d); final double distance2 = distanceTo(createLineString(gf, createCoordinate(gf, 47.226468d, -1.554996d), createCoordinate(gf, 47.226600d, -1.554846d)), createPoint(gf, 47.225782d, -1.554476d)); assertEquals(90d, distance2, 1d); }
@Test public void testDistanceBetweenPointAndPolygon() throws Exception { final double distance1 = distanceTo(createPoint(gf, 47.225782d, -1.554476d), createPolygon(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d))); assertEquals(36d, distance1, 1d); final double distance2 = distanceTo(createPolygon(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d)), createPoint(gf, 47.225782d, -1.554476d)); assertEquals(36d, distance2, 1d); }
@Test public void testDistanceBetweenTwoLineStrings() throws Exception { final double distance = distanceTo(createLineString(gf, createCoordinate(gf, 47.226468d, -1.554996d), createCoordinate(gf, 47.226600d, -1.554846d)), createLineString(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d))); assertEquals(58d, distance, 1d); }
@Test public void testDistanceBetweenLineStringAndPolygon() throws Exception { final double distance1 = distanceTo(createLineString(gf, createCoordinate(gf, 47.226468d, -1.554996d), createCoordinate(gf, 47.226600d, -1.554846d)), createPolygon(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d))); assertEquals(58d, distance1, 1d); final double distance2 = distanceTo(createPolygon(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d)), createLineString(gf, createCoordinate(gf, 47.226468d, -1.554996d), createCoordinate(gf, 47.226600d, -1.554846d))); assertEquals(58d, distance2, 1d); }
@Test public void testDistanceBetweenTwoPolygons() throws Exception { final double distance = distanceTo(createPolygon(gf, createCoordinate(gf, 47.226116d, -1.554169d), createCoordinate(gf, 47.226126d, -1.554097d), createCoordinate(gf, 47.225527d, -1.553986d), createCoordinate(gf, 47.225519d, -1.554061d), createCoordinate(gf, 47.226116d, -1.554169d)), createPolygon(gf, createCoordinate(gf, 47.226219d, -1.554430d), createCoordinate(gf, 47.226237d, -1.554261d), createCoordinate(gf, 47.226122d, -1.554245d), createCoordinate(gf, 47.226106d, -1.554411d), createCoordinate(gf, 47.226219d, -1.554430d))); assertEquals(19d, distance, 1d); }