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

---
 SimuTools/Tools/Handle.cs |  165 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 153 insertions(+), 12 deletions(-)

diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index 555cb3c..24b36b6 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -6,6 +6,7 @@
 using System.Collections.Generic;
 using System.Configuration;
 using System.Diagnostics;
+using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Text;
@@ -15,6 +16,8 @@
 {
     public class Handle
     {
+        public static readonly int MAX = 256 * 256;
+
         public static readonly string GdalPath = ConfigurationManager.AppSettings["gdalPath"];
 
         public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
@@ -37,7 +40,7 @@
             CopeFlow(flowPath, outPath, layer);
             CopeLayerJson(outPath, layer);
 
-            //if (Directory.Exists(temp)) Directory.Delete(temp, true);
+            if (Directory.Exists(temp)) Directory.Delete(temp, true);
         }
 
         /// <summary>
@@ -87,15 +90,6 @@
 
             foreach (int[] sizes in layer.terrain.size)
             {
-                //string filePath = Path.Combine(outPath, sizes[0] + "_" + sizes[1] + ".png");
-
-                //// 濉厖鍐呭瓨鍥惧儚
-                //byte[] buffer = new byte[sizes[0] * sizes[1] * 3];
-                //for (int i = 0; i < buffer.Length; i++)
-                //{
-                //    buffer[i] = (byte)(i % 256);
-                //}
-
                 string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif");
                 Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
                 if (!File.Exists(tif))
@@ -103,7 +97,8 @@
                     continue;
                 }
 
-                //
+                string png = Path.Combine(terrainPath, sizes[0] + "_" + sizes[1] + ".png");
+                Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
             }
         }
 
@@ -112,6 +107,7 @@
         /// </summary>
         private static void Resample(string source, string dest, int width, int height)
         {
+            // https://blog.51cto.com/u_16099346/6691820
             string cmd = string.Format("{0}gdalwarp.exe -t_srs {1} -ts {2} {3} -r {4} -of GTiff \"{5}\" \"{6}\"", GdalPath, "EPSG:4326", width, height, "bilinear", source, dest);
             string err = ExecExe(cmd);
         }
@@ -158,11 +154,157 @@
         }
 
         /// <summary>
+        /// Tif鍊艰浆瀛樿嚦Png
+        /// </summary>
+        private static void Terrain2Png(Domain.Layer layer, string tif, string png, int width, int height)
+        {
+            Dataset ds = null;
+            try
+            {
+                ds = Gdal.Open(tif, Access.GA_ReadOnly);
+                if (null == ds || 0 == ds.RasterCount) return;
+
+                OSGeo.GDAL.Band band = ds.GetRasterBand(1);
+                float[] buffer = new float[width * height];
+                band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
+
+                Bitmap image = new Bitmap(width, height);
+                Graphics graphic = Graphics.FromImage(image);
+                graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹�
+
+                //double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536;
+                //int val = Convert.ToInt32(buffer[x * y]* 100); //Convert.ToInt32((buffer[x * y] - layer.extension.minHeight) * 100);
+                //int val = Convert.ToInt32((buffer[x * y] - layer.extension.minHeight) * 100 / perHeight);
+
+                for (int x = 0; x < width; x++)
+                {
+                    for (int y = 0; y < height; y++)
+                    {
+                        int offset = x + y * height;
+                        if (float.IsNaN(buffer[offset]) || buffer[offset] == -9999)
+                        {
+                            //image.SetPixel(x, y, Color.Transparent);
+                            continue;
+                        }
+
+                        int val = Convert.ToInt32(buffer[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 != ds) ds.Dispose();
+                if (File.Exists(tif)) File.Delete(tif);
+            }
+        }
+
+        /// <summary>
         /// 澶勭悊姘撮潰
         /// </summary>
         private static void CopeWater(string waterPath, string outPath, Domain.Layer layer)
         {
+            List<string> files = GetFiles(waterPath);
+            if (null == files || files.Count == 0) return;
 
+            SetWaterData(layer, files);
+
+            /*for (int i = 0, c = files.Count; i < c; i++)
+            {
+                Dataset ds = null;
+                try
+                {
+                    ds = Gdal.Open(files[i], Access.GA_ReadOnly);
+                    if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) continue;
+
+                    CreateWaterPng(ds, layer, outPath, layer.waters.data[i]);
+                }
+                finally
+                {
+                    if (null != ds) ds.Dispose();
+                }
+            }*/
+            Parallel.For(0, files.Count, i =>
+            {
+                Dataset ds = null;
+                try
+                {
+                    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]);
+                }
+                finally
+                {
+                    if (null != ds) ds.Dispose();
+                }
+            });
+        }
+
+        /// <summary>
+        /// 鍒涘缓鍦板舰鍥惧眰
+        /// </summary>
+        private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks)
+        {
+            string tempPath = Path.Combine(outPath, "temp");
+            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
+            string waterPath = Path.Combine(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");
+                Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
+                if (!File.Exists(tif))
+                {
+                    continue;
+                }
+
+                string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png");
+                Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
+            }
+        }
+
+        /// <summary>
+        /// 鑾峰彇鏂囦欢
+        /// </summary>
+        private static List<String> GetFiles(string path)
+        {
+            string[] files = Directory.GetFiles(path, "*.tif?", SearchOption.TopDirectoryOnly);
+            if (null == files || files.Length == 0) return null;
+
+            List<string> list = files.ToList();
+            list.Sort();
+
+            return list;
+        }
+
+        /// <summary>
+        /// 璁剧疆姘撮潰鏁版嵁
+        /// </summary>
+        private static void SetWaterData(Domain.Layer layer, List<string> files)
+        {
+            DateTime now = DateTime.Now;
+            long startTicks = new DateTime(1970, 1, 1, 8, 0, 0).Ticks;
+
+            foreach (string file in files)
+            {
+                string fileName = Path.GetFileNameWithoutExtension(file);
+                int hour = Convert.ToInt32(fileName.Substring(0, 2));
+                int minute = Convert.ToInt32(fileName.Substring(2, 2));
+                int second = Convert.ToInt32(fileName.Substring(4, 2));
+
+                DateTime d = new DateTime(now.Year, now.Month, now.Day, hour, minute, second);
+                layer.waters.data.Add((d.Ticks - startTicks) / 10000);
+            }
         }
 
         /// <summary>
@@ -211,7 +353,6 @@
 
             destDs.Dispose();
         }
-
         #endregion
     }
 }

--
Gitblit v1.9.3