From 35d1a18c3936bdd2e7640cd37360d6eff8925e39 Mon Sep 17 00:00:00 2001
From: dcb <xgybdcb@163.com>
Date: 星期五, 30 五月 2025 10:24:19 +0800
Subject: [PATCH] 升级求解器版本到2.1.1

---
 src/main/java/com/se/nsl/service/ResolveService.java |   86 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 77 insertions(+), 9 deletions(-)

diff --git a/src/main/java/com/se/nsl/service/ResolveService.java b/src/main/java/com/se/nsl/service/ResolveService.java
index 63b3fa5..5edd663 100644
--- a/src/main/java/com/se/nsl/service/ResolveService.java
+++ b/src/main/java/com/se/nsl/service/ResolveService.java
@@ -14,6 +14,7 @@
 import com.se.nsl.helper.GdalHelper;
 import com.se.nsl.helper.StringHelper;
 import com.se.nsl.helper.WebHelper;
+import com.se.nsl.utils.AreaType;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.gdal.gdal.Band;
@@ -40,6 +41,8 @@
 import java.nio.file.StandardCopyOption;
 import java.sql.Timestamp;
 import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
 import java.util.*;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -128,6 +131,7 @@
             createNsl(data);
 
             update(simu, 10, "瀹屾垚");
+            log.info("妯℃嫙瀹屾垚");
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
             update(simu, 20, ex.getMessage());
@@ -152,6 +156,29 @@
         createDir(inPath + File.separator + "velocity");
         createDir(config.getOutPath() + File.separator + data.getOutPath());
 
+        Short areaType = simu.getAreaType();
+        AreaType at = AreaType.of(areaType);
+        String terrainTif = config.getSourceDem();
+        String landuseTif = config.getSourceLanduse();
+        if (at == AreaType.KEY_DITCH) {
+            File keyDitchDir = new File(config.getKeyDitch());
+            String areaName = simu.getAreaName();
+            Optional<File> first = Arrays.stream(keyDitchDir.listFiles()).filter(f -> f.getName().equals(areaName)).findFirst();
+            if (first.isPresent()) {
+                File targetTifDir = first.get();
+                File[] files = targetTifDir.listFiles();
+                for (File file : files) {
+                    String name = file.getName();
+                    if (name.toLowerCase().contains("dem")) {
+                        terrainTif = file.getAbsolutePath();
+                    }
+                    if (name.toLowerCase().contains("landuse")) {
+                        landuseTif = file.getAbsolutePath();
+                    }
+                }
+            }
+        }
+
         Geometry geom = Geometry.CreateFromWkt(simu.getGeom());
         if (geom.GetGeometryType() == ogr.wkbMultiPolygon) geom = geom.GetGeometryRef(0);
         SpatialReference dstSR = GdalHelper.createSpatialReference(config.getEpsg());
@@ -161,12 +188,14 @@
         String wkt = geom.ExportToWkt();
 
         String terrainFile = inPath + File.separator + config.getTerrainFile();
-        Dataset dsDem = gdal.Open(config.getSourceDem(), gdalconstConstants.GA_ReadOnly);
+
+        Dataset dsDem = gdal.Open(terrainTif, gdalconstConstants.GA_ReadOnly);
         ComHelper.Resample(dsDem, null, terrainFile, null, wkt, null, null);
         dsDem.delete();
 
         String landuseFile = inPath + File.separator + config.getLanduseFile();
-        Dataset dsLanduse = gdal.Open(config.getSourceLanduse(), gdalconstConstants.GA_ReadOnly);
+
+        Dataset dsLanduse = gdal.Open(landuseTif, gdalconstConstants.GA_ReadOnly);
         ComHelper.Resample(dsLanduse, null, landuseFile, null, wkt, null, null);
         dsLanduse.delete();
     }
@@ -208,6 +237,30 @@
         }
         f.mkdirs();
     }
