After finding this question when looking for the an answer to similar question, i thought i'd share my findings:
Description taken from blog post as it explains the problem you are trying to solve:
One of the recurring questions on GIS stackexchange is "What units is
the area calculated by JTS getArea()
". It doesn’t seem to matter how
many times people say that these is no answer to this as it depends on
the projection of your data. The question will not die, so I cooked up
some code that should give a close-ish answer for most polygons.
As with most of these questions the trick is to convert your polygon
to a flat cartesian plane (this is where JTS works best). We can use
the GeoTools auto projection (assuming the polygon is small enough)
and then simply call .getArea()
method.
Solution:
public static void main(String[] args) throws Exception {
GeometryFactory geometryFactory = new GeometryFactory();
WKTReader reader = new WKTReader(geometryFactory);
String s = "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))";
MultiPolygon mpolygon = (MultiPolygon) reader.read(s);
Point centroid = mpolygon.getCentroid();
String autoCode = "AUTO:42001," + centroid.getX() + "," + centroid.getY();
CoordinateReferenceSystem auto = CRS.decode(autoCode);
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);
MultiPolygon projed = (MultiPolygon) JTS.transform(mpolygon, transform);
Measure<Double, Area> measure = Measure.valueOf(projed.getArea(), SI.SQUARE_METRE);
System.out.println(measure);
}
Output:
8.146055550674082E12 m²
Reference: https://blog.ianturton.com/geotools,/projections/2017/08/01/area-of-a-polygon.html