From a10e925cdda6f92a7bbf86188e317f5c5ee839b5 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 26 七月 2024 14:21:20 +0800
Subject: [PATCH] 水面数据在计算时添加建筑物避让功能

---
 SimuTools/Tools/Handle.cs |  342 ++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 209 insertions(+), 133 deletions(-)

diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs
index e6d97d6..96b9021 100644
--- a/SimuTools/Tools/Handle.cs
+++ b/SimuTools/Tools/Handle.cs
@@ -18,6 +18,10 @@
         #region 鎴愬憳鍙橀噺
         public static readonly int MAX = 256 * 256;
 
+        public static readonly double MAX_X_OFFSET = 0.002;
+
+        public static readonly double MIN_X_OFFSET = -0.00005;
+
         public static readonly double WaterHeightOffset = 1.0;
 
         public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
@@ -28,38 +32,37 @@
         /// <summary>
         /// 杩愯
         /// </summary>
-        public static void Run(string terrainFile, string waterPath, string flowPath, string outPath)
+        public static void Run(Args args)
         {
             Domain.Layer layer = new Domain.Layer();
             layer.duration = new Duration();
             layer.terrain = new Terrain();
             layer.waters = new Water();
 
-            string temp = Path.Combine(outPath, "temp");
-            if (!Directory.Exists(temp)) Directory.CreateDirectory(temp);
+            CopeTerrain(args, layer);
+            CopeBuilding(args, layer);
+            CopeWater(args, layer);
+            CopeFlow(args, layer);
+            CopeLayerJson(args, layer);
 
-            CopeTerrain(terrainFile, outPath, layer);
-            CopeWater(waterPath, outPath, layer);
-            CopeFlow(flowPath, outPath, layer);
-            CopeLayerJson(outPath, layer);
-
-            if (Directory.Exists(temp)) Directory.Delete(temp, true);
+            if (Directory.Exists(args.temp)) Directory.Delete(args.temp, true);
         }
 
         #region 鍦板舰
         /// <summary>
         /// 澶勭悊鍦板舰
         /// </summary>
