管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-06-28 5178a8e85f1a544cbd255f01fae1905e40112228
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
using ExportMap.db;
using ExportMap.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
 
namespace ExportMap.cs
{
    public class XYZUtils
    {
        private static string pyFile;
 
        /// <summary>
        /// 获取Python文件
        /// </summary>
        public static string PyFile
        {
            get
            {
                if (string.IsNullOrWhiteSpace(pyFile))
                {
                    pyFile = Path.Combine(ExportUtil.SourcesPath, "xyz.py");
                }
 
                return pyFile;
            }
        }
 
        /// <summary>
        /// QGIS工程
        /// </summary>
        public static string Qgz
        {
            get
            {
                //return Path.Combine(ExportUtil.SourcesPath, "xyz.qgz");
                return "xyz.qgz";
            }
        }
 
        /// <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>
        /// 获取发布地址
        /// </summary>
        public static string GetReleaseUrl(string dircode)
        {
            return "http://{host}/LFData/2d/tiles/" + dircode + "/{z}/{x}/{y}.png";
        }
 
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="args">XYZ参数</param>
        /// <param name="err">错误信息</param>
        /// <returns>数据发布ID集合</returns>
        public static List<int> Generate(XYZArgs args, ref string err)
        {
            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 tifFile = 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);
 
            WriteText(tifFile, list);
 
            string cmd = string.Format("python \"{0}\" -qgz {1} -file \"{2}\" -out \"{3}\" -min {4} -max {5} -noData {6}", PyFile, Qgz, tifFile, xyzPath, args.min, args.max, args.noData);
 
            //err = Tools.ExecCmd(GetCmds(batFile, cmd));            
            err = Tools.ExecCmd(cmd, true, false);
 
            //if (File.Exists(batFile)) File.Delete(batFile);
            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;
                int pubid = InsertToDB(list, args, path);
 
                if (pubid > 0) ids.Add(pubid);
            }
 
            return ids;
        }
 
        /// <summary>
        /// 获取命令行
        /// </summary>
        private static List<string> GetCmds(string batFile, string cmd)
        {
            string str = File.ReadAllText(BaseBat) + cmd;
            File.WriteAllText(batFile, str);
 
            return new List<string>() { "\"" + batFile + "\"" };
        }
 
        /// <summary>
        /// 查询元数据
        /// </summary>
        public static List<SysMeta> SelectMetas(List<int> ids, string types = "")
        {
            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", string.Join(",", ids), types);
            DataTable dt = Tools.DBHelper.GetDataTable(sql);
            List<SysMeta> list = ModelHandler.FillModel<SysMeta>(dt);
 
            return list;
        }
 
        /// <summary>
        /// 写文本文件
        /// </summary>
        private static void WriteText(string file, List<SysMeta> list)
        {
            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);
            }
            //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("\n", files);
 
            File.WriteAllText(file, str);
        }
 
        /// <summary>
        /// 插入数据库
        /// </summary>
        private static int InsertToDB(List<SysMeta> metas, XYZArgs args, string path)
        {
            if (PubDBHelper.IsPublish(args.dircode)) return 1;
 
            SysMeta meta = metas[0];
            meta.type = "DOM";
            meta.name = args.name;
 
            SysPublish sys = Tools.NewPublish(meta, args, GetReleaseUrl(args.dircode), path);
            sys.geom = GetPointZ(args);
 
            int 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]);
        }
    }
}