From 7185f65d9e4087fef18567365ea621845fe2659e Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 28 二月 2024 16:17:08 +0800
Subject: [PATCH] 使用JTS计算面积

---
 src/main/java/com/lf/server/helper/JtsHelper.java             |   44 ++++++++++++++++++++++
 src/main/java/com/lf/server/mapper/sys/ReportMapper.java      |    9 ++++
 src/main/resources/mapper/sys/ReportMapper.xml                |   10 ++++
 src/main/java/com/lf/server/entity/ctrl/CoordinateEntity.java |   32 ++++++++++++++++
 src/main/java/com/lf/server/service/sys/ReportService.java    |   19 ++++++++-
 5 files changed, 111 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/lf/server/entity/ctrl/CoordinateEntity.java b/src/main/java/com/lf/server/entity/ctrl/CoordinateEntity.java
new file mode 100644
index 0000000..570938e
--- /dev/null
+++ b/src/main/java/com/lf/server/entity/ctrl/CoordinateEntity.java
@@ -0,0 +1,32 @@
+package com.lf.server.entity.ctrl;
+
+/**
+ * 鍧愭爣瀹炰綋绫�
+ *
+ * @author WWW
+ * @date 2024-02-28
+ */
+public class CoordinateEntity {
+    public CoordinateEntity() {
+    }
+
+    private Double x;
+
+    private Double y;
+
+    public Double getX() {
+        return x;
+    }
+
+    public void setX(Double x) {
+        this.x = x;
+    }
+
+    public Double getY() {
+        return y;
+    }
+
+    public void setY(Double y) {
+        this.y = y;
+    }
+}
diff --git a/src/main/java/com/lf/server/helper/JtsHelper.java b/src/main/java/com/lf/server/helper/JtsHelper.java
new file mode 100644
index 0000000..4596fd9
--- /dev/null
+++ b/src/main/java/com/lf/server/helper/JtsHelper.java
@@ -0,0 +1,44 @@
+package com.lf.server.helper;
+
+import com.lf.server.entity.ctrl.CoordinateEntity;
+import org.locationtech.jts.geom.Coordinate;
+import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.geom.GeometryFactory;
+import org.locationtech.jts.geom.MultiPoint;
+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);
+
+        // 瀹為檯涓篏eometryCollection锛堢粍鎴愮殑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;
+    }
+}
diff --git a/src/main/java/com/lf/server/mapper/sys/ReportMapper.java b/src/main/java/com/lf/server/mapper/sys/ReportMapper.java
index b508c70..257eb42 100644
--- a/src/main/java/com/lf/server/mapper/sys/ReportMapper.java
+++ b/src/main/java/com/lf/server/mapper/sys/ReportMapper.java
@@ -1,5 +1,6 @@
 package com.lf.server.mapper.sys;
 
+import com.lf.server.entity.ctrl.CoordinateEntity;
 import com.lf.server.entity.ctrl.CountEntity;
 import com.lf.server.entity.sys.ReportEntity;
 import org.apache.ibatis.annotations.Mapper;
@@ -138,4 +139,12 @@
      * @return
      */
     public List<CountEntity> countExplorationPoints();
+
+    /**
+     * 鏍规嵁椤圭洰缂栫爜鏌ヨ閽诲瓟鏁版嵁鍧愭爣鐐�
+     *
+     * @return
+     * @date
+     */
+    public List<CoordinateEntity> selectExplorationPoints(String code);
 }
diff --git a/src/main/java/com/lf/server/service/sys/ReportService.java b/src/main/java/com/lf/server/service/sys/ReportService.java
index a01529c..353c1f6 100644
--- a/src/main/java/com/lf/server/service/sys/ReportService.java
+++ b/src/main/java/com/lf/server/service/sys/ReportService.java
@@ -1,9 +1,9 @@
 package com.lf.server.service.sys;
 
 import com.lf.server.entity.all.RedisCacheKey;
-import com.lf.server.entity.all.ResAuthEntity;
 import com.lf.server.entity.all.SettingData;
 import com.lf.server.entity.all.StaticData;
+import com.lf.server.entity.ctrl.CoordinateEntity;
 import com.lf.server.entity.ctrl.CountEntity;
 import com.lf.server.entity.data.DownloadEntity;
 import com.lf.server.entity.sys.AttachEntity;
@@ -13,7 +13,6 @@
 import com.lf.server.mapper.sys.ReportMapper;
 import com.lf.server.service.all.RedisService;
 import com.lf.server.service.data.DownloadService;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -139,12 +138,28 @@
         return list;
     }
 
+    @Override
+    public List<CoordinateEntity> selectExplorationPoints(String code) {
+        return reportMapper.selectExplorationPoints(StringHelper.getRightLike(code));
+    }
+
     /**
      * 缁熻閽诲瓟鐐归潰绉�
      */
     private void countExplorationArea(List<CountEntity> list) {
+        for (CountEntity ce : list) {
+            if (0 == ce.getCount()) {
+                continue;
+            }
 
+            List<CoordinateEntity> ces = selectExplorationPoints(ce.getM3());
+            if (null == ces || ces.size() < 3) {
+                continue;
+            }
 
+            double area = JtsHelper.calcAreaByPoints(ces);
+            ce.setArea(area);
+        }
     }
 
     /**
diff --git a/src/main/resources/mapper/sys/ReportMapper.xml b/src/main/resources/mapper/sys/ReportMapper.xml
index 5fe8dc2..4bd3d62 100644
--- a/src/main/resources/mapper/sys/ReportMapper.xml
+++ b/src/main/resources/mapper/sys/ReportMapper.xml
@@ -122,7 +122,7 @@
     <!-- 鎸夐」鐩粺璁¢捇瀛旀暟鎹� -->
     <select id="countExplorationPoints" resultType="com.lf.server.entity.ctrl.CountEntity">
         select
-            name "m1",
+            name "m1", a.code "m3",
             (select count(*) from bs.s_explorationpoint b where dirid like a.code || '%') "count",
             (select coalesce(round(sum(sizes)::numeric, 3), 0)
                 from bs.s_explorationpoint b
@@ -133,4 +133,12 @@
         where pid = 0
         order by code;
     </select>
+
+    <select id="selectExplorationPoints" resultType="com.lf.server.entity.ctrl.CoordinateEntity">
+        select ST_X(geom) "x", ST_Y(geom) "y"
+        from bs.s_explorationpoint b
+        <where>
+            dirid like #{code} and geom is not null
+        </where>
+    </select>
 </mapper>
\ No newline at end of file

--
Gitblit v1.9.3