From 663662d22683211608cee07e1c1e32761e992a2a Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 24 七月 2024 13:25:23 +0800
Subject: [PATCH] 1

---
 SimuTools/Tools/Handle.cs |  111 ++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 77 insertions(+), 34 deletions(-)

diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index 67a549e..0a9c3fd 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -40,10 +40,10 @@
 
             CopeTerrain(terrainFile, outPath, layer);
             CopeWater(waterPath, outPath, layer);
-            CopeFlow(flowPath, outPath, layer);
+            //CopeFlow(flowPath, outPath, layer);
             CopeLayerJson(outPath, layer);
 
-            if (Directory.Exists(temp)) Directory.Delete(temp, true);
+            //if (Directory.Exists(temp)) Directory.Delete(temp, true);
         }
 
         #region 鍦板舰
@@ -58,8 +58,8 @@
                 ds = Gdal.Open(terrainFile, Access.GA_ReadOnly);
                 if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
 
-                SetTerrainInfo(ds, layer);
-                //CreateTerrainPng(ds, layer, outPath);
+                SetTerrainInfo(ds, outPath, layer);
+                CreateTerrainPng(ds, layer, outPath);
             }
             finally
             {
@@ -70,10 +70,10 @@
         /// <summary>
         /// 璁剧疆鍦板舰淇℃伅
         /// </summary>
-        private static void SetTerrainInfo(Dataset ds, Domain.Layer layer)
+        private static void SetTerrainInfo(Dataset ds, string outPath, Domain.Layer layer)
         {
-            String srsName = ds.GetSpatialRef().GetName();
-            layer.terrain.epsg = !string.IsNullOrEmpty(srsName) && srsName.Contains(GdalHelper.CGCS2000) ? "EPSG:4490" : "EPSG:4326";
+            //String srsName = ds.GetSpatialRef().GetName();
+            //layer.terrain.epsg = !string.IsNullOrEmpty(srsName) && srsName.Contains(GdalHelper.CGCS2000) ? "EPSG:4490" : "EPSG:4326";
 
             Geometry minPoint = GdalHelper.GetMinPoint(ds);
             Geometry maxPoint = GdalHelper.GetMaxPoint(ds);
@@ -137,23 +137,12 @@
             //string[] options = Gdal.ParseCommandLine(args);
             string[] options = new string[]
             {
-                "-t_srs",
-                layer.terrain.epsg, // "EPSG:4326",
-                "-ts",
-                width.ToString(),
-                height.ToString(),
-                "-te",
-                layer.extension.minx.ToString(),
-                layer.extension.miny.ToString(),
-                layer.extension.maxx.ToString(),
-                layer.extension.maxy.ToString(),
-                "-te_srs",
-                layer.terrain.epsg, // "EPSG:4326",
-                "-r",
-                "bilinear",
-                "-of",
-                "GTiff",
-
+                "-t_srs", "EPSG:4326", // layer.terrain.epsg,
+                "-ts", width.ToString(), height.ToString(),
+                "-te", layer.extension.minx.ToString(), layer.extension.miny.ToString(), layer.extension.maxx.ToString(), layer.extension.maxy.ToString(),
+                "-te_srs", "EPSG:4326", // layer.terrain.epsg,
+                "-r", "bilinear",
+                "-of", "GTiff"
             };
             GDALWarpAppOptions warpAppOptions = new GDALWarpAppOptions(options);
 
@@ -340,7 +329,7 @@
                 ds = Gdal.Open(tif, Access.GA_ReadOnly);
                 if (null == ds || 0 == ds.RasterCount) return;
 
-                OSGeo.GDAL.Band band = ds.GetRasterBand(1);
+                Band band = ds.GetRasterBand(1);
                 float[] buffer = new float[width * height];
                 band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
 
@@ -484,18 +473,11 @@
                     fy = float.IsNaN(fy) ? 0 : fy;
                     double dr = Math.Sqrt(Math.Pow(fx, 2) + Math.Pow(fy, 2));
 
-                    int r = Convert.ToInt32((dr * 0.5 + 0.5) * 255);
+                    int r = Convert.ToInt32(dr / 4 * 255);
                     int g = Convert.ToInt32((fx / dr * 0.5 + 0.5) * 255);
                     int b = Convert.ToInt32((fy / dr * 0.5 + 0.5) * 255);
 
-                    if (r < 0 || r > 255)
-                    {
-                        r = 255;
-                    }
-                    if (g < 0 || g > 255) g = 255;
-                    if (b < 0 || b > 255) b = 255;
-
-                    Color color = Color.FromArgb(127, r, g, b);
+                    Color color = Color.FromArgb(127, GetSafeValue(r), GetSafeValue(g), GetSafeValue(b));
                     image.SetPixel(x, y, color);
                 }
             }
@@ -509,10 +491,71 @@
         {
             return (float.IsNaN(val) || val < -999) ? float.NaN : val;
         }
+
+        /// <summary>
+        /// 鑾峰彇瀹夊叏鍊�
+        /// </summary>
+        private static int GetSafeValue(int val)
+        {
+            if (val < 0) return 0;
+            if (val > 255) val = 255;
+
+            return val;
+        }
         #endregion
 
         #region 鍏冩暟鎹�
         /// <summary>
+        /// 璁剧疆鍦板舰淇℃伅
+        /// </summary>
+        private static void SetTerrainInfo2(Dataset ds, string outPath, Domain.Layer layer)
+        {
+            string tempPath = Path.Combine(outPath, "temp");
+            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
+
+            string tif = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(ds.GetDescription()) + ".tif");
+            Resample(ds, tif);
+            if (!File.Exists(tif)) throw new Exception("楂樼▼鏂囦欢閲嶆姇褰卞け璐ワ紒");
+
+            Dataset tifDS = null;
+            try
+            {
+                tifDS = Gdal.Open(tif, Access.GA_ReadOnly);
+                if (null == tifDS || 0 == tifDS.RasterCount || null == tifDS.GetSpatialRef()) return;
+
+                double[] transform = new double[6];
+                tifDS.GetGeoTransform(transform);
+                double minx = transform[0];
+                double miny = transform[3] - tifDS.RasterYSize * transform[1];
+                double maxx = transform[0] + (tifDS.RasterYSize * transform[1]);
+                double maxy = transform[3];
+
+                layer.extension = new Extension(minx, miny, maxx, maxy, double.MaxValue, double.MinValue);
+
+                Band band = tifDS.GetRasterBand(1);
+                double[] mm = new double[2];
+                band.ComputeRasterMinMax(mm, 0);
+                layer.terrain.SetHeight(GetMinVal(mm[0]), GetMaxVal(mm[1]));
+            }
+            finally
+            {
+                if (null != tifDS) tifDS.Dispose();
+            }
+        }
+
+        /// <summary>
+        /// 閲嶉噰鏍�
+        /// </summary>
+        private static void Resample(Dataset ds, string dest)
+        {
+            string[] options = new string[] { "-t_srs", "EPSG:4326", "-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 CopeLayerJson(string outPath, Domain.Layer layer)

--
Gitblit v1.9.3