From dd5b7a9aa3c5d995ceca91ef080730fdc69772a9 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 16 十一月 2023 11:25:19 +0800
Subject: [PATCH] 修改坡度分析的空间查询模块

---
 src/main/java/com/moon/server/entity/data/PointEntity.java                  |   47 +++++++++++++++++++++++
 src/main/java/com/moon/server/service/data/SlopAnalysisService.java         |   67 +++++++++++++++++++++++++++++++++
 src/main/java/com/moon/server/controller/data/RasterAnalysisController.java |    2 
 src/main/resources/application.yml                                          |    2 
 4 files changed, 115 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/moon/server/controller/data/RasterAnalysisController.java b/src/main/java/com/moon/server/controller/data/RasterAnalysisController.java
index 2396ad7..5ee0722 100644
--- a/src/main/java/com/moon/server/controller/data/RasterAnalysisController.java
+++ b/src/main/java/com/moon/server/controller/data/RasterAnalysisController.java
@@ -159,7 +159,7 @@
     @SysLog()
     @ApiOperation(value = "涓嬭浇鍧″害鍒嗘瀽Excel")
     @ApiImplicitParams({
-            @ApiImplicitParam(name = "wkt", value = "WKT瀛楃涓�", dataType = "String", example = "POLYGON ((56.61 33.94,115.04 33.56,114.09 -7.17,52.22 -6.22,56.61 33.94))")
+            @ApiImplicitParam(name = "wkt", value = "WKT瀛楃涓�", dataType = "String", example = "POLYGON ((-103.2 -12.9,-84.2 -14.3,-86.8 -27.5,-105.1 -25.3,-103.2 -12.9))")
     })
     @GetMapping(value = "/downloadSlopXls")
     public void downloadSlopXls(String wkt, HttpServletRequest req, HttpServletResponse res) {
diff --git a/src/main/java/com/moon/server/entity/data/PointEntity.java b/src/main/java/com/moon/server/entity/data/PointEntity.java
new file mode 100644
index 0000000..7e299ac
--- /dev/null
+++ b/src/main/java/com/moon/server/entity/data/PointEntity.java
@@ -0,0 +1,47 @@
+package com.moon.server.entity.data;
+
+/**
+ * 鐐瑰疄浣撶被
+ * @author WWW
+ * @date 2023-11-16
+ */
+public class PointEntity {
+    private double x;
+
+    private double y;
+
+    private double val;
+
+    public PointEntity() {
+    }
+
+    public PointEntity(double x, double y, double val) {
+        this.x = x;
+        this.y = y;
+        this.val = val;
+    }
+
+    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;
+    }
+
+    public double getVal() {
+        return val;
+    }
+
+    public void setVal(double val) {
+        this.val = val;
+    }
+}
diff --git a/src/main/java/com/moon/server/service/data/SlopAnalysisService.java b/src/main/java/com/moon/server/service/data/SlopAnalysisService.java
index 35130ce..22c87e2 100644
--- a/src/main/java/com/moon/server/service/data/SlopAnalysisService.java
+++ b/src/main/java/com/moon/server/service/data/SlopAnalysisService.java
@@ -1,11 +1,19 @@
 package com.moon.server.service.data;
 
+import com.moon.server.entity.all.StaticData;
+import com.moon.server.entity.data.PointEntity;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.gdal.gdal.Dataset;
+import org.gdal.gdal.gdal;
 import org.gdal.ogr.Geometry;
+import org.gdal.ogr.ogr;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.servlet.http.HttpServletResponse;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * 鏍呮牸鍒嗘瀽鏈嶅姟
@@ -14,13 +22,70 @@
  */
 @Service
 public class SlopAnalysisService {
+    @Value("${sys.path.slopFile}")
+    private String filePath;
+
     private final static Log log = LogFactory.getLog(SlopAnalysisService.class);
 
     /**
      * 涓嬭浇鍧″害鍒嗘瀽Excel
      */
-    public void downloadSlopXls(Geometry polygon, HttpServletResponse res) {
+    public void downloadSlopXls(Geometry polygon, HttpServletResponse res) throws Exception {
+        Dataset ds = null;
+        try {
+            ds = gdal.Open(filePath);
+            if (null == ds) {
+                throw new Exception("鎵撳紑鏍呮牸鏁版嵁澶辫触");
+            }
 
+            List<PointEntity> list = analysisPolygon(polygon, ds);
+            if (null == list || list.isEmpty()) {
+                throw new Exception("鍒嗘瀽缁撴灉涓虹┖锛堟煡璇㈣寖鍥存棤鏁堬級");
+            }
 
+            //
+        } finally {
+            if (null != ds) {
+                ds.delete();
+            }
+        }
+    }
+
+    /**
+     * 鍒嗘瀽澶氳竟褰�
+     */
+    public List<PointEntity> analysisPolygon(Geometry geo, Dataset ds) {
+        double[] transform = ds.GetGeoTransform();
+        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
+        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]);
+        double[] env = new double[4];
+        geo.GetEnvelope(env);
+
+        int xMinPixel = Math.max((int) Math.floor((env[0] - minX) / pixelWidth), 1);
+        int yMinPixel = Math.max((int) Math.floor((maxY - env[3]) / pixelHeight), 1);
+        int xMaxPixel = Math.min((int) Math.floor((env[1] - minX) / pixelWidth), xSize);
+        int yMaxPixel = Math.min((int) Math.floor((maxY - env[2]) / pixelHeight), ySize);
+        if (xMaxPixel < 1 || yMaxPixel < 1 || xMaxPixel - xMinPixel < 0 || yMaxPixel - yMinPixel < 0) {
+            return null;
+        }
+
+        double[] values = new double[1];
+        List<PointEntity> list = new ArrayList<>();
+        for (int y = yMinPixel; y <= yMaxPixel; y++) {
+            for (int x = xMinPixel; x <= xMaxPixel; x++) {
+                Geometry point = new Geometry(ogr.wkbPoint);
+                point.AddPoint(minX + pixelWidth * x, maxY - pixelHeight * y);
+                if (!geo.Intersects(point)) {
+                    continue;
+                }
+
+                ds.GetRasterBand(StaticData.I1).ReadRaster(x, y, 1, 1, values);
+                if (!Double.isNaN(values[0])) {
+                    list.add(new PointEntity(point.GetY(), point.GetX(), values[0]));
+                }
+            }
+        }
+
+        return list;
     }
 }
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 7e5b900..44de3ef 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -151,6 +151,6 @@
     # 涓存椂鐩綍
     temp: D:\Moon\temp
     # 鍧″害鍒嗘瀽
-    slop: D:\Moon\dtm\dtm100m_slope_moon2000.tif
+    slopFile: D:\Moon\dtm\dtm100m_slope_moon2000.tif
   # 涓婁紶闄勪欢琛�
   attachTabs:
\ No newline at end of file

--
Gitblit v1.9.3