| | |
| | | /// <summary> |
| | | /// 运行 |
| | | /// </summary> |
| | | public static void Run(string terrainFile, string waterPath, string flowPath, string outPath) |
| | | public static void Run(Args args) |
| | | { |
| | | Domain.Layer layer = new Domain.Layer(); |
| | | layer.duration = new Duration(); |
| | | layer.terrain = new Terrain(); |
| | | layer.waters = new Water(); |
| | | |
| | | string temp = Path.Combine(outPath, "temp"); |
| | | if (!Directory.Exists(temp)) Directory.CreateDirectory(temp); |
| | | CopeTerrain(args, layer); |
| | | CopeBuilding(args, layer); |
| | | CopeWater(args, layer); |
| | | CopeFlow(args, layer); |
| | | CopeLayerJson(args, layer); |
| | | |
| | | CopeTerrain(terrainFile, waterPath, outPath, layer); |
| | | CopeWater(waterPath, outPath, layer); |
| | | CopeFlow(flowPath, outPath, layer); |
| | | CopeLayerJson(outPath, layer); |
| | | |
| | | if (Directory.Exists(temp)) Directory.Delete(temp, true); |
| | | if (Directory.Exists(args.temp)) Directory.Delete(args.temp, true); |
| | | } |
| | | |
| | | #region 地形 |
| | | /// <summary> |
| | | /// 处理地形 |
| | | /// </summary> |
| | | private static void CopeTerrain(string terrainFile, string waterPath, string outPath, Domain.Layer layer) |
| | | private static void CopeTerrain(Args args, Domain.Layer layer) |
| | | { |
| | | Dataset ds = null; |
| | | try |
| | | { |
| | | ds = Gdal.Open(terrainFile, Access.GA_ReadOnly); |
| | | ds = Gdal.Open(args.terrainFile, Access.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return; |
| | | |
| | | SetTerrainInfo(ds, layer); |
| | | SetWaterInfo(waterPath, layer); |
| | | CreateTerrainPng(ds, layer, outPath); |
| | | SetWaterInfo(args, layer); |
| | | CreateTerrainPng(args, ds, layer); |
| | | } |
| | | finally |
| | | { |
| | |
| | | /// <summary> |
| | | /// 创建地形PNG |
| | | /// </summary> |
| | | private static void CreateTerrainPng(Dataset ds, Domain.Layer layer, string outPath) |
| | | private static void CreateTerrainPng(Args args, Dataset ds, Domain.Layer layer) |
| | | { |
| | | string tempPath = Path.Combine(outPath, "temp"); |
| | | if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); |
| | | string terrainPath = Path.Combine(outPath, "terrain"); |
| | | string terrainPath = Path.Combine(args.outPath, "terrain"); |
| | | if (!Directory.Exists(terrainPath)) Directory.CreateDirectory(terrainPath); |
| | | |
| | | foreach (int[] sizes in layer.terrain.size) |
| | | { |
| | | string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif"); |
| | | string tif = Path.Combine(args.temp, "terrain_" + sizes[0] + "_" + sizes[1] + ".tif"); |
| | | //Resample(ds.GetDescription(), tif, sizes[0], sizes[1]); |
| | | Resample(ds, tif, sizes[0], sizes[1], layer); |
| | | if (!File.Exists(tif)) continue; |
| | |
| | | ds = Gdal.Open(tif, Access.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.RasterCount) return; |
| | | |
| | | OSGeo.GDAL.Band band = ds.GetRasterBand(1); |
| | | Band band = ds.GetRasterBand(1); |
| | | float[] buffer = new float[width * height]; |
| | | band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0); |
| | | |
| | |
| | | graphic.Clear(Color.Transparent); // 填充透明色 |
| | | |
| | | //double perHeight = (layer.terrain.maxHeight - layer.terrain.minHeight) * 100 / 65536; |
| | | //double differ = layer.terrain.maxHeight - layer.terrain.minHeight; |
| | | double differ = layer.extension.maxHeight - layer.extension.minHeight, minHeight = layer.extension.minHeight; |
| | | for (int x = 0; x < width; x++) |
| | | { |
| | |
| | | |
| | | //int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) * 100 / perHeight); |
| | | //int val = Convert.ToInt32((buffer[offset] - layer.terrain.minHeight) / differ * 255); // 65535 |
| | | |
| | | int r = 0, g, b; |
| | | if (buffer[offset] - layer.extension.maxHeight > 0) |
| | | { |
| | |
| | | } |
| | | #endregion |
| | | |
| | | #region 建筑 |
| | | /// <summary> |
| | | /// 处理建筑 |
| | | /// </summary> |
| | | private static void CopeBuilding(Args args, Domain.Layer layer) |
| | | { |
| | | Dataset ds = null; |
| | | try |
| | | { |
| | | ds = Gdal.Open(args.buildingFile, Access.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return; |
| | | |
| | | foreach (int[] sizes in layer.terrain.size) |
| | | { |
| | | string tif = Path.Combine(args.temp, "building_" + sizes[0] + "_" + sizes[1] + ".tif"); |
| | | Resample(ds, tif, sizes[0], sizes[1], layer); |
| | | if (!File.Exists(tif)) continue; |
| | | |
| | | Dataset dataset = Gdal.Open(tif, Access.GA_ReadOnly); |
| | | if (null == dataset || 0 == dataset.RasterCount) return; |
| | | |
| | | float[] buffer = new float[sizes[0] * sizes[1]]; |
| | | dataset.GetRasterBand(1).ReadRaster(0, 0, sizes[0], sizes[1], buffer, sizes[0], sizes[1], 0, 0); |
| | | args.buildings[sizes[0] + "_" + sizes[1]] = buffer; |
| | | |
| | | dataset.Dispose(); |
| | | } |
| | | } |
| | | finally |
| | | { |
| | | if (null != ds) ds.Dispose(); |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | #region 水面 |
| | | /// <summary> |
| | | /// 设置水面信息 |
| | | /// </summary> |
| | | private static void SetWaterInfo(string waterPath, Domain.Layer layer) |
| | | private static void SetWaterInfo(Args args, Domain.Layer layer) |
| | | { |
| | | List<string> files = layer.waters.files = GetFiles(waterPath); |
| | | List<string> files = layer.waters.files = GetFiles(args.waterPath); |
| | | if (null == files || files.Count == 0) return; |
| | | |
| | | SetWaterData(layer, files); |
| | |
| | | /// <summary> |
| | | /// 处理水面 |
| | | /// </summary> |
| | | private static void CopeWater(string waterPath, string outPath, Domain.Layer layer) |
| | | private static void CopeWater(Args args, Domain.Layer layer) |
| | | { |
| | | List<String> files = layer.waters.files; |
| | | if (files.Count == 0 || files.Count != layer.waters.data.Count) return; |
| | | |
| | | ProcessWaters(files, outPath, layer); |
| | | ProcessWaters(args, files, layer); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <summary> |
| | | /// 处理水面数据 |
| | | /// </summary> |
| | | private static void ProcessWaters(List<string> files, string outPath, Domain.Layer layer) |
| | | private static void ProcessWaters(Args args, List<string> files, Domain.Layer layer) |
| | | { |
| | | Parallel.For(0, files.Count, i => |
| | | //for (int i = 0; i < files.Count; i++) |
| | |
| | | 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]); |
| | | CreateWaterPng(args, ds, layer, layer.waters.data[i]); |
| | | } |
| | | finally |
| | | { |
| | |
| | | /// <summary> |
| | | /// 创建水面PNG |
| | | /// </summary> |
| | | private static void CreateWaterPng(Dataset ds, Domain.Layer layer, string outPath, long ticks) |
| | | private static void CreateWaterPng(Args args, Dataset ds, Domain.Layer layer, long ticks) |
| | | { |
| | | string tempPath = Path.Combine(outPath, "temp"); |
| | | if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); |
| | | string waterPath = Path.Combine(outPath, "waters", ticks.ToString()); |
| | | string waterPath = Path.Combine(args.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"); |
| | | string tif = Path.Combine(args.temp, fileName + ".tif"); |
| | | Resample(ds, tif, sizes[0], sizes[1], layer); |
| | | if (!File.Exists(tif)) continue; |
| | | |
| | | string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png"); |
| | | Water2Png(layer, tif, png, sizes[0], sizes[1]); |
| | | Water2Png(args, layer, tif, png, sizes[0], sizes[1]); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 水面转PNG |
| | | /// </summary> |
| | | private static void Water2Png(Domain.Layer layer, string tif, string png, int width, int height) |
| | | private static void Water2Png(Args args, Domain.Layer layer, string tif, string png, int width, int height) |
| | | { |
| | | Dataset ds = null; |
| | | try |
| | |
| | | Band band = ds.GetRasterBand(1); |
| | | float[] buffer = new float[width * height]; |
| | | band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0); |
| | | float[] building = args.buildings[width + "_" + height]; |
| | | |
| | | Bitmap image = new Bitmap(width, height); |
| | | Graphics graphic = Graphics.FromImage(image); |
| | |
| | | { |
| | | int offset = x + y * width; |
| | | if (float.IsNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue; |
| | | if (!float.IsNaN(building[offset]) && building[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; |
| | | |
| | | int r = 0, g, b; |
| | | if (buffer[offset] - layer.extension.maxHeight > 0) |
| | | { |
| | |
| | | /// <summary> |
| | | /// 处理流速流向 |
| | | /// </summary> |
| | | private static void CopeFlow(string flowPath, string outPath, Domain.Layer layer) |
| | | private static void CopeFlow(Args args, Domain.Layer layer) |
| | | { |
| | | List<string> vxFiles = GetFiles(flowPath, "vx"); |
| | | List<string> vyFiles = GetFiles(flowPath, "vy"); |
| | | List<string> vxFiles = GetFiles(args.flowPath, "vx"); |
| | | List<string> vyFiles = GetFiles(args.flowPath, "vy"); |
| | | if (null == vxFiles || null == vyFiles || vxFiles.Count != vyFiles.Count || vxFiles.Count != layer.waters.data.Count) return; |
| | | |
| | | Parallel.For(0, vxFiles.Count, i => |
| | |
| | | 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]); |
| | | CreateFlowPng(args, vxDs, vyDs, layer, layer.waters.data[i]); |
| | | } |
| | | finally |
| | | { |
| | |
| | | /// <summary> |
| | | /// 创建流速流向PNG |
| | | /// </summary> |
| | | private static void CreateFlowPng(Dataset vxDs, Dataset vyDs, Domain.Layer layer, string outPath, long ticks) |
| | | private static void CreateFlowPng(Args args, Dataset vxDs, Dataset vyDs, Domain.Layer layer, long ticks) |
| | | { |
| | | string tempPath = Path.Combine(outPath, "temp"); |
| | | if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); |
| | | string flowPath = Path.Combine(outPath, "flows", ticks.ToString()); |
| | | string flowPath = Path.Combine(args.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"); |
| | | string vxTif = Path.Combine(args.temp, vxName + ".tif"); |
| | | Resample(vxDs, vxTif, sizes[0], sizes[1], layer); |
| | | |
| | | string vyName = Path.GetFileNameWithoutExtension(vyDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1]; |
| | | string vyTif = Path.Combine(tempPath, vyName + ".tif"); |
| | | string vyTif = Path.Combine(args.temp, vyName + ".tif"); |
| | | Resample(vyDs, vyTif, sizes[0], sizes[1], layer); |
| | | |
| | | if (!File.Exists(vxTif) || !File.Exists(vyTif)) continue; |
| | |
| | | /// <summary> |
| | | /// 处理元数据 |
| | | /// </summary> |
| | | private static void CopeLayerJson(string outPath, Domain.Layer layer) |
| | | private static void CopeLayerJson(Args args, Domain.Layer layer) |
| | | { |
| | | if (null == layer) return; |
| | | |
| | | String json = JsonConvert.SerializeObject(layer); |
| | | |
| | | string filePath = Path.Combine(outPath, "layer.json"); |
| | | string filePath = Path.Combine(args.outPath, "layer.json"); |
| | | using (StreamWriter sw = new StreamWriter(filePath)) |
| | | { |
| | | sw.Write(json); |
| | |
| | | /// <summary> |
| | | /// 设置地形信息 |
| | | /// </summary> |
| | | private static void SetTerrainInfo(Dataset ds, string outPath, Domain.Layer layer) |
| | | private static void SetTerrainInfo(Args args, Dataset ds, Domain.Layer layer) |
| | | { |
| | | string tempPath = Path.Combine(outPath, "temp"); |
| | | if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); |
| | | |
| | | string tif = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(ds.GetDescription()) + ".tif"); |
| | | string tif = Path.Combine(args.temp, Path.GetFileNameWithoutExtension(ds.GetDescription()) + ".tif"); |
| | | Resample(ds, tif); |
| | | if (!File.Exists(tif)) throw new Exception("高程文件重投影失败!"); |
| | | |