From 45a773850057dd90c4292d30715b4f9dfdc86740 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 04 十二月 2023 13:42:20 +0800
Subject: [PATCH] 屏蔽NoData

---
 src/main/java/com/moon/server/service/data/RasterAnalysisService.java |  256 +++++++++++++++++++++++++++++++-------------------
 1 files changed, 159 insertions(+), 97 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 b4803b0..14d2d17 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,8 @@
 import com.moon.server.entity.data.PublishEntity;
 import com.moon.server.helper.GeoHelper;
 import com.moon.server.helper.PathHelper;
+import com.moon.server.helper.StringHelper;
+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;
@@ -16,11 +18,13 @@
 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.annotation.Resource;
 import java.awt.geom.Point2D;
 import java.io.File;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.util.*;
 
@@ -31,6 +35,9 @@
  */
 @Service
 public class RasterAnalysisService {
+    @Value("${sys.gdal_path}")
+    String gdalPath;
+
     @Resource
     PathHelper pathHelper;
 
@@ -41,6 +48,11 @@
 
     /**
      * 娴嬭瘯
+     * <p>
+     * POINT (26.72 -48.58)
+     * LINESTRING(18.44 -46.69,20.14 -45.93)
+     * POLYGON ((5.11 -41.64,7.68 -43.59,4.73 -43.3,5.11 -41.64))
+     * POLYGON ((42.6 -40.3,49.1 -40.4,49.1 -65.7,46.3 -65.6,42.6 -40.3))
      */
     public List<AnalysisResultEntity> test(Geometry geo, Integer size) {
         List<AnalysisResultEntity> rs = new ArrayList<>();
@@ -62,11 +74,12 @@
             return;
         }
 
-        // for (PublishEntity pub : pubs) {
+        //for (PublishEntity pub : pubs) {
         pubs.parallelStream().forEach(pub -> {
             try {
                 AnalysisResultEntity entity = new AnalysisResultEntity(token);
                 entity.setLayerName(pub.getName());
+                entity.setUnit(pub.getUnit());
 
                 List<MetaEntity> metas = publishService.selectMetasByPubid(pub.getId());
                 if (null == metas || metas.isEmpty()) {
@@ -75,7 +88,7 @@
                     return;
                 }
 
-                String filePath = pathHelper.getConfig().getUploadPath() + File.separator + metas.get(0).getPath();
+                String filePath = getFilePath(metas);
                 File file = new File(filePath);
                 if (!file.exists() || file.isDirectory()) {
                     setError(entity, "婧愭暟鎹笉瀛樺湪");
@@ -92,6 +105,31 @@
     }
 
     /**
+     * 鑾峰彇鏂囦欢璺緞
+     */
+    private String getFilePath(List<MetaEntity> metas) throws IOException {
+        String basePath = pathHelper.getConfig().getUploadPath() + File.separator;
+        if (metas.size() == 1) {
+            return basePath + metas.get(0).getPath();
+        }
+
+        String tempPath = pathHelper.getTempPath() + File.separator, guid = StringHelper.getGuid();
+        String fileList = tempPath + guid + ".txt";
+        FileWriter fw = new FileWriter(fileList);
+        for (int i = 0, c = metas.size(); i < c; i++) {
+            String str = (i > 0 ? "\r\n" : "") + basePath + metas.get(i).getPath();
+            fw.write(str.toCharArray());
+        }
+        fw.close();
+
+        String vrt = tempPath + guid + ".vrt";
+        String cmd = String.format("\"%s\\gdalbuildvrt.exe\" -input_file_list \"%s\" \"%s\"", gdalPath, fileList, vrt);
+        WebHelper.exec(cmd);
+
+        return vrt;
+    }
+
+    /**
      * 鎺ㄩ�佹秷鎭�
      */
     private void postInfo(AnalysisResultEntity entity) throws IOException {
@@ -99,7 +137,8 @@
         map.put("analysisForPost", entity);
 
         String json = JSONObject.toJSONString(map);
-        // System.out.println(json)
+        //System.out.println(json)
+
         WebSocketService.broadCastInfo(json);
     }
 
@@ -115,26 +154,30 @@
         }
 
         for (PublishEntity pub : pubs) {
-            AnalysisResultEntity entity = new AnalysisResultEntity();
-            entity.setLayerName(pub.getName());
+            try {
+                AnalysisResultEntity entity = new AnalysisResultEntity();
+                entity.setLayerName(pub.getName());
+                entity.setUnit(pub.getUnit());
 
-            List<MetaEntity> metas = publishService.selectMetasByPubid(pub.getId());
-            if (null == metas || metas.isEmpty()) {
-                setError(entity, "鎵句笉鍒板彂甯冩暟鎹�");
-                continue;
+                List<MetaEntity> metas = publishService.selectMetasByPubid(pub.getId());
+                if (null == metas || metas.isEmpty()) {
+                    setError(entity, "鎵句笉鍒板彂甯冩暟鎹�");
+                    continue;
+                }
+
+                String filePath = getFilePath(metas);
+                File file = new File(filePath);
+                if (!file.exists() || file.isDirectory()) {
+                    setError(entity, "婧愭暟鎹笉瀛樺湪");
+                    continue;
+                }
+
+                openRaster(entity, filePath, geo, size);
+                rs.add(entity);
+            } catch (Exception ex) {
+                log.error(ex.getMessage(), ex);
             }
-
-            String filePath = pathHelper.getConfig().getUploadPath() + File.separator + metas.get(0).getPath();
-            File file = new File(filePath);
-            if (!file.exists() || file.isDirectory()) {
-                setError(entity, "婧愭暟鎹笉瀛樺湪");
-                continue;
-            }
-
-            openRaster(entity, filePath, geo, size);
-            rs.add(entity);
         }
-
         return rs;
     }
 
@@ -144,6 +187,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("鎵撳紑鏍呮牸鏁版嵁澶辫触");
@@ -184,31 +228,11 @@
     }
 
     /**
-     * 鍒嗘瀽鐐�
-     */
-    public void analysisPoint2(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) {
-        double x = geo.GetX(), y = geo.GetY();
-        double[] transform = ds.GetGeoTransform();
-        // double rotationX = transform[2]; double rotationY = transform[4]
-        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = transform[5];
-
-        int bandCount = ds.getRasterCount();
-        int xPixel = (int) Math.floor((x - minX) / pixelWidth);
-        int yPixel = (int) Math.floor((maxY - y) / Math.abs(pixelHeight));
-
-        for (int i = 1; i <= bandCount; i++) {
-            double[] pixelValues = new double[size * size];
-            Band band = ds.GetRasterBand(i);
-            band.ReadRaster(xPixel, yPixel, size, size, pixelValues);
-        }
-    }
-
-    /**
      * 鍒嗘瀽绾�
      */
     public void analysisPolyline(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) {
         double[] transform = ds.GetGeoTransform();
-        // double rotationX = transform[2]; double rotationY = transform[4]
+        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
         double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = transform[5];
 
         double len = 0;
@@ -226,56 +250,22 @@
 
             int xPixel = (int) Math.floor((xy[0] - minX) / pixelWidth);
             int yPixel = (int) Math.floor((maxY - xy[1]) / Math.abs(pixelHeight));
+            if (xPixel < 1 || xPixel > xSize || yPixel < 1 || yPixel > ySize) {
+                continue;
+            }
 
             List<Double> vals = new ArrayList<>();
             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]);
-            }
-            entity.addPoint(xy[0], xy[1], len, vals);
-        }
-    }
-
-    /**
-     * 鍒嗘瀽绾�
-     */
-    public void analysisPolyline2(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) {
-        double[] transform = ds.GetGeoTransform();
-        // double rotationX = transform[2]; double rotationY = transform[4]
-        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]);
-
-        double buffer = Math.max(pixelWidth, pixelHeight) * size;
-        double[] env = new double[4];
-        geo = geo.Buffer(buffer);
-        geo.GetEnvelope(env);
-
-        int xMinPixel = (int) Math.floor((env[0] - minX) / pixelWidth);
-        int yMinPixel = (int) Math.floor((maxY - env[3]) / pixelHeight);
-        int xMaxPixel = (int) Math.floor((env[1] - minX) / pixelWidth);
-        int yMaxPixel = (int) Math.floor((maxY - env[2]) / pixelHeight);
-
-        int bandCount = ds.getRasterCount();
-        int xMin = Math.min(xMinPixel, xMaxPixel);
-        int xMax = Math.max(xMinPixel, xMaxPixel);
-        int yMin = Math.min(yMinPixel, yMaxPixel);
-        int yMax = Math.max(yMinPixel, yMaxPixel);
-
-        List<Double> list = new ArrayList<>();
-        for (int y = yMin; y <= yMax; y++) {
-            for (int x = xMin; x <= xMax; x++) {
-                Geometry point = new Geometry(ogr.wkbPoint);
-                point.AddPoint(minX + pixelWidth * x, maxY - pixelHeight * y);
-                if (geo.Intersects(point)) {
-                    for (int i = 1; i <= bandCount; i++) {
-                        double[] values = new double[1];
-                        ds.GetRasterBand(i).ReadRaster(x, y, 1, 1, values);
-                        list.add(values[0]);
-                    }
+                if (!Double.isNaN(pixelValues[0])) {
+                    vals.add(WebHelper.round(pixelValues[0], 3));
                 }
             }
+            if (vals.size() > 0) {
+                entity.addPoint(xy[0], xy[1], len, vals);
+            }
         }
-        // processResult(list, entity)
     }
 
     /**
@@ -283,20 +273,24 @@
      */
     public void analysisPolygon(AnalysisResultEntity entity, Dataset ds, Geometry geo) {
         double[] transform = ds.GetGeoTransform();
-        // double rotationX = transform[2]; double rotationY = transform[4]
-        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]);
+        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 = (int) Math.floor((env[0] - minX) / pixelWidth);
-        int yMinPixel = (int) Math.floor((maxY - env[3]) / pixelHeight);
-        int xMaxPixel = (int) Math.floor((env[1] - minX) / pixelWidth);
-        int yMaxPixel = (int) Math.floor((maxY - env[2]) / pixelHeight);
+        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) {
+            setError(entity, "鏌ヨ鑼冨洿鏃犳晥");
+            return;
+        }
 
         int bandCount = ds.getRasterCount();
