From 1f22a86346c18ccb29a4a712c4acb05c013b33d4 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 22 七月 2024 10:50:57 +0800
Subject: [PATCH] 修改高程和水面的计算方法

---
 SimuTools/Tools/Handle.cs |   88 ++++++++++++++++++++++---------------------
 1 files changed, 45 insertions(+), 43 deletions(-)

diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index 7ada463..6882a03 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -37,8 +37,8 @@
 
             CopeTerrain(terrainFile, outPath, layer);
             CopeWater(waterPath, outPath, layer);
-            CopeFlow(flowPath, outPath, layer);
-            //CopeLayerJson(outPath, layer);
+            //CopeFlow(flowPath, outPath, layer);
+            CopeLayerJson(outPath, layer);
 
             if (Directory.Exists(temp)) Directory.Delete(temp, true);
         }
@@ -92,7 +92,8 @@
             foreach (int[] sizes in layer.terrain.size)
             {
                 string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif");
-                Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
+                //Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
+                Resample(ds, tif, sizes[0], sizes[1]);
                 if (!File.Exists(tif))
                 {
                     continue;
@@ -105,6 +106,32 @@
 
         /// <summary>
         /// 閲嶉噰鏍�
+        /// </summary>
+        private static void Resample(Dataset ds, string dest, int width, int height)
+        {
+            // https://blog.51cto.com/u_16099346/6691820
+            //string args = string.Format("-t_srs {0} -ts {1} {2} -r {3} -of {4}", "EPSG:4326", width, height, "bilinear", "GTiff");
+            //string[] options = Gdal.ParseCommandLine(args);
+            string[] options = new string[]
+            {
+                "-t_srs",
+                "EPSG:4326",
+                "-ts",
+                width.ToString(),
+                height.ToString(),
+                "-r",
+                "bilinear",
+                "-of",
+                "GTiff"
+            };
+            GDALWarpAppOptions warpAppOptions = new GDALWarpAppOptions(options);
+
+            Dataset destDs = Gdal.Warp(dest, new Dataset[] { ds }, warpAppOptions, null, null);
+            destDs.Dispose();
+        }
+
+        /// <summary>
+        /// 閲嶉噰鏍� *
         /// </summary>
         private static void Resample(string source, string dest, int width, int height)
         {
@@ -173,22 +200,16 @@
                 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);
-
+                double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536;
                 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 offset = x + y * width;
+                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) continue;
 
-                        int val = Convert.ToInt32(buffer[offset] * 100);
+                        //int val = Convert.ToInt32(buffer[offset] * 100);
+                        int val = Convert.ToInt32((buffer[offset] - layer.extension.minHeight) * 100 / perHeight);
                         int r = val / 65536;
                         int g = (val - r * 65536) / 256;
                         int b = val % 256;
@@ -197,7 +218,6 @@
                         image.SetPixel(x, y, color);
                     }
                 }
-
                 image.Save(png, System.Drawing.Imaging.ImageFormat.Png);
             }
             finally
@@ -388,20 +408,26 @@
                 Graphics graphic = Graphics.FromImage(image);
                 graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹�
 
+                // 鐢� R閫氶亾琛ㄧず锛屾祦鍚戜负褰掍竴鍖栫殑浜岀淮鍚戦噺锛坸,y锛夛紝G閫氶亾琛ㄧず涓� x *255 , B閫氶亾琛ㄧず涓� y * 255
                 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 offset = x + y * width;
+                        if (float.IsNaN(vxBuffer[offset]) || vxBuffer[offset] < -999 ||
+                            float.IsNaN(vyBuffer[offset]) || vyBuffer[offset] < -999) continue;
 
+                        float vx = vxBuffer[offset], vy = vyBuffer[offset];
                         //int val = Convert.ToInt32(vxBuffer[offset] * 100);
                         //int r = val / 65536;
                         //int g = (val - r * 65536) / 256;
                         //int b = val % 256;
+                        int r = 0;
+                        int g = 0;
+                        int b = 0;
 
-                        //Color color = Color.FromArgb(127, r, g, b);
-                        //image.SetPixel(x, y, color);
+                        Color color = Color.FromArgb(127, r, g, b);
+                        image.SetPixel(x, y, color);
                     }
                 }
 
@@ -432,30 +458,6 @@
             {
                 sw.Write(json);
             }
-        }
-        #endregion
-
-        #region 鏆傛椂涓嶇敤
-        /// <summary>
-        /// 閲嶉噰鏍� *
-        /// </summary>
-        private static void Resample(Dataset ds, string dest, int width, int height)
-        {
-            // 璁剧疆Warp鐨勯�夐」锛歨ttps://blog.csdn.net/qq_43210879/article/details/121350561
-            string[] options = new string[] {
-                "format=GTiff",
-                "width=" + width,
-                "height=" + height,
-                "dstSRS=EPSG:4326"
-            };
-
-            OSGeo.GDAL.Driver driver = Gdal.GetDriverByName("GTiff");
-            Dataset destDs = driver.Create(dest, width, height, ds.RasterCount, ds.GetRasterBand(1).DataType, null);
-
-            GDALWarpAppOptions warpAppOptions = new GDALWarpAppOptions(options);
-            Gdal.Warp(destDs, new Dataset[] { ds }, warpAppOptions, null, null);
-
-            destDs.Dispose();
         }
         #endregion
     }

--
Gitblit v1.9.3