| | |
| | | { |
| | | 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> |
| | | /// 运行 |
| | |
| | | |
| | | 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> |
| | |
| | | int offset = x + y * width; |
| | | float fx = GetFloatValue(vxBuffer[offset]); |
| | | float fy = GetFloatValue(vyBuffer[offset]); |
| | | |
| | | double rv = GetRVal(fx, fy); |
| | | double dv = GetXyValue(fx, fy, rv); |
| | | int r = Convert.ToInt32(double.IsNaN(dv) || dv == 0 ? 0 : (dv * 0.5 + 0.5) * 255); |
| | | if (r < 0 || r > 255) r = 255; |
| | | |
| | | //int g = Convert.ToInt32((float.IsNaN(fx) ? 0 : fx * 0.5 + 0.5) * 255); |
| | | //int b = Convert.ToInt32((float.IsNaN(fy) ? 0 : fy * 0.5 + 0.5) * 255); |
| | | int r= Convert.ToInt32(double.IsNaN(dv) || dv == 0 ? 0 : (dv * 0.5 + 0.5) * 255); |
| | | int g = float.IsNaN(fx) ? 0 : Convert.ToInt32((fx - minHeight) / perHeight); |
| | | int b = float.IsNaN(fy) ? 0 : Convert.ToInt32((fy - minHeight) / perHeight); |
| | | if (r < 0 || r > 255) r = 255; |
| | | if (g < 0 || g > 255) g = 255; |
| | | if (b < 0 || b > 255) b = 255; |
| | | |
| | |
| | | /// </summary> |
| | | private static double GetRVal(float fx, float fy) |
| | | { |
| | | if (float.IsNaN(fx) && float.IsNaN(fy)) return 1.0; |
| | | if (float.IsNaN(fx) && float.IsNaN(fy)) return double.NaN; |
| | | |
| | | if (float.IsNaN(fx)) return Math.Abs(fy); |
| | | |
| | |
| | | return fx / rv; |
| | | } |
| | | |
| | | double v = Math.Sqrt(Math.Pow(fx / rv, 2) + Math.Pow(fy / rv, 2)); |
| | | double dv = fx / rv + fy / rv; |
| | | |
| | | return (fx > 0 && fy > 0) || (fx < 0 && fy < 0) ? v : -v; |
| | | return dv < 0 ? -Math.Sqrt(-dv) : Math.Sqrt(dv); |
| | | } |
| | | #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 |
| | | } |
| | | } |