From 402fdc4cb6f94016cf9749bb1f09a6c5ead5d6d3 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 19 七月 2024 12:43:30 +0800
Subject: [PATCH] 添加重采样功能

---
 SimuTools/Tools/Handle.cs |  107 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 106 insertions(+), 1 deletions(-)

diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index da4768d..555cb3c 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -4,6 +4,8 @@
 using SimuTools.Domain;
 using System;
 using System.Collections.Generic;
+using System.Configuration;
+using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Text;
@@ -13,6 +15,8 @@
 {
     public class Handle
     {
+        public static readonly string GdalPath = ConfigurationManager.AppSettings["gdalPath"];
+
         public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
 
         /// <summary>
@@ -25,10 +29,15 @@
             layer.terrain = new Terrain();
             layer.waters = new Water();
 
+            string temp = Path.Combine(outPath, "temp");
+            if (!Directory.Exists(temp)) Directory.CreateDirectory(temp);
+
             CopeTerrain(terrainFile, outPath, layer);
             CopeWater(waterPath, outPath, layer);
             CopeFlow(flowPath, outPath, layer);
             CopeLayerJson(outPath, layer);
+
+            //if (Directory.Exists(temp)) Directory.Delete(temp, true);
         }
 
         /// <summary>
@@ -60,7 +69,7 @@
             Geometry maxPoint = GdalHelper.GetMaxPoint(ds);
             layer.extension = new Extension(minPoint.GetX(0), minPoint.GetY(0), maxPoint.GetX(0), maxPoint.GetY(0));
 
-            Band band = ds.GetRasterBand(1);
+            OSGeo.GDAL.Band band = ds.GetRasterBand(1);
             double[] mm = new double[2];
             band.ComputeRasterMinMax(mm, 0);
             layer.extension.SetHeight(mm[0], mm[1]);
@@ -71,10 +80,81 @@
         /// </summary>
         private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath)
         {
+            string tempPath = Path.Combine(outPath, "temp");
+            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
+            string terrainPath = Path.Combine(outPath, "terrain");
+            if (!Directory.Exists(terrainPath)) Directory.CreateDirectory(terrainPath);
+
             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))
+                {
+                    continue;
+                }
+
+                //
             }
+        }
+
+        /// <summary>
+        /// 閲嶉噰鏍�
+        /// </summary>
+        private static void Resample(string source, string dest, int width, int height)
+        {
+            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);
+        }
+
+        /// <summary>
+        /// 鎵ц鍛戒护
+        /// </summary>
+        public static string ExecExe(string cmd)
+        {
+            string str = null;
+            Process p = null;
+            try
+            {
+                p = new Process();
+                p.StartInfo.FileName = "cmd.exe";
+                p.StartInfo.UseShellExecute = false;
+                p.StartInfo.CreateNoWindow = true;
+                p.StartInfo.RedirectStandardInput = true;
+                p.StartInfo.RedirectStandardOutput = true;
+                p.StartInfo.RedirectStandardError = true;
+                p.Start();
+
+                StreamWriter si = p.StandardInput;
+                StreamReader se = p.StandardError;
+
+                si.AutoFlush = true;
+                si.WriteLine(cmd);
+                si.WriteLine("exit");
+
+                str = se.ReadToEnd();
+                se.Close();
+                si.Close();
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
+                str = ex.Message;
+            }
+            finally
+            {
+                if (p != null) p.Close();
+            }
+            return str;
         }
 
         /// <summary>
@@ -108,5 +188,30 @@
                 sw.Write(json);
             }
         }
+
+        #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