using LFServer.Models;
|
using System;
|
using System.Collections.Generic;
|
using System.Configuration;
|
using System.Diagnostics;
|
using System.IO;
|
using System.Linq;
|
using System.Web;
|
|
namespace LFServer.cs
|
{
|
public static class ExportUtil
|
{
|
/// <summary>
|
/// 基目录
|
/// </summary>
|
public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
|
|
/// <summary>
|
/// 最大文件数
|
/// </summary>
|
public const int MaxFileCount = 2000;
|
|
private static string sourcesPath;
|
|
/// <summary>
|
/// 获取资源目录
|
/// </summary>
|
public static string SourcesPath
|
{
|
get
|
{
|
if (string.IsNullOrEmpty(sourcesPath))
|
{
|
sourcesPath = Path.Combine(BaseDir, "Sources");
|
}
|
|
return sourcesPath;
|
}
|
}
|
|
private static string pyFile;
|
|
/// <summary>
|
/// 获取Python文件
|
/// </summary>
|
public static string PyFile
|
{
|
get
|
{
|
if (string.IsNullOrWhiteSpace(pyFile))
|
{
|
pyFile = Path.Combine(SourcesPath, "render.py");
|
}
|
|
return pyFile;
|
}
|
}
|
|
/// <summary>
|
/// 获取上期字符串
|
/// </summary>
|
public static string DateStr
|
{
|
get
|
{
|
return DateTime.Now.ToString("yyyyMMddHHmmss");
|
}
|
}
|
|
/// <summary>
|
/// 获取出图目录
|
/// </summary>
|
public static string ExportFolder
|
{
|
get
|
{
|
return ConfigurationManager.AppSettings["exportFolder"];
|
}
|
}
|
|
/// <summary>
|
/// 获取出图子目录
|
/// </summary>
|
public static string GetExportSubFolder()
|
{
|
string root = ExportFolder;
|
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();
|
}
|
|
/// <summary>
|
/// 执行Python
|
/// </summary>
|
/// <param name="py">Python文件</param>
|
/// <param name="qgz">QGIS工程</param>
|
/// <param name="qpt">QGIS模板</param>
|
public static void ExecPython(string py, string qgz, string qpt)
|
{
|
try
|
{
|
string args = string.Format("{0} -qgz {1} -qpt {2}", py, qgz, qpt);
|
|
Process p = new Process();
|
p.StartInfo.UseShellExecute = false;
|
p.StartInfo.ErrorDialog = true;
|
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
p.StartInfo.RedirectStandardError = false;
|
p.StartInfo.FileName = "python";
|
p.StartInfo.Arguments = args;
|
p.StartInfo.CreateNoWindow = true;
|
|
p.Start();
|
}
|
catch
|
{
|
}
|
}
|
|
/// <summary>
|
/// 生成
|
/// </summary>
|
/// <param name="args">出图参数</param>
|
public static void Generate(ExportArgs args)
|
{
|
string date = DateStr;
|
string sub = GetExportSubFolder();
|
string qgz = "Test.qgz";
|
|
args.imgPath = sub + "/" + date + ".png";
|
args.qpt = date + ".qpt";
|
|
CreateTemplate(args);
|
|
ExecPython(PyFile, qgz, args.qpt);
|
}
|
|
/// <summary>
|
/// 创建模板
|
/// </summary>
|
/// <param name="args">出图参数</param>
|
public static void CreateTemplate(ExportArgs args)
|
{
|
string imgPath = Path.Combine(ExportFolder, args.imgPath);
|
string templateFile = Path.Combine(SourcesPath, "Test.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("{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);
|
}
|
}
|
}
|