管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-09-03 3cfb6aa02516135fb174ab1b30620f2007924663
ExportMap/cs/Tools.cs
@@ -6,6 +6,7 @@
using System.Configuration;
using System.Data.Common;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
@@ -16,7 +17,7 @@
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Web;
using System.Web.WebSockets;
namespace ExportMap.cs
{
@@ -26,6 +27,17 @@
        /// 基础目录
        /// </summary>
        public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 获取令牌
        /// </summary>
        public static string Token
        {
            get
            {
                return "c36e4f94-dfde-401e-9967-2c4a449f1300";
            }
        }
        private static string tempDir;
@@ -60,10 +72,23 @@
            {
                if (null == _dbHelper)
                {
                    _dbHelper = new PostgreHelper(DbEnum.langfang);
                    _dbHelper = new PostgreHelper();
                }
                return _dbHelper;
            }
        }
        /// <summary>
        /// 获取Py初始化参数
        /// </summary>
        public static string[] PyInitArgs
        {
            get
            {
                string args = GetSetting("pyInitArgs");
                return args.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            }
        }
@@ -124,7 +149,7 @@
        }
        /// <summary>
        /// 创建目录
        /// 创建目录 *
        /// </summary>
        public static void CreateDirectory(string dir)
        {
@@ -138,7 +163,7 @@
        }
        /// <summary>
        /// 克隆对象
        /// 克隆对象 *
        /// </summary>
        public static T Clone<T>(T source) where T : new()
        {
@@ -176,8 +201,9 @@
            List<string> list = new List<string>();
            if (isPy)
            {
                list.Add("cd \"C:\\Program Files\\QGIS 3.16\\apps\\Python37\"");
                list.Add("\"C:\\Program Files\\QGIS 3.16\\bin\\qgis_process-qgis-ltr.bat\"");
                //list.Add("cd \"C:\\Program Files\\QGIS 3.16\\apps\\Python37\"");
                //list.Add("\"C:\\Program Files\\QGIS 3.16\\bin\\qgis_process-qgis-ltr.bat\"");
                list.AddRange(PyInitArgs);
                //list.Add("\"C:\\Program Files\\QGIS 3.16\\bin\\python-qgis-ltr.bat\"");
                //list.Add("exit()");
            }
@@ -197,7 +223,7 @@
        /// <returns>执行结果或出错信息</returns>
        public static string ExecCmd(SysTask task, List<string> list, bool isOut = false)
        {
            string str = null;
            string str;
            try
            {
                Process p = new Process();
@@ -304,7 +330,95 @@
                if (p != null)
                {
                    p.Close();
                    p = null;
                }
            }
            return str;
        }
        /// <summary>
        /// 执行命令
        /// </summary>
        public static string ExecCmd(List<string> list, ref string rs)
        {
            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 so = p.StandardOutput;
                StreamReader se = p.StandardError;
                LogOut.Info("cmd = " + string.Join(",", list));
                si.AutoFlush = true;
                foreach (string cmd in list)
                {
                    si.WriteLine(cmd);
                }
                si.WriteLine("exit");
                rs = so.ReadToEnd();
                str = se.ReadToEnd();
                se.Close();
                so.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>
        /// 执行命令
        /// </summary>
        /// <param name="list">命令集合</param>
        public static string ExecExe(string exe, string args, bool noWin = true)
        {
            string str = null;
            Process p = null;
            try
            {
                p = new Process();
                p.StartInfo.FileName = exe;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = noWin;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                if (!string.IsNullOrEmpty(args)) p.StartInfo.Arguments = args;
                p.Start();
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                str = ex.Message;
            }
            finally
            {
                if (p != null)
                {
                    p.Close();
                }
            }
            return str;
@@ -345,6 +459,30 @@
        }
        /// <summary>
        /// 获取EPSG代码
        /// </summary>
        public static string GetEPSG(string file)
        {
            string gdalPath = GetSetting("gdalPath");
            string cmd = string.Format("{0}\\gdalsrsinfo.exe \"{1}\" -o epsg", gdalPath, file);
            string rs = null;
            string ostr = ExecCmd(new List<string> { cmd }, ref rs);
            if (string.IsNullOrEmpty(rs)) return null;
            string[] strs = rs.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str in strs)
            {
                if (str.Contains("EPSG:"))
                {
                    return str;
                }
            }
            return null;
        }
        /// <summary>
        /// 设置单体模型参数
        /// </summary>
        public static void SetIsModel(XYZArgs args, List<SysMeta> list)