From 052c83d9d32880ed81110152d989705735f801bc Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 22 七月 2024 16:16:46 +0800 Subject: [PATCH] 修改水面数据 --- SimuTools/Domain/Terrain.cs | 19 +++ SimuTools/Domain/Extension.cs | 13 ++ SimuTools/Tools/Handle.cs | 262 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 196 insertions(+), 98 deletions(-) diff --git a/SimuTools/Domain/Extension.cs b/SimuTools/Domain/Extension.cs index 870d9b0..743e3b5 100644 --- a/SimuTools/Domain/Extension.cs +++ b/SimuTools/Domain/Extension.cs @@ -1,4 +1,5 @@ -锘縰sing System; +锘縰sing Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,6 +9,9 @@ { public class Extension { + [JsonIgnore] + private static readonly object _obj = new object(); + public double minx { set; get; } public double miny { set; get; } @@ -48,8 +52,11 @@ public void SetHeight(double minHeight, double maxHeight) { - this.minHeight = minHeight; - this.maxHeight = maxHeight; + lock (_obj) + { + if (this.minHeight > minHeight) this.minHeight = minHeight; + if (this.maxHeight < maxHeight) this.maxHeight = maxHeight; + } } } } diff --git a/SimuTools/Domain/Terrain.cs b/SimuTools/Domain/Terrain.cs index e212558..8b3b338 100644 --- a/SimuTools/Domain/Terrain.cs +++ b/SimuTools/Domain/Terrain.cs @@ -1,4 +1,5 @@ -锘縰sing System; +锘縰sing Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Configuration; using System.Linq; @@ -11,9 +12,15 @@ { public List<int[]> size { set; get; } + [JsonIgnore] + public double minHeight { set; get; } + + [JsonIgnore] + public double maxHeight { set; get; } + public Terrain() { - size = new List<int[]>(); + this.size = new List<int[]>(); string str = ConfigurationManager.AppSettings["sizes"]; if (string.IsNullOrEmpty(str)) return; @@ -24,7 +31,7 @@ int i; if (int.TryParse(s, out i)) { - size.Add(new int[] { i, i }); + this.size.Add(new int[] { i, i }); } } } @@ -33,5 +40,11 @@ { this.size = size; } + + public void SetHeight(double minHeight, double maxHeight) + { + this.minHeight = minHeight; + this.maxHeight = maxHeight; + } } } diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs index b0dff5d..f950a34 100644 --- a/SimuTools/Tools/Handle.cs +++ b/SimuTools/Tools/Handle.cs @@ -17,9 +17,11 @@ { public static readonly int MAX = 256 * 256; - public static readonly string GdalPath = ConfigurationManager.AppSettings["gdalPath"]; + public static readonly double WaterHeightOffset = 1.0; public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory; + + public static readonly string GdalPath = ConfigurationManager.AppSettings["gdalPath"]; /// <summary> /// 杩愯 @@ -36,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); @@ -55,7 +57,7 @@ if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return; SetTerrainInfo(ds, layer); - //CreateTerrainPng(ds, layer, outPath); + CreateTerrainPng(ds, layer, outPath); } finally { @@ -70,12 +72,28 @@ { 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)); + layer.extension = new Extension(minPoint.GetX(0), minPoint.GetY(0), maxPoint.GetX(0), maxPoint.GetY(0), double.MaxValue, double.MinValue); - OSGeo.GDAL.Band band = ds.GetRasterBand(1); + Band band = ds.GetRasterBand(1); double[] mm = new double[2]; band.ComputeRasterMinMax(mm, 0); - layer.extension.SetHeight(mm[0], mm[1]); + layer.terrain.SetHeight(GetMinVal(mm[0]), GetMaxVal(mm[1])); + } + + /// <summary> + /// 鑾峰彇鏈�灏忓�� + /// </summary> + private static double GetMinVal(double val) + { + return Convert.ToInt32(Math.Floor(val * 1000)) / 1000.0; + } + + /// <summary> + /// 鑾峰彇鏈�澶у�� + /// </summary> + private static double GetMaxVal(double val) + { + return Convert.ToInt32(Math.Ceiling(val * 1000)) / 1000.0; } /// <summary> @@ -93,13 +111,10 @@ 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]); - if (!File.Exists(tif)) - { - continue; - } + if (!File.Exists(tif)) continue; string png = Path.Combine(terrainPath, sizes[0] + "_" + sizes[1] + ".png"); - Tif2Png(layer, tif, png, sizes[0], sizes[1]); + Terrain2Png(layer, tif, png, sizes[0], sizes[1]); } } @@ -181,9 +196,9 @@ } /// <summary> - /// Tif鍊艰浆瀛樿嚦Png + /// 鍦板舰杞琍NG /// </summary> - private static void Tif2Png(Domain.Layer layer, string tif, string png, int width, int height, int heightOffset = 0) + private static void Terrain2Png(Domain.Layer layer, string tif, string png, int width, int height) { Dataset ds = null; try @@ -199,7 +214,7 @@ Graphics graphic = Graphics.FromImage(image); graphic.Clear(Color.Transparent); // 濉厖閫忔槑鑹� - double perHeight = (layer.extension.maxHeight - layer.extension.minHeight + heightOffset) * 100 / 65536; + double perHeight = (layer.terrain.maxHeight - layer.terrain.minHeight) * 100 / 65536; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) @@ -208,7 +223,7 @@ if (float.IsNaN(buffer[offset]) || buffer[offset] < -999) continue; //int val = Convert.ToInt32(buffer[offset] * 100); - int val = Convert.ToInt32((buffer[offset] - layer.extension.minHeight + heightOffset) * 100 / perHeight); + 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; @@ -237,71 +252,10 @@ 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 ProcessWaters(List<string> files, string outPath, Domain.Layer layer) - { - /*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], 1); - } + ProcessWaters(files, outPath, layer); } /// <summary> @@ -337,6 +291,123 @@ layer.waters.data.Add((d.Ticks - startTicks) / 10000); } } + + /// <summary> + /// 璁剧疆姘撮潰楂樺害 + /// </summary> + private static void SetWaterHeight(Domain.Layer layer, List<string> files) + { + 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; + + double[] mm = new double[2]; + ds.GetRasterBand(1).ComputeRasterMinMax(mm, 0); + layer.extension.SetHeight(mm[0], mm[1]); + } + finally + { + if (null != ds) ds.Dispose(); + } + }); + layer.extension.minHeight = GetMinVal(layer.extension.minHeight); + layer.extension.maxHeight = GetMaxVal(layer.extension.maxHeight + WaterHeightOffset); + } + + /// <summary> + /// 澶勭悊姘撮潰鏁版嵁 + /// </summary> + private static void ProcessWaters(List<string> files, string outPath, Domain.Layer layer) + { + Parallel.For(0, files.Count, i => + //for (int i = 0; i < 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"); + Water2Png(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) + { + Dataset ds = null; + try + { + ds = Gdal.Open(tif, Access.GA_ReadOnly); + if (null == ds || 0 == ds.RasterCount) return; + + OSGeo.GDAL.Band band = ds.GetRasterBand(1); + float[] buffer = new float[width * height]; + 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); // 濉厖閫忔槑鑹� + + double perHeight = (layer.extension.maxHeight - layer.extension.minHeight) * 100 / 65536; + 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; + + //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; + + 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 娴侀�熸祦鍚� @@ -349,8 +420,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 @@ -367,7 +438,7 @@ if (null != vxDs) vxDs.Dispose(); if (null != vyDs) vyDs.Dispose(); } - }); + }//); } /// <summary> @@ -413,14 +484,15 @@ 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[] 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, png, width, height); + CreateFlowPng(vxBuffer, vyBuffer, perHeight, png, width, height); } finally { @@ -434,7 +506,7 @@ /// <summary> /// 鍒涘缓娴侀�熸祦鍚慞NG /// </summary> - private static void CreateFlowPng(float[] vxBuffer, float[] vyBuffer, string png, int width, int height) + private static void CreateFlowPng(float[] vxBuffer, float[] vyBuffer, double perHeight, string png, int width, int height) { Bitmap image = new Bitmap(width, height); Graphics graphic = Graphics.FromImage(image); @@ -454,7 +526,10 @@ double dy = float.IsNaN(fy) ? 0 : fy * 0.5 + 0.5; int r = Convert.ToInt32(dv * 255); - if (r > 255) r = 255; + if (r > 255) + { + r = 255; + } int g = Convert.ToInt32(dx * 255); int b = Convert.ToInt32(dy * 255); @@ -472,12 +547,15 @@ { //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; + //if (val > 1) return 1; + //if (val < -1) return -1; return val; } + /// <summary> + /// 鑾峰彇XY鍊� + /// </summary> private static double GetXyValue(float fx, float fy) { if (float.IsNaN(fx) && float.IsNaN(fy)) -- Gitblit v1.9.3