From 2975fe8b88c0dd70a2dd5e06ad2fcd7d375c5043 Mon Sep 17 00:00:00 2001
From: wuww <252740454@qq.com>
Date: 星期二, 15 四月 2025 09:57:08 +0800
Subject: [PATCH] 修改测试服务(处理源数据文件)

---
 libs/gdal.jar                                     |    0 
 src/main/java/com/se/nsl/service/TestService.java |   98 +++++++++++++++++++++++++++-----
 src/main/resources/application-dev.yml            |    4 +
 src/main/resources/application-prod.yml           |    3 
 src/test/java/com/se/nsl/AppTest.java             |   58 ++++++++++++++++++
 pom.xml                                           |    7 +-
 libs/gdalalljni.dll                               |    0 
 7 files changed, 147 insertions(+), 23 deletions(-)

diff --git a/libs/gdal.jar b/libs/gdal.jar
new file mode 100644
index 0000000..4f8c99e
--- /dev/null
+++ b/libs/gdal.jar
Binary files differ
diff --git a/libs/gdalalljni.dll b/libs/gdalalljni.dll
new file mode 100644
index 0000000..0cdce0f
--- /dev/null
+++ b/libs/gdalalljni.dll
Binary files differ
diff --git a/pom.xml b/pom.xml
index 971fd74..53a1143 100644
--- a/pom.xml
+++ b/pom.xml
@@ -117,11 +117,12 @@
         <dependency>
             <groupId>org.gdal</groupId>
             <artifactId>gdal</artifactId>
-            <!--<version>3.9.0</version>-->
             <version>3.5.0</version>
-            <!--version>3.2.0</version-->
+            <!--<version>3.10.0</version>
+            <scope>system</scope>
+            <systemPath>${project.basedir}/libs/gdal.jar</systemPath>-->
         </dependency>
-        <!--rainfall-->
+<!--rainfall-->
         <dependency>
             <groupId>com.mathworks.toolbox</groupId>
             <artifactId>javabuilder</artifactId>
diff --git a/src/main/java/com/se/nsl/service/TestService.java b/src/main/java/com/se/nsl/service/TestService.java
index d5033f1..dc05269 100644
--- a/src/main/java/com/se/nsl/service/TestService.java
+++ b/src/main/java/com/se/nsl/service/TestService.java
@@ -3,10 +3,7 @@
 import cn.hutool.core.io.FileUtil;
 import com.alibaba.fastjson.JSON;
 import com.se.nsl.config.PropertiesConfig;
-import com.se.nsl.domain.dto.ExtensionDto;
-import com.se.nsl.domain.dto.LayerDto;
-import com.se.nsl.domain.dto.PointDto;
-import com.se.nsl.domain.dto.ResultDto;
+import com.se.nsl.domain.dto.*;
 import com.se.nsl.domain.po.DataPo;
 import com.se.nsl.domain.po.PondingPo;
 import com.se.nsl.domain.vo.BuildingDepthVo;
@@ -17,7 +14,8 @@
 import org.gdal.gdal.Band;
 import org.gdal.gdal.Dataset;
 import org.gdal.gdal.gdal;
-import org.gdal.gdalconst.gdalconst;
+//import org.gdal.gdalconstConstants.gdalconst;
+import org.gdal.gdalconst.gdalconstConstants;
 import org.gdal.ogr.*;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
@@ -35,6 +33,7 @@
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 @Slf4j
 @Service
