| | |
| | | using LFServer.Models; |
| | | using ExportMap.cs; |
| | | using ExportMap.db; |
| | | using ExportMap.Models; |
| | | using Newtonsoft.Json; |
| | | using System; |
| | | using System.Collections.Generic; |
| | |
| | | using System.Text; |
| | | using System.Web; |
| | | |
| | | namespace LFServer.cs |
| | | namespace ExportMap.cs |
| | | { |
| | | public static class ExportUtil |
| | | { |
| | | /// <summary> |
| | | /// 基目录 |
| | | /// </summary> |
| | | public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory; |
| | | |
| | | /// <summary> |
| | | /// 最大文件数 |
| | | /// </summary> |
| | |
| | | { |
| | | if (string.IsNullOrEmpty(sourcesPath)) |
| | | { |
| | | sourcesPath = Path.Combine(BaseDir, "Sources"); |
| | | sourcesPath = Path.Combine(Tools.BaseDir, "Sources"); |
| | | } |
| | | |
| | | return sourcesPath; |
| | |
| | | } |
| | | |
| | | /// <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); |
| | | |
| | | 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(cmdText); |
| | | si.WriteLine("exit"); |
| | | |
| | | string info = so.ReadToEnd(); |
| | | str = se.ReadToEnd(); |
| | | if (p.HasExited == false) |
| | | { |
| | | p.Kill(); |
| | | } |
| | | |
| | | si.Close(); |
| | | so.Close(); |
| | | se.Close(); |
| | | p.Close(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | str = ex.Message; |
| | | } |
| | | |
| | | return str; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 生成 |
| | | /// </summary> |
| | | /// <param name="args">出图参数</param> |
| | | /// <param name="err">错误信息</param> |
| | | /// <returns>图片路径</returns> |
| | | public static string Generate(ExportArgs args) |
| | | public static string Generate(ExportArgs args, ref string err) |
| | | { |
| | | string date = DateStr; |
| | | string sub = GetExportSubFolder(); |
| | |
| | | |
| | | args.SetDefault(); |
| | | CreateTemplate(args); |
| | | string info = ExecPython(PyFile, qgz, args.qpt); |
| | | |
| | | string cmd = string.Format("python \"{0}\" -qgz {1} -qpt {2}", PyFile, qgz, args.qpt); |
| | | SysTask task = TaskDBHelper.CreateTask(ToXYZArgs(args), "PNG", "在线制图(PNG)"); |
| | | err = Tools.ExecCmd(task, cmd, true, true); |
| | | |
| | | string qptFile = Path.Combine(SourcesPath, args.qpt); |
| | | if (File.Exists(qptFile)) |
| | | { |
| | | File.Delete(qptFile); |
| | | } |
| | | if (File.Exists(qptFile)) File.Delete(qptFile); |
| | | |
| | | string imgPath = Path.Combine(DownloadFolder, args.imgPath); |
| | | bool flag = File.Exists(imgPath); |
| | | |
| | | return flag ? args.imgPath : null; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 转换为XYZ参数 |
| | | /// </summary> |
| | | public static XYZArgs ToXYZArgs(ExportArgs ea) |
| | | { |
| | | XYZArgs args = new XYZArgs(); |
| | | args.name = ea.title; |
| | | args.ids = new List<int>(); |
| | | args.depcode = null; |
| | | args.dircode = null; |
| | | args.userId = TaskDBHelper.SelectUserIdByToken(ea.token); |
| | | |
| | | return args; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | string imgPath = Path.Combine(DownloadFolder, args.imgPath); |
| | | string templateFile = Path.Combine(SourcesPath, "Template.qpt"); |
| | | string qptFile = Path.Combine(SourcesPath, args.qpt); |
| | | if (File.Exists(qptFile)) |
| | | { |
| | | File.Delete(qptFile); |
| | | } |
| | | LogOut.Info("imgPath = " + imgPath + ", tempFile = " + templateFile + ", qptFile = " + qptFile); |
| | | |
| | | string xml = File.ReadAllText(templateFile); |
| | | xml = xml |
| | | .Replace("{dpi}", args.dpi.ToString()) |
| | | .Replace("{title}", args.title) |
| | | .Replace("{sourcesPath}", SourcesPath) |
| | | .Replace("{rotation}", args.rotation.ToString()) |
| | | .Replace("{xmin}", args.xmin.ToString()) |
| | | .Replace("{ymin}", args.ymin.ToString()) |
| | |
| | | .Replace("{province}", args.province) |
| | | .Replace("{scale}", args.scale) |
| | | .Replace("{resolution}", args.resolution) |
| | | .Replace("{date}", args.date) |
| | | //.Replace("{date}", args.date) |
| | | .Replace("{date}", DateTime.Now.ToString("yyyy.MM.dd")) |
| | | .Replace("{layers}", args.layers) |
| | | .Replace("{sourcesPath}", SourcesPath) |
| | | .Replace("{imgPath}", imgPath); |
| | | |
| | | File.WriteAllText(qptFile, xml); |
| | |
| | | |
| | | return rm != null && rm.code == 200 && rm.result; |
| | | } |
| | | catch (Exception ex) |
| | | catch |
| | | { |
| | | return false; |
| | | } |
| | |
| | | /// <returns>数据</returns> |
| | | public static string GetData(string url) |
| | | { |
| | | |
| | | Uri uri = new Uri(url); |
| | | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); |
| | | request.Method = "GET"; |