GeometryUtils { public static double getGeodesicLength(@NonNull final Geometry geometry) { double length = 0.0; switch (geometry.getGeometryType()) { case "LineString": case "LinearRing": if (!geometry.isEmpty()) { for (int i = 1; i < geometry.getNumPoints(); i++) { length += new GeoPoint(((LineString) geometry).getPointN(i)).distanceTo(new GeoPoint(((LineString) geometry).getPointN(i - 1))); } } break; case "Polygon": final LineString exteriorRing = ((Polygon) geometry).getExteriorRing(); length = getGeodesicLength(exteriorRing); if (!exteriorRing.isClosed()) { length += new GeoPoint(exteriorRing.getEndPoint()).distanceTo(new GeoPoint(exteriorRing.getStartPoint())); } break; default: Log.w(TAG, "getGeodesicLength: no implementation found for geometry '" + geometry.getGeometryType() + "'"); } return length; }  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 testGeodesicLengthForLineString() throws Exception { final double length = getGeodesicLength(createLineString(gf, createCoordinate(gf, 47.226116d, -1.554169d), createCoordinate(gf, 47.226126d, -1.554097d), createCoordinate(gf, 47.225527d, -1.553986d))); assertEquals(74d, length, 1d); }
@Test public void testGeodesicLengthForPolygon() throws Exception { final double length = getGeodesicLength(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))); assertEquals(148d, length, 1d); }