| | |
| | | { |
| | | if (string.IsNullOrWhiteSpace(pyFile)) |
| | | { |
| | | pyFile = Path.Combine(ExportUtil.SourcesPath, "xyz.py"); |
| | | pyFile = Path.Combine(ExportUtil.SourcesPath, "xyz2.py"); |
| | | } |
| | | |
| | | return pyFile; |
| | |
| | | } |
| | | |
| | | /// <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> |
| | | /// 获取发布地址:http://localhost/ExportMap/tile0/{z}/{x}/{y}.png?path=2d\tiles\0102 |
| | | /// </summary> |
| | | public static string GetReleaseUrl(string dircode) |
| | | { |
| | | //return "http://{host}/LFData/2d/tiles/" + dircode + "/{z}/{x}/{y}.png"; |
| | | return "{host}/ExportMap/tile0/{z}/{x}/{y}.png?path=2d/tiles/" + dircode; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 生成 |
| | | /// </summary> |
| | | /// <param name="args">XYZ参数</param> |
| | | /// <param name="err">错误信息</param> |
| | | /// <returns>数据发布ID</returns> |
| | | public static int Generate(XYZArgs args, ref string err) |
| | | /// <returns>数据发布ID集合</returns> |
| | | public static List<int> Generate(XYZArgs args, ref string err) |
| | | { |
| | | string tifFile = Path.Combine(Tool.TempDir, ExportUtil.DateStr + ".txt"); |
| | | string xyzPath = Path.Combine(SGUtils.LFData, "2d\\tiles", args.id.ToString()); |
| | | List<SysMeta> 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 txtFile = Path.Combine(Tools.TempDir, ExportUtil.DateStr + ".txt"); |
| | | string xyzPath = Path.Combine(SGUtils.LFData, "2d\\tiles", args.dircode); |
| | | //if (args.isNew && Directory.Exists(xyzPath)) Tools.DelPath(xyzPath); // 已存在的,删除 |
| | | if (!Directory.Exists(xyzPath)) Directory.CreateDirectory(xyzPath); |
| | | |
| | | List<SysMeta> list = selectMetas(args.ids, "and type in ('tif', 'tiff', 'img')"); |
| | | if (null == list || list.Count == 0) return 0; |
| | | int pubid = PubDBHelper.GetPushlishId(args.dircode, "DOM"); |
| | | List<int> mids = PubDBHelper.GetPublishMetaId(pubid); |
| | | WriteText(txtFile, list, mids); |
| | | |
| | | WriteText(tifFile, list); |
| | | string cmd = string.Format("python \"{0}\" -qgz {1} -file \"{2}\" -out \"{3}\" -min {4} -max {5} -noData {6}", PyFile, Qgz, txtFile, xyzPath, args.min, args.max, args.noData); |
| | | SysTask task = TaskDBHelper.CreateTask(args, "DOM", "影像数据(DOM)"); |
| | | err = Tools.ExecCmd(task, cmd, true); |
| | | |
| | | string cmd = string.Format("python \"{0}\" -qgz {1} -file \"{2}\" -out \"{3}\" -min {4} -max {5}", PyFile, Qgz, tifFile, xyzPath, args.min, args.max); |
| | | //if (File.Exists(batFile)) File.Delete(batFile); |
| | | if (File.Exists(txtFile)) File.Delete(txtFile); |
| | | |
| | | //string pyText = File.ReadAllText(PyFile); |
| | | //pyText = pyText |
| | | // .Replace(@"E:\terrait\TianJin\ExportMap\ExportMap\Sources\xyz.qgz", Qgz) |
| | | // .Replace(@"D:\xyz\zy.txt", tifFile) |
| | | // .Replace(@"D:\xyz\tiles\zy", xyzPath) |
| | | // .Replace("=12,", "=" + args.min + ",") |
| | | // .Replace("=15,", "=" + args.max + ","); |
| | | |
| | | //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); |
| | | |
| | | if (File.Exists(tifFile)) File.Delete(tifFile); |
| | | |
| | | List<int> ids = new List<int>(); |
| | | string viewFile = Path.Combine(xyzPath, "view.html"); |
| | | if (File.Exists(viewFile)) |
| | | { |
| | | string path = "2d\\tiles" + "\\" + args.dircode; |
| | | pubid = InsertToDB(list, args, path); |
| | | |
| | | return File.Exists(viewFile) ? args.id : 0; |
| | | if (pubid > 0) ids.Add(pubid); |
| | | } |
| | | |
| | | return ids; |
| | | } |
| | | |
| | | /// <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); |
| | | 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 desc", string.Join(",", ids), types); |
| | | DataTable dt = Tools.DBHelper.GetDataTable(sql); |
| | | List<SysMeta> list = ModelHandler.FillModel<SysMeta>(dt); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据ID查询元数据 |
| | | /// </summary> |
| | | public static SysMeta SelectMeta(int id) |
| | | { |
| | | string sql = string.Format("select a.*, fn_get_fullname(dircode, 2) dirname from lf.sys_meta a where id = {0}", id); |
| | | |
| | | DataTable dt = Tools.DBHelper.GetDataTable(sql); |
| | | List<SysMeta> list = ModelHandler.FillModel<SysMeta>(dt); |
| | | |
| | | return null == list || list.Count == 0 ? null : list[0]; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 写文本文件 |
| | | /// </summary> |
| | | private static void WriteText(string file, List<SysMeta> list) |
| | | private static void WriteText(string file, List<SysMeta> list, List<int> mids) |
| | | { |
| | | 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); |
| | | if (!File.Exists(filePath)) continue; |
| | | |
| | | bool exists = mids.Contains(meta.id); |
| | | files.Add(string.Format("{0},{1}", filePath, exists ? 1 : 0)); |
| | | } |
| | | string str = string.Join("\n", files); |
| | | //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("\r\n", files); |
| | | |
| | | File.WriteAllText(file, str); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 执行CMD |
| | | /// 插入数据库 |
| | | /// </summary> |
| | | /// <param name="cmd">命令行</param> |
| | | /// <returns>执行结果或出错信息</returns> |
| | | public static String ExecCmd(string cmd) |
| | | private static int InsertToDB(List<SysMeta> metas, XYZArgs args, string path) |
| | | { |
| | | string str = null; |
| | | try |
| | | //if (PubDBHelper.IsPublish(args.dircode, "DOM")) return 1; |
| | | int pubid = PubDBHelper.GetPushlishId(args.dircode, "DOM"); |
| | | if (pubid > 0) // 更新发布 |
| | | { |
| | | 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(); |
| | | List<int> ids = PubDBHelper.GetPublishMetaId(pubid); |
| | | foreach (SysMeta m in metas) |
| | | { |
| | | if (!ids.Contains(m.id)) PubDBHelper.InsertMetaPub(m.id, pubid, args.userId); |
| | | } |
| | | string geom = GetPointZ(args); |
| | | PubDBHelper.UpdatePublish(pubid, args.name, args.userId, geom); |
| | | |
| | | 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 pubid; |
| | | } |
| | | |
| | | return str; |
| | | SysMeta meta = metas[0]; |
| | | meta.type = "DOM"; |
| | | meta.name = args.name; |
| | | meta.dircode = args.dircode; |
| | | |
| | | SysPublish sys = Tools.NewPublish(meta, args, GetReleaseUrl(args.dircode), path); |
| | | sys.geom = GetPointZ(args); |
| | | |
| | | pubid = PubDBHelper.InsertPublish(sys); |
| | | if (pubid > 0) |
| | | { |
| | | sys.id = pubid; |
| | | PubDBHelper.InsertLayer(sys, new SysMeta() |
| | | { |
| | | name = args.name, |
| | | type = meta.type, |
| | | dirname = meta.dirname |
| | | }); |
| | | foreach (SysMeta m in metas) |
| | | { |
| | | PubDBHelper.InsertMetaPub(m.id, pubid, args.userId); |
| | | } |
| | | } |
| | | |
| | | return pubid; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取中心点 |
| | | /// </summary> |
| | | 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]); |
| | | } |
| | | } |
| | | } |