From a10e925cdda6f92a7bbf86188e317f5c5ee839b5 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 26 七月 2024 14:21:20 +0800
Subject: [PATCH] 水面数据在计算时添加建筑物避让功能

---
 SimuTools/Tools/Handle.cs |  131 +++++++++++++++++++++++++------------------
 1 files changed, 75 insertions(+), 56 deletions(-)

diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index 0b3938d..96b9021 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -32,39 +32,37 @@
         /// <summary>
         /// 杩愯
         /// </summary>
-        public static void Run(string terrainFile, string waterPath, string flowPath, string outPath)
+        public static void Run(Args args)
         {
             Domain.Layer layer = new Domain.Layer();
             layer.duration = new Duration();
             layer.terrain = new Terrain();
             layer.waters = new Water();
 
-            string temp = Path.Combine(outPath, "temp");
-            if (!Directory.Exists(temp)) Directory.CreateDirectory(temp);
+            CopeTerrain(args, layer);
+            CopeBuilding(args, layer);
+            CopeWater(args, layer);
+            CopeFlow(args, layer);
+            CopeLayerJson(args, layer);
 
-            CopeTerrain(terrainFile, waterPath, outPath, layer);
-            CopeWater(waterPath, outPath, layer);
-            CopeFlow(flowPath, outPath, layer);
-            CopeLayerJson(outPath, layer);
-
-            if (Directory.Exists(temp)) Directory.Delete(temp, true);
+            if (Directory.Exists(args.temp)) Directory.Delete(args.temp, true);
         }
 
         #region 鍦板舰
         /// <summary>
         /// 澶勭悊鍦板舰
         /// </summary>
