using ExportMap.cs; using LFServer.Models; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; namespace LFServer.cs { public static class ExportUtil { /// /// 基目录 /// public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory; /// /// 最大文件数 /// public const int MaxFileCount = 2000; private static string sourcesPath; /// /// 获取资源目录 /// public static string SourcesPath { get { if (string.IsNullOrEmpty(sourcesPath)) { sourcesPath = Path.Combine(BaseDir, "Sources"); } return sourcesPath; } } private static string pyFile; /// /// 获取Python文件 /// public static string PyFile { get { if (string.IsNullOrWhiteSpace(pyFile)) { pyFile = Path.Combine(SourcesPath, "render.py"); } return pyFile; } } /// /// 获取上期字符串 /// public static string DateStr { get { return DateTime.Now.ToString("yyyyMMddHHmmss"); } } /// /// 获取下载目录 /// public static string DownloadFolder { get { return ConfigurationManager.AppSettings["downloadFolder"]; } } /// /// 后台服务地址 /// public static string LFServer { get { return ConfigurationManager.AppSettings["lfServer"]; } } /// /// 获取出图子目录 /// public static string GetExportSubFolder() { string root = DownloadFolder; if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } int i = 1; while (true) { string subFolder = Path.Combine(root, i.ToString()); if (!Directory.Exists(subFolder)) { Directory.CreateDirectory(subFolder); break; } DirectoryInfo dir = new DirectoryInfo(subFolder); FileInfo[] files = dir.GetFiles(); if (files == null || files.Length < MaxFileCount) { break; } i++; } return i.ToString(); } /// /// 执行Python /// /// Python文件 /// QGIS工程 /// QGIS模板 /// 执行结果 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 (!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; } /// /// 生成 /// /// 出图参数 /// 错误信息 /// 图片路径 public static string Generate(ExportArgs args, ref string err) { string date = DateStr; string sub = GetExportSubFolder(); string qgz = "Test.qgz"; args.imgPath = sub + "\\" + date + ".png"; args.qpt = date + ".qpt"; args.SetDefault(); CreateTemplate(args); err = ExecPython(PyFile, qgz, args.qpt); string qptFile = Path.Combine(SourcesPath, args.qpt); if (File.Exists(qptFile)) { File.Delete(qptFile); } string imgPath = Path.Combine(DownloadFolder, args.imgPath); bool flag = File.Exists(imgPath); return flag ? args.imgPath : null; } /// /// 创建模板 /// /// 出图参数 public static void CreateTemplate(ExportArgs args) { 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); } 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("{ymax}", args.ymax.ToString()) .Replace("{xmax}", args.xmax.ToString()) .Replace("{province}", args.province) .Replace("{scale}", args.scale) .Replace("{resolution}", args.resolution) .Replace("{date}", args.date) .Replace("{layers}", args.layers) .Replace("{imgPath}", imgPath); File.WriteAllText(qptFile, xml); } /// /// 验证令牌 /// /// 令牌 /// 是/否有效 public static bool VerifyToken(string token) { try { string url = LFServer + "/sign/check?token=" + token.Trim(); string json = GetData(url); if (string.IsNullOrWhiteSpace(json)) { return false; } ResponseMsg rm = JsonConvert.DeserializeObject>(json); return rm != null && rm.code == 200 && rm.result; } catch { return false; } } /// /// Get获取数据 /// /// URL /// 数据 public static string GetData(string url) { Uri uri = new Uri(url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string str = reader.ReadToEnd(); reader.Close(); return str; } } }