From 115146311d2521fcb3b57c562df0f2a8e9972311 Mon Sep 17 00:00:00 2001
From: dcb <xgybdcb@163.com>
Date: 星期三, 28 五月 2025 11:43:12 +0800
Subject: [PATCH] 根据不同的区域类型选择不同的地形

---
 src/main/java/com/se/nsl/service/ResolveService.java  |   30 +++++++++
 src/main/java/com/se/nsl/utils/AreaType.java          |   28 +++++++++
 src/main/java/com/se/nsl/config/PropertiesConfig.java |   10 +++
 src/main/resources/application-dev.yml                |    3 +
 src/main/java/com/se/nsl/service/SimuService.java     |   68 ++++++++++++++--------
 5 files changed, 113 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/se/nsl/config/PropertiesConfig.java b/src/main/java/com/se/nsl/config/PropertiesConfig.java
index e035421..c10521f 100644
--- a/src/main/java/com/se/nsl/config/PropertiesConfig.java
+++ b/src/main/java/com/se/nsl/config/PropertiesConfig.java
@@ -98,6 +98,8 @@
 
     private Integer epsg;
 
+    private String keyDitch;
+
     public String getVer() {
         return ver;
     }
@@ -453,4 +455,12 @@
     public void setLanduseFile(String landuseFile) {
         this.landuseFile = landuseFile;
     }
+
+    public String getKeyDitch() {
+        return keyDitch;
+    }
+
+    public void setKeyDitch(String keyDitch) {
+        this.keyDitch = keyDitch;
+    }
 }
diff --git a/src/main/java/com/se/nsl/service/ResolveService.java b/src/main/java/com/se/nsl/service/ResolveService.java
index 0ee73a6..475cd87 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;
@@ -154,6 +155,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());
@@ -163,12 +187,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();
     }
diff --git a/src/main/java/com/se/nsl/service/SimuService.java b/src/main/java/com/se/nsl/service/SimuService.java
index a222fe0..2cbe0bf 100644
--- a/src/main/java/com/se/nsl/service/SimuService.java
+++ b/src/main/java/com/se/nsl/service/SimuService.java
@@ -134,7 +134,7 @@
 //        System.out.println(String.format("杞崲鍓嶇殑鍧愭爣锛歺:%s,y:%s", lon, lat));
 //        System.out.println(String.format("杞崲鍚庣殑鍧愭爣锛歺:%s,y:%s", xy[0], xy[1]));
         //read from zarr
-//        return null;
+//        return queryByZarr(xy, time, serviceName);
 
         //read from tif file
         return queryByTif(xy, time, serviceName);
@@ -146,28 +146,29 @@
         double y = xy[1];
         String prefix = formatTime(time);
         File inPath = new File(config.getInPath());
-        File tifDir = new File(inPath, serviceName + File.separator + "depth");
-        int index = 0;
-        File[] files = tifDir.listFiles();
-        for (File file : files) {
-            String name = file.getName();
-            if (!name.endsWith(TIF_EXTSION)) continue;
-            if (name.equals(prefix + TIF_EXTSION)) {
-                break;
-            }
-            index++;
-        }
-        //TODO闇�瑕佽绠楄鍒楀彿
-        int col = 0;
-        int row = 0;
-
+//        File tifDir = new File(inPath, serviceName + File.separator + "depth");
+//        int index = 0;
+//        File[] files = tifDir.listFiles();
+//        for (File file : files) {
+//            String name = file.getName();
+//            if (!name.endsWith(TIF_EXTSION)) continue;
+//            if (name.equals(prefix + TIF_EXTSION)) {
+//                break;
+//            }
+//            index++;
+//        }
+        File dem = new File(inPath, serviceName + File.separator + "DEM.tif");
+        ColumnRow cr = getColumnRow(dem.getAbsoluteFile(), x, y);
+        if (cr == null) return null;
 
         File zarr = new File(inPath, serviceName + File.separator + "result.zarr");
         try {
             ZarrGroup group = ZarrGroup.open(zarr.toPath());
             ZarrArray depthArray = group.openArray("depth");
-            Object object = depthArray.read();
-            System.out.println(object);
+            int[] shape = new int[] {60, 637, 351};
+//            int[] offset = new int[]{210, 384};
+            float[] depth = (float[]) depthArray.read(shape);
+            System.out.println("depth:" + depth.length);
         } catch (IOException | InvalidRangeException e) {
             throw new RuntimeException(e);
         }
