管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2022-11-10 00700f5ef12d33da898e9b4391aa5b4a09828ee3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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>
        /// <returns>执行结果</returns>
        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 (p.HasExited == false)
                {
                    p.Kill();
                }
 
                si.Close();
                so.Close();
                se.Close();
                p.Close();
            }
            catch (Exception ex)
            {
                str = ex.Message;
            }
 
            return str;
        }
 
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="args">出图参数</param>
        /// <returns>图片路径</returns>
        public static string Generate(ExportArgs args)
        {
            string date = DateStr;
            string sub = GetExportSubFolder();
            string qgz = "Test.qgz";
 
            args.imgPath = sub + "\\" + date + ".png";
            args.qpt = date + ".qpt";
 
            args.SetDefault();
            CreateTemplate(args);
            string info = ExecPython(PyFile, qgz, args.qpt);
 
            string qptFile = Path.Combine(SourcesPath, args.qpt);
            if (File.Exists(qptFile))
            {
                File.Delete(qptFile);
            }
 
            return args.imgPath;
        }
 
        /// <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);
        }
    }
}