package com.lf.server.helper; import com.lf.server.entity.ctrl.CoordinateEntity; import org.locationtech.jts.geom.*; import org.locationtech.jts.triangulate.ConformingDelaunayTriangulationBuilder; import java.util.List; /** * JTS帮助类 * * @author WWW * @date 2024-02-28 */ public class JtsHelper { /** * 根据坐标点计算面积 */ public static double calcAreaByPoints(List ces) { Coordinate[] cs = new Coordinate[ces.size()]; for (int i = 0, c = ces.size(); i < c; i++) { cs[i] = new Coordinate(ces.get(i).getX(), ces.get(i).getY()); } GeometryFactory gf = new GeometryFactory(); MultiPoint mp = gf.createMultiPointFromCoords(cs); ConformingDelaunayTriangulationBuilder builder = new ConformingDelaunayTriangulationBuilder(); builder.setSites(mp); // 实际为GeometryCollection(组成的geometry紧密相连) Geometry ts = builder.getTriangles(gf); // 以0的距离进行缓冲(因为各多边形两两共边),此时则将点云构造成了多边形 Geometry union = ts.buffer(0); // String text = union.toText() double area = union.getArea(); return Math.round(area * 100.0) / 100.0; } }