@@ -187,6 +188,18 @@
             return null;
         }
 
+        ColumnRow cr = getColumnRow(tifFile, x, y);
+        if (cr == null) return null;
+        System.out.println("col:" + cr.col + " ,row:" + cr.row);
+        float depth = readPixelValue(cr.dataset, cr.col, cr.row, 1);
+        float velocity = calcVelocity(cr.dataset, cr.col, cr.row);
+        SimuResult result = new SimuResult();
+        result.setDepth(depth);
+        result.setVelocity(velocity);
+        return  result;
+    }
+
+    private static ColumnRow getColumnRow(File tifFile, double x, double y) {
         Dataset dataset = gdal.Open(tifFile.getAbsolutePath(), gdalconstConstants.GA_ReadOnly);
         // 鑾峰彇鍦扮悊鍙樻崲鍙傛暟锛�6鍏冪礌鏁扮粍锛�
         // [0]: 宸︿笂瑙扻鍧愭爣, [1]: 鍍忓厓瀹藉害, [2]: X鏂瑰悜鏃嬭浆,
@@ -201,12 +214,19 @@
             log.warn("琛屽垪鍙蜂笉鍦╰if鑼冨洿鍐�");
             return null;
         }
-        float depth = readPixelValue(dataset, col, row, 1);
-        float velocity = calcVelocity(dataset, col, row);
-        SimuResult result = new SimuResult();
-        result.setDepth(depth);
-        result.setVelocity(velocity);
-        return  result;
+        return new ColumnRow(dataset, col, row);
+    }
+
+    private static class ColumnRow {
+        public final Dataset dataset;
+        public final int col;
+        public final int row;
+
+        public ColumnRow(Dataset dataset, int col, int row) {
+            this.dataset = dataset;
+            this.col = col;
+            this.row = row;
+        }
     }
 
     private float calcVelocity(Dataset dataset, int col, int row) {
diff --git a/src/main/java/com/se/nsl/utils/AreaType.java b/src/main/java/com/se/nsl/utils/AreaType.java
new file mode 100644
index 0000000..cce8cf0
--- /dev/null
+++ b/src/main/java/com/se/nsl/utils/AreaType.java
@@ -0,0 +1,28 @@
+package com.se.nsl.utils;
+
+
+/**
+ * 鍖哄煙绫诲瀷
+ */
+public enum AreaType {
+    CUSTOM(0),
+    ADMINISTRATIVE_AREA(1),
+    KEY_AREA(2),
+    KEY_DITCH(3);
+
+    private int val;
+
+    AreaType(int val) {
+        this.val = val;
+    }
+
+    public static AreaType of(int val) {
+        AreaType[] values = values();
+        for (AreaType type : values) {
+            if (type.val == val) {
+                return type;
+            }
+        }
+        return AreaType.ADMINISTRATIVE_AREA;
+    }
+}
diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml
index 9196b5e..955b14b 100644
--- a/src/main/resources/application-dev.yml
+++ b/src/main/resources/application-dev.yml
@@ -169,3 +169,6 @@
   sourceLanduse: D:\other\simu\CudaUWSolver-2.0\Beijing-Data-10m\Beijing-4548-Landuse-10m-Nonodata.tif
   copyTif: false
   tifPath: D:\other\simu\uwsolver\5ca43c87cd8e48c5a9c5399a5da46dbc\tongzhou_raster_4548_1m_clip_river_fill.tif
+  #tif涓殑楂樼▼鏂囦欢鍚嶈鍚湁dem锛屽湡鍦板埄鐢ㄨ鍚湁landuse锛岀珯鐐硅鍚湁station锛屼笉闄愬埗澶у皬鍐欙紝鏂囧瓧灏介噺鐢ㄨ嫳鏂囧瓧姣嶈〃杈�
+  keyDitch: D:\other\simu\CudaUWSolver-2.0\KeyDitch
+

--
Gitblit v1.9.3