管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2024-07-23 379a98780e346a793c5dae062efae417ade0117d
SimuTools/Tools/Handle.cs
@@ -15,6 +15,7 @@
{
    public class Handle
    {
        #region 成员变量
        public static readonly int MAX = 256 * 256;
        public static readonly double WaterHeightOffset = 1.0;
@@ -22,6 +23,7 @@
        public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
        public static readonly string GdalPath = ConfigurationManager.AppSettings["gdalPath"];
        #endregion
        /// <summary>
        /// 运行
@@ -147,57 +149,6 @@
            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>
@@ -525,16 +476,14 @@
                    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;
                    if (double.IsNaN(rv) || rv == 0) continue;
                    //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);
                    double dv = GetXyValue(fx, fy, rv);
                    int r = Convert.ToInt32(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;
@@ -558,7 +507,7 @@
        /// </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);
@@ -572,8 +521,6 @@
        /// </summary>
        private static double GetXyValue(float fx, float fy, double rv)
        {
            if ((float.IsNaN(fx) && float.IsNaN(fy)) || double.IsNaN(rv)) return 0;
            if (float.IsNaN(fx))
            {
                return fy / rv;
@@ -583,9 +530,9 @@
                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
@@ -606,5 +553,58 @@
            }
        }
        #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
    }
}