From 1d480ee465a84fd08c71c96efb870ef38e7f3481 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 22 九月 2023 17:39:00 +0800
Subject: [PATCH] 删除无效代码

---
 src/main/java/com/moon/server/service/data/RasterAnalysisService.java |   43 +++++++++++++++++++++++++++++--------------
 1 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/src/main/java/com/moon/server/service/data/RasterAnalysisService.java b/src/main/java/com/moon/server/service/data/RasterAnalysisService.java
index 05db3f1..fad1561 100644
--- a/src/main/java/com/moon/server/service/data/RasterAnalysisService.java
+++ b/src/main/java/com/moon/server/service/data/RasterAnalysisService.java
@@ -7,6 +7,7 @@
 import com.moon.server.entity.data.PublishEntity;
 import com.moon.server.helper.GeoHelper;
 import com.moon.server.helper.PathHelper;
+import com.moon.server.helper.WebHelper;
 import com.moon.server.service.all.WebSocketService;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -144,6 +145,7 @@
     private void openRaster(AnalysisResultEntity entity, String filePath, Geometry geo, int size) {
         Dataset ds = null;
         try {
+            // filePath = "D:\\Moon\\data\\DOM\\test.tif"
             ds = gdal.Open(filePath);
             if (null == ds) {
                 throw new Exception("鎵撳紑鏍呮牸鏁版嵁澶辫触");
@@ -188,7 +190,7 @@
      */
     public void analysisPolyline(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) {
         double[] transform = ds.GetGeoTransform();
-        double xSize = ds.getRasterXSize(), ySize = ds.getRasterXSize();
+        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
         double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = transform[5];
 
         double len = 0;
@@ -214,9 +216,13 @@
             for (int j = 1; j <= bandCount; j++) {
                 double[] pixelValues = new double[1];
                 ds.GetRasterBand(j).ReadRaster(xPixel, yPixel, 1, 1, pixelValues);
-                vals.add(pixelValues[0]);
+                if (!Double.isNaN(pixelValues[0])) {
+                    vals.add(WebHelper.round(pixelValues[0], 3));
+                }
             }
-            entity.addPoint(xy[0], xy[1], len, vals);
+            if (vals.size() > 0) {
+                entity.addPoint(xy[0], xy[1], len, vals);
+            }
         }
     }
 
@@ -225,23 +231,24 @@
      */
     public void analysisPolygon(AnalysisResultEntity entity, Dataset ds, Geometry geo) {
         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]), rotationX = transform[2], rotationY = transform[4];
         double[] env = new double[4];
         geo.GetEnvelope(env);
+        entity.addPoint(geo.Centroid().GetX(), geo.Centroid().GetY(), 0, null);
 
         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), ds.getRasterXSize());
-        int yMaxPixel = Math.min((int) Math.floor((maxY - env[2]) / pixelHeight), ds.getRasterYSize());
-
-        int bandCount = ds.getRasterCount();
-        int width = Math.abs(xMaxPixel - xMinPixel);
-        int height = Math.abs(yMaxPixel - yMinPixel);
-        if (width < 1 || height < 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) {
             setError(entity, "鏌ヨ鑼冨洿鏃犳晥");
             return;
         }
 
+        int bandCount = ds.getRasterCount();
+        int width = xMaxPixel - xMinPixel;
+        int height = yMaxPixel - yMinPixel;
         if (width * height > StaticData.I64 * StaticData.I64) {
             readRasterForBlocks(entity, ds, bandCount, xMinPixel, yMinPixel, width, height);
             return;
@@ -258,8 +265,8 @@
      * 鎸夌収鍧楄鍙栨爡鏍兼暟鎹�
      */
     private void readRasterForBlocks(AnalysisResultEntity entity, Dataset ds, int bandCount, int xMinPixel, int yMinPixel, int width, int height) {
-        List<Integer> xList = getSamples(xMinPixel, width);
-        List<Integer> yList = getSamples(yMinPixel, height);
+        List<Integer> xList = getSamples(xMinPixel, width - 1);
+        List<Integer> yList = getSamples(yMinPixel, height - 1);
 
         double[] pixelValues = new double[1];
         for (int i = 1; i <= bandCount; i++) {
@@ -267,7 +274,9 @@
             for (Integer x : xList) {
                 for (Integer y : yList) {
                     ds.GetRasterBand(i).ReadRaster(x, y, 1, 1, pixelValues);
-                    list.add(pixelValues[0]);
+                    if (!Double.isNaN(pixelValues[0])) {
+                        list.add(pixelValues[0]);
+                    }
                 }
             }
             setBandVals(entity, list);
@@ -306,7 +315,9 @@
 
         List<Double> list = new ArrayList<>();
         for (double val : pixelValues) {
-            list.add(val);
+            if (!Double.isNaN(val)) {
+                list.add(val);
+            }
         }
         setBandVals(entity, list);
     }
@@ -315,6 +326,10 @@
      * 璁剧疆Band鍊�
      */
     private void setBandVals(AnalysisResultEntity entity, List<Double> list) {
+        if (null == list || list.isEmpty()) {
+            return;
+        }
+
         double min = Collections.min(list);
         double max = Collections.max(list);
         double avg = list.stream().mapToDouble(Double::valueOf).average().getAsDouble();

--
Gitblit v1.9.3