| | |
| | | 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 |
| | | { |
| | |
| | | string temp = Path.Combine(outPath, "temp"); |
| | | if (!Directory.Exists(temp)) Directory.CreateDirectory(temp); |
| | | |
| | | //CopeTerrain(terrainFile, outPath, 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(temp)) Directory.Delete(temp, true); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | |
| | | 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]); |
| | | if (!File.Exists(tif)) |
| | |
| | | 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 (buffer[x * y] == double.NaN || buffer[x * y] == -9999) |
| | | int offset = x + y * height; |
| | | if (float.IsNaN(buffer[offset]) || buffer[offset] == -9999) |
| | | { |
| | | 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); |
| | | } |
| | | } |
| | |
| | | finally |
| | | { |
| | | if (null != ds) ds.Dispose(); |
| | | if (File.Exists(tif)) File.Delete(tif); |
| | | } |
| | | } |
| | | |
| | |
| | | /// </summary> |
| | | private static void CopeWater(string waterPath, string outPath, Domain.Layer layer) |
| | | { |
| | | string watersPath = Path.Combine(outPath, "waters"); |
| | | if (!Directory.Exists(watersPath)) Directory.CreateDirectory(watersPath); |
| | | |
| | | List<string> files = GetFiles(waterPath); |
| | | if (null == files || files.Count == 0) return; |
| | | |
| | | SetWaterData(layer, files); |
| | | |
| | | /*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> |
| | | /// 创建地形图层 |
| | | /// </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"); |
| | | Terrain2Png(layer, tif, png, sizes[0], sizes[1]); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | private static void SetWaterData(Domain.Layer layer, List<string> files) |
| | | { |
| | | DateTime now = DateTime.Now; |
| | | long startTicks = new DateTime(1970, 1, 1, 0, 0, 0).Ticks; |
| | | long startTicks = new DateTime(1970, 1, 1, 8, 0, 0).Ticks; |
| | | |
| | | foreach (string file in files) |
| | | { |