-        private static void CopeTerrain(string terrainFile, string outPath, Domain.Layer layer)
+        private static void CopeTerrain(Args args, Domain.Layer layer)
         {
             Dataset ds = null;
             try
             {
-                ds = Gdal.Open(terrainFile, Access.GA_ReadOnly);
+                ds = Gdal.Open(args.terrainFile, Access.GA_ReadOnly);
                 if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
 
                 SetTerrainInfo(ds, layer);
-                CreateTerrainPng(ds, layer, outPath);
+                SetWaterInfo(args, layer);
+                CreateTerrainPng(args, ds, layer);
             }
             finally
             {
@@ -72,9 +75,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) + MAX_X_OFFSET, 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,32 +95,30 @@
         /// <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>
         /// 鍒涘缓鍦板舰PNG
         /// </summary>
-        private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath)
+        private static void CreateTerrainPng(Args args, Dataset ds, Domain.Layer layer)
         {
-            string tempPath = Path.Combine(outPath, "temp");
-            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
-            string terrainPath = Path.Combine(outPath, "terrain");
+            string terrainPath = Path.Combine(args.outPath, "terrain");
             if (!Directory.Exists(terrainPath)) Directory.CreateDirectory(terrainPath);
 
             foreach (int[] sizes in layer.terrain.size)
             {
-                string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif");
+                string tif = Path.Combine(args.temp, "terrain_" + sizes[0] + "_" + sizes[1] + ".tif");
                 //Resample(ds.GetDescription(), tif, sizes[0], sizes[1]);
                 Resample(ds, tif, sizes[0], sizes[1], layer);
                 if (!File.Exists(tif)) continue;
@@ -130,20 +138,12 @@
             //string[] options = Gdal.ParseCommandLine(args);
             string[] options = new string[]
             {
-                "-t_srs",
-                "EPSG:4326",
-                "-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",
-                "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);
 
@@ -162,7 +162,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);
 
@@ -170,19 +170,28 @@
                 Graphics graphic = Graphics.FromImage(image);
                 graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹�
 
-                double perHeight = (layer.terrain.maxHeight - layer.terrain.minHeight) * 100 / 65536;
+                //double perHeight = (layer.terrain.maxHeight - layer.terrain.minHeight) * 100 / 65536;
+                double differ = layer.extension.maxHeight - layer.extension.minHeight, minHeight = layer.extension.minHeight;
                 for (int x = 0; x < width; x++)
                 {
                     for (int y = 0; y < height; y++)
                     {
                         int offset = x + y * width;
-                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) continue;
+                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
 
-                        //int val = Convert.ToInt32(buffer[offset] * 100);
-                        int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) * 100 / perHeight);
-                        int r = val / 65536;
-                        int g = (val - r * 65536) / 256;
-                        int b = val % 256;
+                        //int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) * 100 / perHeight);
+                        //int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) / differ * 255); // 65535
+                        int r = 0, g, b;
+                        if (buffer[offset] - layer.extension.maxHeight > 0)
+                        {
+                            g = b = 255;
+                        }
+                        else
+                        {
+                            int val = Convert.ToInt32((buffer[offset] - minHeight) / differ * 65535);
+                            g = val / 256;
+                            b = val % 256;
+                        }
 
                         Color color = Color.FromArgb(127, r, g, b);
                         image.SetPixel(x, y, color);
@@ -193,25 +202,68 @@
             finally
             {
                 if (null != ds) ds.Dispose();
-                if (File.Exists(tif)) File.Delete(tif);
+                //if (File.Exists(tif)) File.Delete(tif);
+            }
+        }
+        #endregion
+
+        #region 寤虹瓚
+        /// <summary>
+        /// 澶勭悊寤虹瓚
+        /// </summary>
+        private static void CopeBuilding(Args args, Domain.Layer layer)
+        {
+            Dataset ds = null;
+            try
+            {
+                ds = Gdal.Open(args.buildingFile, Access.GA_ReadOnly);
+                if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
+
+                foreach (int[] sizes in layer.terrain.size)
+                {
+                    string tif = Path.Combine(args.temp, "building_" + sizes[0] + "_" + sizes[1] + ".tif");
+                    Resample(ds, tif, sizes[0], sizes[1], layer);
+                    if (!File.Exists(tif)) continue;
+
+                    Dataset dataset = Gdal.Open(tif, Access.GA_ReadOnly);
+                    if (null == dataset || 0 == dataset.RasterCount) return;
+
+                    float[] buffer = new float[sizes[0] * sizes[1]];
+                    dataset.GetRasterBand(1).ReadRaster(0, 0, sizes[0], sizes[1], buffer, sizes[0], sizes[1], 0, 0);
+                    args.buildings[sizes[0] + "_" + sizes[1]] = buffer;
+
+                    dataset.Dispose();
+                }
+            }
+            finally
+            {
+                if (null != ds) ds.Dispose();
             }
         }
         #endregion
 
         #region 姘撮潰
         /// <summary>
-        /// 澶勭悊姘撮潰
+        /// 璁剧疆姘撮潰淇℃伅
         /// </summary>
-        private static void CopeWater(string waterPath, string outPath, Domain.Layer layer)
+        private static void SetWaterInfo(Args args, Domain.Layer layer)
         {
-            List<string> files = GetFiles(waterPath);
+            List<string> files = layer.waters.files = GetFiles(args.waterPath);
             if (null == files || files.Count == 0) return;
 
             SetWaterData(layer, files);
             SetWaterHeight(layer, files);
-            if (files.Count != layer.waters.data.Count) return;
+        }
 
-            ProcessWaters(files, outPath, layer);
+        /// <summary>
+        /// 澶勭悊姘撮潰
+        /// </summary>
+        private static void CopeWater(Args args, Domain.Layer layer)
+        {
+            List<String> files = layer.waters.files;
+            if (files.Count == 0 || files.Count != layer.waters.data.Count) return;
+
+            ProcessWaters(args, files, layer);
         }
 
         /// <summary>
@@ -220,7 +272,7 @@
         private static List<String> GetFiles(string path, string prefix = "")
         {
             string[] files = Directory.GetFiles(path, prefix + "*.tif?", SearchOption.TopDirectoryOnly);
-            if (null == files || files.Length == 0) return null;
+            if (null == files || files.Length == 0) return new List<string>();
 
             List<string> list = files.ToList();
             list.Sort();
@@ -246,6 +298,8 @@
                 DateTime d = new DateTime(now.Year, now.Month, now.Day, hour, minute, second);
                 layer.waters.data.Add((d.Ticks - startTicks) / 10000);
             }
+            layer.duration.start = layer.waters.data[0];
+            layer.duration.end = layer.waters.data[layer.waters.data.Count - 1];
         }
 
         /// <summary>
@@ -270,14 +324,14 @@
                     if (null != ds) ds.Dispose();
                 }
             });
