管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-09-07 8d7a67ab1d635cb954337d8a767878ae526dd3dc
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
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 TerrainUtils
    {
        private static int terrainMaxLevel = 0;
 
        private const string EPSG4326 = "EPSG:4326";
 
        /// <summary>
        /// 地形最大级别
        /// </summary>
        public static int TERRAIN_MAX_LEVEL
        {
            get
            {
                if (0 == terrainMaxLevel)
                {
                    string str = Tools.GetSetting("terrainMaxLevel");
                    if (!int.TryParse(str, out terrainMaxLevel))
                    {
                        terrainMaxLevel = 14;
                    }
                }
 
                return terrainMaxLevel;
            }
        }
 
        /// <summary>
        /// 默认最大文件大小:5GB
        /// </summary>
        public static long DEFAULT_MAX_SIZE = 5L * 1024 * 1024 * 1024;
 
        /// <summary>
        /// 获取发布地址:http://127.0.0.1/ExportMap/terra0?path=3d\terrain\dem\t
        /// </summary>
        public static string GetReleaseUrl(string dircode)
        {
            //return "http://{host}/LFData/3d/terrain/" + dircode;
            return "{host}/ExportMap/terra0?path=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)
        {
            try
            {
                List<SysMeta> metas = XYZUtils.SelectMetas(args.ids, "and type in ('tif', 'tiff', 'dem')");
                if (null == metas || metas.Count == 0) return PrintInfo("找不到元数据");
 
                string dirPath = GetTerrainPath(args.dircode);
                //if (Directory.Exists(dirPath)) Tools.DelPath(dirPath); // 已存在的,删除
                if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
                string subPath = Path.Combine(dirPath, "subs");
                if (!Directory.Exists(subPath)) Directory.CreateDirectory(subPath);
 
                int pubid = PubDBHelper.GetPushlishId(args.dircode, "DEM");
                List<int> metsIds = PubDBHelper.GetPublishMetaId(pubid);
 
                List<string> files = new List<string>();
                string uploadFolder = Tools.GetSetting("uploadFolder");
                foreach (SysMeta meta in metas)
                {
                    if (metsIds.Contains(meta.id))
                    {
                        PrintInfo("元数据[" + meta.id + "] 已发布。");
                        continue;
                    }
                    string sourceFile = Path.Combine(uploadFolder, meta.path);
                    if (!File.Exists(sourceFile))
                    {
                        PrintInfo("元数据[" + meta.id + "] 不存在。");
                        continue;
                    }
 
                    string targetFile = Path.Combine(subPath, meta.id + ".tif"); // meta.path.Split(new string[] { "\\", "//" }, StringSplitOptions.None)[1]
                    ConvertRaster(sourceFile, targetFile);
                    if (File.Exists(targetFile))
                    {
                        files.Add(targetFile);
                        CreateTerrain(args, targetFile, ref err);
                    }
                }
 
                CreateLayerJson(args, files, ref err);
                string json = Path.Combine(dirPath, "layer.json");
                if (!File.Exists(json)) return PrintInfo("找不到layer.json文件");
 
                Complement(args);
                pubid = InsertToDB(metas, args);
 
                return new List<int> { pubid };
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                err = ex.Message;
                return null;
            }
        }
 
        /// <summary>
        /// 打印信息
        /// </summary>
        private static List<int> PrintInfo(string info)
        {
            LogOut.Info("TerrainUtils:" + info);
            return null;
        }
 
        /// <summary>
        /// 转换栅格文件
        /// </summary>
        private static void ConvertRaster(string sourceFile, string targetFile)
        {
            if (File.Exists(targetFile)) return;
 
            string epsg = Tools.GetEPSG(sourceFile);
            if (epsg == EPSG4326)
            {
                File.Copy(sourceFile, targetFile);
                return;
            }
 
            Reproject(sourceFile, targetFile, epsg, EPSG4326);
        }
 
        /// <summary>
        /// 创建高程切片
        /// </summary>
        private static void CreateTerrain(XYZArgs args, string tifFile, ref string err)
        {
            string ctbPath = Tools.GetSetting("ctbJL");
            string dirPath = GetTerrainPath(args.dircode);
            //int maxLevel =  GetTerrainMaxLevel(args, tifFile);
 
            string gdal_data = string.Format("set GDAL_DATA={0}\\data", ctbPath);
            // -N 顶点法线, -C 强制创建缺失根瓦片, -R 不覆盖现有文件,-f Mesh
            string createMesh = string.Format("{0}\\ctb-tile.exe -s {1} -o \"{2}\" \"{3}\"", ctbPath, TERRAIN_MAX_LEVEL, dirPath, tifFile);
 
            List<string> list = new List<string>() { gdal_data, createMesh };
            SysTask task = TaskDBHelper.CreateTask(args, "DEM", "高程数据(DEM)");
            err = Tools.ExecCmd(task, list);
        }
 
        /// <summary>
        /// 创建layer.json
        /// </summary>
        private static string CreateLayerJson(XYZArgs args, List<string> files, ref string err)
        {
            string dirPath = GetTerrainPath(args.dircode);
            string vrt = Path.Combine(dirPath, "subs", "dem.vrt");
            CreateVrt(files, vrt);
            if (!File.Exists(vrt)) return null;
 
            string ctbPath = Tools.GetSetting("ctbJL");
            string gdal_data = string.Format("set GDAL_DATA={0}\\data", ctbPath);
            string createLayer = string.Format("{0}\\ctb-tile.exe -l -s {1} -o \"{2}\" \"{3}\"", ctbPath, TERRAIN_MAX_LEVEL, dirPath, vrt);
            Tools.ExecCmd(new List<string>() { gdal_data, createLayer });
 
            return vrt;
        }
 
        /// <summary>
        /// 创建VRT
        /// </summary>
        private static void CreateVrt(List<string> files, string vrt)
        {
            string str = string.Join("\r\n", files);
            string filelist = vrt.Replace("dem.vrt", "list.txt");
            File.WriteAllText(filelist, str);
 
            string gdalPath = Tools.GetSetting("gdalPath");
            string cmd = string.Format("\"{0}\\gdalbuildvrt.exe\" -input_file_list \"{1}\" \"{2}\" -overwrite", gdalPath, filelist, vrt);
            Tools.ExecCmd(new List<string> { cmd });
        }
 
        /// <summary>
        /// 获取地形最大级别
        /// </summary>
        private static int GetTerrainMaxLevel(XYZArgs args, string tifFile)
        {
            FileInfo fi = new FileInfo(tifFile);
            if (fi.Length > DEFAULT_MAX_SIZE) return TERRAIN_MAX_LEVEL;
 
            string ctbPath = Tools.GetSetting("ctbJL");
            string dirPath = GetTerrainPath(args.dircode);
            string subPath = Path.Combine(dirPath, "subs");
 
            string gdal_data = string.Format("set GDAL_DATA={0}\\data", ctbPath);
            string createLayer = string.Format("{0}\\ctb-tile.exe -l -o \"{1}\" \"{2}\"", ctbPath, subPath, tifFile);
 
            Tools.ExecCmd(new List<string>() { gdal_data, createLayer });
 
            string layerJson = Path.Combine(subPath, "layer.json");
            if (!File.Exists(layerJson)) return TERRAIN_MAX_LEVEL;
 
            string[] lines = File.ReadAllLines(layerJson, Encoding.UTF8);
 
            int level = -1;
            foreach (string line in lines)
            {
                if (line.IndexOf("startX") > -1) level++;
            }
            if (File.Exists(layerJson)) File.Delete(layerJson);
 
            return level > TERRAIN_MAX_LEVEL ? TERRAIN_MAX_LEVEL : level;
        }
 
        /// <summary>
        /// 补充文件
        /// </summary>
        public 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 = Tools.BaseDir + "\\Sources\\0.terrain";
            string d_0_0_0 = Path.Combine(dirPath, "0", "0", "0.terrain");
            if (!File.Exists(d_0_0_0)) File.Copy(s_0, d_0_0_0, true);
 
            string d_0_1_0 = Path.Combine(dirPath, "0", "1", "0.terrain");
            if (!File.Exists(d_0_1_0)) File.Copy(s_0, d_0_1_0, true);
 
            string layerJson = Path.Combine(dirPath, "layer.json");
            string[] lines = File.ReadAllLines(layerJson, Encoding.UTF8);
            //lines[12] = "    [ { \"startX\": 0, \"startY\": 0, \"endX\": 1, \"endY\": 0 } ]";
            for (int i = 0, c = lines.Length; i < c; i++)
            {
                if (lines[i].IndexOf("startX") > -1)
                {
                    lines[i] = "    [ { \"startX\": 0, \"startY\": 0, \"endX\": 1, \"endY\": 0 } ]";
                    break;
                }
            }
 
            File.WriteAllLines(layerJson, lines, Encoding.UTF8);
        }
 
        /// <summary>
        /// 插入数据库
        /// </summary>
        private static int InsertToDB(List<SysMeta> metas, XYZArgs args)
        {
            //if (PubDBHelper.IsPublish(args.dircode, "DEM")) return 1;
            int pubid = PubDBHelper.GetPushlishId(args.dircode, "DEM");
            if (pubid > 0) // 更新发布
            {
                List<int> ids = PubDBHelper.GetPublishMetaId(pubid);
                foreach (SysMeta m in metas)
                {
                    if (!ids.Contains(m.id)) PubDBHelper.InsertMetaPub(m.id, pubid, args.userId);
                }
                string geom = GetPointZ(args);
                int rows = PubDBHelper.UpdatePublish(pubid, args.name, args.userId, geom);
 
                return pubid;
            }
 
            SysMeta meta = metas[0];
            meta.type = "DEM";
            meta.name = args.name;
            meta.dircode = args.dircode;
 
            SysPublish sys = Tools.NewPublish(meta, args, GetReleaseUrl(args.dircode), "3d\\terrain\\" + args.dircode);
            sys.geom = GetPointZ(args);
 
            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>
        public static string GetPointZ(XYZArgs args)
        {
            string dirPath = GetTerrainPath(args.dircode);
            string vrt = Path.Combine(dirPath, "subs", "dem.vrt");
            if (!File.Exists(vrt)) return null;
 
            string gdalPath = Tools.GetSetting("gdalPath");
            string cmd = string.Format("\"{0}\\gdalinfo.exe\" \"{1}\"", gdalPath, vrt);
 
            string rs = null;
            Tools.ExecCmd(new List<string> { cmd }, ref rs);
            string center = GetCenter(rs);
            if (string.IsNullOrEmpty(rs)) return null;
 
            string[] strs = center.Split(new string[] { "," }, StringSplitOptions.None);
            double x = double.Parse(strs[0]);
            double y = double.Parse(strs[1]);
 
            return string.Format("ST_GeomFromText('POINT Z ({0} {1} {2})')", y, x, 12);
        }
 
        /// <summary>
        /// 获取中心坐标
        /// </summary>
        private static string GetCenter(string rs)
        {
            if (string.IsNullOrEmpty(rs)) return null;
 
            string[] strs = rs.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str in strs)
            {
                if (str.StartsWith("Center "))
                {
                    int start = str.IndexOf("(");
                    int end = str.IndexOf(")");
 
                    return str.Substring(start + 1, end - start - 1).Replace(" ", "");
                }
            }
 
            return null;
        }
 
        /// <summary>
        /// 重投影
        /// </summary>
        public static void Reproject(string sourceFile, string targetFile, string sourceSrs, string targetSrs)
        {
            string gdalPath = Tools.GetSetting("gdalPath");
            string cmd = string.Format("\"{0}\\gdalwarp.exe\" -s_srs {1} -t_srs {2} -r near -of GTiff \"{3}\" \"{4}\"", gdalPath, sourceSrs, targetSrs, sourceFile, targetFile);
 
            Tools.ExecCmd(new List<string> { cmd });
        }
 
        /// <summary>
        /// 指定投影 *
        /// </summary>
        public static void Project(string sourceFile, string targetSrs)
        {
            List<string> list = new List<string>();
            list.Add("cd \"C:\\Program Files\\QGIS 3.16\\apps\\Python37\"");
            //list.Add("set GDAL_DATA=\"C:\\Program Files\\QGIS 3.16\\share\\gdal\"");
            //list.Add("set PROJ_LIB=\"C:\\Program Files\\QGIS 3.16\\share\\proj\"");
            list.Add("\"C:\\Program Files\\QGIS 3.16\\bin\\qgis_process-qgis-ltr.bat\"");
 
            string cmd = string.Format("python \"C:\\Program Files\\QGIS 3.16\\apps\\Python37\\Scripts\\gdal_edit.py\" -a_srs {0} {1}", targetSrs, sourceFile);
            list.Add(cmd);
 
            string rs = "";
            string err = Tools.ExecCmd(new List<string> { cmd }, ref rs);
        }
    }
}