管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2023-10-12 7e063d4b2812aeaabc247c2c3f0fc5b12b5478e8
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using MoonExp.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
 
namespace MoonExp.cs
{
    public class ExportUtil
    {
        /// <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(Tools.BaseDir, "Sources");
                }
 
                return sourcesPath;
            }
        }
 
        private static string pyFile;
 
        /// <summary>
        /// 获取Python文件
        /// </summary>
        public static string PyFile
        {
            get
            {
                if (string.IsNullOrWhiteSpace(pyFile))
                {
                    pyFile = Path.Combine(SourcesPath, "MoonExp.py");
                }
 
                return pyFile;
            }
        }
 
        /// <summary>
        /// 获取上期字符串
        /// </summary>
        public static string DateStr
        {
            get
            {
                return DateTime.Now.ToString("yyyyMMddHHmmss");
            }
        }
 
        /// <summary>
        /// 获取下载目录
        /// </summary>
        public static string DownloadFolder
        {
            get
            {
                return ConfigurationManager.AppSettings["downloadFolder"];
            }
        }
 
        /// <summary>
        /// 后台服务地址
        /// </summary>
        public static string MoonServer
        {
            get
            {
                return ConfigurationManager.AppSettings["moonServer"];
            }
        }
 
        /// <summary>
        /// 获取出图子目录
        /// </summary>
        public static string GetExportSubFolder()
        {
            string root = DownloadFolder;
            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();
        }
 
        private static readonly string selectResSql = "select a.id, a.url, b.url || case when length(b.args) > 0 then '?' || b.args else '' end \"surl\" from lf.sys_layer a left join lf.sys_res b on a.resid = b.id where a.category = 3 and a.is_layer = 1 and a.id in ({0})";
 
        public ExportUtil() { }
 
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="args">出图参数</param>
        /// <param name="err">错误信息</param>
        /// <returns>图片路径</returns>
        public static string Generate(ExportArgs args, ref string err)
        {
            string date = DateStr;
            string sub = GetExportSubFolder();
            string qgz = "Moon.qgz";
 
            args.imgPath = sub + "\\" + date + ".png";
            args.qpt = date + ".qpt";
 
            args.SetDefault();
            CreateTemplate(args);
 
            string cmd = string.Format("python \"{0}\" -qgz {1} -qpt {2}", PyFile, qgz, args.qpt);
            err = Tools.ExecCmd(cmd, true, true);
 
            string qptFile = Path.Combine(SourcesPath, args.qpt);
            if (File.Exists(qptFile)) File.Delete(qptFile);
 
            string imgPath = Path.Combine(DownloadFolder, args.imgPath);
            bool flag = File.Exists(imgPath);
 
            return flag ? args.imgPath : null;
        }
 
        /// <summary>
        /// 转换为XYZ参数
        /// </summary>
        private static XYZArgs ToXYZArgs(ExportArgs ea)
        {
            XYZArgs args = new XYZArgs();
            args.name = ea.title;
            args.ids = new List<int>();
            args.depcode = null;
            args.dircode = null;
            args.userId = Tools.SelectUserIdByToken(ea.token);
 
            return args;
        }
 
        /// <summary>
        /// 创建模板
        /// </summary>
        /// <param name="args">出图参数</param>
        public static void CreateTemplate(ExportArgs args)
        {
            string imgPath = Path.Combine(DownloadFolder, args.imgPath);
            string templateFile = Path.Combine(SourcesPath, "MoonTemplate.qpt");
            string qptFile = Path.Combine(SourcesPath, args.qpt);
            LogOut.Info("imgPath = " + imgPath + ", tempFile = " + templateFile + ", qptFile = " + qptFile);
 
            string xml = File.ReadAllText(templateFile);
            xml = xml
                .Replace("{dpi}", args.dpi.ToString())
                .Replace("{title}", args.title)
                .Replace("{sourcesPath}", SourcesPath)
                .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("{date}", DateTime.Now.ToString("yyyy.MM.dd"))
                .Replace("{layers}", args.layers)
                .Replace("{imgPath}", imgPath)
                .Replace("{wmsUrl}", getWmtsUrl(args));
 
            File.WriteAllText(qptFile, xml);
        }
 
        /// <summary>
        /// 验证令牌
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns>是/否有效</returns>
        public static bool VerifyToken(string token)
        {
            try
            {
                string url = MoonServer + "/sign/check?token=" + token.Trim();
                string json = GetData(url);
                if (string.IsNullOrWhiteSpace(json))
                {
                    return false;
                }
 
                ResponseMsg<bool> rm = JsonConvert.DeserializeObject<ResponseMsg<bool>>(json);
 
                return rm != null && rm.code == 200 && rm.result;
            }
            catch
            {
                return false;
            }
        }
 
        /// <summary>
        /// Get获取数据
        /// </summary>
        /// <param name="url">URL</param>
        /// <returns>数据</returns>
        public static string GetData(string url)
        {
            Uri uri = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
 
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string str = reader.ReadToEnd();
            reader.Close();
 
            return str;
        }
 
        /// <summary>
        /// 获取WMTS地址,多个以英文逗号隔开
        /// </summary>
        public static string getWmtsUrl(ExportArgs args)
        {
            List<string> list = new List<string>();
            foreach (int id in args.layerIds)
            {
                List<string> resList = SelectMapUrls(id);
                if (null != resList && resList.Count > 0)
                {
                    list.AddRange(resList);
                }
            }
 
            if (0 == list.Count)
            {
                string defaultUrl = Tools.GetSetting("wmtsUrl");
                if (!string.IsNullOrEmpty(defaultUrl)) list.Add(defaultUrl);
            }
 
            string wmtsUrl = string.Join(",", list);
 
            return wmtsUrl;
        }
 
        /// <summary>
        /// 查询地图Url
        /// </summary>
        public static List<string> SelectMapUrls(int id)
        {
            string sql = string.Format(selectResSql, id);
            DataTable dt = Tools.DBHelper.GetDataTable(sql);
            if (null == dt || 0 == dt.Rows.Count)
            {
                return null;
            }
 
            List<SysRes> resList = ModelHandler.FillModel<SysRes>(dt);
            if (null == resList || 0 == resList.Count)
            {
                return null;
            }
 
            List<string> list = new List<string>();
            foreach (SysRes res in resList)
            {
                if (!string.IsNullOrEmpty(res.surl))
                {
                    list.Add(res.surl);
                    continue;
                }
                if (!string.IsNullOrEmpty(res.url))
                {
                    list.Add(res.url);
                }
            }
 
            return list;
        }
    }
}