From f74d84c3b758e9a6ba56f3a73ceb63a5e90c5349 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 24 七月 2024 10:04:12 +0800
Subject: [PATCH] 1

---
 SimuTools/Tools/Handle.cs |  101 +++++++++++++++++++-------------------------------
 1 files changed, 38 insertions(+), 63 deletions(-)

diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index e6d97d6..67a549e 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -59,7 +59,7 @@
                 if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
 
                 SetTerrainInfo(ds, layer);
-                CreateTerrainPng(ds, layer, outPath);
+                //CreateTerrainPng(ds, layer, outPath);
             }
             finally
             {
@@ -72,9 +72,16 @@
         /// </summary>
         private static void SetTerrainInfo(Dataset ds, Domain.Layer layer)
         {
+            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);
-            layer.extension = new Extension(minPoint.GetX(0), minPoint.GetY(0), maxPoint.GetX(0), maxPoint.GetY(0), double.MaxValue, double.MinValue);
+            double minx = GetMinVal(minPoint.GetX(0), 10000000);
+            double miny = GetMinVal(minPoint.GetY(0), 10000000);
+            double maxx = GetMaxVal(maxPoint.GetX(0), 10000000);
+            double maxy = GetMaxVal(maxPoint.GetY(0), 10000000);
+            layer.extension = new Extension(minx, miny, maxx, maxy, double.MaxValue, double.MinValue);
 
             Band band = ds.GetRasterBand(1);
             double[] mm = new double[2];
@@ -85,17 +92,17 @@
         /// <summary>
         /// 鑾峰彇鏈�灏忓��
         /// </summary>
-        private static double GetMinVal(double val)
+        private static double GetMinVal(double val, double radix = 1000)
         {
-            return Convert.ToInt32(Math.Floor(val * 1000)) / 1000.0;
+            return Convert.ToInt32(Math.Floor(val * radix)) / radix;
         }
 
         /// <summary>
         /// 鑾峰彇鏈�澶у��
         /// </summary>
-        private static double GetMaxVal(double val)
+        private static double GetMaxVal(double val, double radix = 1000)
         {
-            return Convert.ToInt32(Math.Ceiling(val * 1000)) / 1000.0;
+            return Convert.ToInt32(Math.Ceiling(val * radix)) / radix;
         }
 
         /// <summary>
@@ -131,7 +138,7 @@
             string[] options = new string[]
             {
                 "-t_srs",
-                "EPSG:4326",
+                layer.terrain.epsg, // "EPSG:4326",
                 "-ts",
                 width.ToString(),
                 height.ToString(),
@@ -140,10 +147,13 @@
                 layer.extension.miny.ToString(),
                 layer.extension.maxx.ToString(),
                 layer.extension.maxy.ToString(),
+                "-te_srs",
+                layer.terrain.epsg, // "EPSG:4326",
                 "-r",
                 "bilinear",
                 "-of",
-                "GTiff"
+                "GTiff",
+
             };
             GDALWarpAppOptions warpAppOptions = new GDALWarpAppOptions(options);
 
@@ -193,7 +203,7 @@
             finally
             {
                 if (null != ds) ds.Dispose();
-                if (File.Exists(tif)) File.Delete(tif);
+                //if (File.Exists(tif)) File.Delete(tif);
             }
         }
         #endregion
@@ -211,7 +221,7 @@
             SetWaterHeight(layer, files);
             if (files.Count != layer.waters.data.Count) return;
 
-            ProcessWaters(files, outPath, layer);
+            //ProcessWaters(files, outPath, layer);
         }
 
         /// <summary>
@@ -361,7 +371,7 @@
             finally
             {
                 if (null != ds) ds.Dispose();
-                if (File.Exists(tif)) File.Delete(tif);
+                //if (File.Exists(tif)) File.Delete(tif);
             }
         }
         #endregion
@@ -440,29 +450,21 @@
                 vxDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer, width, height, 0, 0);
                 vyDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vyBuffer, width, height, 0, 0);
 
