| | |
| | | { |
| | | public class Handle |
| | | { |
| | | #region 成员变量 |
| | | public static readonly int MAX = 256 * 256; |
| | | |
| | | public static readonly double WaterHeightOffset = 1.0; |
| | |
| | | public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory; |
| | | |
| | | public static readonly string GdalPath = ConfigurationManager.AppSettings["gdalPath"]; |
| | | #endregion |
| | | |
| | | /// <summary> |
| | | /// 运行 |
| | |
| | | |
| | | CopeTerrain(terrainFile, outPath, layer); |
| | | CopeWater(waterPath, outPath, layer); |
| | | //CopeFlow(flowPath, outPath, layer); |
| | | CopeFlow(flowPath, outPath, layer); |
| | | CopeLayerJson(outPath, layer); |
| | | |
| | | if (Directory.Exists(temp)) Directory.Delete(temp, true); |
| | |
| | | if (null == ds || 0 == ds.RasterCount || null == ds.GetSpatialRef()) return; |
| | | |
| | | SetTerrainInfo(ds, layer); |
| | | CreateTerrainPng(ds, layer, outPath); |
| | | //CreateTerrainPng(ds, layer, outPath); |
| | | } |
| | | finally |
| | | { |
| | |
| | | /// </summary> |
| | | private static void SetTerrainInfo(Dataset ds, Domain.Layer layer) |
| | | { |
| | | String srsName = ds.GetSpatialRef().GetName(); |
| | | layer.terrain.epsg = !string.IsNullOrEmpty(srsName) && srsName.Contains(GdalHelper.CGCS2000) ? "EPSG:4490" : "EPSG:4326"; |
| | | |
| | | Geometry minPoint = GdalHelper.GetMinPoint(ds); |
| | | Geometry maxPoint = GdalHelper.GetMaxPoint(ds); |
| | | layer.extension = new Extension(minPoint.GetX(0), minPoint.GetY(0), maxPoint.GetX(0), maxPoint.GetY(0), double.MaxValue, double.MinValue); |
| | | double minx = GetMinVal(minPoint.GetX(0), 10000000); |
| | | double miny = GetMinVal(minPoint.GetY(0), 10000000); |
| | | double maxx = GetMaxVal(maxPoint.GetX(0), 10000000); |
| | | double maxy = GetMaxVal(maxPoint.GetY(0), 10000000); |
| | | layer.extension = new Extension(minx, miny, maxx, maxy, double.MaxValue, double.MinValue); |
| | | |
| | | Band band = ds.GetRasterBand(1); |
| | | double[] mm = new double[2]; |
| | |
| | | /// <summary> |
| | | /// 获取最小值 |
| | | /// </summary> |
| | | private static double GetMinVal(double val) |
| | | private static double GetMinVal(double val, double radix = 1000) |
| | | { |
| | | return Convert.ToInt32(Math.Floor(val * 1000)) / 1000.0; |
| | | return Convert.ToInt32(Math.Floor(val * radix)) / radix; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取最大值 |
| | | /// </summary> |
| | | private static double GetMaxVal(double val) |
| | | private static double GetMaxVal(double val, double radix = 1000) |
| | | { |
| | | return Convert.ToInt32(Math.Ceiling(val * 1000)) / 1000.0; |
| | | return Convert.ToInt32(Math.Ceiling(val * radix)) / radix; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | string tif = Path.Combine(tempPath, DateTime.Now.Ticks.ToString() + ".tif"); |
| | | //Resample(ds.GetDescription(), tif, sizes[0], sizes[1]); |
| | | Resample(ds, tif, sizes[0], sizes[1]); |
| | | Resample(ds, tif, sizes[0], sizes[1], layer); |
| | | if (!File.Exists(tif)) continue; |
| | | |
| | | string png = Path.Combine(terrainPath, sizes[0] + "_" + sizes[1] + ".png"); |
| | |
| | | /// <summary> |
| | | /// 重采样 |
| | | /// </summary> |
| | | private static void Resample(Dataset ds, string dest, int width, int height) |
| | | private static void Resample(Dataset ds, string dest, int width, int height, Domain.Layer layer) |
| | | { |
| | | // https://blog.51cto.com/u_16099346/6691820 |
| | | //string args = string.Format("-t_srs {0} -ts {1} {2} -r {3} -of {4}", "EPSG:4326", width, height, "bilinear", "GTiff"); |
| | |
| | | string[] options = new string[] |
| | | { |
| | | "-t_srs", |
| | | "EPSG:4326", |
| | | layer.terrain.epsg, // "EPSG:4326", |
| | | "-ts", |
| | | width.ToString(), |
| | | height.ToString(), |
| | | "-te", |
| | | layer.extension.minx.ToString(), |
| | | layer.extension.miny.ToString(), |
| | | layer.extension.maxx.ToString(), |
| | | layer.extension.maxy.ToString(), |
| | | "-te_srs", |
| | | layer.terrain.epsg, // "EPSG:4326", |
| | | "-r", |
| | | "bilinear", |
| | | "-of", |
| | | "GTiff" |
| | | "GTiff", |
| | | |
| | | }; |
| | | GDALWarpAppOptions warpAppOptions = new GDALWarpAppOptions(options); |
| | | |
| | | Dataset destDs = Gdal.Warp(dest, new Dataset[] { ds }, warpAppOptions, null, null); |
| | | destDs.Dispose(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 重采样 * |
| | | /// </summary> |
| | | private static void Resample(string source, string dest, int width, int height) |
| | | { |
| | | // https://blog.51cto.com/u_16099346/6691820 |
| | | string cmd = string.Format("{0}gdalwarp.exe -t_srs {1} -ts {2} {3} -r {4} -of GTiff \"{5}\" \"{6}\"", GdalPath, "EPSG:4326", width, height, "bilinear", source, dest); |
| | | string err = ExecExe(cmd); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 执行命令 |
| | | /// </summary> |
| | | public static string ExecExe(string cmd) |
| | | { |
| | | string str = null; |
| | | Process p = null; |
| | | try |
| | | { |
| | | p = new Process(); |
| | | p.StartInfo.FileName = "cmd.exe"; |
| | | p.StartInfo.UseShellExecute = false; |
| | | p.StartInfo.CreateNoWindow = true; |
| | | p.StartInfo.RedirectStandardInput = true; |
| | | p.StartInfo.RedirectStandardOutput = true; |
| | | p.StartInfo.RedirectStandardError = true; |
| | | p.Start(); |
| | | |
| | | StreamWriter si = p.StandardInput; |
| | | StreamReader se = p.StandardError; |
| | | |
| | | si.AutoFlush = true; |
| | | si.WriteLine(cmd); |
| | | si.WriteLine("exit"); |
| | | |
| | | str = se.ReadToEnd(); |
| | | se.Close(); |
| | | si.Close(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | str = ex.Message; |
| | | } |
| | | finally |
| | | { |
| | | if (p != null) p.Close(); |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | finally |
| | | { |
| | | if (null != ds) ds.Dispose(); |
| | | if (File.Exists(tif)) File.Delete(tif); |
| | | //if (File.Exists(tif)) File.Delete(tif); |
| | | } |
| | | } |
| | | #endregion |
| | |
| | | SetWaterHeight(layer, files); |
| | | if (files.Count != layer.waters.data.Count) return; |
| | | |
| | | ProcessWaters(files, outPath, layer); |
| | | //ProcessWaters(files, outPath, layer); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | 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]); |
| | | Resample(ds, tif, sizes[0], sizes[1], layer); |
| | | if (!File.Exists(tif)) continue; |
| | | |
| | | string png = Path.Combine(waterPath, sizes[0] + "_" + sizes[1] + ".png"); |
| | |
| | | finally |
| | | { |
| | | if (null != ds) ds.Dispose(); |
| | | if (File.Exists(tif)) File.Delete(tif); |
| | | //if (File.Exists(tif)) File.Delete(tif); |
| | | } |
| | | } |
| | | #endregion |
| | |
| | | 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 => |
| | | for (int i = 0; i < vxFiles.Count; i++) |
| | | Parallel.For(0, vxFiles.Count, i => |
| | | //for (int i = 0; i < vxFiles.Count; i++) |
| | | { |
| | | Dataset vxDs = null, vyDs = null; |
| | | try |
| | |
| | | if (null != vxDs) vxDs.Dispose(); |
| | | if (null != vyDs) vyDs.Dispose(); |
| | | } |
| | | }//); |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | 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]); |
| | | 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"); |
| | | Resample(vyDs.GetDescription(), vyTif, sizes[0], sizes[1]); |
| | | Resample(vyDs, vyTif, sizes[0], sizes[1], layer); |
| | | |
| | | if (!File.Exists(vxTif) || !File.Exists(vyTif)) continue; |
| | | |
| | |
| | | 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); |
| | | |
| | | double[] vxMm = new double[2]; |
| | | vxDs.GetRasterBand(1).ComputeRasterMinMax(vxMm, 0); |
| | | double[] vyMm = new double[2]; |
| | | vyDs.GetRasterBand(1).ComputeRasterMinMax(vyMm, 0); |
| | | double min = vxMm[0] < vyMm[0] ? vxMm[0] : vyMm[0]; |
| | | double max = vxMm[1] > vyMm[1] ? vxMm[1] : vyMm[1]; |
| | | double perHeight = (max - min) / 255; |
| | | |
| | | CreateFlowPng(vxBuffer, vyBuffer, perHeight, png, width, height); |
| | | CreateFlowPng(vxBuffer, vyBuffer, png, width, height); |
| | | } |
| | | 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); |
| | | //if (File.Exists(vxTif)) File.Delete(vxTif); |
| | | //if (File.Exists(vyTif)) File.Delete(vyTif); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 创建流速流向PNG |
| | | /// </summary> |
| | | private static void CreateFlowPng(float[] vxBuffer, float[] vyBuffer, double perHeight, string png, int width, int height) |
| | | private static void CreateFlowPng(float[] vxBuffer, float[] vyBuffer, string png, int width, int height) |
| | | { |
| | | Bitmap image = new Bitmap(width, height); |
| | | Graphics graphic = Graphics.FromImage(image); |
| | |
| | | int offset = x + y * width; |
| | | float fx = GetFloatValue(vxBuffer[offset]); |
| | | float fy = GetFloatValue(vyBuffer[offset]); |
| | | if (float.IsNaN(fx) && float.IsNaN(fy) || (fx == 0 && fy == 0)) continue; |
| | | |
| | | double dv = GetXyValue(fx, fy); |
| | | double dx = float.IsNaN(fx) ? 0 : fx * 0.5 + 0.5; |
| | | double dy = float.IsNaN(fy) ? 0 : fy * 0.5 + 0.5; |
| | | fx = float.IsNaN(fx) ? 0 : fx; |
| | | fy = float.IsNaN(fy) ? 0 : fy; |
| | | double dr = Math.Sqrt(Math.Pow(fx, 2) + Math.Pow(fy, 2)); |
| | | |
| | | int r = Convert.ToInt32(dv * 255); |
| | | if (r > 255) |
| | | int r = Convert.ToInt32((dr * 0.5 + 0.5) * 255); |
| | | int g = Convert.ToInt32((fx / dr * 0.5 + 0.5) * 255); |
| | | int b = Convert.ToInt32((fy / dr * 0.5 + 0.5) * 255); |
| | | |
| | | if (r < 0 || r > 255) |
| | | { |
| | | r = 255; |
| | | } |
| | | int g = Convert.ToInt32(dx * 255); |
| | | int b = Convert.ToInt32(dy * 255); |
| | | if (g < 0 || g > 255) g = 255; |
| | | if (b < 0 || b > 255) b = 255; |
| | | |
| | | Color color = Color.FromArgb(127, r, g, b); |
| | | image.SetPixel(x, y, color); |
| | |
| | | /// </summary> |
| | | private static float GetFloatValue(float val) |
| | | { |
| | | //return (float.IsNaN(val) || val < -999) ? float.NaN : val; |
| | | if (float.IsNaN(val) || val < -999) return float.NaN; |
| | | //if (val > 1) return 1; |
| | | //if (val < -1) return -1; |
| | | |
| | | return val; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取XY值 |
| | | /// </summary> |
| | | private static double GetXyValue(float fx, float fy) |
| | | { |
| | | if (float.IsNaN(fx) && float.IsNaN(fy)) |
| | | { |
| | | return 0; |
| | | } |
| | | if (float.IsNaN(fx)) |
| | | { |
| | | return Math.Abs(fy); |
| | | } |
| | | if (float.IsNaN(fy)) |
| | | { |
| | | return Math.Abs(fx); |
| | | } |
| | | |
| | | return Math.Sqrt(Math.Pow(fx, 2) + Math.Pow(fy, 2)); |
| | | return (float.IsNaN(val) || val < -999) ? float.NaN : val; |
| | | } |
| | | #endregion |
| | | |
| | |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | #region 暂时不用 |
| | | /// <summary> |
| | | /// 重采样 * |
| | | /// </summary> |
| | | private static void Resample(string source, string dest, int width, int height) |
| | | { |
| | | // https://blog.51cto.com/u_16099346/6691820 |
| | | string cmd = string.Format("{0}gdalwarp.exe -t_srs {1} -ts {2} {3} -r {4} -of GTiff \"{5}\" \"{6}\"", GdalPath, "EPSG:4326", width, height, "bilinear", source, dest); |
| | | string err = ExecExe(cmd); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 执行命令 |
| | | /// </summary> |
| | | public static string ExecExe(string cmd) |
| | | { |
| | | string str = null; |
| | | Process p = null; |
| | | try |
| | | { |
| | | p = new Process(); |
| | | p.StartInfo.FileName = "cmd.exe"; |
| | | p.StartInfo.UseShellExecute = false; |
| | | p.StartInfo.CreateNoWindow = true; |
| | | p.StartInfo.RedirectStandardInput = true; |
| | | p.StartInfo.RedirectStandardOutput = true; |
| | | p.StartInfo.RedirectStandardError = true; |
| | | p.Start(); |
| | | |
| | | StreamWriter si = p.StandardInput; |
| | | StreamReader se = p.StandardError; |
| | | |
| | | si.AutoFlush = true; |
| | | si.WriteLine(cmd); |
| | | si.WriteLine("exit"); |
| | | |
| | | str = se.ReadToEnd(); |
| | | se.Close(); |
| | | si.Close(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); |
| | | str = ex.Message; |
| | | } |
| | | finally |
| | | { |
| | | if (p != null) p.Close(); |
| | | } |
| | | return str; |
| | | } |
| | | #endregion |
| | | } |
| | | } |