@@ -69,7 +68,7 @@
         try {
             copeTerrain(dto, layer);
             //copeBuilding(dto, layer);
-            //List<BuildingDepthVo> buildings = copeWater(dto, layer);
+            List<BuildingDepthVo> buildings = copeWater(dto, layer);
             //copeFlow(dto, layer);
             copeLayerJson(dto, layer);
             //copeRainFallJson(dto, layer);
@@ -85,7 +84,8 @@
     public void copeTerrain(ResultDto dto, LayerDto layer) {
         Dataset ds = null;
         try {
-            ds = gdal.Open(dto.getTerrainFile(), gdalconst.GA_ReadOnly);
+            // gdalconst锛実dalconstConstants
+            ds = gdal.Open(dto.getTerrainFile(), gdalconstConstants.GA_ReadOnly);
             if (null == ds || 0 == ds.getRasterCount()) return;
 
             setTerrainInfo(ds, layer);
@@ -130,7 +130,7 @@
     public void Terrain2Png(LayerDto layer, String tif, String png, int width, int height) {
         Dataset ds = null;
         try {
-            ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
+            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
             if (null == ds || 0 == ds.getRasterCount()) return;
 
             Band band = ds.GetRasterBand(1);
@@ -175,6 +175,19 @@
         setWaterHeight(layer, files);
     }
 
+    public List<BuildingDepthVo> copeWater(ResultDto dto, LayerDto layer) {
+        List<String> files = layer.getWaters().getFiles();
+        if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return null;
+
+        processWaters(dto, files, layer);
+
+        if (new File(dto.getBuildingFile()).exists()) {
+            return processBuilding(dto, files, layer);
+        }
+
+        return null;
+    }
+
     public List<String> getFiles(String path, String suffix) {
         List<String> files = new ArrayList<>();
         ComHelper.getFiles(files, new File(path), suffix);
@@ -214,7 +227,7 @@
         files.parallelStream().forEach(file -> {
             Dataset ds = null;
             try {
-                ds = gdal.Open(file, gdalconst.GA_ReadOnly);
+                ds = gdal.Open(file, gdalconstConstants.GA_ReadOnly);
                 if (null == ds || 0 == ds.getRasterCount()) return;
 
                 double[] mm = new double[2];
@@ -234,7 +247,7 @@
         for (int i = 0, c = files.size(); i < c; i++) {
             Dataset ds = null;
             try {
-                ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
+                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                 if (null == ds || 0 == ds.getRasterCount()) return;
                 if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
 
@@ -266,7 +279,7 @@
     public void water2Png(ResultDto dto, LayerDto layer, String tif, String png, int width, int height) {
         Dataset ds = null;
         try {
-            ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
+            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
             if (null == ds || 0 == ds.getRasterCount()) return;
 
             Band band = ds.GetRasterBand(1);
@@ -282,7 +295,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 || Float.isNaN(ts[offset])) continue;
+                    if (Float.isNaN(buffer[offset]) || buffer[offset] <= 0.001 || 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];
@@ -308,11 +321,64 @@
         }
     }
 
+    public List<BuildingDepthVo> processBuilding(ResultDto dto, List<String> files, LayerDto layer) {
+        List<BuildingDepthVo> list = new CopyOnWriteArrayList<>();
+        for (int i = 0, c = files.size(); i < c; i++) {
+            Dataset ds = null;
+            try {
+                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
+                if (null == ds || 0 == ds.getRasterCount()) continue;
+                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
+
+                copeBuildingDepth(dto, ds, layer.getWaters().getData().get(i), list);
+            } finally {
+                if (null != ds) ds.delete();
+            }
+        }
+
+        return list;
+    }
+
+    public void copeBuildingDepth(ResultDto dto, Dataset ds, long ticks, List<BuildingDepthVo> list) {
+        double[] transform = ds.GetGeoTransform();
+        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
+        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]);
+
+        for (BuildingDto building : dto.getBuildingList()) {
+            Double val = getValue(ds, building, xSize, ySize, minX, maxY, pixelWidth, pixelHeight);
+            list.add(new BuildingDepthVo(building.getId(), ticks, val));
+        }
+    }
+
+    public Double getValue(Dataset ds, BuildingDto building, int xSize, int ySize, double minX, double maxY, double pixelWidth, double pixelHeight) {
+        double[] env = new double[4];
+        building.getGeom().GetEnvelope(env);
+
+        int startX = (int) Math.floor((env[0] - minX) / pixelWidth);
+        int endX = (int) Math.floor((env[1] - minX) / pixelWidth);
+        int startY = (int) Math.floor((maxY - env[3]) / Math.abs(pixelHeight));
+        int endY = (int) Math.floor((maxY - env[2]) / Math.abs(pixelHeight));
+        if (startX < 0) startX = 0;
+        if (startY < 0) startY = 0;
+        if (endX > ds.getRasterXSize()) endX = ds.getRasterXSize();
+        if (endY > ds.getRasterYSize()) endY = ds.getRasterYSize();
+        if (endX - startX < 1 || endY - startY < 1) return null;
+
+        int width = endX - startX;
+        int height = endY - startY;
+        double[] pixelValues = new double[width * height];
+        ds.GetRasterBand(1).ReadRaster(startX, startY, width, height, pixelValues);
+
+        Double val = Arrays.stream(pixelValues).max().getAsDouble();
+
+        return ComHelper.isValid(val) ? val : null;
+    }
+
     public void createVectors(ResultDto dto, Dataset ds, LayerDto layer, Long ticks) {
         String filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "polygonize.geojson";
         ShpHelper.polygonize2Geojson(ds, filePath);
 
-        List<Geometry> geometries = getGeometries(filePath);
+        /*List<Geometry> geometries = getGeometries(filePath);
         if (CollectionUtils.isEmpty(geometries)) return;
 
         List<PondingPo> list = copePonding(dto, ds, layer, geometries);
@@ -323,7 +389,7 @@
             ComHelper.writeJson(filePath, JSON.toJSONString(list));
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
-        }
+        }*/
     }
 
     public List<Geometry> getGeometries(String filePath) {
@@ -407,7 +473,7 @@
         for (int i = 0, c = files.size(); i < c; i++) {
             Dataset ds = null;
             try {
-                ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
+                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                 if (null == ds || 0 == ds.getRasterCount()) return;
                 if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
 
@@ -437,7 +503,7 @@
     public void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) {
         Dataset ds = null;
         try {
-            ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
+            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
             if (null == ds || 0 == ds.getRasterCount()) return;
 
             float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height];
diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml
index b6a6d58..6148f89 100644
--- a/src/main/resources/application-dev.yml
+++ b/src/main/resources/application-dev.yml
@@ -112,6 +112,7 @@
   cacheTime: 60
   # Gdal椹卞姩鐩綍
   gdalPath: D:\soft\release-1928-x64-dev\release-1928-x64\bin
+  #gdalPath: C:\Program Files\GDAL
   #inPath: D:\simu\in
   inPath: D:\other\simu\uwsolver
   outPath: D:\other\simu\out
@@ -140,7 +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: 64,128,256,512,1024,2048
+  sizes: 1024
   # 杈撳嚭鏂囦欢
   terrainFile: DEM.tif
   buildingFile: buildings_inside.shp
diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml
index 7804889..a4eb0a0 100644
--- a/src/main/resources/application-prod.yml
+++ b/src/main/resources/application-prod.yml
@@ -140,7 +140,8 @@
   flowUnits: CMS
   solverBat: H:\simu\uwsolver\run_solver.bat
   sww2tifBat: H:\simu\uwsolver\sww2tif.bat
-  sizes: 64,128,256,512,1024,2048
+  #sizes: 64,128,256,512,1024,2048
+  sizes: 1024
   # 杈撳嚭鏂囦欢
   terrainFile: DEM.tif
   buildingFile: buildings_inside.shp
diff --git a/src/test/java/com/se/nsl/AppTest.java b/src/test/java/com/se/nsl/AppTest.java
index b3a8521..2affd86 100644
--- a/src/test/java/com/se/nsl/AppTest.java
+++ b/src/test/java/com/se/nsl/AppTest.java
@@ -1,5 +1,11 @@
 package com.se.nsl;
 
+import com.se.nsl.helper.GdalHelper;
+import org.gdal.gdal.Dataset;
+import org.gdal.gdal.Driver;
+import org.gdal.gdal.gdal;
+import org.gdal.gdalconst.gdalconst;
+import org.gdal.gdalconst.gdalconstConstants;
 import org.junit.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 
@@ -22,7 +28,55 @@
 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
 public class AppTest {
     @Test
-    public void t1() {
-        System.out.println("t1...");
+    public void procDepthFiles() {
+        String path = "D:\\other\\simu\\uwsolver\\20250412\\depth";
+        for (File file : new File(path).listFiles()) {
+            if (!file.exists() || !file.isDirectory()) continue;
+
+            File tif = new File(path + "\\" + file.getName() + "\\depth.tif");
+            if (!tif.exists() || tif.isDirectory()) continue;
+
+            File newFile = new File(path + "\\2025041200" + file.getName() + ".tif");
+            System.out.println(newFile.getPath());
+            tif.renameTo(newFile);
+            file.delete();
+        }
+    }
+
+    //@Test
+    public void readZarr() throws Exception {
+        GdalHelper.init("D:\\soft\\release-1928-x64-dev\\release-1928-x64\\bin");
+
+        String zarrPath = "D:\\other\\simu\\CudaUWSolver.zarr";
+        Dataset ds = null;
+        try {
+            Driver driver = gdal.GetDriverByName("Zarr");
+            ds = gdal.Open(zarrPath, gdalconst.GA_ReadOnly);
+            if (null == ds || ds.getRasterCount() < 1) return;
+
+            System.out.println(ds.getRasterCount());
+        } finally {
+            if (null != ds) ds.delete();
+        }
+    }
+
+    //@Test
+    public void readZarr2() throws Exception {
+        String zarrInputPath = "D:\\other\\simu\\out\\out.zarr";
+        gdal.AllRegister();
+        System.out.println("GDAL Initialized. Version: " + gdal.VersionInfo("RELEASE_NAME"));
+
+        Dataset inputDataset = gdal.Open(zarrInputPath, gdalconst.GA_ReadOnly);
+
+        if (inputDataset == null) {
+            System.err.println("Could not open input dataset: " + zarrInputPath);
+            return;
+        }
+
+        System.out.println("Successfully opened");
+        System.out.println(
+                "Driver: " + inputDataset.GetDriver().getShortName() + "/" + inputDataset.GetDriver().getLongName());
+        System.out.println("Size: " + inputDataset.getRasterXSize() + "x" + inputDataset.getRasterYSize() + "x"
+                + inputDataset.getRasterCount());
     }
 }

--
Gitblit v1.9.3