From a2ee6e0dcdcfd9d1b8011a3cecb4e0fc4f6eeea3 Mon Sep 17 00:00:00 2001
From: dcb <xgybdcb@163.com>
Date: 星期五, 06 六月 2025 18:10:32 +0800
Subject: [PATCH] 实时模拟功能实现

---
 src/main/java/com/se/nsl/service/SimuService.java |   78 +++++++++++++++++++++++++--------------
 1 files changed, 50 insertions(+), 28 deletions(-)

diff --git a/src/main/java/com/se/nsl/service/SimuService.java b/src/main/java/com/se/nsl/service/SimuService.java
index a222fe0..1c314bc 100644
--- a/src/main/java/com/se/nsl/service/SimuService.java
+++ b/src/main/java/com/se/nsl/service/SimuService.java
@@ -13,6 +13,7 @@
 import com.se.nsl.helper.StringHelper;
 import com.se.nsl.mapper.SimuMapper;
 import com.se.nsl.utils.CoordinateTransformer;
+import com.se.nsl.utils.TimeFormatUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.gdal.gdal.Band;
 import org.gdal.gdal.Dataset;
@@ -134,7 +135,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 +147,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 +189,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 +215,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) {
@@ -233,10 +254,11 @@
     }
 
     private String formatTime(long time) {
-        Instant instant = Instant.ofEpochMilli(time);
-        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
-        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
-        return localDateTime.format(formatter);
+//        Instant instant = Instant.ofEpochMilli(time);
+//        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
+//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
+//        return localDateTime.format(formatter);
+        return TimeFormatUtil.formatTime(time, "yyyyMMddHHmmss");
     }
 
 }

--
Gitblit v1.9.3