+    
+    enum RainFallUnit {
+        MM_H("mm/h", 60),
+        MM_15MIN("mm/15min", 15),
+        MM_5MIN("mm/5min", 5),
+        MM_MIN("mm/min", 1);
+        private String unit;
+        private int c; //绯绘暟
+        RainFallUnit(String unit, int c) {
+            this.unit = unit;
+            this.c = c;
+        }
+
+        public static RainFallUnit of(String unit) {
+            RainFallUnit[] values = values();
+            for (RainFallUnit v : values) {
+                if (v.unit.equals(unit)) {
+                    return v;
+                }
+            }
+            return MM_H; //榛樿鎸夌収mm/h璁$畻
+        }
+
+    }
 
     public void createRainfallFile(Simu simu, SimuData data) throws Exception {
         List<Rainfall> rainfalls = data.getRainfalls();
@@ -226,8 +279,12 @@
         double centerX = ComHelper.getMinVal((data.getMinx() + data.getMaxx()) / 2, DIGIT);
         double centerY = ComHelper.getMinVal((data.getMiny() + data.getMaxy()) / 2, DIGIT);
         String prefix = config.getRainfallSite() + " " + centerX + " " + centerY + " ";
-        int unit = StringUtils.isEmpty(data.getIntensityUnit()) || "mm/h".equals(data.getIntensityUnit()) ? 60 : 5;
-
+        //濡傛灉娌℃湁鍗曚綅鎴栬�呭崟浣嶄负mm/h鍒欐寜鐓� mm/h璁$畻锛屽惁鍒欐寜鐓m/5min璁$畻
+//        int unit = StringUtils.isEmpty(data.getIntensityUnit()) || "mm/h".equals(data.getIntensityUnit()) ? 60 : 5;
+        String iu = data.getIntensityUnit();
+        RainFallUnit rfUnit = RainFallUnit.of(iu);
+        data.setIntensityUnit(rfUnit.unit);
+        int unit = rfUnit.c;
         int c = rainfalls.size() - 1;
         for (int i = 0; i < c; i++) {
             Rainfall r1 = rainfalls.get(i);
@@ -237,7 +294,7 @@
         }
         //list.add(prefix + YYYYMDHM.format(rainfalls.get(c).getTime()) + getMinVal(rainfalls.get(c).getIntensity() / unit, DIGIT));
         list.add(String.format("%s%s%f", prefix, YYYYMDHM.format(rainfalls.get(c).getTime()), getMinVal(rainfalls.get(c).getIntensity() / unit, DIGIT)));
-        list.add(0, "1 " + (list.size() - 1));
+//        list.add(0, "1 " + (list.size() - 1));
 
         Files.write(Paths.get(rainfallFile), list, StandardCharsets.UTF_8);
     }
@@ -273,7 +330,14 @@
 
         int duration = 3600 * data.getDuration(); // 绉掓暟
         if (null != data.getRainfalls() && data.getRainfalls().size() > 1) {
-            duration = (int) (Math.abs(data.getRainfalls().get(data.getRainfalls().size() - 1).getTime().getTime() - data.getRainfalls().get(0).getTime().getTime()) / 60);
+            List<Rainfall> rainfalls = data.getRainfalls();
+            int size = rainfalls.size();
+            Rainfall last = rainfalls.get(size - 1);
+            Rainfall first = rainfalls.get(0);
+            Instant end = last.getTime().toInstant();
+            Instant start = first.getTime().toInstant();
+            long diff = ChronoUnit.SECONDS.between(end, start);
+            duration = (int) (Math.abs(diff));
         }
 
         String inPath = config.getInPath() + File.separator + data.getInPath();
@@ -281,7 +345,10 @@
         String landuseFile = inPath + File.separator + config.getLanduseFile();
         String rainfallFile = (inPath + File.separator + "rainfall.dat");
         String saveName = inPath + File.separator + "result.zarr";
-        ConfigVo vo = new ConfigVo(terrainFile, landuseFile, terrainFile, rainfallFile, saveName, duration, config.getSaveFrames());
+//        ConfigVo vo = new ConfigVo(terrainFile, landuseFile, terrainFile, rainfallFile, saveName, duration, config.getSaveFrames());
+        Integer saveFrameInterval = config.getSaveFrameInterval();
+        int saveFrames = duration / 60 / saveFrameInterval;
+        ConfigVo vo = new ConfigVo(terrainFile, landuseFile, terrainFile, rainfallFile, saveName, duration, saveFrames);
 
         String configFile = config.getInPath() + File.separator + data.getInPath() + File.separator + data.getInPath() + ".json";
         ComHelper.writeJson(configFile, JSON.toJSONString(vo));
@@ -321,8 +388,9 @@
             try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
                 String line;
                 while ((line = reader.readLine()) != null) {
-                    System.out.println(line);
+//                    System.out.println(line);
                     //sb.append(line);
+                    log.info(line);
                 }
             }
 
@@ -473,7 +541,7 @@
 
             cal.add(Calendar.MINUTE, 1);
         }
-        list.add(0, "1 " + (list.size() - 1));
+//        list.add(0, "1 " + (list.size() - 1));
 
         Files.write(Paths.get(dat), list, StandardCharsets.UTF_8);
     }

--
Gitblit v1.9.3