From aaa7a1f1bd3ab90648a62bb0316b988136b35227 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 14 十一月 2024 10:34:32 +0800
Subject: [PATCH] 1

---
 src/main/java/com/se/simu/service/ResultService.java |  122 ++++++++++++++++++++++++++++++++--------
 1 files changed, 97 insertions(+), 25 deletions(-)

diff --git a/src/main/java/com/se/simu/service/ResultService.java b/src/main/java/com/se/simu/service/ResultService.java
index d6a980b..ef859a7 100644
--- a/src/main/java/com/se/simu/service/ResultService.java
+++ b/src/main/java/com/se/simu/service/ResultService.java
@@ -8,6 +8,7 @@
 import com.se.simu.domain.dto.LayerDto;
 import com.se.simu.domain.dto.ResultDto;
 import com.se.simu.domain.po.DataPo;
+import com.se.simu.domain.po.PondingPo;
 import com.se.simu.domain.vo.BuildingDepthVo;
 import com.se.simu.helper.GdalHelper;
 import com.se.simu.helper.ShpHelper;
@@ -106,11 +107,11 @@
         layer.setExtension(new ExtensionDto(minx, miny, maxx, maxy, getMinVal(mm[0] - 1, 1000), getMaxVal(mm[1] + 1, 1000)));
     }
 
-    private static double getMinVal(double val, double radix) {
+    private double getMinVal(double val, double radix) {
         return ((long) Math.floor(val * radix)) / radix;
     }
 
-    private static double getMaxVal(double val, double radix) {
+    private double getMaxVal(double val, double radix) {
         return ((long) Math.ceil(val * radix)) / radix;
     }
 
@@ -129,7 +130,7 @@
         }
     }
 
