From d20fdf2fccbfdd4a5ac2e6e79523a9558b9a2b51 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 18 七月 2024 15:44:27 +0800
Subject: [PATCH] 初始化GDAL

---
 SimuTools/FrmMain.cs          |   16 +++++++-
 SimuTools/gdal_wrap.dll       |    0 
 SimuTools/gdalconst_wrap.dll  |    0 
 SimuTools/Tools/GdalHelper.cs |   84 ++++++++++++++++++++++++++++++++++++++++++
 SimuTools/SimuTools.csproj    |    9 ++++
 SimuTools/Tools/Handle.cs     |    2 +
 6 files changed, 109 insertions(+), 2 deletions(-)

diff --git a/SimuTools/FrmMain.cs b/SimuTools/FrmMain.cs
index 9fced93..c888011 100644
--- a/SimuTools/FrmMain.cs
+++ b/SimuTools/FrmMain.cs
@@ -17,6 +17,16 @@
         private void FrmMain_Load(object sender, EventArgs e)
         {
             LogOut.Info("鍚姩绋嬪簭...");
+            try
+            {
+                GdalHelper gdal = GdalHelper.Instance;
+                LogOut.Info("> 鍒濆鍖朑DAL瀹屾垚 <");
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.StackTrace);
+                ShowErr("GDAL鍒濆鍖栧嚭閿欙細" + ex.Message);
+            }
         }
 
         private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
@@ -97,9 +107,11 @@
             try
             {
                 flag = true;
-                LogOut.Info("寮�濮嬭繍琛� >>");
+                LogOut.Info(">> 寮�濮嬭繍琛� >>");
+
                 Tools.Handle.Run(terrainFile, waterPath, flowPath, outPath);
-                LogOut.Info("杩愯缁撴潫 >>");
+
+                LogOut.Info("<< 杩愯缁撴潫 <<");
                 flag = false;
             }
             catch (Exception ex)
diff --git a/SimuTools/SimuTools.csproj b/SimuTools/SimuTools.csproj
index 41ab085..2999351 100644
--- a/SimuTools/SimuTools.csproj
+++ b/SimuTools/SimuTools.csproj
@@ -45,6 +45,8 @@
       <SpecificVersion>False</SpecificVersion>
       <HintPath>dlls\log4net4.dll</HintPath>
     </Reference>
+    <Reference Include="ogr_csharp, Version=3.5.2.0, Culture=neutral, PublicKeyToken=db5a52b08dc5b321, processorArchitecture=MSIL" />
+    <Reference Include="osr_csharp, Version=3.5.2.0, Culture=neutral, PublicKeyToken=db5a52b08dc5b321, processorArchitecture=MSIL" />
     <Reference Include="System" />
     <Reference Include="System.Configuration" />
     <Reference Include="System.Core" />
@@ -71,6 +73,7 @@
     </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Tools\GdalHelper.cs" />
     <Compile Include="Tools\Handle.cs" />
     <Compile Include="Tools\LogOut.cs" />
     <EmbeddedResource Include="FrmMain.resx">
@@ -560,6 +563,12 @@
     <Content Include="dlls\ogr_wrap.dll" />
     <Content Include="dlls\osr_csharp.dll" />
     <Content Include="dlls\osr_wrap.dll" />
+    <EmbeddedResource Include="gdalconst_wrap.dll">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </EmbeddedResource>
+    <EmbeddedResource Include="gdal_wrap.dll">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </EmbeddedResource>
     <EmbeddedResource Include="gdal-data\bag_template.xml">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </EmbeddedResource>
diff --git a/SimuTools/Tools/GdalHelper.cs b/SimuTools/Tools/GdalHelper.cs
new file mode 100644
index 0000000..96a3f4c
--- /dev/null
+++ b/SimuTools/Tools/GdalHelper.cs
@@ -0,0 +1,84 @@
+锘縰sing OSGeo.GDAL;
+using OSGeo.OGR;
+using OSGeo.OSR;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SimuTools.Tools
+{
+    public class GdalHelper
+    {
+        private static bool isInited;
+
+        private static GdalHelper instance;
+
+        private static readonly object obj = new object();
+
+        public static SpatialReference sr4326 { set; get; }
+
+        public static SpatialReference sr4490 { set; get; }
+
+        /// <summary>
+        /// 鏋勯�犲嚱鏁�
+        /// </summary>
+        private GdalHelper()
+        {
+            lock (obj)
+            {
+                if (!isInited)
+                {
+                    RegisterGDal();
+
+                    sr4326 = new SpatialReference(null);
+                    sr4326.ImportFromEPSG(4326);
+
+                    sr4490 = new SpatialReference(null);
+                    sr4490.ImportFromEPSG(4490);
+
+                    isInited = true;
+                }
+            }
+        }
+
+        /// <summary>
+        /// 瀹炰緥
+        /// </summary>
+        public static GdalHelper Instance
+        {
+            get
+            {
+                lock (obj)
+                {
+                    if (null == instance)
+                    {
+                        instance = new GdalHelper();
+                    }
+
+                    return instance;
+                }
+            }
+        }
+
+        /// <summary>
+        /// 娉ㄥ唽GDAL
+        /// </summary>
+        public void RegisterGDal()
+        {
+            string gdalData = Path.Combine(Handle.BaseDir, "gdal-data");
+            Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
+
+            string proj7 = Path.Combine(Handle.BaseDir, "proj7\\share");
+            Environment.SetEnvironmentVariable("PROJ_LIB", proj7);
+
+            Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); // NO,YES
+            Gdal.SetConfigOption("SHAPE_ENCODING", ""); // 绌�,gb2312,CP936
+
+            Ogr.RegisterAll();
+            Gdal.AllRegister();
+        }
+    }
+}
diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index b29c6f4..c733210 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -9,6 +9,8 @@
 {
     public class Handle
     {
+        public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
+
         public static void Run(string terrainFile, string waterPath, string flowPath, string outPath)
         {
             //
diff --git a/SimuTools/gdal_wrap.dll b/SimuTools/gdal_wrap.dll
new file mode 100644
index 0000000..71c441b
--- /dev/null
+++ b/SimuTools/gdal_wrap.dll
Binary files differ
diff --git a/SimuTools/gdalconst_wrap.dll b/SimuTools/gdalconst_wrap.dll
new file mode 100644
index 0000000..9c8c331
--- /dev/null
+++ b/SimuTools/gdalconst_wrap.dll
Binary files differ

--
Gitblit v1.9.3