管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-03-16 7e6bc3b86674ec026b3c5c3f25574116a3ecaa28
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
using ExportMap.db;
using ExportMap.Models;
using System;
using System.Collections.Generic;
using System.Data;
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>
        /// 生成
        /// </summary>
        /// <param name="args">XYZ参数</param>
        /// <param name="err">错误信息</param>
        /// <returns>数据发布ID</returns>
        public static int Generate(XYZArgs args, ref string err)
        {
            string tifFile = Path.Combine(Tool.TempDir, ExportUtil.DateStr + ".txt");
            string xyzPath = Path.Combine(SGUtils.LFData, "2d\\tiles", args.id.ToString());
            if (!Directory.Exists(xyzPath)) Directory.CreateDirectory(xyzPath);
 
            List<SysMeta> list = selectMetas(args.ids, "and type in ('tif', 'tiff', 'img')");
            if (null == list || list.Count == 0) return 0;
 
            WriteText(tifFile, list);
 
            //string cmd = string.Format("python \"{0}\" -qgz {1} -file \"{2}\" -out \"{3}\" -min {4} -max {5}", PyFile, Qgz, tifFile, xyzPath, args.min, args.max);
 
            string pyText = File.ReadAllText(PyFile);
            pyText = pyText
                //.Replace("xyz.qgz", Path.Combine(ExportUtil.SourcesPath, "xyz.qgz"))
                .Replace("D:\\xyz\\zy.txt", tifFile)
                .Replace("D:\\xyz\\tiles\\zy", xyzPath)
                .Replace("=12,", "=" + args.min + ",")
                .Replace("=18,", "=" + args.max + ",");
 
            string newPy = tifFile.Replace(".txt", ".py").Replace("\\", "\\\\");
            File.WriteAllText(newPy, pyText);
           string cmd = string.Format("exec(open('{0}', 'r', encoding='utf-8').read()) & exit()", newPy);
 
            err = Tool.ExecCmd(cmd, true);
 
            if (File.Exists(tifFile)) File.Delete(tifFile);
 
            string viewFile = Path.Combine(xyzPath, "view.html");
 
            return File.Exists(viewFile) ? args.id : 0;
        }
 
        /// <summary>
        /// 查询元数据
        /// </summary>
        public static List<SysMeta> selectMetas(List<int> ids, string types = "")
        {
            string sql = string.Format("select * from lf.sys_meta where id in ({0}) {1} order by id", string.Join(",", ids), types);
            DataTable dt = Tool.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 = Tool.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(file, str);
        }
    }
}