-    private static void Resample(Dataset ds, String dest, int width, int height, LayerDto layer) {
+    private void Resample(Dataset ds, String dest, int width, int height, LayerDto layer) {
         Vector<String> vector = new Vector<>();
         //vector.add("-s_srs");
         //vector.add("EPSG:" + 4548);
@@ -155,7 +156,7 @@
         destDs.delete();
     }
 
-    private static void Terrain2Png(LayerDto layer, String tif, String png, int width, int height) {
+    private void Terrain2Png(LayerDto layer, String tif, String png, int width, int height) {
         Dataset ds = null;
         try {
             ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
@@ -193,7 +194,7 @@
         }
     }
 
-    private static BufferedImage createImage(int width, int height) {
+    private BufferedImage createImage(int width, int height) {
         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
         Graphics2D graphic = image.createGraphics();
         Color transparent = new Color(0, 0, 0, 0);
@@ -204,7 +205,7 @@
         return image;
     }
 
-    private static void savePng(BufferedImage image, String png) {
+    private void savePng(BufferedImage image, String png) {
         try {
             ImageIO.write(image, "png", new File(png));
         } catch (Exception ex) {
@@ -259,7 +260,7 @@
         return processBuilding(dto, files, layer);
     }
 
-    private static List<String> getFiles(String path, String suffix) {
+    private List<String> getFiles(String path, String suffix) {
         List<String> files = new ArrayList<>();
         getFiles(files, new File(path), suffix);
         files.sort((a, b) -> a.compareToIgnoreCase(b));
@@ -267,7 +268,7 @@
         return files;
     }
 
-    private static void getFiles(List<String> files, File file, String suffix) {
+    private void getFiles(List<String> files, File file, String suffix) {
         if (!file.exists()) return;
 
         if (file.isDirectory()) {
@@ -288,7 +289,7 @@
         }
     }
 
-    private static void setWaterData(LayerDto layer, List<String> files) {
+    private void setWaterData(LayerDto layer, List<String> files) {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(new Date());
         calendar.set(Calendar.MILLISECOND, 0);
@@ -315,7 +316,7 @@
         layer.getDuration().setEnd(layer.getWaters().getData().get(layer.getWaters().getData().size() - 1));
     }
 
-    private static void setWaterHeight(LayerDto layer, List<String> files) {
+    private void setWaterHeight(LayerDto layer, List<String> files) {
         files.parallelStream().forEach(file -> {
             Dataset ds = null;
             try {
@@ -336,7 +337,7 @@
         layer.getWaters().setMaxHeight(getMaxVal(layer.getWaters().getMaxHeight() + 1, 1000));
     }
 
-    private static void processWaters(ResultDto dto, List<String> files, LayerDto layer) {
+    private void processWaters(ResultDto dto, List<String> files, LayerDto layer) {
         for (int i = 0, c = files.size(); i < c; i++) {
             Dataset ds = null;
             try {
@@ -346,14 +347,14 @@
 
                 createWaterPng(dto, ds, layer, layer.getWaters().getData().get(i));
                 copyWaterTif(dto, ds, layer.getWaters().getData().get(i));
-                createVectorShp(dto, ds, layer, layer.getWaters().getData().get(i));
+                createVectors(dto, ds, layer, layer.getWaters().getData().get(i));
             } finally {
                 if (null != ds) ds.delete();
             }
         }
     }
 
-    private static void createWaterPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
+    private void createWaterPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
         String waterPath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks;
         File dir = new File(waterPath);
         if (!dir.exists() || !dir.isDirectory()) dir.mkdirs();
@@ -369,11 +370,11 @@
         }
     }
 
-    private static String getNameWithExt(String file) {
+    private String getNameWithExt(String file) {
         return file.substring(file.lastIndexOf(File.separator) + 1, file.lastIndexOf("."));
     }
 
-    private static void water2Png(ResultDto dto, LayerDto layer, String tif, String png, int width, int height) {
+    private 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);
@@ -416,7 +417,7 @@
         }
     }
 
-    private static BuildingDto intersects(ResultDto dto, double x, double y) {
+    private BuildingDto intersects(ResultDto dto, double x, double y) {
         Geometry p = new Geometry(ogr.wkbPoint);
         p.AddPoint_2D(x, y);
         p.AssignSpatialReference(dto.getBuildingList().get(0).getGeom().GetSpatialReference());
@@ -477,19 +478,90 @@
         return isValid(val) ? val : null;
     }
 
-    public static boolean isValid(Double val) {
+    public boolean isValid(Double val) {
         return !Double.isNaN(val) && val > Integer.MIN_VALUE;
     }
 
-    private static void copyWaterTif(ResultDto dto, Dataset ds, long ticks) {
+    private void copyWaterTif(ResultDto dto, Dataset ds, long ticks) {
         String source = ds.GetDescription();
         String target = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.tif";
         FileUtil.copyFile(source, target);
     }
 
-    private static void createVectorShp(ResultDto dto, Dataset ds, LayerDto layer, Long ticks) {
-        String filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.geojson";
+    private 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);
+        if (CollectionUtils.isEmpty(geometries)) return;
+
+        List<PondingPo> list = copePonding(dto, ds, layer, geometries);
+    }
+
+    private List<Geometry> getGeometries(String filePath) {
+        if (!FileUtil.exist(filePath)) return null;
+
+        Driver driver = null;
+        DataSource dataSource = null;
+        org.gdal.ogr.Layer layer = null;
+        try {
+            driver = ogr.GetDriverByName("GeoJSON");
+            if (null == driver) return null;
+
+            DataSource ds = driver.Open(filePath);
+            if (null == ds) return null;
+
+            layer = ds.GetLayer(0);
+            List<Geometry> list = new ArrayList<>();
+            for (long i = 0, d = layer.GetFeatureCount(); i < d; i++) {
+                Feature f = layer.GetFeature(i);
+                if (f.GetFieldAsDouble("val") < -999) continue;
+
+                list.add(f.GetGeometryRef());
+            }
+
+            return list;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return null;
+        } finally {
+            GdalHelper.delete(layer, dataSource, driver);
+        }
+    }
+
+    private List<PondingPo> copePonding(ResultDto dto, Dataset ds, LayerDto layer, List<Geometry> geometries) {
+        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]);
+
+        List<PondingPo> list = new ArrayList<>();
+        for (Geometry geometry : geometries) {
+            double[] values = getValues(ds, geometry, xSize, ySize, minX, maxY, pixelWidth, pixelHeight);
+        }
+
+        return list;
+    }
+
+    private double[] getValues(Dataset ds, Geometry geometry, int xSize, int ySize, double minX, double maxY, double pixelWidth, double pixelHeight) {
+        double[] env = new double[4];
+        geometry.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);
+
+        return pixelValues;
     }
 
     private void copeFlow(ResultDto dto, LayerDto layer) {
@@ -510,7 +582,7 @@
         }
     }
 
-    private static void createFlowPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
+    private void createFlowPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
         String flowPath = dto.getOutPath() + File.separator + "flows" + File.separator + ticks;
         File dir = new File(flowPath);
         if (!dir.exists() || !dir.isDirectory()) dir.mkdirs();
@@ -526,7 +598,7 @@
         }
     }
 
-    private static void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) {
+    private void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) {
         Dataset ds = null;
         try {
             ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
@@ -542,7 +614,7 @@
         }
     }
 
-    private static void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
+    private void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
         for (int x = 0; x < width; x++) {
             for (int y = 0; y < height; y++) {
@@ -566,11 +638,11 @@
         savePng(image, png);
     }
 
-    private static float getFloatValue(float val) {
+    private float getFloatValue(float val) {
         return (Float.isNaN(val) || val < -999) ? Float.NaN : val;
     }
 
-    private static int getSafeValue(int val) {
+    private int getSafeValue(int val) {
         if (val < 0) return 0;
         if (val > 255) return 255;
 

--
Gitblit v1.9.3