管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-03-21 9d9f97954c5eef9ee909eda39f082e0f19247111
ExportMap/cs/XYZUtils.cs
@@ -43,6 +43,29 @@
        }
        /// <summary>
        /// bat路径
        /// </summary>
        public static string BatPath
        {
            get
            {
                return @"C:\Program Files\QGIS 3.16\bin\";
            }
        }
        /// <summary>
        /// 基础bat文件
        /// </summary>
        public static string BaseBat
        {
            get
            {
                // python-qgis-ltr.bat,qgis_process-qgis-ltr.bat
                return @"C:\Program Files\QGIS 3.16\bin\qgis_process-qgis-ltr.bat";
            }
        }
        /// <summary>
        /// 获取发布地址
        /// </summary>
        public static string GetReleaseUrl(string dircode)
@@ -58,10 +81,12 @@
        /// <returns>数据发布ID</returns>
        public static int Generate(XYZArgs args, ref string err)
        {
            List<SysMeta> list = selectMetas(args.ids, "and type in ('tif', 'tiff', 'img')");
            List<SysMeta> list = SelectMetas(args.ids, "and type in ('tif', 'tiff', 'img')");
            if (null == list || list.Count == 0) return 0;
            string tifFile = Path.Combine(Tool.TempDir, ExportUtil.DateStr + ".txt");
            string dateStr = ExportUtil.DateStr;
            string batFile = Path.Combine(BatPath, dateStr + ".bat");
            string tifFile = Path.Combine(Tools.TempDir, ExportUtil.DateStr + ".txt");
            string xyzPath = Path.Combine(SGUtils.LFData, "2d\\tiles", args.dircode);
            if (!Directory.Exists(xyzPath)) Directory.CreateDirectory(xyzPath);
@@ -80,10 +105,17 @@
            //string newPy = tifFile.Replace(".txt", ".py").Replace("\\", "\\\\");
            //File.WriteAllText(newPy, pyText);
            //string cmd = string.Format("exec(open('{0}', 'r', encoding='utf-8').read()) & exit()", newPy);
            //err = ExecCmd(cmd);
            err = ExecCmd(cmd);
            //List<string> cmds = GetCmds(cmd);
            //err = Tools.ExecCmd(cmds);
            List<string> cmds = GetCmds(batFile, cmd);
            err = Tools.ExecCmd(cmds);
            if (File.Exists(tifFile)) File.Delete(tifFile);
            if (File.Exists(batFile)) File.Delete(batFile);
            string viewFile = Path.Combine(xyzPath, "view.html");
            if (File.Exists(viewFile))
            {
@@ -96,12 +128,23 @@
        }
        /// <summary>
        /// 获取命令行
        /// </summary>
        private static List<string> GetCmds(string batFile, string cmd)
        {
            string str = File.ReadAllText(BaseBat) + cmd;
            File.WriteAllText(batFile, str);
            return new List<string>() { "\"" + batFile + "\"" };
        }
        /// <summary>
        /// 查询元数据
        /// </summary>
        public static List<SysMeta> selectMetas(List<int> ids, string types = "")
        public static List<SysMeta> SelectMetas(List<int> ids, string types = "")
        {
            string sql = string.Format("select * from lf.sys_meta where id in ({0}) {1} order by id", string.Join(",", ids), types);
            DataTable dt = Tool.DBHelper.GetDataTable(sql);
            DataTable dt = Tools.DBHelper.GetDataTable(sql);
            List<SysMeta> list = ModelHandler.FillModel<SysMeta>(dt);
            return list;
@@ -112,70 +155,19 @@
        /// </summary>
        private static void WriteText(string file, List<SysMeta> list)
        {
            string uploadFolder = Tool.GetSetting("uploadFolder");
            string uploadFolder = Tools.GetSetting("uploadFolder");
            List<string> files = new List<string>();
            foreach (SysMeta meta in list)
            {
                string filePath = Path.Combine(uploadFolder, meta.path);
                if (File.Exists(filePath)) files.Add(filePath);
            }
            //foreach (SysMeta meta in list)
            //{
            //    string filePath = Path.Combine(uploadFolder, meta.path);
            //    if (File.Exists(filePath)) files.Add(filePath);
            //}
            files.Add(@"E:\01.Data\33.DOM\2.大庆DOM\5154.50-415.50.tif");
            files.Add(@"E:\01.Data\33.DOM\2.大庆DOM\5154.50-416.00.tif");
            string str = string.Join("\n", files);
            File.WriteAllText(file, str);
        }
        /// <summary>
        /// 执行CMD
        /// </summary>
        /// <param name="cmd">命令行</param>
        /// <returns>执行结果或出错信息</returns>
        public static String ExecCmd(string cmd)
        {
            string str = null;
            try
            {
                Process 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 = " + cmd);
                si.AutoFlush = true;
                si.WriteLine("cd \"C:\\Program Files\\QGIS 3.16\\apps\\Python37\"");
                si.WriteLine("\"C:\\Program Files\\QGIS 3.16\\bin\\qgis_process-qgis-ltr.bat\"");
                //si.WriteLine("\"C:\\Program Files\\QGIS 3.16\\bin\\python-qgis-ltr.bat\"");
                //si.WriteLine("exit()");
                si.WriteLine(cmd);
                si.WriteLine("exit");
                string info = so.ReadToEnd();
                str = se.ReadToEnd();
                if (!string.IsNullOrEmpty(info)) LogOut.Debug(info);
                if (!string.IsNullOrEmpty(str)) LogOut.Error(str);
                if (p.HasExited == false) p.Kill();
                se.Close();
                so.Close();
                si.Close();
                p.Close();
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.StackTrace);
                str = ex.Message;
            }
            return str;
        }
        /// <summary>