管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-01-08 33d81a2224ff1327f134374a80f7d9f681ba3883
JiangSu/cs/Tools.cs
@@ -3,8 +3,12 @@
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
@@ -92,5 +96,112 @@
            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");
        }
    }
}