From 1cae540cd2b99803808d31a3a5dd35e3216f19c7 Mon Sep 17 00:00:00 2001
From: wuww <252740454@qq.com>
Date: 星期三, 30 四月 2025 14:50:54 +0800
Subject: [PATCH] 插值处理-四邻域

---
 data/ai.backup                                          |    0 
 src/main/java/com/se/nsl/controller/TestController.java |   51 ++++++++++--
 src/main/java/com/se/nsl/domain/po/SimuData.java        |    3 
 src/main/java/com/se/nsl/service/TestService.java       |   73 +++++++++++++++++-
 src/main/java/com/se/nsl/helper/GdalHelper.java         |    6 
 src/main/java/com/se/nsl/domain/po/Rainfall.java        |   54 +++++++++++++
 src/main/java/com/se/nsl/helper/ComHelper.java          |   28 ++++++
 src/main/resources/application-dev.yml                  |    4 
 8 files changed, 199 insertions(+), 20 deletions(-)

diff --git a/data/ai.backup b/data/ai.backup
index 5296be1..da8ad37 100644
--- a/data/ai.backup
+++ b/data/ai.backup
Binary files differ
diff --git a/src/main/java/com/se/nsl/controller/TestController.java b/src/main/java/com/se/nsl/controller/TestController.java
index 3f4a6c4..a306916 100644
--- a/src/main/java/com/se/nsl/controller/TestController.java
+++ b/src/main/java/com/se/nsl/controller/TestController.java
@@ -175,24 +175,34 @@
     @ApiOperation(value = "04.testPngList <")
     @GetMapping("/testPngList")
     @ApiImplicitParams({
-            @ApiImplicitParam(name = "path", value = "璺緞", dataType = "String", paramType = "query", example = "D:\\other\\simu\\out\\chicago\\waters"),
+            @ApiImplicitParam(name = "path", value = "璺緞", dataType = "String", paramType = "query", example = "D:\\other\\simu\\out\\fs24cubic"),
             @ApiImplicitParam(name = "size", value = "灏哄", dataType = "Integer", paramType = "query", example = "1024")
     })
     public R<Object> testPngList(String path, Integer size) {
         try {
             if (null == size) size = (Integer) 1024;
+
+            String newPath = config.getOutPath() + File.separator + path + File.separator + "waters";
+            String pngPath = config.getOutPath() + File.separator + path + File.separator + "png";
+            if (!new File(pngPath).exists()) new File(pngPath).mkdirs();
+
+            int i = 1;
             List<String> list = new ArrayList<>();
-            for (File file : new File(path).listFiles()) {
+            for (File file : new File(newPath).listFiles()) {
                 if (!file.exists() || file.isFile()) continue;
 
                 File pngFile = new File(file.getPath() + File.separator + size + "_" + size + ".png");
                 if (!pngFile.exists() || pngFile.isDirectory()) continue;
 
-                list.add("file '" + pngFile.getPath().replace("\\", "/") + "'");
+                String pngName = i + ".png"; // String.format("f%06d", i)
+                Files.copy(pngFile.toPath(), Paths.get(pngPath + File.separator + pngName), StandardCopyOption.REPLACE_EXISTING);
+
+                list.add("file '" + pngName + "'");
                 System.out.println(list.get(list.size() - 1));
+                i++;
             }
 
-            Path outPath = Paths.get(path + File.separator + "list.txt");
+            Path outPath = Paths.get(pngPath + File.separator + "list.txt");
             try {
                 Files.write(outPath, list, StandardCharsets.UTF_8);
             } catch (IOException e) {
@@ -232,7 +242,7 @@
     @ApiImplicitParams({
             @ApiImplicitParam(name = "tifPath", value = "Tif璺緞", dataType = "String", paramType = "query", example = "D:\\other\\simu\\uwsolver\\chicago"),
             @ApiImplicitParam(name = "inPath", value = "杈撳叆璺緞", dataType = "String", paramType = "query", example = "20250425"),
-            @ApiImplicitParam(name = "startTime", value = "寮�濮嬫椂闂�", dataType = "Integer", paramType = "query", example = "2025-04-25 00:00:00"),
+            @ApiImplicitParam(name = "startTime", value = "寮�濮嬫椂闂�", dataType = "Integer", paramType = "query", example = "2025-04-29 00:00:00"),
             @ApiImplicitParam(name = "epsg", value = "鍧愭爣绯籌D", dataType = "Integer", paramType = "query", example = "4548")
     })
     public R<Object> testCreateNsl(String tifPath, String inPath, String startTime, Integer epsg) {
@@ -242,7 +252,7 @@
             data.setStartTime(StringHelper.YMDHMS_FORMAT.parse(startTime));
             data.setEpsg(epsg);
 
-            procTifs(tifPath, data.getStartTime());
+            procTifs(tifPath, inPath, data.getStartTime());
             testService.test(data);
 
             return success("ok");
@@ -251,13 +261,15 @@
         }
     }
 
-    private void procTifs(String tifPath, Date startTime) {
+    private void procTifs(String tifPath, String inPath, Date startTime) {
         if (StringUtils.isEmpty(tifPath)) return;
 
         Calendar calendar = Calendar.getInstance();
-        calendar.setTime(new Date(startTime.getYear() - 1900, startTime.getMonth() - 1, startTime.getDay(), 0, 0, 0));
+        //calendar.setTime(new Date(startTime.getYear() - 1900, startTime.getMonth() - 1, startTime.getDay(), 0, 0, 0));
+        calendar.setTime(startTime);
         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
-        String newPath = config.getInPath() + File.separator + "depth";
+        String newPath = config.getInPath() + File.separator + inPath + File.separator + "depth";
+        if (!new File(newPath).exists()) new File(newPath).mkdirs();
 
         for (File file : new File(tifPath).listFiles()) {
             if (!file.exists() || !file.isDirectory()) continue;
@@ -275,6 +287,27 @@
         }
     }
 
+    @ApiOperation(value = "11.renametifs <")
+    @GetMapping("/renametifs")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "path", value = "璺緞", dataType = "String", paramType = "query", example = "D:\\other\\simu\\uwsolver\\chicago\\depth"),
+            @ApiImplicitParam(name = "prefix", value = "鍓嶇紑", dataType = "Integer", paramType = "query", example = "01250302"),
+            @ApiImplicitParam(name = "newPrefix", value = "鏂板墠缂�", dataType = "Integer", paramType = "query", example = "20250429")
+    })
+    public R<Object> renametifs(String path, String prefix, String newPrefix) {
+        try {
+            File[] files = new File(path).listFiles();
+            for (File file : files) {
+                String newName = file.getPath().replace(prefix, newPrefix);
+                file.renameTo(new File(newName));
+            }
+
+            return success("ok");
+        } catch (Exception ex) {
+            return fail(ex, null);
+        }
+    }
+
     @ApiOperation(value = "10.褰撳墠鏃堕棿 *")
     @GetMapping("/getTime")
     public Object getTime() {
diff --git a/src/main/java/com/se/nsl/domain/po/Rainfall.java b/src/main/java/com/se/nsl/domain/po/Rainfall.java
new file mode 100644
index 0000000..853e3b0
--- /dev/null
+++ b/src/main/java/com/se/nsl/domain/po/Rainfall.java
@@ -0,0 +1,54 @@
+package com.se.nsl.domain.po;
+
+import com.alibaba.fastjson.annotation.JSONField;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModelProperty;
+
+import java.util.Date;
+
+@SuppressWarnings("ALL")
+public class Rainfall {
+    @ApiModelProperty("鏃堕棿(yyyy-MM-dd HH:mm:ss)")
+    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private Date time;
+
+    @ApiModelProperty("闆ㄥ己(mm/h)")
+    private Double intensity;
+
+    @ApiModelProperty("绱闆ㄩ噺(mm)")
+    private Double total;
+
+    public Rainfall() {
+    }
+
+    public Rainfall(Date time, Double intensity, Double total) {
+        this.time = time;
+        this.intensity = intensity;
+        this.total = total;
+    }
+
+    public Date getTime() {
+        return time;
+    }
+
+    public void setTime(Date time) {
+        this.time = time;
+    }
+
+    public Double getIntensity() {
+        return intensity;
+    }
+
+    public void setIntensity(Double intensity) {
+        this.intensity = intensity;
+    }
+
+    public Double getTotal() {
+        return total;
+    }
+
+    public void setTotal(Double total) {
+        this.total = total;
+    }
+}
diff --git a/src/main/java/com/se/nsl/domain/po/SimuData.java b/src/main/java/com/se/nsl/domain/po/SimuData.java
index 6db1aac..6589508 100644
--- a/src/main/java/com/se/nsl/domain/po/SimuData.java
+++ b/src/main/java/com/se/nsl/domain/po/SimuData.java
@@ -65,6 +65,9 @@
     @ApiModelProperty("闆ㄩ噺璁″垪琛�")
     private List<RainGauge> gauges;
 
+    @ApiModelProperty("闄嶉洦鍒楄〃")
+    private List<Rainfall> rainfalls;
+
     public SimuData() {
         gauges = new ArrayList<>();
     }
diff --git a/src/main/java/com/se/nsl/helper/ComHelper.java b/src/main/java/com/se/nsl/helper/ComHelper.java
index ee09a54..77fed45 100644
--- a/src/main/java/com/se/nsl/helper/ComHelper.java
+++ b/src/main/java/com/se/nsl/helper/ComHelper.java
@@ -61,7 +61,33 @@
         //vector.add("-te_srs");
         //vector.add("EPSG:" + 4326);
         vector.add("-r");
-        vector.add("bilinear");
+        vector.add("bilinear"); // 鍙岀嚎鎬ф彃鍊�
+        vector.add("-of");
+        vector.add("GTiff");
+        WarpOptions warpOptions = new WarpOptions(vector);
+
+        Dataset destDs = gdal.Warp(dest, new Dataset[]{ds}, warpOptions);
+        destDs.delete();
+    }
+
+    public static void Resample2(Dataset ds, String dest, int width, int height, LayerDto layer) {
+        Vector<String> vector = new Vector<>();
+        //vector.add("-s_srs");
+        //vector.add("EPSG:" + 4548);
+        //vector.add("-t_srs");
+        //vector.add("EPSG:" + 4326);
+        vector.add("-ts");
+        vector.add("" + width);
+        vector.add("" + height);
+        //vector.add("-te");
+        //vector.add("" + layer.getExtension().getMinx());
+        //vector.add("" + layer.getExtension().getMiny());
+        //vector.add("" + layer.getExtension().getMaxx());
+        //vector.add("" + layer.getExtension().getMaxy());
+        //vector.add("-te_srs");
+        //vector.add("EPSG:" + 4326);
+        vector.add("-r");
+        vector.add("average");
         vector.add("-of");
         vector.add("GTiff");
         WarpOptions warpOptions = new WarpOptions(vector);
diff --git a/src/main/java/com/se/nsl/helper/GdalHelper.java b/src/main/java/com/se/nsl/helper/GdalHelper.java
index 85380f6..db31d49 100644
--- a/src/main/java/com/se/nsl/helper/GdalHelper.java
+++ b/src/main/java/com/se/nsl/helper/GdalHelper.java
@@ -35,7 +35,7 @@
                 gdal.SetConfigOption("GDAL_HOME", gdalPath);
                 gdal.SetConfigOption("GDAL_DATA", gdalPath + File.separator + "gdal-data");
                 gdal.SetConfigOption("PROJ_LIB", gdalPath + File.separator + "projlib"); // /proj7/share
-                gdal.SetConfigOption("GDAL_DRIVER_PATH", gdalPath + File.separator + "gdalplugins");
+                //gdal.SetConfigOption("GDAL_DRIVER_PATH", gdalPath + File.separator + "gdalplugins");
 
                 String path = System.getenv("PATH");
                 if (!path.contains(gdalPath)) {
@@ -48,8 +48,8 @@
 
         gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
         gdal.SetConfigOption("SHAPE_ENCODING", "");
-        gdal.SetConfigOption("PGEO_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s");
-        gdal.SetConfigOption("MDB_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s");
+        //gdal.SetConfigOption("PGEO_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s");
+        //gdal.SetConfigOption("MDB_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s");
 
         gdal.AllRegister();
         ogr.RegisterAll();
diff --git a/src/main/java/com/se/nsl/service/TestService.java b/src/main/java/com/se/nsl/service/TestService.java
index 764be0e..c5d56d2 100644
--- a/src/main/java/com/se/nsl/service/TestService.java
+++ b/src/main/java/com/se/nsl/service/TestService.java
@@ -45,6 +45,8 @@
     @Resource
     ResultService resultService;
 
+    public final static double MIN_VAL = 0.00001;
+
     public final static double MAX_X_OFFSET = 0;
 
     public final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
@@ -285,7 +287,7 @@
         for (int[] sizes : layer.getTerrain().getSize()) {
             String fileName = ComHelper.getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
             String tif = dto.getTemp() + File.separator + fileName + ".tif";
-            ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer);
+            ComHelper.Resample2(ds, tif, sizes[0], sizes[1], layer);
             if (!new File(tif).exists()) continue;
 
             String png = waterPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
@@ -303,6 +305,7 @@
             float[] buffer = new float[width * height];
             band.ReadRaster(0, 0, width, height, buffer);
             double[] transform = ds.GetGeoTransform();
+            buffer = interpolateGrid(buffer, width);
 
             BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
             //double differ = layer.getWaters().getMaxHeight() - layer.getWaters().getMinHeight(), minHeight = layer.getWaters().getMinHeight();
@@ -312,7 +315,7 @@
                 for (int y = 0; y < height; y++) {
                     int offset = x + y * width;
                     //if (Float.isNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
-                    if (Float.isNaN(buffer[offset]) || buffer[offset] <= 0.00001 || Float.isNaN(ts[offset])) continue;
+                    if (Float.isNaN(buffer[offset]) || buffer[offset] < MIN_VAL || Float.isNaN(ts[offset])) continue;
 
                     //double X = transform[0] + x * transform[1] + y * transform[2];
                     //double Y = transform[3] + x * transform[4] + y * transform[5];
@@ -337,6 +340,66 @@
         } finally {
             if (null != ds) ds.delete();
         }
+    }
+
+    /**
+     * 鎻掑�煎鐞�-鍥涢偦鍩�
+     */
+    public float[] interpolateGrid(float[] buffer, int size) {
+        float[] tempBuffer = new float[size * size];
+        for (int i = 0; i < size; i++) {
+            for (int j = 0; j < size; j++) {
+                int index = i * size + j;
+                float current = buffer[index];
+                if (current == 0) {
+                    float sum = 0;
+                    int count = 0;
+                    // 妫�鏌ヤ笂閭�
+                    if (i > 0) {
+                        float neighbor = buffer[(i - 1) * size + j];
+                        if (neighbor > MIN_VAL) {
+                            sum += neighbor;
+                            count++;
+                        }
+                    }
+                    // 妫�鏌ヤ笅閭�
+                    if (i < size - 1) {
+                        float neighbor = buffer[(i + 1) * size + j];
+                        if (neighbor > MIN_VAL) {
+                            sum += neighbor;
+                            count++;
+                        }
+                    }
+                    // 妫�鏌ュ乏閭�
+                    if (j > 0) {
+                        float neighbor = buffer[i * size + (j - 1)];
+                        if (neighbor > MIN_VAL) {
+                            sum += neighbor;
+                            count++;
+                        }
+                    }
+                    // 妫�鏌ュ彸閭�
+                    if (j < size - 1) {
+                        float neighbor = buffer[i * size + (j + 1)];
+                        if (neighbor > MIN_VAL) {
+                            sum += neighbor;
+                            count++;
+                        }
+                    }
+                    // 璁$畻鏂板��
+                    if (count > 0) {
+                        tempBuffer[index] = (sum / count) * 0.5f;
+                    } else {
+                        tempBuffer[index] = 0;
+                    }
+                } else {
+                    tempBuffer[index] = current;
+                }
+            }
+        }
+        // 灏嗕复鏃舵暟缁勫鍒跺洖鍘熸暟缁�
+        //System.arraycopy(tempBuffer, 0, buffer, 0, tempBuffer.length);
+        return tempBuffer;
     }
 
     public List<BuildingDepthVo> processBuilding(ResultDto dto, List<String> files, LayerDto layer) {
@@ -541,7 +604,7 @@
                 int offset = x + y * width;
                 float fx = ComHelper.getFloatValue(vxBuffer[offset]);
                 float fy = ComHelper.getFloatValue(vyBuffer[offset]);
-                if (Float.isNaN(fx) && Float.isNaN(fy) || (fx == 0.00001 && fy == 0.00001)) continue;
+                if (Float.isNaN(fx) && Float.isNaN(fy) || (fx < MIN_VAL && fy < MIN_VAL)) continue;
 
                 fx = Float.isNaN(fx) ? 0 : fx;
                 fy = Float.isNaN(fy) ? 0 : fy;
@@ -562,8 +625,8 @@
         layer.getWaters().setFiles(null);
         layer.getTerrain().setEpsg(null);
         layer.getExtension().setDiffer(null);
-        layer.setWaterUrl("/hls/w" + config.getOutPath() + ".m3u8");
-        layer.setFlowUrl("//hls/f" + config.getOutPath() + ".m3u8");
+        layer.setWaterUrl("/hls/w" + dto.getOutPath() + ".m3u8");
+        layer.setFlowUrl("/hls/f" + dto.getOutPath() + ".m3u8");
 
         String json = JSON.toJSONString(layer);
         // String json = JSONUtil.toJsonPrettyStr(layer);
diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml
index 54a321e..da1f26e 100644
--- a/src/main/resources/application-dev.yml
+++ b/src/main/resources/application-dev.yml
@@ -141,8 +141,8 @@
   flowUnits: CMS
   solverBat: D:\other\simu\uwsolver\run_solver.bat
   sww2tifBat: D:\other\simu\uwsolver\sww2tif.bat
-  #sizes: 64,128,256,512,1024,2048
-  sizes: 1024,2048,4096
+  #sizes: 64,128,256,512,1024,2048,4096
+  sizes: 1024
   # 杈撳嚭鏂囦欢
   terrainFile: DEM.tif
   buildingFile: buildings_inside.shp

--
Gitblit v1.9.3