From 0a70f99e51b0ea0c83288213e3b1f7904d78ede0 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期四, 18 七月 2024 17:52:46 +0800 Subject: [PATCH] 创建PNG --- SimuTools/FrmMain.cs | 4 +- SimuTools/Tools/GdalHelper.cs | 74 +++++++++++++++++++++++++++++-------- SimuTools/Tools/Handle.cs | 13 ++++++ 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/SimuTools/FrmMain.cs b/SimuTools/FrmMain.cs index 41faf90..63e50df 100644 --- a/SimuTools/FrmMain.cs +++ b/SimuTools/FrmMain.cs @@ -113,7 +113,7 @@ try { flag = true; - LogOut.Info(">> 寮�濮嬭繍琛� >>"); + LogOut.Info("寮�濮嬭繍琛� >>"); outPath = Path.Combine(outPath, serviceName); if (!Directory.Exists(outPath)) @@ -124,7 +124,7 @@ Tools.Handle.Run(terrainFile, waterPath, flowPath, outPath); flag = false; - LogOut.Info("<< 杩愯缁撴潫 <<"); + LogOut.Info("杩愯缁撴潫 <<"); MessageBox.Show("杩愯缁撴潫锛�", "鎻愮ず", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) diff --git a/SimuTools/Tools/GdalHelper.cs b/SimuTools/Tools/GdalHelper.cs index 3da84f1..0a6e6ba 100644 --- a/SimuTools/Tools/GdalHelper.cs +++ b/SimuTools/Tools/GdalHelper.cs @@ -4,9 +4,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace SimuTools.Tools { @@ -83,9 +80,9 @@ Gdal.AllRegister(); } - /** - * 鑾峰彇Dataset鐨勬渶灏忕偣 - */ + /// <summary> + /// 鑾峰彇Dataset鐨勬渶灏忕偣 + /// </summary> public static Geometry GetMinPoint(Dataset ds) { double[] transform = new double[6]; @@ -100,9 +97,9 @@ return Transform(ds, point); } - /** - * 鑾峰彇Dataset鐨勬渶澶х偣 - */ + /// <summary> + /// 鑾峰彇Dataset鐨勬渶澶х偣 + /// </summary> public static Geometry GetMaxPoint(Dataset ds) { /* @@ -126,9 +123,9 @@ return Transform(ds, point); } - /** - * 鍧愭爣杞崲 - */ + /// <summary> + /// 鍧愭爣杞崲 + /// </summary> public static Geometry Transform(Dataset ds, Geometry point) { point.AssignSpatialReference(ds.GetSpatialRef()); @@ -151,9 +148,9 @@ return point; } - /** - * 鍒涘缓閲戝瓧濉� - */ + /// <summary> + /// 鍒涘缓閲戝瓧濉� + /// </summary> public static void CreatePyramid(String file) { Dataset ds = null; @@ -164,7 +161,7 @@ ds = Gdal.Open(file, Access.GA_ReadOnly); if (null == ds) return; - Band band = ds.GetRasterBand(1); + OSGeo.GDAL.Band band = ds.GetRasterBand(1); if (0 == band.GetOverviewCount()) { ds.BuildOverviews("nearest", new int[] { 2, 4, 6, 8, 16 }); @@ -179,5 +176,50 @@ if (null != ds) ds.Dispose(); } } + + /// <summary> + /// 鍒涘缓PNG + /// </summary> + public void CreatePng(string filePath, int width, int height, int bands = 3) + { + // 鍒涘缓鍐呭瓨椹卞姩 + OSGeo.GDAL.Driver memDriver = Gdal.GetDriverByName("MEM"); + // 鍒涘缓鍐呭瓨鏁版嵁闆� + Dataset ds = memDriver.Create("mem::", width, height, bands, DataType.GDT_Byte, null); + + // 璁剧疆鍥惧儚淇℃伅 + for (int i = 1; i <= bands; i++) + { + Band band = ds.GetRasterBand(i); + band.SetRasterColorInterpretation((ColorInterp)i); + } + + // 濉厖鍐呭瓨鍥惧儚 + byte[] buffer = new byte[width * height * bands]; + for (int i = 0; i < buffer.Length; i++) + { + buffer[i] = (byte)(i % 256); + } + + // 鍐欏叆鍐呭瓨鍥惧儚 + for (int i = 1; i <= bands; i++) + { + Band band = ds.GetRasterBand(i); + band.WriteRaster(0, 0, width, height, buffer, width, height, 0, 0); + } + + // 鍒涘缓PNG椹卞姩 + OSGeo.GDAL.Driver pngDriver = Gdal.GetDriverByName("PNG"); + + // 淇濆瓨涓篜NG鏂囦欢 + //ds.SetProjection(Gdal.GetProjectionRef("")); + //ds.SetGeoTransform(Gdal.GetGeoTransform("")); + pngDriver.CreateCopy(filePath, ds, 0, null, null, null); + pngDriver.Dispose(); + + // 鍏抽棴鏁版嵁闆� + ds.Dispose(); + memDriver.Dispose(); + } } } diff --git a/SimuTools/Tools/Handle.cs b/SimuTools/Tools/Handle.cs index f41d648..da4768d 100644 --- a/SimuTools/Tools/Handle.cs +++ b/SimuTools/Tools/Handle.cs @@ -22,6 +22,7 @@ { Domain.Layer layer = new Domain.Layer(); layer.duration = new Duration(); + layer.terrain = new Terrain(); layer.waters = new Water(); CopeTerrain(terrainFile, outPath, layer); @@ -42,6 +43,7 @@ if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return; SetTerrainInfo(ds, layer); + CreateTerrainPng(ds, layer, outPath); } finally { @@ -65,6 +67,17 @@ } /// <summary> + /// 鍒涘缓鍦板舰鍥惧眰 + /// </summary> + private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath) + { + foreach (int[] sizes in layer.terrain.size) + { + + } + } + + /// <summary> /// 澶勭悊姘撮潰 /// </summary> private static void CopeWater(string waterPath, string outPath, Domain.Layer layer) -- Gitblit v1.9.3