| | |
| | | CopeTerrain(terrainFile, outPath, layer); |
| | | CopeWater(waterPath, outPath, layer); |
| | | CopeFlow(flowPath, outPath, layer); |
| | | CopeLayerJson(outPath, layer); |
| | | //CopeLayerJson(outPath, layer); |
| | | |
| | | if (Directory.Exists(temp)) Directory.Delete(temp, true); |
| | | } |
| | | |
| | | #region 地形 |
| | | /// <summary> |
| | | /// 处理地形 |
| | | /// </summary> |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建地形图层 |
| | | /// 创建地形PNG |
| | | /// </summary> |
| | | private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath) |
| | | { |
| | |
| | | } |
| | | |
| | | 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> |
| | | /// 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 |
| | |
| | | if (File.Exists(tif)) File.Delete(tif); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | #region 水面 |
| | | /// <summary> |
| | | /// 处理水面 |
| | | /// </summary> |
| | |
| | | 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++) |
| | | { |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建地形图层 |
| | | /// 创建水面PNG |
| | | /// </summary> |
| | | private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks) |
| | | { |
| | |
| | | } |
| | | |
| | | 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(); |
| | |
| | | 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> |
| | | /// 创建流速流向PNG |
| | | /// </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> |
| | | /// 流速流向Tif值转存至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); // 填充透明色 |
| | | |
| | | for (int x = 0; x < width; x++) |
| | | { |
| | | for (int y = 0; y < height; y++) |
| | | { |
| | | //int offset = x + y * height; |
| | | //if (float.IsNaN(vxBuffer[offset]) || vxBuffer[offset] == -9999) continue; |
| | | |
| | | //int val = Convert.ToInt32(vxBuffer[offset] * 100); |
| | | //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 != 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> |
| | |
| | | sw.Write(json); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | #region 暂时不用 |
| | | /// <summary> |