-                double[] vxMm = new double[2];
-                vxDs.GetRasterBand(1).ComputeRasterMinMax(vxMm, 0);
-                double[] vyMm = new double[2];
-                vyDs.GetRasterBand(1).ComputeRasterMinMax(vyMm, 0);
-                double min = vxMm[0] < vyMm[0] ? vxMm[0] : vyMm[0];
-                double max = vxMm[1] > vyMm[1] ? vxMm[1] : vyMm[1];
-                double perHeight = (max - min) / 255;
-
-                CreateFlowPng(vxBuffer, vyBuffer, min, perHeight, png, width, height);
+                CreateFlowPng(vxBuffer, vyBuffer, png, width, height);
             }
             finally
             {
                 if (null != vxDs) vxDs.Dispose();
                 if (null != vyDs) vyDs.Dispose();
-                if (File.Exists(vxTif)) File.Delete(vxTif);
-                if (File.Exists(vyTif)) File.Delete(vyTif);
+                //if (File.Exists(vxTif)) File.Delete(vxTif);
+                //if (File.Exists(vyTif)) File.Delete(vyTif);
             }
         }
 
         /// <summary>
         /// 鍒涘缓娴侀�熸祦鍚慞NG
         /// </summary>
-        private static void CreateFlowPng(float[] vxBuffer, float[] vyBuffer, double minHeight, double perHeight, string png, int width, int height)
+        private static void CreateFlowPng(float[] vxBuffer, float[] vyBuffer, string png, int width, int height)
         {
             Bitmap image = new Bitmap(width, height);
             Graphics graphic = Graphics.FromImage(image);
@@ -476,14 +478,20 @@
                     int offset = x + y * width;
                     float fx = GetFloatValue(vxBuffer[offset]);
                     float fy = GetFloatValue(vyBuffer[offset]);
-                    double rv = GetRVal(fx, fy);
-                    if (double.IsNaN(rv) || rv == 0) continue;
+                    if (float.IsNaN(fx) && float.IsNaN(fy) || (fx == 0 && fy == 0)) continue;
 
-                    double dv = GetXyValue(fx, fy, rv);
-                    int r = Convert.ToInt32(dv == 0 ? 0 : (dv * 0.5 + 0.5) * 255);
-                    int g = float.IsNaN(fx) ? 0 : Convert.ToInt32((fx - minHeight) / perHeight);
-                    int b = float.IsNaN(fy) ? 0 : Convert.ToInt32((fy - minHeight) / perHeight);
-                    if (r < 0 || r > 255) r = 255;
+                    fx = float.IsNaN(fx) ? 0 : fx;
+                    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 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;
 
@@ -500,39 +508,6 @@
         private static float GetFloatValue(float val)
         {
             return (float.IsNaN(val) || val < -999) ? float.NaN : val;
-        }
-
-        /// <summary>
-        /// 鑾峰彇R鍊�
-        /// </summary>
-        private static double GetRVal(float fx, float fy)
-        {
-            if (float.IsNaN(fx) && float.IsNaN(fy)) return double.NaN;
-
-            if (float.IsNaN(fx)) return Math.Abs(fy);
-
-            if (float.IsNaN(fy)) return Math.Abs(fx);
-
-            return Math.Sqrt(Math.Pow(fx, 2) + Math.Pow(fy, 2));
-        }
-
-        /// <summary>
-        /// 鑾峰彇XY鍊�
-        /// </summary>
-        private static double GetXyValue(float fx, float fy, double rv)
-        {
-            if (float.IsNaN(fx))
-            {
-                return fy / rv;
-            }
-            if (float.IsNaN(fy))
-            {
-                return fx / rv;
-            }
-
-            double dv = fx / rv + fy / rv;
-
-            return dv < 0 ? -Math.Sqrt(-dv) : Math.Sqrt(dv);
         }
         #endregion
 

--
Gitblit v1.9.3