From 1f22a86346c18ccb29a4a712c4acb05c013b33d4 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 22 七月 2024 10:50:57 +0800 Subject: [PATCH] 修改高程和水面的计算方法 --- SimuTools/Tools/Handle.cs | 327 +++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 260 insertions(+), 67 deletions(-) diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs index af7aef4..6882a03 100644 --- a/SimuTools/Tools/Handle.cs +++ b/SimuTools/Tools/Handle.cs @@ -5,12 +5,12 @@ using System; using System.Collections.Generic; using System.Configuration; -using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; +using System.Threading.Tasks; namespace SimuTools.Tools { @@ -37,12 +37,13 @@ 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 鍦板舰 /// <summary> /// 澶勭悊鍦板舰 /// </summary> @@ -79,7 +80,7 @@ } /// <summary> - /// 鍒涘缓鍦板舰鍥惧眰 + /// 鍒涘缓鍦板舰PNG /// </summary> private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath) { @@ -90,28 +91,47 @@ foreach (int[] sizes in layer.terrain.size) { - //string filePath = Path.Combine(outPath, sizes[0] + "_" + sizes[1] + ".png"); - //// 濉厖鍐呭瓨鍥惧儚 - //byte[] buffer = new byte[sizes[0] * sizes[1] * 3]; - //for (int i = 0; i < buffer.Length; i++) - //{ - // buffer[i] = (byte)(i % 256); - //} - string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif"); - Resample(ds.GetDescription(), tif, sizes[0], sizes[1]); + //Resample(ds.GetDescription(), tif, sizes[0], sizes[1]); + Resample(ds, tif, sizes[0], sizes[1]); if (!File.Exists(tif)) { continue; } string png = Path.Combine(terrainPath, sizes[0] + "_" + sizes[1] + ".png"); - Terrain2Png(layer, tif, png, sizes[0], sizes[1]); + Tif2Png(layer, tif, png, sizes[0], sizes[1]); } } /// <summary> /// 閲嶉噰鏍� + /// </summary> + private static void Resample(Dataset ds, string dest, int width, int height) + { + // 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"); + //string[] options = Gdal.ParseCommandLine(args); + string[] options = new string[] + { + "-t_srs", + "EPSG:4326", + "-ts", + width.ToString(), + height.ToString(), + "-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) { @@ -164,7 +184,7 @@ /// <summary> /// Tif鍊艰浆瀛樿嚦Png /// </summary> - private static void Terrain2Png(Domain.Layer layer, string tif, string png, int width, int height) + private static void Tif2Png(Domain.Layer layer, string tif, string png, int width, int height) { Dataset ds = null; try @@ -177,28 +197,236 @@ band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0); Bitmap image = new Bitmap(width, height); - //Graphics graphic = Graphics.FromImage(image); - //graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹� + Graphics graphic = Graphics.FromImage(image); + graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹� double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536; - for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { - if (buffer[x * y] == double.NaN || buffer[x * y] == -9999) - { - image.SetPixel(x, y, Color.Transparent); - continue; - } + int offset = x + y * width; + if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) continue; - //int val = Convert.ToInt32(buffer[x * y]* 100); //Convert.ToInt32((buffer[x * y] - layer.extension.minHeight) * 100); - int val = Convert.ToInt32((buffer[x * y] - layer.extension.minHeight) * 100 / perHeight); + //int val = Convert.ToInt32(buffer[offset] * 100); + int val = Convert.ToInt32((buffer[offset] - layer.extension.minHeight) * 100 / perHeight); int r = val / 65536; int g = (val - r * 65536) / 256; int b = val % 256; - Color color = Color.FromArgb(0, r, g, b); + Color color = Color.FromArgb(127, r, g, b); + image.SetPixel(x, y, color); + } + } + image.Save(png, System.Drawing.Imaging.ImageFormat.Png); + } + finally + { + if (null != ds) ds.Dispose(); + if (File.Exists(tif)) File.Delete(tif); + } + } + #endregion + + #region 姘撮潰 + /// <summary> + /// 澶勭悊姘撮潰 + /// </summary> + private static void CopeWater(string waterPath, string outPath, Domain.Layer layer) + { + List<string> files = GetFiles(waterPath); + if (null == files || files.Count == 0) return; + + SetWaterData(layer, files); + if (files.Count != layer.waters.data.Count) return; + + /*for (int i = 0, c = files.Count; i < c; i++) + { + Dataset ds = null; + try + { + ds = Gdal.Open(files[i], Access.GA_ReadOnly); + if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) continue; + + CreateWaterPng(ds, layer, outPath, layer.waters.data[i]); + } + finally + { + if (null != ds) ds.Dispose(); + } + }*/ + Parallel.For(0, files.Count, i => + { + Dataset ds = null; + try + { + 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]); + } + finally + { + if (null != ds) ds.Dispose(); + } + }); + } + + /// <summary> + /// 鍒涘缓姘撮潰PNG + /// </summary> + private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks) + { + string tempPath = Path.Combine(outPath, "temp"); + if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); + string waterPath = Path.Combine(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"); + Resample(ds.GetDescription(), tif, sizes[0], sizes[1]); + if (!File.Exists(tif)) + { + continue; + } + + string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png"); + Tif2Png(layer, tif, png, sizes[0], sizes[1]); + } + } + + /// <summary> + /// 鑾峰彇鏂囦欢 + /// </summary> + 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; + + List<string> list = files.ToList(); + list.Sort(); + + return list; + } + + /// <summary> + /// 璁剧疆姘撮潰鏁版嵁 + /// </summary> + private static void SetWaterData(Domain.Layer layer, List<string> files) + { + DateTime now = DateTime.Now; + long startTicks = new DateTime(1970, 1, 1, 8, 0, 0).Ticks; + + foreach (string file in files) + { + string fileName = Path.GetFileNameWithoutExtension(file); + int hour = Convert.ToInt32(fileName.Substring(0, 2)); + int minute = Convert.ToInt32(fileName.Substring(2, 2)); + int second = Convert.ToInt32(fileName.Substring(4, 2)); + + DateTime d = new DateTime(now.Year, now.Month, now.Day, hour, minute, second); + layer.waters.data.Add((d.Ticks - startTicks) / 10000); + } + } + #endregion + + #region 娴侀�熸祦鍚� + /// <summary> + /// 澶勭悊娴侀�熸祦鍚� + /// </summary> + private static void CopeFlow(string flowPath, string outPath, Domain.Layer layer) + { + List<string> vxFiles = GetFiles(flowPath, "vx"); + 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 => + { + Dataset vxDs = null, vyDs = null; + try + { + vxDs = Gdal.Open(vxFiles[i], Access.GA_ReadOnly); + 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]); + } + finally + { + if (null != vxDs) vxDs.Dispose(); + if (null != vyDs) vyDs.Dispose(); + } + }); + } + + /// <summary> + /// 鍒涘缓娴侀�熸祦鍚慞NG + /// </summary> + private static void CreateFlowPng(Dataset vxDs, Dataset vyDs, Domain.Layer layer, string outPath, long ticks) + { + string tempPath = Path.Combine(outPath, "temp"); + if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); + string flowPath = Path.Combine(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"); + Resample(vxDs.GetDescription(), vxTif, sizes[0], sizes[1]); + + 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]); + + if (!File.Exists(vxTif) || !File.Exists(vyTif)) continue; + + string png = Path.Combine(flowPath, sizes[0] + "_" + sizes[1] + ".png"); + VxyTif2Png(layer, vxTif, vyTif, png, sizes[0], sizes[1]); + } + } + + /// <summary> + /// 娴侀�熸祦鍚慣if鍊艰浆瀛樿嚦Png + /// </summary> + private static void VxyTif2Png(Domain.Layer layer, string vxTif, string vyTif, string png, int width, int height) + { + Dataset vxDs = null, vyDs = null; + try + { + vxDs = Gdal.Open(vxTif, Access.GA_ReadOnly); + vyDs = Gdal.Open(vyTif, Access.GA_ReadOnly); + if (null == vxDs || 0 == vxDs.RasterCount || null == vyDs || 0 == vyDs.RasterCount) return; + + float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height]; + 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); + + Bitmap image = new Bitmap(width, height); + Graphics graphic = Graphics.FromImage(image); + graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹� + + // 鐢� R閫氶亾琛ㄧず锛屾祦鍚戜负褰掍竴鍖栫殑浜岀淮鍚戦噺锛坸,y锛夛紝G閫氶亾琛ㄧず涓� x *255 , B閫氶亾琛ㄧず涓� y * 255 + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + int offset = x + y * width; + if (float.IsNaN(vxBuffer[offset]) || vxBuffer[offset] < -999 || + float.IsNaN(vyBuffer[offset]) || vyBuffer[offset] < -999) continue; + + float vx = vxBuffer[offset], vy = vyBuffer[offset]; + //int val = Convert.ToInt32(vxBuffer[offset] * 100); + //int r = val / 65536; + //int g = (val - r * 65536) / 256; + //int b = val % 256; + int r = 0; + int g = 0; + int b = 0; + + Color color = Color.FromArgb(127, r, g, b); image.SetPixel(x, y, color); } } @@ -207,26 +435,15 @@ } finally { - if (null != ds) ds.Dispose(); + if (null != vxDs) vxDs.Dispose(); + if (null != vyDs) vyDs.Dispose(); + if (File.Exists(vxTif)) File.Delete(vxTif); + if (File.Exists(vyTif)) File.Delete(vyTif); } } + #endregion - /// <summary> - /// 澶勭悊姘撮潰 - /// </summary> - private static void CopeWater(string waterPath, string outPath, Domain.Layer layer) - { - - } - - /// <summary> - /// 澶勭悊娴侀�熸祦鍚� - /// </summary> - private static void CopeFlow(string flowPath, string outPath, Domain.Layer layer) - { - - } - + #region 鍏冩暟鎹� /// <summary> /// 澶勭悊鍏冩暟鎹� /// </summary> @@ -242,30 +459,6 @@ sw.Write(json); } } - - #region 鏆傛椂涓嶇敤 - /// <summary> - /// 閲嶉噰鏍� * - /// </summary> - private static void Resample(Dataset ds, string dest, int width, int height) - { - // 璁剧疆Warp鐨勯�夐」锛歨ttps://blog.csdn.net/qq_43210879/article/details/121350561 - string[] options = new string[] { - "format=GTiff", - "width=" + width, - "height=" + height, - "dstSRS=EPSG:4326" - }; - - OSGeo.GDAL.Driver driver = Gdal.GetDriverByName("GTiff"); - Dataset destDs = driver.Create(dest, width, height, ds.RasterCount, ds.GetRasterBand(1).DataType, null); - - GDALWarpAppOptions warpAppOptions = new GDALWarpAppOptions(options); - Gdal.Warp(destDs, new Dataset[] { ds }, warpAppOptions, null, null); - - destDs.Dispose(); - } - #endregion } } -- Gitblit v1.9.3