using ExportMap.db; using ExportMap.Models; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.Web; namespace ExportMap.cs { public class XYZUtils { private static string pyFile; /// /// 获取Python文件 /// public static string PyFile { get { if (string.IsNullOrWhiteSpace(pyFile)) { pyFile = Path.Combine(ExportUtil.SourcesPath, "xyz.py"); } return pyFile; } } /// /// QGIS工程 /// public static string Qgz { get { //return Path.Combine(ExportUtil.SourcesPath, "xyz.qgz"); return "xyz.qgz"; } } /// /// bat路径 /// public static string BatPath { get { return @"C:\Program Files\QGIS 3.16\bin\"; } } /// /// 基础bat文件 /// 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"; } } /// /// 获取发布地址 /// public static string GetReleaseUrl(string dircode) { return "http://{host}/LFData/2d/tiles/" + dircode + "/{z}/{x}/{y}.png"; } /// /// 生成 /// /// XYZ参数 /// 错误信息 /// 数据发布ID集合 public static List Generate(XYZArgs args, ref string err) { List list = SelectMetas(args.ids, "and type in ('tif', 'tiff', 'img', 'png', 'jpg')"); if (null == list || list.Count == 0) return null; 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); WriteText(tifFile, list); string cmd = string.Format("python \"{0}\" -qgz {1} -file \"{2}\" -out \"{3}\" -min {4} -max {5} -noData {6}", PyFile, Qgz, tifFile, xyzPath, args.min, args.max, args.noData); //err = Tools.ExecCmd(GetCmds(batFile, cmd)); err = Tools.ExecCmd(cmd, true); //if (File.Exists(batFile)) File.Delete(batFile); if (File.Exists(tifFile)) File.Delete(tifFile); List ids = new List(); string viewFile = Path.Combine(xyzPath, "view.html"); if (File.Exists(viewFile)) { string path = "2d\\tiles" + "\\" + args.dircode; int pubid = InsertToDB(list, args, path); if (pubid > 0) ids.Add(pubid); } return ids; } /// /// 获取命令行 /// private static List GetCmds(string batFile, string cmd) { string str = File.ReadAllText(BaseBat) + cmd; File.WriteAllText(batFile, str); return new List() { "\"" + batFile + "\"" }; } /// /// 查询元数据 /// public static List SelectMetas(List ids, string types = "") { string sql = string.Format("select a.*, fn_get_fullname(dircode, 2) dirname from lf.sys_meta a where id in ({0}) {1} order by id", string.Join(",", ids), types); DataTable dt = Tools.DBHelper.GetDataTable(sql); List list = ModelHandler.FillModel(dt); return list; } /// /// 写文本文件 /// private static void WriteText(string file, List list) { string uploadFolder = Tools.GetSetting("uploadFolder"); List files = new List(); 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); } /// /// 插入数据库 /// private static int InsertToDB(List metas, XYZArgs args, string path) { if (PubDBHelper.IsPublish(args.dircode)) return 0; SysPublish sys = NewPublish(metas[0], args); sys.path = path; int pubid = PubDBHelper.InsertPublish(sys); if (pubid > 0) { sys.id = pubid; PubDBHelper.InsertLayer(sys, new SysMeta() { name = args.name, type = metas[0].type, dirname = metas[0].dirname }); foreach (SysMeta meta in metas) { PubDBHelper.InsertMetaPub(meta.id, pubid, args.userId); } } return pubid; } /// /// 创建数据发布类 /// private static SysPublish NewPublish(SysMeta meta, XYZArgs args) { SysPublish sp = new SysPublish(); sp.name = args.name; sp.url = GetReleaseUrl(args.dircode); sp.type = "DOM"; sp.status = 3; sp.dirid = args.dircode; sp.depid = args.depcode; sp.min = args.min; sp.max = args.max; sp.json = null; sp.create_user = args.userId; sp.geom = GetPointZ(args); sp.bak = null; return sp; } /// /// 获取中心点 /// private static string GetPointZ(XYZArgs args) { string viewFile = Path.Combine(SGUtils.LFData, "2d\\tiles", args.dircode, "view.html"); if (!File.Exists(viewFile)) return null; string str = File.ReadAllText(viewFile); int start = str.IndexOf(".setView([") + ".setView([".Length; int end = str.IndexOf(");", start); string coords = str.Substring(start, end - start).Replace("]", "").Replace(" ", ""); string[] strs = coords.Split(new char[] { ',' }); return string.Format("ST_GeomFromText('POINT Z ({0} {1} {2})')", strs[0], strs[1], strs[2]); } } }