From 8e96c01eb724a292d8c7f8e1c74dbf4384e56188 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 18 七月 2024 16:50:58 +0800
Subject: [PATCH] 修改GDAL帮助类

---
 SimuTools/Tools/GdalHelper.cs |   99 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 99 insertions(+), 0 deletions(-)

diff --git a/SimuTools/Tools/GdalHelper.cs b/SimuTools/Tools/GdalHelper.cs
index 96a3f4c..20b8b4e 100644
--- a/SimuTools/Tools/GdalHelper.cs
+++ b/SimuTools/Tools/GdalHelper.cs
@@ -22,6 +22,8 @@
 
         public static SpatialReference sr4490 { set; get; }
 
+        public static readonly String CGCS2000 = "CGCS2000";
+
         /// <summary>
         /// 鏋勯�犲嚱鏁�
         /// </summary>
@@ -80,5 +82,102 @@
             Ogr.RegisterAll();
             Gdal.AllRegister();
         }
+
+        /**
+         * 鑾峰彇Dataset鐨勬渶灏忕偣
+         */
+        private static Geometry GetMinPoint(Dataset ds)
+        {
+            double[] transform = new double[6];
+            ds.GetGeoTransform(transform);
+
+            double xMin = transform[0];
+            double yMin = transform[3] - ds.RasterYSize * transform[1];
+
+            Geometry point = new Geometry(wkbGeometryType.wkbPoint);
+            point.AddPoint(xMin, yMin, 0);
+
+            return Transform(ds, point);
+        }
+
+        /**
+         * 鑾峰彇Dataset鐨勬渶澶х偣
+         */
+        private static Geometry GetMaxPoint(Dataset ds)
+        {
+            /*
+             * transform[0] 宸︿笂瑙抶鍧愭爣
+             * transform[1] 涓滆タ鏂瑰悜鍒嗚鲸鐜�
+             * transform[2] 鏃嬭浆瑙掑害, 0琛ㄧず鍥惧儚 "鍖楁柟鏈濅笂"
+             *
+             * transform[3] 宸︿笂瑙抷鍧愭爣
+             * transform[4] 鏃嬭浆瑙掑害, 0琛ㄧず鍥惧儚 "鍖楁柟鏈濅笂"
+             * transform[5] 鍗楀寳鏂瑰悜鍒嗚鲸鐜�
+             */
+            double[] transform = new double[6];
+            ds.GetGeoTransform(transform);
+
+            double xMax = transform[0] + (ds.RasterYSize * transform[1]);
+            double yMax = transform[3];
+
+            Geometry point = new Geometry(wkbGeometryType.wkbPoint);
+            point.AddPoint(xMax, yMax, 0);
+
+            return Transform(ds, point);
+        }
+
+        /**
+         * 鍧愭爣杞崲
+         */
+        public static Geometry Transform(Dataset ds, Geometry point)
+        {
+            point.AssignSpatialReference(ds.GetSpatialRef());
+            if (ds.GetSpatialRef().IsGeographic() > 0)
+            {
+                return point;
+            }
+
+            String srsName = ds.GetSpatialRef().GetName();
+            if (srsName.Contains(CGCS2000))
+            {
+                point.TransformTo(sr4490);
+            }
+            else
+            {
+                point.TransformTo(sr4326);
+            }
+            point.SwapXY();
+
+            return point;
+        }
+
+        /**
+         * 鍒涘缓閲戝瓧濉�
+         */
+        public static void CreatePyramid(String file)
+        {
+            Dataset ds = null;
+            try
+            {
+                if (!File.Exists(file)) return;
+
+                ds = Gdal.Open(file, Access.GA_ReadOnly);
+                if (null == ds) return;
+
+                Band band = ds.GetRasterBand(1);
+                if (0 == band.GetOverviewCount())
+                {
+                    ds.BuildOverviews("nearest", new int[] { 2, 4, 6, 8, 16 });
+                }
+            }
+            catch (Exception ex)
+            {
+                LogOut.Error(ex.Message);
+            }
+            finally
+            {
+                if (null != ds) ds.Dispose();
+            }
+        }
     }
 }

--
Gitblit v1.9.3