-        int width = Math.abs(xMaxPixel - xMinPixel);
-        int height = Math.abs(yMaxPixel - yMinPixel);
-
+        int width = xMaxPixel - xMinPixel;
+        int height = yMaxPixel - yMinPixel;
         if (width * height > StaticData.I64 * StaticData.I64) {
             readRasterForBlocks(entity, ds, bandCount, xMinPixel, yMinPixel, width, height);
             return;
@@ -313,8 +307,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++) {
@@ -322,7 +316,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);
@@ -361,7 +357,9 @@
 
         List<Double> list = new ArrayList<>();
         for (double val : pixelValues) {
-            list.add(val);
+            if (!Double.isNaN(val) && val > Integer.MIN_VALUE) {
+                list.add(val);
+            }
         }
         setBandVals(entity, list);
     }
@@ -370,6 +368,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();
@@ -416,12 +418,72 @@
                 d += distance;
             }
         }
-
         list.add(geo.GetPoint(c));
 
         return list;
     }
 
+    /**
+     * 鍒嗘瀽鐐� 2
+     */
+    public void analysisPoint2(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) {
+        double x = geo.GetX(), y = geo.GetY();
+        double[] transform = ds.GetGeoTransform();
+        // double rotationX = transform[2]; double rotationY = transform[4]
+        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = transform[5];
+
+        int bandCount = ds.getRasterCount();
+        int xPixel = (int) Math.floor((x - minX) / pixelWidth);
+        int yPixel = (int) Math.floor((maxY - y) / Math.abs(pixelHeight));
+
+        for (int i = 1; i <= bandCount; i++) {
+            double[] pixelValues = new double[size * size];
+            Band band = ds.GetRasterBand(i);
+            band.ReadRaster(xPixel, yPixel, size, size, pixelValues);
+        }
+    }
+
+    /**
+     * 鍒嗘瀽绾� 2
+     */
+    public void analysisPolyline2(AnalysisResultEntity entity, Dataset ds, Geometry geo, int size) {
+        double[] transform = ds.GetGeoTransform();
+        // double rotationX = transform[2]; double rotationY = transform[4]
+        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]);
+
+        double buffer = Math.max(pixelWidth, pixelHeight) * size;
+        double[] env = new double[4];
+        geo = geo.Buffer(buffer);
+        geo.GetEnvelope(env);
+
+        int xMinPixel = (int) Math.floor((env[0] - minX) / pixelWidth);
+        int yMinPixel = (int) Math.floor((maxY - env[3]) / pixelHeight);
+        int xMaxPixel = (int) Math.floor((env[1] - minX) / pixelWidth);
+        int yMaxPixel = (int) Math.floor((maxY - env[2]) / pixelHeight);
+
+        int bandCount = ds.getRasterCount();
+        int xMin = Math.min(xMinPixel, xMaxPixel);
+        int xMax = Math.max(xMinPixel, xMaxPixel);
+        int yMin = Math.min(yMinPixel, yMaxPixel);
+        int yMax = Math.max(yMinPixel, yMaxPixel);
+
+        List<Double> list = new ArrayList<>();
+        for (int y = yMin; y <= yMax; y++) {
+            for (int x = xMin; x <= xMax; x++) {
+                Geometry point = new Geometry(ogr.wkbPoint);
+                point.AddPoint(minX + pixelWidth * x, maxY - pixelHeight * y);
+                if (geo.Intersects(point)) {
+                    for (int i = 1; i <= bandCount; i++) {
+                        double[] values = new double[1];
+                        ds.GetRasterBand(i).ReadRaster(x, y, 1, 1, values);
+                        list.add(values[0]);
+                    }
+                }
+            }
+        }
+        // processResult(list, entity)
+    }
+
     private void processClippedDataByLine(String imagePath, String geometryString) {
         // 娉ㄥ唽GDAL椹卞姩
         gdal.AllRegister();

--
Gitblit v1.9.3