From dd1fcc5e6e1fc7dcb2ae4efe72a035c04d9d63da Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 19 七月 2024 20:57:40 +0800
Subject: [PATCH] 1

---
 SimuTools/Tools/Handle.cs |  120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 112 insertions(+), 8 deletions(-)

diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index 24b36b6..7ada463 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -38,11 +38,12 @@
             CopeTerrain(terrainFile, outPath, layer);
             CopeWater(waterPath, outPath, layer);
             CopeFlow(flowPath, outPath, layer);
-            CopeLayerJson(outPath, layer);
+            //CopeLayerJson(outPath, layer);
 
             if (Directory.Exists(temp)) Directory.Delete(temp, true);
         }
 
+        #region 鍦板舰
         /// <summary>
         /// 澶勭悊鍦板舰
         /// </summary>
@@ -79,7 +80,7 @@
         }
 
         /// <summary>
-        /// 鍒涘缓鍦板舰鍥惧眰
+        /// 鍒涘缓鍦板舰PNG
         /// </summary>
         private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath)
         {
@@ -98,7 +99,7 @@
                 }
 
                 string png = Path.Combine(terrainPath, sizes[0] + "_" + sizes[1] + ".png");
-                Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
+                Tif2Png(layer, tif, png, sizes[0], sizes[1]);
             }
         }
 
@@ -156,7 +157,7 @@
         /// <summary>
         /// Tif鍊艰浆瀛樿嚦Png
         /// </summary>
-        private static void Terrain2Png(Domain.Layer layer, string tif, string png, int width, int height)
+        private static void Tif2Png(Domain.Layer layer, string tif, string png, int width, int height)
         {
             Dataset ds = null;
             try
@@ -205,7 +206,9 @@
                 if (File.Exists(tif)) File.Delete(tif);
             }
         }
+        #endregion
 
+        #region 姘撮潰
         /// <summary>
         /// 澶勭悊姘撮潰
         /// </summary>
@@ -215,6 +218,7 @@
             if (null == files || files.Count == 0) return;
 
             SetWaterData(layer, files);
+            if (files.Count != layer.waters.data.Count) return;
 
             /*for (int i = 0, c = files.Count; i < c; i++)
             {
@@ -249,7 +253,7 @@
         }
 
         /// <summary>
-        /// 鍒涘缓鍦板舰鍥惧眰
+        /// 鍒涘缓姘撮潰PNG
         /// </summary>
         private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks)
         {
@@ -269,16 +273,16 @@
                 }
 
                 string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png");
-                Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
+                Tif2Png(layer, tif, png, sizes[0], sizes[1]);
             }
         }
 
         /// <summary>
         /// 鑾峰彇鏂囦欢
         /// </summary>
-        private static List<String> GetFiles(string path)
+        private static List<String> GetFiles(string path, string prefix = "")
         {
-            string[] files = Directory.GetFiles(path, "*.tif?", SearchOption.TopDirectoryOnly);
+            string[] files = Directory.GetFiles(path, prefix + "*.tif?", SearchOption.TopDirectoryOnly);
             if (null == files || files.Length == 0) return null;
 
             List<string> list = files.ToList();
@@ -306,15 +310,114 @@
                 layer.waters.data.Add((d.Ticks - startTicks) / 10000);
             }
         }
+        #endregion
 
+        #region 娴侀�熸祦鍚�
         /// <summary>
         /// 澶勭悊娴侀�熸祦鍚�
         /// </summary>
         private static void CopeFlow(string flowPath, string outPath, Domain.Layer layer)
         {
+            List<string> vxFiles = GetFiles(flowPath, "vx");
+            List<string> vyFiles = GetFiles(flowPath, "vy");
+            if (null == vxFiles || null == vyFiles || vxFiles.Count != vyFiles.Count || vxFiles.Count != layer.waters.data.Count) return;
 
+            Parallel.For(0, vxFiles.Count, i =>
+            {
+                Dataset vxDs = null, vyDs = null;
+                try
+                {
+                    vxDs = Gdal.Open(vxFiles[i], Access.GA_ReadOnly);
+                    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]);
+                }
+                finally
+                {
+                    if (null != vxDs) vxDs.Dispose();
+                    if (null != vyDs) vyDs.Dispose();
+                }
+            });
         }
 
+        /// <summary>
+        /// 鍒涘缓娴侀�熸祦鍚慞NG
+        /// </summary>
+        private static void CreateFlowPng(Dataset vxDs, Dataset vyDs, Domain.Layer layer, string outPath, long ticks)
+        {
+            string tempPath = Path.Combine(outPath, "temp");
+            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
+            string flowPath = Path.Combine(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");
+                Resample(vxDs.GetDescription(), vxTif, sizes[0], sizes[1]);
+
+                string vyName = Path.GetFileNameWithoutExtension(vyDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
+                string vyTif = Path.Combine(tempPath, vyName + ".tif");
+                Resample(vyDs.GetDescription(), vyTif, sizes[0], sizes[1]);
+
+                if (!File.Exists(vxTif) || !File.Exists(vyTif)) continue;
+
+                string png = Path.Combine(flowPath, sizes[0] + "_" + sizes[1] + ".png");
+                VxyTif2Png(layer, vxTif, vyTif, png, sizes[0], sizes[1]);
+            }
+        }
+
+        /// <summary>
+        /// 娴侀�熸祦鍚慣if鍊艰浆瀛樿嚦Png
+        /// </summary>
+        private static void VxyTif2Png(Domain.Layer layer, string vxTif, string vyTif, string png, int width, int height)
+        {
+            Dataset vxDs = null, vyDs = null;
+            try
+            {
+                vxDs = Gdal.Open(vxTif, Access.GA_ReadOnly);
+                vyDs = Gdal.Open(vyTif, Access.GA_ReadOnly);
+                if (null == vxDs || 0 == vxDs.RasterCount || null == vyDs || 0 == vyDs.RasterCount) return;
+
+                float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height];
+                vxDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer, width, height, 0, 0);
+                vyDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vyBuffer, width, height, 0, 0);
+
+                Bitmap image = new Bitmap(width, height);
+                Graphics graphic = Graphics.FromImage(image);
+                graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹�
+
+                for (int x = 0; x < width; x++)
+                {
+                    for (int y = 0; y < height; y++)
+                    {
+                        //int offset = x + y * height;
+                        //if (float.IsNaN(vxBuffer[offset]) || vxBuffer[offset] == -9999) continue;
+
+                        //int val = Convert.ToInt32(vxBuffer[offset] * 100);
+                        //int r = val / 65536;
+                        //int g = (val - r * 65536) / 256;
+                        //int b = val % 256;
+
+                        //Color color = Color.FromArgb(127, r, g, b);
+                        //image.SetPixel(x, y, color);
+                    }
+                }
+
+                image.Save(png, System.Drawing.Imaging.ImageFormat.Png);
+            }
+            finally
+            {
+                if (null != vxDs) vxDs.Dispose();
+                if (null != vyDs) vyDs.Dispose();
+                if (File.Exists(vxTif)) File.Delete(vxTif);
+                if (File.Exists(vyTif)) File.Delete(vyTif);
+            }
+        }
+        #endregion
+
+        #region 鍏冩暟鎹�
         /// <summary>
         /// 澶勭悊鍏冩暟鎹�
         /// </summary>
@@ -330,6 +433,7 @@
                 sw.Write(json);
             }
         }
+        #endregion
 
         #region 鏆傛椂涓嶇敤
         /// <summary>

--
Gitblit v1.9.3