13693261870
2025-06-24 8565bd83fcd670ec8379084d600eb97d18037d21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.terra.system.helper;
 
import com.terra.system.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<CoordinateEntity> 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;
    }
}