From ee26ff7331614b14c9f156c5590724e29e99dc06 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期六, 02 十一月 2024 17:08:05 +0800
Subject: [PATCH] 1

---
 src/main/java/com/se/simu/service/ResultService.java |  137 ++++++++++++++++++++++++++++++++++++---------
 1 files changed, 109 insertions(+), 28 deletions(-)

diff --git a/src/main/java/com/se/simu/service/ResultService.java b/src/main/java/com/se/simu/service/ResultService.java
index f7796cb..8cb6be8 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.vo.BuildingDepthVo;
 import com.se.simu.helper.GdalHelper;
 import lombok.extern.slf4j.Slf4j;
 import org.gdal.gdal.Band;
@@ -17,6 +18,7 @@
 import org.gdal.gdalconst.gdalconst;
 import org.gdal.ogr.*;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
 import javax.annotation.Resource;
 import javax.imageio.ImageIO;
@@ -29,6 +31,8 @@
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.stream.DoubleStream;
 
 /**
  * 澶勭悊缁撴灉鏈嶅姟绫�
@@ -45,6 +49,8 @@
 
     public final static double MAX_X_OFFSET = 0;
 
+    private final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+
     public void process(DataPo data) throws Exception {
         String basePath = config.getInPath() + File.separator + data.getInPath() + File.separator;
         ResultDto dto = new ResultDto(
@@ -53,7 +59,8 @@
                 basePath + config.getBuildingFile(),
                 basePath + config.getWaterPath(),
                 basePath + config.getFlowPath(),
-                config.getOutPath());
+                config.getOutPath(),
+                data.getEpsg());
         LayerDto layer = new LayerDto(config.getVer(), data.getEpsg(), config.getSizes());
         process(dto, layer);
     }
@@ -62,10 +69,11 @@
         try {
             copeTerrain(dto, layer);
             copeBuilding(dto, layer);
-            copeWater(dto, layer);
+            List<BuildingDepthVo> buildings = copeWater(dto, layer);
             copeFlow(dto, layer);
             copeLayerJson(dto, layer);
             copeRainFallJson(dto, layer);
+            copeBuildingDepthJson(dto, buildings);
         } finally {
             File dir = new File(dto.getTemp());
             if (dir.exists()) {
@@ -259,11 +267,13 @@
         setWaterHeight(layer, files);
     }
 
-    private void copeWater(ResultDto dto, LayerDto layer) {
+    private List<BuildingDepthVo> copeWater(ResultDto dto, LayerDto layer) {
         List<String> files = layer.getWaters().getFiles();
-        if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return;
+        if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return null;
 
         processWaters(dto, files, layer);
+        
+        return processBuilding(dto, files, layer);
     }
 
     private static List<String> getFiles(String path, String suffix) {
@@ -346,8 +356,10 @@
             try {
                 ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
                 if (null == ds || 0 == ds.getRasterCount()) return;
+                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
 
                 createWaterPng(dto, ds, layer, layer.getWaters().getData().get(i));
+                copyWaterTif(dto, ds, layer.getWaters().getData().get(i));
             } finally {
                 if (null != ds) ds.delete();
             }
@@ -368,6 +380,12 @@
             String png = waterPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
             water2Png(dto, layer, tif, png, sizes[0], sizes[1]);
         }
+    }
+
+    private static 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 String getNameWithExt(String file) {
@@ -392,8 +410,8 @@
                     int offset = x + y * width;
                     if (Float.isNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
 
-                    double X = transform[0] + x * transform[1] + y * transform[2];
-                    double Y = transform[3] + x * transform[4] + y * transform[5];
+                    //double X = transform[0] + x * transform[1] + y * transform[2];
+                    //double Y = transform[3] + x * transform[4] + y * transform[5];
                     //BuildingDto building = intersects(dto, X, Y);
                     //if (null != building) continue;
 
@@ -428,6 +446,61 @@
         return dto.getBuildingList().parallelStream().filter(b -> b.getGeom().Intersects(p)).findFirst().orElse(null);
     }
 
+    private 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), gdalconst.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;
+    }
+
+    private 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, null));
+        }
+    }
+
+    private 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 xMinPixel = Math.max((int) Math.floor((env[0] - minX) / pixelWidth), 1);
+        int yMinPixel = Math.max((int) Math.floor((maxY - env[3]) / pixelHeight), 1);
+        int xMaxPixel = Math.min((int) Math.floor((env[1] - minX) / pixelWidth), xSize);
+        int yMaxPixel = Math.min((int) Math.floor((maxY - env[2]) / pixelHeight), ySize);
+        if (xMaxPixel < 1 || yMaxPixel < 1 || xMaxPixel - xMinPixel < 0 || yMaxPixel - yMinPixel < 0) {
+            return null;
+        }
+
+        int width = xMaxPixel - xMinPixel;
+        int height = yMaxPixel - yMinPixel;
+        double[] pixelValues = new double[width * height];
+        ds.GetRasterBand(1).ReadRaster(xMinPixel, yMinPixel, width, height, pixelValues);
+
+        Double val = Arrays.stream(pixelValues).max().getAsDouble();
+
+        return isValid(val) ? val : null;
+    }
+
+    public static boolean isValid(Double val) {
+        return !Double.isNaN(val) && val > Integer.MIN_VALUE;
+    }
+
     private void copeFlow(ResultDto dto, LayerDto layer) {
         List<String> files = getFiles(dto.getFlowPath(), ".tif");
         if (null == files || files.size() != layer.getWaters().getData().size()) return;
@@ -437,6 +510,7 @@
             try {
                 ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
                 if (null == ds || 0 == ds.getRasterCount()) return;
+                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
 
                 createFlowPng(dto, ds, layer, layer.getWaters().getData().get(i));
             } finally {
@@ -521,14 +595,10 @@
         layer.getTerrain().setEpsg(null);
 
         String json = JSON.toJSONString(layer);
-        //String json = JSONUtil.toJsonPrettyStr(layer);
+        // String json = JSONUtil.toJsonPrettyStr(layer);
         String filePath = dto.getOutPath() + File.separator + "layer.json";
 
-        FileWriter fw = new FileWriter(filePath);
-        BufferedWriter bw = new BufferedWriter(fw);
-        bw.write(json);
-        bw.close();
-        fw.close();
+        writeJson(filePath, json);
     }
 
     /**
@@ -538,28 +608,39 @@
         String rainGageFilePath = config.getInPath() + File.separator + dto.getServiceName() + File.separator + "RainGage.dat";
         String filePath = dto.getOutPath() + File.separator + "rainfall.json";
 
-        String line;
-        Map<String, Double> rainFallJsons = new LinkedHashMap<>();
-        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+        Map<String, Double> map = new LinkedHashMap<>();
+        FileReader fr = new FileReader(rainGageFilePath);
+        BufferedReader br = new BufferedReader(fr);
 
-        BufferedReader br = new BufferedReader(new FileReader(rainGageFilePath));
-        // 澶勭悊绗竴琛屾暟鎹�
-        if ((line = br.readLine()) != null) {
-            while ((line = br.readLine()) != null) {
-                // 澶勭悊姣忎竴琛屾暟鎹�
-                String[] rainFall = line.split(" ");
+        String line = br.readLine();
+        while ((line = br.readLine()) != null) {
+            String[] rainFall = line.split(" ");
+            if (rainFall.length < 7) continue;
 
-                if (rainFall.length < 7) continue;
-
-                String sdt = rainFall[1] + "-" + rainFall[2] + "-" + rainFall[3] + " " + rainFall[4] + ":" + rainFall[5];
-                BigDecimal num = new BigDecimal(rainFall[6]);
-                rainFallJsons.put("" + sdf.parse(sdt).getTime(), num.setScale(2, RoundingMode.HALF_UP).doubleValue());
-            }
+            String sdt = rainFall[1] + "-" + rainFall[2] + "-" + rainFall[3] + " " + rainFall[4] + ":" + rainFall[5];
+            BigDecimal num = new BigDecimal(rainFall[6]);
+            map.put("" + SDF.parse(sdt).getTime(), num.setScale(2, RoundingMode.HALF_UP).doubleValue());
         }
+        br.close();
+        fr.close();
 
+        String json = JSON.toJSONString(map);
+        writeJson(filePath, json);
+    }
+
+    private void copeBuildingDepthJson(ResultDto dto, List<BuildingDepthVo> list) throws IOException {
+        if (CollectionUtils.isEmpty(list)) return;
+
+        String json = JSON.toJSONString(list);
+        String filePath = dto.getOutPath() + File.separator + "building.json";
+
+        writeJson(filePath, json);
+    }
+
+    private void writeJson(String filePath, String json) throws IOException {
         FileWriter fw = new FileWriter(filePath);
         BufferedWriter bw = new BufferedWriter(fw);
-        bw.write(JSON.toJSONString(rainFallJsons));
+        bw.write(json);
         bw.close();
         fw.close();
     }

--
Gitblit v1.9.3