using ExportMap.db;
using ExportMap.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace ExportMap.cs
{
public class TerraUtils
{
private static string pyFile;
///
/// 获取Python文件
///
public static string PyFile
{
get
{
if (string.IsNullOrWhiteSpace(pyFile))
{
pyFile = Path.Combine(ExportUtil.SourcesPath, "merge.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/3d/terrain/" + dircode;
}
///
/// 获取地形路径
///
public static string GetTerrainPath(string dircode)
{
return Path.Combine(SGUtils.LFData, "3d\\terrain", dircode);
}
///
/// 生成
///
/// XYZ参数
/// 错误信息
/// 数据发布ID集合
public static List Generate(XYZArgs args, ref string err)
{
string tifFile = null;
try
{
List metas = XYZUtils.SelectMetas(args.ids, "and type in ('tif', 'tiff')");
if (null == metas || metas.Count == 0) return null;
tifFile = Merge(metas, args, ref err);
if (!File.Exists(tifFile)) return null;
Generate(args, tifFile, ref err);
string path = GetTerrainPath(args.dircode);
string json = Path.Combine(path, "layer.json");
if (!File.Exists(json)) return null;
List ids = new List();
int pubid = InsertToDB(metas, args);
if (pubid > 0) ids.Add(pubid);
return ids;
}
catch (Exception ex)
{
LogOut.Error(ex.Message);
err = ex.Message;
return null;
}
finally
{
if (!string.IsNullOrEmpty(tifFile) && File.Exists(tifFile))
{
File.Delete(tifFile);
}
}
}
///
/// 合并
///
public static string Merge(List metas, XYZArgs args, ref string err)
{
string txtFile = null;
try
{
txtFile = Path.Combine(Tools.TempDir, ExportUtil.DateStr + ".txt");
string dirPath = GetTerrainPath(args.dircode);
if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
string tifFile = Path.Combine(dirPath, args.dircode + ".tif");
if (File.Exists(tifFile)) File.Delete(tifFile);
WriteText(txtFile, metas);
string cmd = string.Format("python \"{0}\" -qgz {1} -file \"{2}\" -out \"{3}\"", PyFile, Qgz, txtFile, tifFile);
err = Tools.ExecCmd(cmd, true);
return tifFile;
}
catch (Exception ex)
{
LogOut.Error(ex.Message);
err = ex.Message;
return null;
}
finally
{
if (!string.IsNullOrEmpty(txtFile) && File.Exists(txtFile))
{
File.Delete(txtFile);
}
}
}
///
/// 写文本文件
///
private static void WriteText(string txtFile, 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);
}
string str = string.Join("\n", files);
File.WriteAllText(txtFile, str);
}
///
/// 生成高程切片
///
private static void Generate(XYZArgs args, string tifFile, ref string err)
{
string dirPath = GetTerrainPath(args.dircode).Replace("\\", "/");
string name = "ctb_" + ExportUtil.DateStr;
string runDocker = string.Format("docker run -id --name {0} -v \"{1}\":\"/data\" tumgis/ctb-quantized-mesh", name, dirPath);
string createMesh = string.Format("docker exec {0} ctb-tile -f Mesh -C -N -s {1} -e {2} -o /data /data/{3}.tif", name, args.max, args.min, args.dircode);
string createLayer = string.Format("docker exec {0} ctb-tile -f Mesh -C -N -s {1} -e {2} -l -o /data /data/{3}.tif", name, args.max, args.min, args.dircode);
string stop = string.Format("docker stop {0}", name);
string rm = string.Format("docker rm {0}", name);
List list = new List { runDocker, createMesh, createLayer, stop, rm };
err = Tools.ExecCmd(list);
}
///
/// 插入数据库
///
private static int InsertToDB(List metas, XYZArgs args)
{
if (PubDBHelper.IsPublish(args.dircode, "DEM")) return 0;
metas[0].type = "DEM";
SysPublish sys = NewPublish(metas[0], args);
sys.path = "3d\\terrain\\" + args.dircode;
int pubid = PubDBHelper.InsertPublish(sys);
if (pubid > 0)
{
}
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 = meta.type;
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 = null;
sp.bak = null;
return sp;
}
}
}