From ecfe110922f3c542df7aaef9ac9e26cdb0b73e55 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期六, 20 七月 2024 09:52:58 +0800 Subject: [PATCH] 1 --- SimuTools/Tools/Handle.cs | 209 +++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 157 insertions(+), 52 deletions(-) diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs index 6ff3f22..74796fc 100644 --- a/SimuTools/Tools/Handle.cs +++ b/SimuTools/Tools/Handle.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Configuration; -using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; @@ -37,13 +36,14 @@ if (!Directory.Exists(temp)) Directory.CreateDirectory(temp); CopeTerrain(terrainFile, outPath, layer); - CopeWater(waterPath, outPath, layer); + //CopeWater(waterPath, 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> @@ -56,7 +56,7 @@ if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return; SetTerrainInfo(ds, layer); - CreateTerrainPng(ds, layer, outPath); + //CreateTerrainPng(ds, layer, outPath); } finally { @@ -80,7 +80,7 @@ } /// <summary> - /// 鍒涘缓鍦板舰鍥惧眰 + /// 鍒涘缓鍦板舰PNG /// </summary> private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath) { @@ -91,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) { @@ -165,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 @@ -178,28 +197,30 @@ 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; + //double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536; + //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); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { - if (float.IsNaN(buffer[x * y]) || buffer[x * y] == -9999) + int offset = x + y * width; + if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) { - image.SetPixel(x, y, Color.Transparent); + //image.SetPixel(x, y, Color.Transparent); 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 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); } } @@ -212,7 +233,9 @@ if (File.Exists(tif)) File.Delete(tif); } } + #endregion + #region 姘撮潰 /// <summary> /// 澶勭悊姘撮潰 /// </summary> @@ -222,6 +245,7 @@ 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++) { @@ -238,7 +262,6 @@ if (null != ds) ds.Dispose(); } }*/ - Parallel.For(0, files.Count, i => { Dataset ds = null; @@ -257,7 +280,7 @@ } /// <summary> - /// 鍒涘缓鍦板舰鍥惧眰 + /// 鍒涘缓姘撮潰PNG /// </summary> private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks) { @@ -277,16 +300,16 @@ } string png = Path.Combine(waterPath, 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 List<String> GetFiles(string path) + private static List<String> GetFiles(string path, string prefix = "") { - string[] files = Directory.GetFiles(path, "*.tif?", SearchOption.TopDirectoryOnly); + string[] files = Directory.GetFiles(path, prefix + "*.tif?", SearchOption.TopDirectoryOnly); if (null == files || files.Length == 0) return null; List<string> list = files.ToList(); @@ -314,15 +337,120 @@ 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); + } + } + + image.Save(png, System.Drawing.Imaging.ImageFormat.Png); + } + 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); + } + } + #endregion + + #region 鍏冩暟鎹� /// <summary> /// 澶勭悊鍏冩暟鎹� /// </summary> @@ -337,29 +465,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