管道基础大数据平台系统开发-【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
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 OsgbUtils
    {
        /// <summary>
        /// 获取路径
        /// </summary>
        public static string GetPath(int id)
        {
            return Path.Combine(SGUtils.LFData, "3d\\3dtiles\\osgb", id.ToString());
        }
 
        /// <summary>
        /// 获取发布地址
        /// </summary>
        public static string GetReleaseUrl(SysMeta meta)
        {
            return "{host}/LFData/3d/3dtiles/osgb/" + meta.id + "/tileset.json";
        }
 
        /// <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 = 'osgb'");
                if (null == list || list.Count == 0) return null;
 
                string d3tilesPath = Tools.GetSetting("d3tilesPath");
                string uploadFolder = Tools.GetSetting("uploadFolder");
 
                List<int> ids = new List<int>();
                foreach (SysMeta meta in list)
                {
                    string osgbPath = Path.Combine(uploadFolder, meta.path);
                    if (!Directory.Exists(osgbPath)) continue;
 
                    meta.ismeta = 0; // 0-倾斜摄影数据
                    string outPath = GetPath(meta.id);
                    string jsonFile = Path.Combine(outPath, "tileset.json");
 
                    if (args.isNew && Directory.Exists(outPath)) Tools.DelPath(outPath);
                    if (!Directory.Exists(outPath)) Directory.CreateDirectory(outPath);
                    if (File.Exists(jsonFile)) File.Delete(jsonFile);
 
                    string srs = findSRS(osgbPath);
                    if (null != srs) writeSRS(osgbPath, getSRS(srs));
 
                    int idx = args.ids.IndexOf(meta.id);
                    string cmd = string.Format("{0}\\3dtile.exe -f osgb -i \"{1}\" -o \"{2}\" -c \"{{\\\"offset\\\": {3}}}\"", d3tilesPath, osgbPath, outPath, args.zs[idx]);
                    SysTask task = TaskDBHelper.CreateTask(args, meta, "OSGB", "倾斜数据(OSGB)");
                    err = Tools.ExecCmd(task, cmd, false);
                    err = null;
 
                    if (null != srs) writeSRS(osgbPath, srs);
                    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>
        /// 查找SRS
        /// </summary>
        private static string findSRS(string osgbPath)
        {
            string file = Path.Combine(osgbPath, "metadata.xml");
            if (!File.Exists(file)) return null;
 
            string[] lines = File.ReadAllLines(file);
            foreach (string line in lines)
            {
                if (line.Contains("<SRS>EPSG:") && line.Contains("+"))
                {
                    return line;
                }
            }
 
            return null;
        }
 
        /// <summary>
        /// 写入SRS
        /// </summary>
        private static void writeSRS(string osgbPath, string newSRS)
        {
            string file = Path.Combine(osgbPath, "metadata.xml");
            if (!File.Exists(file)) return;
 
            string[] lines = File.ReadAllLines(file);
            for (int i = 0, c = lines.Length; i < c; i++)
            {
                if (lines[i].Contains("<SRS>EPSG:"))
                {
                    lines[i] = newSRS;
                    break;
                }
            }
 
            File.WriteAllLines(file, lines);
        }
 
        /// <summary>
        /// 获取SRS
        /// </summary>
        /// <param name="srs"></param>
        private static string getSRS(string srs)
        {
            int start = srs.IndexOf("+");
            int end = srs.IndexOf("</SRS>");
            string other = srs.Substring(start, end - start);
 
            return srs.Replace(other, "");
        }
 
        /// <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(meta), path);
 
            pubid = PubDBHelper.InsertPublish(sys);
            if (pubid > 0)
            {
                sys.id = pubid;
                PubDBHelper.InsertLayer(sys, meta);
                PubDBHelper.InsertMetaPub(meta.id, pubid, args.userId);
            }
 
            return pubid;
        }
    }
}