| | |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | |
| | | namespace SimuTools.Tools |
| | | { |
| | |
| | | { |
| | | for (int y = 0; y < height; y++) |
| | | { |
| | | if (buffer[x * y] == double.NaN || buffer[x * y] == -9999) |
| | | if (float.IsNaN(buffer[x * y]) || buffer[x * y] == -9999) |
| | | { |
| | | image.SetPixel(x, y, Color.Transparent); |
| | | continue; |
| | |
| | | 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) |
| | | { |
| | | 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> |
| | | /// 获取文件 |
| | | /// </summary> |
| | | private static List<String> GetFiles(string path) |
| | | { |
| | | string[] files = Directory.GetFiles(path, "*.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); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | |
| | | destDs.Dispose(); |
| | | } |
| | | |
| | | #endregion |
| | | } |
| | | } |