管道基础大数据平台系统开发-【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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SQLite;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Web;
 
namespace JiangSu.cs
{
    public class Tools
    {
        private static PostgreHelper _pgHelper;
 
        /// <summary>
        /// DB帮助类
        /// </summary>
        public static PostgreHelper PGHelper
        {
            get
            {
                if (null == _pgHelper)
                {
                    _pgHelper = new PostgreHelper();
                }
 
                return _pgHelper;
            }
        }
 
        /// <summary>
        /// 获取参数列表
        /// </summary>
        public static DbParameter[] GetPGParams<T>(string sql, T t)
        {
            List<DbParameter> list = new List<DbParameter>();
            Type tType = typeof(T);
            BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
 
            int start = sql.IndexOf("@");
            while (start != -1)
            {
                int end = sql.IndexOf(",", start);
                if (end == -1) end = sql.IndexOf(")", start);
                if (end == -1) end = sql.IndexOf(" ", start);
                if (end == -1) end = sql.Length;
 
                string name = sql.Substring(start + 1, end - start - 1);
                PropertyInfo pi = tType.GetProperty(name, flags);
                if (pi != null)
                {
                    object value = pi.GetValue(t, null);
                    NpgsqlParameter dp = new NpgsqlParameter("@" + name, value);
                    list.Add(dp);
                }
 
                start = sql.IndexOf("@", end);
            }
 
            return list.ToArray();
        }
 
        /// <summary>
        /// 获取参数列表
        /// </summary>
        public static SQLiteParameter[] GetSQLiteParams<T>(string sql, T t)
        {
            List<SQLiteParameter> list = new List<SQLiteParameter>();
            Type tType = typeof(T);
            BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
 
            int start = sql.IndexOf("@");
            while (start != -1)
            {
                int end = sql.IndexOf(",", start);
                if (end == -1) end = sql.IndexOf(")", start);
                if (end == -1) end = sql.IndexOf(" ", start);
                if (end == -1) end = sql.Length;
 
                string name = sql.Substring(start + 1, end - start - 1);
                PropertyInfo pi = tType.GetProperty(name, flags);
                if (pi != null)
                {
                    object value = pi.GetValue(t, null);
                    SQLiteParameter dp = new SQLiteParameter("@" + name, value);
                    list.Add(dp);
                }
 
                start = sql.IndexOf("@", end);
            }
 
            return list.ToArray();
        }
 
        /// <summary>
        /// 字节格式化
        /// </summary>
        public static string FormatBytes(long bytes)
        {
            string[] Suffix = { "Byte", "KB", "MB", "GB", "TB" };
 
            int i = 0;
            double dblSByte = bytes;
            if (bytes > 1024)
                for (i = 0; (bytes / 1024) > 0; i++, bytes /= 1024)
                    dblSByte = bytes / 1024.0;
 
            return String.Format("{0:0.##}{1}", dblSByte, Suffix[i]);
        }
 
        /// <summary>
        /// 计算文件的MD5码
        /// </summary>
        public static string CalcFileMD5(string filePath)
        {
            using (var md5 = new MD5CryptoServiceProvider())
            {
                using (var stream = File.OpenRead(filePath))
                {
                    byte[] hash = md5.ComputeHash(stream);
                    return BitConverter.ToString(hash).Replace("-", "").ToLower();
                }
            }
        }
 
        /// <summary>
        /// 获取图片的GPS信息
        /// </summary>
        public static List<string> GetImgGps(string path)
        {
            Image img = null;
            try
            {
                img = Image.FromFile(path);
                var items = img.PropertyItems.OrderByDescending(x => x.Id);
 
                List<string> list = new List<string> { "0", "0", "0" };
                foreach (PropertyItem item in items)
                {
                    if (item.Id >= 0x0000 && item.Id <= 0x001e) // 只取Id范围为0x0000到0x001e
                    {
                        switch (item.Id)
                        {
                            case 0x0002: // 设置纬度
                                list[1] = GetGpsVal(item.Value);
                                break;
                            case 0x0004: // 设置经度
                                list[0] = GetGpsVal(item.Value);
                                break;
                            case 0x0006:
                                if (item.Value.Length == 8)
                                {
                                    double altitude = BitConverter.ToUInt32(item.Value, 0) * 1.0d / BitConverter.ToUInt32(item.Value, 4);
                                    list[2] = altitude.ToString("0.000");
                                }
                                break;
                        }
                    }
                }
 
                return list;
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                return null;
            }
            finally
            {
                if (null != img) img.Dispose();
            }
        }
 
        public static string GetImgPointZ(string path)
        {
            List<string> list = GetImgGps(path);
 
            return null == list ? null : string.Format("POINT Z ({0})", string.Join(" ", list.ToArray()));
        }
 
        /// <summary>
        /// 获取GPS值
        /// </summary>
        private static string GetGpsVal(byte[] bytes)
        {
            if (bytes.Length != 24) return "0";
 
            //degrees(将byte[0]~byte[3]转成uint, 除以byte[4]~byte[7]转成的uint)   
            double d = BitConverter.ToUInt32(bytes, 0) * 1.0d / BitConverter.ToUInt32(bytes, 4);
 
            //minutes(将byte[8]~byte[11]转成uint, 除以byte[12]~byte[15]转成的uint)   
            double m = BitConverter.ToUInt32(bytes, 8) * 1.0d / BitConverter.ToUInt32(bytes, 12);
 
            //seconds(将byte[16]~byte[19]转成uint, 除以byte[20]~byte[23]转成的uint)   
            double s = BitConverter.ToUInt32(bytes, 16) * 1.0d / BitConverter.ToUInt32(bytes, 20);
 
            double val = (((s / 60 + m) / 60) + d);
 
            return val.ToString("0.00000000");
        }
    }
}