GeometryUtils { public static double getGeodesicArea(@NonNull final LineString lineString) { double area = 0.0; for (int i = 0; i < ((lineString.isClosed()) ? lineString.getNumPoints() - 1 : lineString.getNumPoints()); i++) { GeoPoint p1 = new GeoPoint(lineString.getPointN(i)); GeoPoint p2 = new GeoPoint(lineString.getPointN((i + 1) % lineString.getNumPoints())); area += ((p2.getLongitude() - p1.getLongitude()) * DEG2RAD) * (2 + Math.sin(p1.getLatitude() * DEG2RAD) + Math.sin(p2.getLatitude() * DEG2RAD)); } area = area * IGeoConstants.RADIUS_EARTH_METERS * IGeoConstants.RADIUS_EARTH_METERS / 2.0; return Math.abs(area); }  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 testGeodesicAreaForLineString() throws Exception { final double area1 = getGeodesicArea(createLineString(gf, createCoordinate(gf, 47.226116d, -1.554169d), createCoordinate(gf, 47.226126d, -1.554097d), createCoordinate(gf, 47.225527d, -1.553986d))); assertEquals(186d, area1, 1d); final double area2 = getGeodesicArea(createLineString(gf, createCoordinate(gf, 47.226116d, -1.554169d), createCoordinate(gf, 47.226126d, -1.554097d), createCoordinate(gf, 47.225527d, -1.553986d), createCoordinate(gf, 47.226116d, -1.554169d))); assertEquals(area1, area2, 0d); }
@Test public void testGeodesicAreaForSimplePolygon() throws Exception { final double area = getGeodesicArea(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)), false); assertEquals(378d, area, 1d); }
@Test public void testGeodesicAreaForPolygonWithHoles() throws Exception { final double areaWithHole = getGeodesicArea(createPolygon(gf, createLinearRing(gf, createCoordinate(gf, 47.226257d, -1.554564d), createCoordinate(gf, 47.226295d, -1.554202d), createCoordinate(gf, 47.226075d, -1.554169d), createCoordinate(gf, 47.226049d, -1.554496d), createCoordinate(gf, 47.226257d, -1.554564d)), createLinearRing(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))), true); assertEquals(470d, areaWithHole, 1d); final double area = getGeodesicArea(createPolygon(gf, createLinearRing(gf, createCoordinate(gf, 47.226257d, -1.554564d), createCoordinate(gf, 47.226295d, -1.554202d), createCoordinate(gf, 47.226075d, -1.554169d), createCoordinate(gf, 47.226049d, -1.554496d), createCoordinate(gf, 47.226257d, -1.554564d)), createLinearRing(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))), false); assertEquals(634d, area, 1d); }