From 59b432c883011119649c283cc9d4c1d357599802 Mon Sep 17 00:00:00 2001
From: dcb <xgybdcb@163.com>
Date: 星期一, 30 六月 2025 15:13:56 +0800
Subject: [PATCH] 对于没有不能获取到数据的雨量计,使用其他雨量计数据填充

---
 src/main/java/com/se/nsl/service/SimuService.java |  131 +++++++++++++++++--------------------------
 1 files changed, 53 insertions(+), 78 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..b98d3ee 100644
--- a/src/main/java/com/se/nsl/service/SimuService.java
+++ b/src/main/java/com/se/nsl/service/SimuService.java
@@ -13,21 +13,18 @@
 import com.se.nsl.helper.StringHelper;
 import com.se.nsl.mapper.SimuMapper;
 import com.se.nsl.utils.CoordinateTransformer;
+import com.se.nsl.utils.SolverTifUtil;
+import com.se.nsl.utils.TimeFormatUtil;
 import lombok.extern.slf4j.Slf4j;
-import org.gdal.gdal.Band;
-import org.gdal.gdal.Dataset;
-import org.gdal.gdal.gdal;
-import org.gdal.gdalconst.gdalconstConstants;
 import org.springframework.stereotype.Service;
 import ucar.ma2.InvalidRangeException;
 
 import javax.annotation.Resource;
 import java.io.File;
 import java.io.IOException;
-import java.time.Instant;
-import java.time.LocalDateTime;
-import java.time.ZoneId;
-import java.time.format.DateTimeFormatter;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
@@ -36,6 +33,7 @@
 @SuppressWarnings("ALL")
 public class SimuService {
     public static final String TIF_EXTSION = ".tif";
+    public static final String YYYY_MM_DD_HH_MM_SS = "yyyyMMddHHmmss";
     @Resource
     SimuMapper simuMapper;
 
@@ -55,9 +53,7 @@
         Page<Simu> page = new Page<>(pageNum, pageSize);
         page.addOrder(OrderItem.desc("id"));
 
-        IPage<Simu> paged = simuMapper.selectPage(page, wrapper);
-
-        return paged;
+        return simuMapper.selectPage(page, wrapper);
     }
 
     private QueryWrapper<Simu> getPageWrapper(SimuVo vo, int pageNum, int pageSize) {
@@ -134,10 +130,23 @@
 //        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);
+
+    }
+
+    public List<SimuResult> queryByPosition(double lon, double lat, String serviceName) {
+        //transform coordiante from 4326 to 4548
+        double[] xy = CoordinateTransformer.transform(4326, config.getEpsg(), lon, lat);
+//        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 queryByZarr(xy, time, serviceName);
+
+        //read from tif file
+        return queryByTif(xy, serviceName);
 
     }
 
@@ -146,39 +155,35 @@
         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");
+        //TODO
 
         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);
         }
-
-
         return null;
     }
 
     private SimuResult queryByTif(double[] xy, long time, String serviceName) {
-        double x = xy[0];
-        double y = xy[1];
         File inPath = new File(config.getInPath());
         String prefix = formatTime(time);
         String child = serviceName + File.separator + "depth" + File.separator + prefix + TIF_EXTSION;
@@ -186,57 +191,27 @@
         if (!tifFile.exists()) {
             return null;
         }
-
-        Dataset dataset = gdal.Open(tifFile.getAbsolutePath(), gdalconstConstants.GA_ReadOnly);
-        // 鑾峰彇鍦扮悊鍙樻崲鍙傛暟锛�6鍏冪礌鏁扮粍锛�
-        // [0]: 宸︿笂瑙扻鍧愭爣, [1]: 鍍忓厓瀹藉害, [2]: X鏂瑰悜鏃嬭浆,
-        // [3]: 宸︿笂瑙扽鍧愭爣, [4]: Y鏂瑰悜鏃嬭浆, [5]: 鍍忓厓楂樺害锛堣礋鍊艰〃绀篩杞村悜涓嬶級
-        double[] geoTransform = dataset.GetGeoTransform();
-        //璁$畻鏍呮牸琛屽垪鍙�
-        int col = (int) ((x - geoTransform[0]) / geoTransform[1]);
-        int row = (int) ((geoTransform[3] - y) / Math.abs(geoTransform[5]));
-        int width = dataset.getRasterXSize();
-        int height = dataset.getRasterYSize();
-        if (col < 0 || col > width || row < 0 || row > height) {
-            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  SolverTifUtil.getSimuResult(tifFile, xy);
     }
 
-    private float calcVelocity(Dataset dataset, int col, int row) {
-        float x = readPixelValue(dataset, col, row, 2);
-        float y = readPixelValue(dataset, col, row, 3);
-        float velocity = 0f;
-        if (Float.isNaN(x) && Float.isNaN(y)) {
-            velocity = 0f;
-        } else if (Float.isNaN(x)) {
-            velocity = y;
-        } else if (Float.isNaN(y)) {
-            velocity = x;
-        } else {
-            velocity = (float) Math.sqrt(x * x + y * y);
+    private List<SimuResult> queryByTif(double[] xy, String serviceName) {
+        List<SimuResult> res = new ArrayList<>();
+        File inPath = new File(config.getInPath());
+        Path depthPath = Paths.get(inPath.getAbsolutePath(), serviceName, "depth");
+        File depthDir = depthPath.toFile();
+        File[] files = depthDir.listFiles();
+        for (File tifFile : files) {
+            String name = tifFile.getName();
+            if (!name.endsWith(TIF_EXTSION)) continue;
+            SimuResult result = SolverTifUtil.getSimuResult(tifFile, xy);
+            if (result == null) continue;
+            res.add(result);
         }
-        return velocity;
-    }
-
-    private float readPixelValue(Dataset dataset, int col, int row, int bandNum) {
-        Band band = dataset.GetRasterBand(bandNum);
-        float[] values = new float[1];
-        band.ReadRaster(col, row, 1, 1, values);
-        return values[0];
+        return res;
     }
 
     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);
+        return TimeFormatUtil.formatTime(time, YYYY_MM_DD_HH_MM_SS);
     }
 
 }

--
Gitblit v1.9.3