| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 执行Python |
| | | /// </summary> |
| | | /// <param name="py">Python文件</param> |
| | | /// <param name="qgz">QGIS工程</param> |
| | | /// <param name="qpt">QGIS模板</param> |
| | | /// <returns>执行结果</returns> |
| | | public static string ExecPython(string py, string qgz, string qpt) |
| | | { |
| | | string str = null; |
| | | try |
| | | { |
| | | string cmdText = string.Format("python {0} -qgz {1} -qpt {2}", py, qgz, qpt); |
| | | LogOut.Info("cmd = " + cmdText); |
| | | |
| | | 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; // 标准错误流 |
| | | |
| | | si.AutoFlush = true; |
| | | //si.WriteLine("cd \"C:\\Program Files\\QGIS 3.16\\apps\\Python37\""); |
| | | //si.WriteLine("set PYTHONHOME=\"C:\\Program Files\\QGIS 3.16\\apps\\Python37\""); |
| | | //si.WriteLine("set PYTHONPATH=\"C:\\Program Files\\QGIS 3.16\\apps\\qgis-ltr\\python;C:\\Program Files\\QGIS 3.16\\apps\\qgis-ltr\\bin;C:\\Program Files\\QGIS 3.16\\apps\\qgis-ltr\\python\\qgis;C:\\Program Files\\QGIS 3.16\\apps\\Python37\\Lib;C:\\Program Files\\QGIS 3.16\\apps\\Python37\\Lib\\site-packages;C:\\Program Files\\QGIS 3.16\\apps\\Python37\\DLLs;C:\\Program Files\\QGIS 3.16\\apps\\Python37\\Scripts;\""); |
| | | //si.WriteLine("set Path=\"C:\\Program Files\\QGIS 3.16\\apps\\Python37;C:\\Program Files\\QGIS 3.16\\bin;C:\\Program Files\\QGIS 3.16\\apps\\qgis-ltr\\\\bin\";%Path%"); |
| | | //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(cmdText); |
| | | 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(); |
| | | |
| | | si.Close(); |
| | | so.Close(); |
| | | se.Close(); |
| | | p.Close(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogOut.Error(ex.StackTrace); |
| | | str = ex.Message; |
| | | } |
| | | |
| | | return str; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 生成 |
| | | /// </summary> |
| | | /// <param name="args">出图参数</param> |
| | |
| | | |
| | | args.SetDefault(); |
| | | CreateTemplate(args); |
| | | err = ExecPython(PyFile, qgz, args.qpt); |
| | | |
| | | string cmd = string.Format("python {0} -qgz {1} -qpt {2}", PyFile, qgz, args.qpt); |
| | | err = Tool.ExecPython(cmd); |
| | | |
| | | string qptFile = Path.Combine(SourcesPath, args.qpt); |
| | | if (File.Exists(qptFile)) |