-            layer.extension.minHeight = GetMinVal(layer.extension.minHeight);
-            layer.extension.maxHeight = GetMaxVal(layer.extension.maxHeight + WaterHeightOffset);
+            layer.extension.minHeight = GetMinVal(layer.extension.minHeight - 1);
+            layer.extension.maxHeight = GetMaxVal(layer.extension.maxHeight + 1);
         }
 
         /// <summary>
         /// 澶勭悊姘撮潰鏁版嵁
         /// </summary>
-        private static void ProcessWaters(List<string> files, string outPath, Domain.Layer layer)
+        private static void ProcessWaters(Args args, List<string> files, Domain.Layer layer)
         {
             Parallel.For(0, files.Count, i =>
             //for (int i = 0; i < files.Count; i++)
@@ -288,7 +342,7 @@
                     ds = Gdal.Open(files[i], Access.GA_ReadOnly);
                     if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return;
 
-                    CreateWaterPng(ds, layer, outPath, layer.waters.data[i]);
+                    CreateWaterPng(args, ds, layer, layer.waters.data[i]);
                 }
                 finally
                 {
@@ -300,29 +354,27 @@
         /// <summary>
         /// 鍒涘缓姘撮潰PNG
         /// </summary>
-        private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks)
+        private static void CreateWaterPng(Args args, Dataset ds, Domain.Layer layer, long ticks)
         {
-            string tempPath = Path.Combine(outPath, "temp");
-            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
-            string waterPath = Path.Combine(outPath, "waters", ticks.ToString());
+            string waterPath = Path.Combine(args.outPath, "waters", ticks.ToString());
             if (!Directory.Exists(waterPath)) Directory.CreateDirectory(waterPath);
 
             foreach (int[] sizes in layer.terrain.size)
             {
                 string fileName = Path.GetFileNameWithoutExtension(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
-                string tif = Path.Combine(tempPath, fileName + ".tif");
+                string tif = Path.Combine(args.temp, fileName + ".tif");
                 Resample(ds, tif, sizes[0], sizes[1], layer);
                 if (!File.Exists(tif)) continue;
 
                 string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png");
-                Water2Png(layer, tif, png, sizes[0], sizes[1]);
+                Water2Png(args, layer, tif, png, sizes[0], sizes[1]);
             }
         }
 
         /// <summary>
         /// 姘撮潰杞琍NG
         /// </summary>
-        private static void Water2Png(Domain.Layer layer, string tif, string png, int width, int height)
+        private static void Water2Png(Args args, Domain.Layer layer, string tif, string png, int width, int height)
         {
             Dataset ds = null;
             try
@@ -330,27 +382,37 @@
                 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);
+                float[] building = args.buildings[width + "_" + height];
 
                 Bitmap image = new Bitmap(width, height);
                 Graphics graphic = Graphics.FromImage(image);
                 graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹�
 
-                double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536;
+                //double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536;
+                double differ = layer.extension.maxHeight - layer.extension.minHeight, minHeight = layer.extension.minHeight;
                 for (int x = 0; x < width; x++)
                 {
                     for (int y = 0; y < height; y++)
                     {
                         int offset = x + y * width;
-                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) continue;
+                        if (float.IsNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
+                        if (!float.IsNaN(building[offset]) && building[offset] > -999) continue;
 
-                        //int val = Convert.ToInt32(buffer[offset] * 100);
-                        int val = Convert.ToInt32((buffer[offset] - layer.extension.minHeight + WaterHeightOffset) * 100 / perHeight);
-                        int r = val / 65536;
-                        int g = (val - r * 65536) / 256;
-                        int b = val % 256;
+                        //int val = Convert.ToInt32((buffer[offset] - layer.extension.minHeight + WaterHeightOffset) * 100 / perHeight);
+                        int r = 0, g, b;
+                        if (buffer[offset] - layer.extension.maxHeight > 0)
+                        {
+                            g = b = 255;
+                        }
+                        else
+                        {
+                            int val = Convert.ToInt32((buffer[offset] - minHeight) / differ * 65535);
+                            g = val / 256;
+                            b = val % 256;
+                        }
 
                         Color color = Color.FromArgb(127, r, g, b);
                         image.SetPixel(x, y, color);
@@ -361,7 +423,7 @@
             finally
             {
                 if (null != ds) ds.Dispose();
-                if (File.Exists(tif)) File.Delete(tif);
+                //if (File.Exists(tif)) File.Delete(tif);
             }
         }
         #endregion
@@ -370,10 +432,10 @@
         /// <summary>
         /// 澶勭悊娴侀�熸祦鍚�
         /// </summary>
-        private static void CopeFlow(string flowPath, string outPath, Domain.Layer layer)
+        private static void CopeFlow(Args args, Domain.Layer layer)
         {
-            List<string> vxFiles = GetFiles(flowPath, "vx");
-            List<string> vyFiles = GetFiles(flowPath, "vy");
+            List<string> vxFiles = GetFiles(args.flowPath, "vx");
+            List<string> vyFiles = GetFiles(args.flowPath, "vy");
             if (null == vxFiles || null == vyFiles || vxFiles.Count != vyFiles.Count || vxFiles.Count != layer.waters.data.Count) return;
 
             Parallel.For(0, vxFiles.Count, i =>
@@ -387,7 +449,7 @@
                     if (null == vxDs || 0 == vxDs.RasterCount || null == vxDs.GetSpatialRef() ||
                         null == vyDs || 0 == vyDs.RasterCount || null == vyDs.GetSpatialRef()) return;
 
-                    CreateFlowPng(vxDs, vyDs, layer, outPath, layer.waters.data[i]);
+                    CreateFlowPng(args, vxDs, vyDs, layer, layer.waters.data[i]);
                 }
                 finally
                 {
@@ -400,21 +462,19 @@
         /// <summary>
         /// 鍒涘缓娴侀�熸祦鍚慞NG
         /// </summary>
-        private static void CreateFlowPng(Dataset vxDs, Dataset vyDs, Domain.Layer layer, string outPath, long ticks)
+        private static void CreateFlowPng(Args args, Dataset vxDs, Dataset vyDs, Domain.Layer layer, long ticks)
         {
-            string tempPath = Path.Combine(outPath, "temp");
-            if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath);
-            string flowPath = Path.Combine(outPath, "flows", ticks.ToString());
+            string flowPath = Path.Combine(args.outPath, "flows", ticks.ToString());
             if (!Directory.Exists(flowPath)) Directory.CreateDirectory(flowPath);
 
             foreach (int[] sizes in layer.terrain.size)
             {
                 string vxName = Path.GetFileNameWithoutExtension(vxDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1];
-                string vxTif = Path.Combine(tempPath, vxName + ".tif");
+                string vxTif = Path.Combine(args.temp, vxName + ".tif");
                 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");
+                string vyTif = Path.Combine(args.temp, vyName + ".tif");
                 Resample(vyDs, vyTif, sizes[0], sizes[1], layer);
 
                 if (!File.Exists(vxTif) || !File.Exists(vyTif)) continue;
@@ -440,29 +500,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,18 +528,17 @@
                     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;
-                    if (g < 0 || g > 255) g = 255;
-                    if (b < 0 || b > 255) b = 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));
 
-                    Color color = Color.FromArgb(127, r, g, b);
+                    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);
+
+                    Color color = Color.FromArgb(127, GetSafeValue(r), GetSafeValue(g), GetSafeValue(b));
                     image.SetPixel(x, y, color);
                 }
             }
@@ -503,36 +554,14 @@
         }
 
         /// <summary>
