From c3c0df05c1670e8506c5bc956afc1345ad881d57 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期二, 23 七月 2024 11:07:14 +0800
Subject: [PATCH] 统一重采样的范围

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

diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index f950a34..2c60f97 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -38,7 +38,7 @@
 
             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);
@@ -110,7 +110,7 @@
             {
                 string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif");
                 //Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
-                Resample(ds, tif, sizes[0], sizes[1]);
+                Resample(ds, tif, sizes[0], sizes[1], layer);
                 if (!File.Exists(tif)) continue;
 
                 string png = Path.Combine(terrainPath, sizes[0] + "_" + sizes[1] + ".png");
@@ -121,7 +121,7 @@
         /// <summary>
         /// 閲嶉噰鏍�
         /// </summary>
-        private static void Resample(Dataset ds, string dest, int width, int height)
+        private static void Resample(Dataset ds, string dest, int width, int height, Domain.Layer layer)
         {
             // https://blog.51cto.com/u_16099346/6691820
             //string args = string.Format("-t_srs {0} -ts {1} {2} -r {3} -of {4}", "EPSG:4326", width, height, "bilinear", "GTiff");
@@ -133,6 +133,11 @@
                 "-ts",
                 width.ToString(),
                 height.ToString(),
+                "-te",
+                layer.extension.minx.ToString(),
+                layer.extension.miny.ToString(),
+                layer.extension.maxx.ToString(),
+                layer.extension.maxy.ToString(),
                 "-r",
                 "bilinear",
                 "-of",
@@ -355,7 +360,7 @@
             {
                 string fileName = Path.GetFileNameWithoutExtension(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
                 string tif = Path.Combine(tempPath, fileName + ".tif");
-                Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
+                Resample(ds, tif, sizes[0], sizes[1], layer);
                 if (!File.Exists(tif)) continue;
 
                 string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png");
@@ -420,8 +425,8 @@
             List<string> vyFiles = GetFiles(flowPath, "vy");
             if (null == vxFiles || null == vyFiles || vxFiles.Count != vyFiles.Count || vxFiles.Count != layer.waters.data.Count) return;
 
-            //Parallel.For(0, vxFiles.Count, i =>
-            for (int i = 0; i < vxFiles.Count; i++)
+            Parallel.For(0, vxFiles.Count, i =>
+            //for (int i = 0; i < vxFiles.Count; i++)
             {
                 Dataset vxDs = null, vyDs = null;
                 try
@@ -438,7 +443,7 @@
                     if (null != vxDs) vxDs.Dispose();
                     if (null != vyDs) vyDs.Dispose();
                 }
-            }//);
+            });
         }
 
         /// <summary>
@@ -455,11 +460,11 @@
             {
                 string vxName = Path.GetFileNameWithoutExtension(vxDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
                 string vxTif = Path.Combine(tempPath, vxName + ".tif");
-                Resample(vxDs.GetDescription(), vxTif, sizes[0], sizes[1]);
+                Resample(vxDs, vxTif, sizes[0], sizes[1], layer);
 
                 string vyName = Path.GetFileNameWithoutExtension(vyDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
                 string vyTif = Path.Combine(tempPath, vyName + ".tif");
-                Resample(vyDs.GetDescription(), vyTif, sizes[0], sizes[1]);
+                Resample(vyDs, vyTif, sizes[0], sizes[1], layer);
 
                 if (!File.Exists(vxTif) || !File.Exists(vyTif)) continue;
 
@@ -492,7 +497,7 @@
                 double max = vxMm[1] > vyMm[1] ? vxMm[1] : vyMm[1];
                 double perHeight = (max - min) / 255;
 
-                CreateFlowPng(vxBuffer, vyBuffer, perHeight, png, width, height);
+                CreateFlowPng(vxBuffer, vyBuffer, min, perHeight, png, width, height);
             }
             finally
             {
@@ -506,7 +511,7 @@
         /// <summary>
         /// 鍒涘缓娴侀�熸祦鍚慞NG
         /// </summary>
-        private static void CreateFlowPng(float[] vxBuffer, float[] vyBuffer, double perHeight, string png, int width, int height)
+        private static void CreateFlowPng(float[] vxBuffer, float[] vyBuffer, double minHeight, double perHeight, string png, int width, int height)
         {
             Bitmap image = new Bitmap(width, height);
             Graphics graphic = Graphics.FromImage(image);
@@ -521,17 +526,17 @@
                     float fx = GetFloatValue(vxBuffer[offset]);
                     float fy = GetFloatValue(vyBuffer[offset]);
 
-                    double dv = GetXyValue(fx, fy);
-                    double dx = float.IsNaN(fx) ? 0 : fx * 0.5 + 0.5;
-                    double dy = float.IsNaN(fy) ? 0 : fy * 0.5 + 0.5;
+                    double rv = GetRVal(fx, fy);
+                    double dv = GetXyValue(fx, fy, rv);
+                    int r = Convert.ToInt32(double.IsNaN(dv) || dv == 0 ? 0 : (dv * 0.5 + 0.5) * 255);
+                    if (r < 0 || r > 255) r = 255;
 
-                    int r = Convert.ToInt32(dv * 255);
-                    if (r > 255)
-                    {
-                        r = 255;
-                    }
-                    int g = Convert.ToInt32(dx * 255);
-                    int b = Convert.ToInt32(dy * 255);
+                    //int g = Convert.ToInt32((float.IsNaN(fx) ? 0 : fx * 0.5 + 0.5) * 255);
+                    //int b = Convert.ToInt32((float.IsNaN(fy) ? 0 : fy * 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 (g < 0 || g > 255) g = 255;
+                    if (b < 0 || b > 255) b = 255;
 
                     Color color = Color.FromArgb(127, r, g, b);
                     image.SetPixel(x, y, color);
@@ -545,33 +550,42 @@
         /// </summary>
         private static float GetFloatValue(float val)
         {
-            //return (float.IsNaN(val) || val < -999) ? float.NaN : val;
-            if (float.IsNaN(val) || val < -999) return float.NaN;
-            //if (val > 1) return 1;
-            //if (val < -1) return -1;
+            return (float.IsNaN(val) || val < -999) ? float.NaN : val;
+        }
 
-            return val;
+        /// <summary>
+        /// 鑾峰彇R鍊�
+        /// </summary>
+        private static double GetRVal(float fx, float fy)
+        {
+            if (float.IsNaN(fx) && float.IsNaN(fy)) return 1.0;
+
+            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)
+        private static double GetXyValue(float fx, float fy, double rv)
         {
-            if (float.IsNaN(fx) && float.IsNaN(fy))
-            {
-                return 0;
-            }
+            if ((float.IsNaN(fx) && float.IsNaN(fy)) || double.IsNaN(rv)) return 0;
+
             if (float.IsNaN(fx))
             {
-                return Math.Abs(fy);
+                return fy / rv;
             }
             if (float.IsNaN(fy))
             {
-                return Math.Abs(fx);
+                return fx / rv;
             }
 
-            return Math.Sqrt(Math.Pow(fx, 2) + Math.Pow(fy, 2));
+            double v = Math.Sqrt(Math.Pow(fx / rv, 2) + Math.Pow(fy / rv, 2));
+
+            return (fx > 0 && fy > 0) || (fx < 0 && fy < 0) ? v : -v;
         }
         #endregion
 

--
Gitblit v1.9.3