using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Aspose.Words; namespace GpsConvert { class Program { static void Main(string[] args) { //double[] utm = LatLonToUtm(30.65156708, 103.6880587); // double[] utm = LatLonToUtm(31.108761,121.314085); // Console.WriteLine($"X:{utm[0]},Y:{utm[1]},区域:{utm[2]}"); //var ss= Wgs84Tosh(31.0158865505,121.2238203740); // Console.WriteLine($"X:{ss[0]},Y:{ss[1]}"); //HandleGuaranteeDoc(@"C:\Users\zhangwy\Desktop\线路事故紧急抢修单.doc", @"C:\Users\zhangwy\Desktop\线路事故紧急抢修单222.pdf"); var rr= GetDistance( 33.392427, 120.198509, 33.401484,120.213206); Console.WriteLine(rr); Console.ReadLine(); } private const double EARTH_RADIUS = 6378137; /// /// 计算两点位置的距离,返回两点的距离,单位 米 /// 该公式为GOOGLE提供,误差小于0.2米 /// /// 第一点纬度 /// 第一点经度 /// 第二点纬度 /// 第二点经度 /// public static double GetDistance(double lat1, double lng1, double lat2, double lng2) { double radLat1 = Rad(lat1); double radLng1 = Rad(lng1); double radLat2 = Rad(lat2); double radLng2 = Rad(lng2); double a = radLat1 - radLat2; double b = radLng1 - radLng2; double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS; return result; } /// /// 经纬度转化成弧度 /// /// /// private static double Rad(double d) { return (double) d * Math.PI / 180d; } /// /// 非危保函(将指定路径的模板Path_TempleteDoc输出至Path_out路径) /// /// 模板文件路径,包含文件名 /// 文件输出路径,包含文件名 private static void HandleGuaranteeDoc(string Path_TempleteDoc, string Path_out) { string tempFile = Path.GetFullPath(Path_TempleteDoc).ToString(); //获取模板路径,这个根据个人模板路径而定。 Document doc = new Document(tempFile); DocumentBuilder builder = new DocumentBuilder(doc); //操作word Dictionary dic = new Dictionary(); //创建键值对 第一个string 为书签名称 第二个string为要填充的数据 dic.Add("XkUser", "zhangsan忘"); dic.Add("CompanyName", "国网上海松江供电"); dic.Add("DutyUser", "zhangsan2"); dic.Add("OrderNo", "K-1912-抢001"); dic.Add("TaskInfo", "xx杆变故障抢修"); foreach (var key in dic.Keys) //循环键值对 { builder.MoveToBookmark(key); //将光标移入书签的位置 builder.Write(dic[key]); //填充值 } //doc.Save(Path_out); //保存word doc.Save(Path_out, SaveFormat.Pdf); } static double pi = Math.PI; static double sm_a = 6378137.0; static double sm_b = 6356752.314; //static double sm_EccSquared = 6.69437999013e-03; static double UTMScaleFactor = 0.9996; //得到的结果是:x坐标,y坐标,区域编号 public static double[] LatLonToUtm (double lat, double lon) { double zone = Math.Floor((lon + 180.0) / 6) + 1; double cm = UtmCentralMeridian(zone); MapLatLonToXy(lat / 180.0 * pi, lon / 180 * pi, cm, out var xy); /* Adjust easting and northing for UTM system. */ xy[0] = xy[0] * UTMScaleFactor + 500000.0; xy[1] = xy[1] * UTMScaleFactor; if (xy[1] < 0.0) { xy[1] = xy[1] + 10000000.0; } return new double[] { xy[0], xy[1], zone }; } public static double UtmCentralMeridian (double zone) { double cmeridian; double deg = -183.0 + (zone * 6.0); cmeridian = deg / 180.0 * pi; return cmeridian; } internal static void MapLatLonToXy (double phi, double lambda, double lambda0, out double[] xy) { /* Precalculate ep2 */ var ep2 = (Math.Pow(sm_a, 2.0) - Math.Pow(sm_b, 2.0)) / Math.Pow(sm_b, 2.0); /* Precalculate nu2 */ var nu2 = ep2 * Math.Pow(Math.Cos(phi), 2.0); /* Precalculate N */ var N = Math.Pow(sm_a, 2.0) / (sm_b * Math.Sqrt(1 + nu2)); /* Precalculate t */ var t = Math.Tan (phi); var t2 = t * t; /* Precalculate l */ var l = lambda - lambda0; /* Precalculate coefficients for l**n in the equations below so a normal human being can read the expressions for easting and northing -- l**1 and l**2 have coefficients of 1.0 */ var l3coef = 1.0 - t2 + nu2; var l4coef = 5.0 - t2 + 9 * nu2 + 4.0 * (nu2 * nu2); var l5coef = 5.0 - 18.0 * t2 + (t2 * t2) + 14.0 * nu2 - 58.0 * t2 * nu2; var l6coef = 61.0 - 58.0 * t2 + (t2 * t2) + 270.0 * nu2 - 330.0 * t2 * nu2; var l7coef = 61.0 - 479.0 * t2 + 179.0 * (t2 * t2) - (t2 * t2 * t2); var l8coef = 1385.0 - 3111.0 * t2 + 543.0 * (t2 * t2) - (t2 * t2 * t2); /* Calculate easting (x) */ xy = new double[2]; xy[0] = N * Math.Cos (phi) * l + (N / 6.0 * Math.Pow (Math.Cos (phi), 3.0) * l3coef * Math.Pow (l, 3.0)) + (N / 120.0 * Math.Pow(Math.Cos(phi), 5.0) * l5coef * Math.Pow(l, 5.0)) + (N / 5040.0 * Math.Pow(Math.Cos(phi), 7.0) * l7coef * Math.Pow(l, 7.0)); /* Calculate northing (y) */ xy[1] = ArcLengthOfMeridian (phi) + (t / 2.0 * N * Math.Pow(Math.Cos(phi), 2.0) * Math.Pow(l, 2.0)) + (t / 24.0 * N * Math.Pow(Math.Cos(phi), 4.0) * l4coef * Math.Pow(l, 4.0)) + (t / 720.0 * N * Math.Pow(Math.Cos(phi), 6.0) * l6coef * Math.Pow(l, 6.0)) + (t / 40320.0 * N * Math.Pow(Math.Cos(phi), 8.0) * l8coef * Math.Pow(l, 8.0)); return; } internal static double ArcLengthOfMeridian(double phi) { double alpha, beta, gamma, delta, epsilon, n; double result; /* Precalculate n */ n = (sm_a - sm_b) / (sm_a + sm_b); /* Precalculate alpha */ alpha = ((sm_a + sm_b) / 2.0) * (1.0 + (Math.Pow(n, 2.0) / 4.0) + (Math.Pow(n, 4.0) / 64.0)); /* Precalculate beta */ beta = (-3.0 * n / 2.0) + (9.0 * Math.Pow(n, 3.0) / 16.0) + (-3.0 * Math.Pow(n, 5.0) / 32.0); /* Precalculate gamma */ gamma = (15.0 * Math.Pow(n, 2.0) / 16.0) + (-15.0 * Math.Pow(n, 4.0) / 32.0); /* Precalculate delta */ delta = (-35.0 * Math.Pow(n, 3.0) / 48.0) + (105.0 * Math.Pow(n, 5.0) / 256.0); /* Precalculate epsilon */ epsilon = (315.0 * Math.Pow(n, 4.0) / 512.0); /* Now calculate the sum of the series and return */ result = alpha * (phi + (beta * Math.Sin (2.0 * phi)) + (gamma * Math.Sin(4.0 * phi)) + (delta * Math.Sin(6.0 * phi)) + (epsilon * Math.Sin(8.0 * phi))); return result; } // 经纬度坐标转换为上海地方坐标 public static double[] Wgs84Tosh(double lat, double lon) { double xx, yy, r2d, tolat, tolon, rearth, PI; PI = 3.141592653589793; r2d = 57.2957795131; tolat = (31 + (14.0 + 7.55996 / 60.0) / 60.0) / r2d; tolon = (121.0 + (28.0 + 1.80651 / 60.0) / 60) / r2d; rearth = 6371006.84; double hor, frlat, frlon, gcdist, clatf, clatt, slatf, slatt, gcbrg; double dlon, cdlon, sdlon, sdist, cdist, sbrg, cbrg, temp; double intlat, intlon; intlat = lat; intlon = lon; frlat = lat / r2d; frlon = lon / r2d; clatt = Math.Cos(frlat); clatf = Math.Cos(tolat); slatt = Math.Sin(frlat); slatf = Math.Sin(tolat); dlon = frlon - tolon; cdlon = Math.Cos(dlon); sdlon = Math.Sin(dlon); cdist = slatf * slatt + clatf * clatt * cdlon; temp = (clatt * sdlon) * (clatt * sdlon) + (clatf * slatt - slatf * clatt * cdlon) * (clatf * slatt - slatf * clatt * cdlon); sdist = Math.Sqrt(Math.Abs(temp)); if ((Math.Abs(sdist) > 1e-7) || (Math.Abs(cdist) > 1e-7)) gcdist = Math.Atan2(sdist, cdist); else gcdist = 0; sbrg = sdlon * clatt; cbrg = (clatf * slatt - slatf * clatt * cdlon); if ((Math.Abs(sbrg) > 1e-7) || (Math.Abs(cbrg) > 1e-7)) { temp = Math.Atan2(sbrg, cbrg); while (temp < 0) { temp = temp + 2 * PI; gcbrg = temp; } } else gcbrg = 0; hor = gcdist * rearth; xx = hor * Math.Sin(temp); yy = hor * Math.Cos(temp); double[] res = new double[2]; res[0] = xx; res[1] = yy; return res; } } }