-        private static void CopeTerrain(string terrainFile, string waterPath, string outPath, Domain.Layer layer)
+        private static void CopeTerrain(Args args, Domain.Layer layer)
         {
             Dataset ds = null;
             try
             {
-                ds = Gdal.Open(terrainFile, Access.GA_ReadOnly);
+                ds = Gdal.Open(args.terrainFile, Access.GA_ReadOnly);
                 if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
 
                 SetTerrainInfo(ds, layer);
-                SetWaterInfo(waterPath, layer);
-                CreateTerrainPng(ds, layer, outPath);
+                SetWaterInfo(args, layer);
+                CreateTerrainPng(args, ds, layer);
             }
             finally
             {
@@ -113,16 +111,14 @@
         /// <summary>
         /// 鍒涘缓鍦板舰PNG
         /// </summary>
-        private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath)
+        private static void CreateTerrainPng(Args args, Dataset ds, Domain.Layer layer)
         {
-            string tempPath = Path.Combine(outPath, "temp");
-            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
-            string terrainPath = Path.Combine(outPath, "terrain");
+            string terrainPath = Path.Combine(args.outPath, "terrain");
             if (!Directory.Exists(terrainPath)) Directory.CreateDirectory(terrainPath);
 
             foreach (int[] sizes in layer.terrain.size)
             {
-                string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif");
+                string tif = Path.Combine(args.temp, "terrain_" + sizes[0] + "_" + sizes[1] + ".tif");
                 //Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
                 Resample(ds, tif, sizes[0], sizes[1], layer);
                 if (!File.Exists(tif)) continue;
@@ -166,7 +162,7 @@
                 ds = Gdal.Open(tif, Access.GA_ReadOnly);
                 if (null == ds || 0 == ds.RasterCount) return;
 
-                OSGeo.GDAL.Band band = ds.GetRasterBand(1);
+                Band band = ds.GetRasterBand(1);
                 float[] buffer = new float[width * height];
                 band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
 
@@ -175,7 +171,6 @@
                 graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹�
 
                 //double perHeight = (layer.terrain.maxHeight - layer.terrain.minHeight) * 100 / 65536;
-                //double differ = layer.terrain.maxHeight - layer.terrain.minHeight;
                 double differ = layer.extension.maxHeight - layer.extension.minHeight, minHeight = layer.extension.minHeight;
                 for (int x = 0; x < width; x++)
                 {
@@ -186,7 +181,6 @@
 
                         //int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) * 100 / perHeight);
                         //int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) / differ * 255); // 65535
-
                         int r = 0, g, b;
                         if (buffer[offset] - layer.extension.maxHeight > 0)
                         {
@@ -213,13 +207,48 @@
         }
         #endregion
 
+        #region 寤虹瓚
+        /// <summary>
+        /// 澶勭悊寤虹瓚
+        /// </summary>
+        private static void CopeBuilding(Args args, Domain.Layer layer)
+        {
+            Dataset ds = null;
+            try
+            {
+                ds = Gdal.Open(args.buildingFile, Access.GA_ReadOnly);
+                if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
+
+                foreach (int[] sizes in layer.terrain.size)
+                {
+                    string tif = Path.Combine(args.temp, "building_" + sizes[0] + "_" + sizes[1] + ".tif");
+                    Resample(ds, tif, sizes[0], sizes[1], layer);
+                    if (!File.Exists(tif)) continue;
+
+                    Dataset dataset = Gdal.Open(tif, Access.GA_ReadOnly);
+                    if (null == dataset || 0 == dataset.RasterCount) return;
+
+                    float[] buffer = new float[sizes[0] * sizes[1]];
+                    dataset.GetRasterBand(1).ReadRaster(0, 0, sizes[0], sizes[1], buffer, sizes[0], sizes[1], 0, 0);
+                    args.buildings[sizes[0] + "_" + sizes[1]] = buffer;
+
+                    dataset.Dispose();
+                }
+            }
+            finally
+            {
+                if (null != ds) ds.Dispose();
+            }
+        }
+        #endregion
+
         #region 姘撮潰
         /// <summary>
         /// 璁剧疆姘撮潰淇℃伅
         /// </summary>
-        private static void SetWaterInfo(string waterPath, Domain.Layer layer)
+        private static void SetWaterInfo(Args args, Domain.Layer layer)
         {
-            List<string> files = layer.waters.files = GetFiles(waterPath);
+            List<string> files = layer.waters.files = GetFiles(args.waterPath);
             if (null == files || files.Count == 0) return;
 
             SetWaterData(layer, files);
@@ -229,12 +258,12 @@
         /// <summary>
         /// 澶勭悊姘撮潰
         /// </summary>
-        private static void CopeWater(string waterPath, string outPath, Domain.Layer layer)
+        private static void CopeWater(Args args, Domain.Layer layer)
         {
             List<String> files = layer.waters.files;
             if (files.Count == 0 || files.Count != layer.waters.data.Count) return;
 
-            ProcessWaters(files, outPath, layer);
+            ProcessWaters(args, files, layer);
         }
 
         /// <summary>
@@ -302,7 +331,7 @@
         /// <summary>
         /// 澶勭悊姘撮潰鏁版嵁
         /// </summary>
-        private static void ProcessWaters(List<string> files, string outPath, Domain.Layer layer)
+        private static void ProcessWaters(Args args, List<string> files, Domain.Layer layer)
         {
             Parallel.For(0, files.Count, i =>
             //for (int i = 0; i < files.Count; i++)
@@ -313,7 +342,7 @@
                     ds = Gdal.Open(files[i], Access.GA_ReadOnly);
                     if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
 
-                    CreateWaterPng(ds, layer, outPath, layer.waters.data[i]);
+                    CreateWaterPng(args, ds, layer, layer.waters.data[i]);
                 }
                 finally
                 {
@@ -325,29 +354,27 @@
         /// <summary>
         /// 鍒涘缓姘撮潰PNG
         /// </summary>
-        private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks)
+        private static void CreateWaterPng(Args args, Dataset ds, Domain.Layer layer, long ticks)
         {
-            string tempPath = Path.Combine(outPath, "temp");
-            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
-            string waterPath = Path.Combine(outPath, "waters", ticks.ToString());
+            string waterPath = Path.Combine(args.outPath, "waters", ticks.ToString());
             if (!Directory.Exists(waterPath)) Directory.CreateDirectory(waterPath);
 
             foreach (int[] sizes in layer.terrain.size)
             {
                 string fileName = Path.GetFileNameWithoutExtension(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
-                string tif = Path.Combine(tempPath, fileName + ".tif");
+                string tif = Path.Combine(args.temp, fileName + ".tif");
                 Resample(ds, tif, sizes[0], sizes[1], layer);
                 if (!File.Exists(tif)) continue;
 
                 string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png");
-                Water2Png(layer, tif, png, sizes[0], sizes[1]);
+                Water2Png(args, layer, tif, png, sizes[0], sizes[1]);
             }
         }
 
         /// <summary>
         /// 姘撮潰杞琍NG
         /// </summary>
-        private static void Water2Png(Domain.Layer layer, string tif, string png, int width, int height)
+        private static void Water2Png(Args args, Domain.Layer layer, string tif, string png, int width, int height)
         {
             Dataset ds = null;
             try
@@ -358,6 +385,7 @@
                 Band band = ds.GetRasterBand(1);
                 float[] buffer = new float[width * height];
                 band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
+                float[] building = args.buildings[width + "_" + height];
 
                 Bitmap image = new Bitmap(width, height);
                 Graphics graphic = Graphics.FromImage(image);
@@ -371,13 +399,9 @@
                     {
                         int offset = x + y * width;
                         if (float.IsNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
+                        if (!float.IsNaN(building[offset]) && building[offset] > -999) continue;
 
-                        //int val = Convert.ToInt32(buffer[offset] * 100);
                         //int val = Convert.ToInt32((buffer[offset] - layer.extension.minHeight + WaterHeightOffset) * 100 / perHeight);
-                        //int r = val / 65536;
-                        //int g = (val - r * 65536) / 256;
-                        //int b = val % 256;
-
                         int r = 0, g, b;
                         if (buffer[offset] - layer.extension.maxHeight > 0)
                         {
@@ -408,10 +432,10 @@
         /// <summary>
         /// 澶勭悊娴侀�熸祦鍚�
         /// </summary>
-        private static void CopeFlow(string flowPath, string outPath, Domain.Layer layer)
+        private static void CopeFlow(Args args, Domain.Layer layer)
         {
-            List<string> vxFiles = GetFiles(flowPath, "vx");
-            List<string> vyFiles = GetFiles(flowPath, "vy");
+            List<string> vxFiles = GetFiles(args.flowPath, "vx");
+            List<string> vyFiles = GetFiles(args.flowPath, "vy");
             if (null == vxFiles || null == vyFiles || vxFiles.Count != vyFiles.Count || vxFiles.Count != layer.waters.data.Count) return;
 
             Parallel.For(0, vxFiles.Count, i =>
@@ -425,7 +449,7 @@
                     if (null == vxDs || 0 == vxDs.RasterCount || null == vxDs.GetSpatialRef() ||
                         null == vyDs || 0 == vyDs.RasterCount || null == vyDs.GetSpatialRef()) return;
 
-                    CreateFlowPng(vxDs, vyDs, layer, outPath, layer.waters.data[i]);
+                    CreateFlowPng(args, vxDs, vyDs, layer, layer.waters.data[i]);
                 }
                 finally
                 {
@@ -438,21 +462,19 @@
         /// <summary>
         /// 鍒涘缓娴侀�熸祦鍚慞NG
         /// </summary>
-        private static void CreateFlowPng(Dataset vxDs, Dataset vyDs, Domain.Layer layer, string outPath, long ticks)
+        private static void CreateFlowPng(Args args, Dataset vxDs, Dataset vyDs, Domain.Layer layer, long ticks)
         {
-            string tempPath = Path.Combine(outPath, "temp");
-            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
-            string flowPath = Path.Combine(outPath, "flows", ticks.ToString());
+            string flowPath = Path.Combine(args.outPath, "flows", ticks.ToString());
             if (!Directory.Exists(flowPath)) Directory.CreateDirectory(flowPath);
 
             foreach (int[] sizes in layer.terrain.size)
             {
                 string vxName = Path.GetFileNameWithoutExtension(vxDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
-                string vxTif = Path.Combine(tempPath, vxName + ".tif");
+                string vxTif = Path.Combine(args.temp, vxName + ".tif");
                 Resample(vxDs, vxTif, sizes[0], sizes[1], layer);
 
                 string vyName = Path.GetFileNameWithoutExtension(vyDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
-                string vyTif = Path.Combine(tempPath, vyName + ".tif");
+                string vyTif = Path.Combine(args.temp, vyName + ".tif");
                 Resample(vyDs, vyTif, sizes[0], sizes[1], layer);
 
                 if (!File.Exists(vxTif) || !File.Exists(vyTif)) continue;
@@ -547,13 +569,13 @@
         /// <summary>
         /// 澶勭悊鍏冩暟鎹�
         /// </summary>
-        private static void CopeLayerJson(string outPath, Domain.Layer layer)
+        private static void CopeLayerJson(Args args, Domain.Layer layer)
         {
             if (null == layer) return;
 
             String json = JsonConvert.SerializeObject(layer);
 
-            string filePath = Path.Combine(outPath, "layer.json");
+            string filePath = Path.Combine(args.outPath, "layer.json");
             using (StreamWriter sw = new StreamWriter(filePath))
             {
                 sw.Write(json);
@@ -565,12 +587,9 @@
         /// <summary>
         /// 璁剧疆鍦板舰淇℃伅
         /// </summary>
-        private static void SetTerrainInfo(Dataset ds, string outPath, Domain.Layer layer)
+        private static void SetTerrainInfo(Args args, Dataset ds, Domain.Layer layer)
         {
-            string tempPath = Path.Combine(outPath, "temp");
-            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
-
-            string tif = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(ds.GetDescription()) + ".tif");
+            string tif = Path.Combine(args.temp, Path.GetFileNameWithoutExtension(ds.GetDescription()) + ".tif");
             Resample(ds, tif);
             if (!File.Exists(tif)) throw new Exception("楂樼▼鏂囦欢閲嶆姇褰卞け璐ワ紒");
 

--
Gitblit v1.9.3