管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-09-03 3cfb6aa02516135fb174ab1b30620f2007924663
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
using ExportMap.db;
using ExportMap.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
 
namespace ExportMap.cs
{
    public class LasUtils
    {
        /// <summary>
        /// 获取路径
        /// </summary>
        public static string GetPath(int id)
        {
            return Path.Combine(SGUtils.LFData, "3d\\3dtiles\\las", id.ToString());
        }
 
        /// <summary>
        /// 获取发布地址
        /// </summary>
        public static string GetReleaseUrl(string path)
        {
            return "{host}/LFData/" + path.Replace("\\", "/");
        }
 
        /// <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> list = XYZUtils.SelectMetas(args.ids, "and type in ('las', 'laz')");
                if (null == list || list.Count == 0) return null;
 
                string tilerPath = Tools.GetSetting("tilerPath");
                string uploadFolder = Tools.GetSetting("uploadFolder");
 
                List<int> ids = new List<int>();
                foreach (SysMeta meta in list)
                {
                    string lasPath = Path.Combine(uploadFolder, meta.path);
                    if (!File.Exists(lasPath)) continue;
 
                    meta.ismeta = 0; // 0-倾斜摄影数据
                    string outPath = GetPath(meta.id);
 
                    if (Directory.Exists(outPath)) Tools.DelPath(outPath);
                    if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath);
                    if ("laz" == meta.type) lasPath = toLas(lasPath, Path.Combine(outPath, meta.id + ".las"));
 
                    int idx = args.ids.IndexOf(meta.id);
                    string cmd = string.Format("{0}\\gocesiumtiler.exe -i \"{1}\" -o \"{2}\" -e {3} -z {4} -g -s", tilerPath, lasPath, outPath, args.srids[idx], args.zs[idx]);
                    SysTask task = TaskDBHelper.CreateTask(args, meta, "LAS", "点云数据(LAS)");
                    err = Tools.ExecCmd(task, cmd, false);
 
                    string jsonFile = findTileset(meta, outPath);
                    if ("laz" == meta.type && File.Exists(lasPath)) File.Delete(lasPath);
                    if (File.Exists(jsonFile))
                    {
                        string path = jsonFile.Replace(Tools.GetSetting("lfData") + "\\", "");
                        int pubid = InsertToDB(meta, args, path);
                        if (pubid > 0) ids.Add(pubid);
                    }
                }
 
                return ids;
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                err = ex.Message;
                return null;
            }
        }
 
        /// <summary>
        /// laz 转换为 las
        /// </summary>
        private static string toLas(string lazPath, string outPath)
        {
            string lasPath = outPath.Replace("laz", "laz");
            string tilerPath = Tools.GetSetting("tilerPath");
 
            string cmd = string.Format("{0}\\laszip64.exe -i \"{1}\" -o \"{2}\"", tilerPath, lazPath, lasPath);
            Tools.ExecCmd(new List<string> { cmd });
 
            return lasPath;
        }
 
        /// <summary>
        /// 查找tileset.json
        /// </summary>
        private static string findTileset(SysMeta meta, string path)
        {
            PathRename(meta, path);
 
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] fis = di.GetFiles("tileset.json", SearchOption.AllDirectories);
 
            return null == fis || 0 == fis.Length ? null : fis[0].FullName;
        }
 
        /// <summary>
        /// 路径重命名
        /// </summary>
        private static void PathRename(SysMeta meta, string path)
        {
            try
            {
                string subPath = Path.Combine(path, meta.guid);
                if (Directory.Exists(subPath))
                {
                    string newPath = Path.Combine(path, meta.id.ToString());
                    Directory.Move(subPath, newPath);
                }
            }
            catch
            {
            }
        }
 
        /// <summary>
        /// 插入数据库
        /// </summary>
        private static int InsertToDB(SysMeta meta, XYZArgs args, string path)
        {
            //if (PubDBHelper.IsPublish(meta.id)) return 1;
            int pubid = PubDBHelper.GetPushlishId(meta.id);
            if (pubid > 0)
            {
                PubDBHelper.UpdatePublish(pubid, meta.name, args.userId, null);
                return pubid;
            }
 
            SysPublish sys = Tools.NewPublish(meta, args, GetReleaseUrl(path), path);
 
            pubid = PubDBHelper.InsertPublish(sys);
            if (pubid > 0)
            {
                sys.id = pubid;
                PubDBHelper.InsertLayer(sys, meta);
                PubDBHelper.InsertMetaPub(meta.id, pubid, args.userId);
            }
 
            return pubid;
        }
 
        /// <summary>
        /// 读取Las坐标系
        /// </summary>
        public static List<int> ReadLasCs(XYZArgs args, ref string err)
        {
            List<SysMeta> list = XYZUtils.SelectMetas(args.ids, "and type in ('las', 'laz')");
            if (null == list || list.Count == 0) return null;
 
            string uploadFolder = Tools.GetSetting("uploadFolder");
 
            List<int> ids = new List<int>();
            foreach (SysMeta meta in list)
            {
                try
                {
                    string lasPath = Path.Combine(uploadFolder, meta.path);
                    if (!File.Exists(lasPath))
                    {
                        ids.Add(-1);
                        continue;
                    }
 
                    int epsg = Tools.get_las_cs(lasPath.Replace("\\", "/"));
                    ids.Add(epsg);
                }
                catch (Exception ex)
                {
                    LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                    ids.Add(-1);
                }
            }
 
            return ids;
        }
    }
}