-        /// 鑾峰彇R鍊�
+        /// 鑾峰彇瀹夊叏鍊�
         /// </summary>
-        private static double GetRVal(float fx, float fy)
+        private static int GetSafeValue(int val)
         {
-            if (float.IsNaN(fx) && float.IsNaN(fy)) return double.NaN;
+            if (val < 0) return 0;
+            if (val > 255) return 255;
 
-            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);
+            return val;
         }
         #endregion
 
@@ -540,13 +569,13 @@
         /// <summary>
         /// 澶勭悊鍏冩暟鎹�
         /// </summary>
-        private static void CopeLayerJson(string outPath, Domain.Layer layer)
+        private static void CopeLayerJson(Args args, Domain.Layer layer)
         {
             if (null == layer) return;
 
             String json = JsonConvert.SerializeObject(layer);
 
-            string filePath = Path.Combine(outPath, "layer.json");
+            string filePath = Path.Combine(args.outPath, "layer.json");
             using (StreamWriter sw = new StreamWriter(filePath))
             {
                 sw.Write(json);
@@ -556,7 +585,54 @@
 
         #region 鏆傛椂涓嶇敤
         /// <summary>
-        /// 閲嶉噰鏍� *
+        /// 璁剧疆鍦板舰淇℃伅
+        /// </summary>
+        private static void SetTerrainInfo(Args args, Dataset ds, Domain.Layer layer)
+        {
+            string tif = Path.Combine(args.temp, 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 Resample(string source, string dest, int width, int height)
         {

--
Gitblit v1.9.3