管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-05-16 ebb30d317e2d2a709adc64f3b132e209565f474f
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
using ExportMap.db;
using ExportMap.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
 
namespace ExportMap.cs
{
    public class TerraUtils
    {
        private static string pyFile;
 
        /// <summary>
        /// 获取Python文件
        /// </summary>
        public static string PyFile
        {
            get
            {
                if (string.IsNullOrWhiteSpace(pyFile))
                {
                    pyFile = Path.Combine(ExportUtil.SourcesPath, "merge.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/3d/terrain/" + dircode;
        }
 
        /// <summary>
        /// 获取地形路径
        /// </summary>
        public static string GetTerrainPath(string dircode)
        {
            return Path.Combine(SGUtils.LFData, "3d\\terrain", dircode);
        }
 
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="args">XYZ参数</param>
        /// <param name="err">错误信息</param>
        /// <returns>数据发布ID集合</returns>
        public static List<int> Generate(XYZArgs args, ref string err)
        {
            string tifFile = null;
            try
            {
                List<SysMeta> metas = XYZUtils.SelectMetas(args.ids, "and type in ('tif', 'tiff')");
                if (null == metas || metas.Count == 0) return null;
 
                string dirPath = GetTerrainPath(args.dircode);
                if (Directory.Exists(dirPath)) Tools.DelPath(dirPath);
 
                tifFile = Merge(metas, args, ref err);
                if (!File.Exists(tifFile)) return null;
 
                //Generate(args, tifFile, ref err);
                CreateTerrain(args, tifFile, ref err);
 
                string json = Path.Combine(dirPath, "layer.json");
                if (!File.Exists(json)) return null;
 
                Complement(args);
 
                List<int> ids = new List<int>();
                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);
                }
            }
        }
 
        /// <summary>
        /// 合并
        /// </summary>
        public static string Merge(List<SysMeta> 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);
                }
            }
        }
 
        /// <summary>
        /// 写文本文件
        /// </summary>
        private static void WriteText(string txtFile, 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);
            }
            string str = string.Join("\n", files);
 
            File.WriteAllText(txtFile, str);
        }
 
        /// <summary>
        /// 生成高程切片
        /// </summary>
        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); // Mesh, Terrain
            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); // Mesh, Terrain
            string stop = string.Format("docker stop {0}", name);
            string rm = string.Format("docker rm {0}", name);
 
            List<string> list = new List<string> { runDocker, createMesh, createLayer, stop, rm };
            err = Tools.ExecCmd(list);
        }
 
        /// <summary>
        /// 创建高程切片
        /// </summary>
        private static void CreateTerrain(XYZArgs args, string tifFile, ref string err)
        {
            string ctbPath = Tools.GetSetting("ctbPath");
            string dirPath = GetTerrainPath(args.dircode);
 
            string gdal_data = string.Format("set GDAL_DATA={0}\\data", ctbPath);
            string createMesh = string.Format("{0}\\ctb-tile.exe -o \"{1}\" -f Mesh \"{2}\\{3}.tif\"", ctbPath, dirPath, dirPath, args.dircode);
            string createLayer = string.Format("{0}\\ctb-tile.exe -l -o \"{1}\" -f Mesh \"{2}\\{3}.tif\"", ctbPath, dirPath, dirPath, args.dircode);
 
            List<string> list = new List<string>() { gdal_data, createMesh, createLayer };
            err = Tools.ExecCmd(list);
        }
 
        /// <summary>
        /// 补充文件
        /// </summary>
        private static void Complement(XYZArgs args)
        {
            string dirPath = GetTerrainPath(args.dircode);
            string p_0_0 = Path.Combine(dirPath, "0", "0");
            if (!Directory.Exists(p_0_0)) Directory.CreateDirectory(p_0_0);
 
            string p_0_1 = Path.Combine(dirPath, "0", "1");
            if (!Directory.Exists(p_0_1)) Directory.CreateDirectory(p_0_1);
 
 
            string s_0_0_0 = Path.Combine(SGUtils.LFData, "dem", "0", "0", "0.terrain");
            string d_0_0_0 = Path.Combine(dirPath, "0", "0", "0.terrain");
            File.Copy(s_0_0_0, d_0_0_0, true);
 
            string s_0_1_0 = Path.Combine(SGUtils.LFData, "dem", "0", "1", "0.terrain");
            string d_0_1_0 = Path.Combine(dirPath, "0", "1", "0.terrain");
            File.Copy(s_0_1_0, d_0_1_0, true);
 
            string layerJson = Path.Combine(dirPath, "layer.json");
            string[] lines = File.ReadAllLines(layerJson, Encoding.UTF8);
            lines[12] = "    [ {\"endX\":1,\"endY\":0,\"startX\":0,\"startY\":0}],[{\"endX\":3,\"endY\":1,\"startX\":2,\"startY\":1} ]";
 
            File.WriteAllLines(layerJson, lines, Encoding.UTF8);
        }
 
        /// <summary>
        /// 插入数据库
        /// </summary>
        private static int InsertToDB(List<SysMeta> metas, XYZArgs args)
        {
            if (PubDBHelper.IsPublish(args.dircode, "DEM")) return 0;
 
            SysMeta meta = metas[0];
            meta.type = "DEM";
            SysPublish sys = Tools.NewPublish(meta, args, GetReleaseUrl(args.dircode), "3d\\terrain\\" + args.dircode);
 
            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;
        }
    }
}