Program.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Aspose.Words;
  8. namespace GpsConvert
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //double[] utm = LatLonToUtm(30.65156708, 103.6880587);
  15. // double[] utm = LatLonToUtm(31.108761,121.314085);
  16. // Console.WriteLine($"X:{utm[0]},Y:{utm[1]},区域:{utm[2]}");
  17. //var ss= Wgs84Tosh(31.0158865505,121.2238203740);
  18. // Console.WriteLine($"X:{ss[0]},Y:{ss[1]}");
  19. //HandleGuaranteeDoc(@"C:\Users\zhangwy\Desktop\线路事故紧急抢修单.doc", @"C:\Users\zhangwy\Desktop\线路事故紧急抢修单222.pdf");
  20. var rr= GetDistance( 33.392427, 120.198509, 33.401484,120.213206);
  21. Console.WriteLine(rr);
  22. Console.ReadLine();
  23. }
  24. private const double EARTH_RADIUS = 6378137;
  25. /// <summary>
  26. /// 计算两点位置的距离,返回两点的距离,单位 米
  27. /// 该公式为GOOGLE提供,误差小于0.2米
  28. /// </summary>
  29. /// <param name="lat1">第一点纬度</param>
  30. /// <param name="lng1">第一点经度</param>
  31. /// <param name="lat2">第二点纬度</param>
  32. /// <param name="lng2">第二点经度</param>
  33. /// <returns></returns>
  34. public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
  35. {
  36. double radLat1 = Rad(lat1);
  37. double radLng1 = Rad(lng1);
  38. double radLat2 = Rad(lat2);
  39. double radLng2 = Rad(lng2);
  40. double a = radLat1 - radLat2;
  41. double b = radLng1 - radLng2;
  42. 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;
  43. return result;
  44. }
  45. /// <summary>
  46. /// 经纬度转化成弧度
  47. /// </summary>
  48. /// <param name="d"></param>
  49. /// <returns></returns>
  50. private static double Rad(double d)
  51. {
  52. return (double) d * Math.PI / 180d;
  53. }
  54. /// <summary>
  55. /// 非危保函(将指定路径的模板Path_TempleteDoc输出至Path_out路径)
  56. /// </summary>
  57. /// <param name="Path_TempleteDoc">模板文件路径,包含文件名</param>
  58. /// <param name="Path_out">文件输出路径,包含文件名</param>
  59. private static void HandleGuaranteeDoc(string Path_TempleteDoc, string Path_out)
  60. {
  61. string tempFile = Path.GetFullPath(Path_TempleteDoc).ToString(); //获取模板路径,这个根据个人模板路径而定。
  62. Document doc = new Document(tempFile);
  63. DocumentBuilder builder = new DocumentBuilder(doc); //操作word
  64. Dictionary<string, string> dic = new Dictionary<string, string>(); //创建键值对 第一个string 为书签名称 第二个string为要填充的数据
  65. dic.Add("XkUser", "zhangsan忘");
  66. dic.Add("CompanyName", "国网上海松江供电");
  67. dic.Add("DutyUser", "zhangsan2");
  68. dic.Add("OrderNo", "K-1912-抢001");
  69. dic.Add("TaskInfo", "xx杆变故障抢修");
  70. foreach (var key in dic.Keys) //循环键值对
  71. {
  72. builder.MoveToBookmark(key); //将光标移入书签的位置
  73. builder.Write(dic[key]); //填充值
  74. }
  75. //doc.Save(Path_out); //保存word
  76. doc.Save(Path_out, SaveFormat.Pdf);
  77. }
  78. static double pi = Math.PI;
  79. static double sm_a = 6378137.0;
  80. static double sm_b = 6356752.314;
  81. //static double sm_EccSquared = 6.69437999013e-03;
  82. static double UTMScaleFactor = 0.9996;
  83. //得到的结果是:x坐标,y坐标,区域编号
  84. public static double[] LatLonToUtm (double lat, double lon)
  85. {
  86. double zone = Math.Floor((lon + 180.0) / 6) + 1;
  87. double cm = UtmCentralMeridian(zone);
  88. MapLatLonToXy(lat / 180.0 * pi, lon / 180 * pi, cm, out var xy);
  89. /* Adjust easting and northing for UTM system. */
  90. xy[0] = xy[0] * UTMScaleFactor + 500000.0;
  91. xy[1] = xy[1] * UTMScaleFactor;
  92. if (xy[1] < 0.0)
  93. {
  94. xy[1] = xy[1] + 10000000.0;
  95. }
  96. return new double[] { xy[0], xy[1], zone };
  97. }
  98. public static double UtmCentralMeridian (double zone)
  99. {
  100. double cmeridian;
  101. double deg = -183.0 + (zone * 6.0);
  102. cmeridian = deg / 180.0 * pi;
  103. return cmeridian;
  104. }
  105. internal static void MapLatLonToXy (double phi, double lambda, double lambda0, out double[] xy)
  106. {
  107. /* Precalculate ep2 */
  108. var ep2 = (Math.Pow(sm_a, 2.0) - Math.Pow(sm_b, 2.0)) / Math.Pow(sm_b, 2.0);
  109. /* Precalculate nu2 */
  110. var nu2 = ep2 * Math.Pow(Math.Cos(phi), 2.0);
  111. /* Precalculate N */
  112. var N = Math.Pow(sm_a, 2.0) / (sm_b * Math.Sqrt(1 + nu2));
  113. /* Precalculate t */
  114. var t = Math.Tan (phi);
  115. var t2 = t * t;
  116. /* Precalculate l */
  117. var l = lambda - lambda0;
  118. /* Precalculate coefficients for l**n in the equations below
  119. so a normal human being can read the expressions for easting
  120. and northing
  121. -- l**1 and l**2 have coefficients of 1.0 */
  122. var l3coef = 1.0 - t2 + nu2;
  123. var l4coef = 5.0 - t2 + 9 * nu2 + 4.0 * (nu2 * nu2);
  124. var l5coef = 5.0 - 18.0 * t2 + (t2 * t2) + 14.0 * nu2
  125. - 58.0 * t2 * nu2;
  126. var l6coef = 61.0 - 58.0 * t2 + (t2 * t2) + 270.0 * nu2
  127. - 330.0 * t2 * nu2;
  128. var l7coef = 61.0 - 479.0 * t2 + 179.0 * (t2 * t2) - (t2 * t2 * t2);
  129. var l8coef = 1385.0 - 3111.0 * t2 + 543.0 * (t2 * t2) - (t2 * t2 * t2);
  130. /* Calculate easting (x) */
  131. xy = new double[2];
  132. xy[0] = N * Math.Cos (phi) * l
  133. + (N / 6.0 * Math.Pow (Math.Cos (phi), 3.0) * l3coef * Math.Pow (l, 3.0))
  134. + (N / 120.0 * Math.Pow(Math.Cos(phi), 5.0) * l5coef * Math.Pow(l, 5.0))
  135. + (N / 5040.0 * Math.Pow(Math.Cos(phi), 7.0) * l7coef * Math.Pow(l, 7.0));
  136. /* Calculate northing (y) */
  137. xy[1] = ArcLengthOfMeridian (phi)
  138. + (t / 2.0 * N * Math.Pow(Math.Cos(phi), 2.0) * Math.Pow(l, 2.0))
  139. + (t / 24.0 * N * Math.Pow(Math.Cos(phi), 4.0) * l4coef * Math.Pow(l, 4.0))
  140. + (t / 720.0 * N * Math.Pow(Math.Cos(phi), 6.0) * l6coef * Math.Pow(l, 6.0))
  141. + (t / 40320.0 * N * Math.Pow(Math.Cos(phi), 8.0) * l8coef * Math.Pow(l, 8.0));
  142. return;
  143. }
  144. internal static double ArcLengthOfMeridian(double phi)
  145. {
  146. double alpha, beta, gamma, delta, epsilon, n;
  147. double result;
  148. /* Precalculate n */
  149. n = (sm_a - sm_b) / (sm_a + sm_b);
  150. /* Precalculate alpha */
  151. alpha = ((sm_a + sm_b) / 2.0)
  152. * (1.0 + (Math.Pow(n, 2.0) / 4.0) + (Math.Pow(n, 4.0) / 64.0));
  153. /* Precalculate beta */
  154. beta = (-3.0 * n / 2.0) + (9.0 * Math.Pow(n, 3.0) / 16.0)
  155. + (-3.0 * Math.Pow(n, 5.0) / 32.0);
  156. /* Precalculate gamma */
  157. gamma = (15.0 * Math.Pow(n, 2.0) / 16.0)
  158. + (-15.0 * Math.Pow(n, 4.0) / 32.0);
  159. /* Precalculate delta */
  160. delta = (-35.0 * Math.Pow(n, 3.0) / 48.0)
  161. + (105.0 * Math.Pow(n, 5.0) / 256.0);
  162. /* Precalculate epsilon */
  163. epsilon = (315.0 * Math.Pow(n, 4.0) / 512.0);
  164. /* Now calculate the sum of the series and return */
  165. result = alpha
  166. * (phi + (beta * Math.Sin (2.0 * phi))
  167. + (gamma * Math.Sin(4.0 * phi))
  168. + (delta * Math.Sin(6.0 * phi))
  169. + (epsilon * Math.Sin(8.0 * phi)));
  170. return result;
  171. }
  172. // 经纬度坐标转换为上海地方坐标
  173. public static double[] Wgs84Tosh(double lat, double lon)
  174. {
  175. double xx, yy, r2d, tolat, tolon, rearth, PI;
  176. PI = 3.141592653589793;
  177. r2d = 57.2957795131;
  178. tolat = (31 + (14.0 + 7.55996 / 60.0) / 60.0) / r2d;
  179. tolon = (121.0 + (28.0 + 1.80651 / 60.0) / 60) / r2d;
  180. rearth = 6371006.84;
  181. double hor, frlat, frlon, gcdist, clatf, clatt, slatf, slatt, gcbrg;
  182. double dlon, cdlon, sdlon, sdist, cdist, sbrg, cbrg, temp;
  183. double intlat, intlon;
  184. intlat = lat;
  185. intlon = lon;
  186. frlat = lat / r2d;
  187. frlon = lon / r2d;
  188. clatt = Math.Cos(frlat);
  189. clatf = Math.Cos(tolat);
  190. slatt = Math.Sin(frlat);
  191. slatf = Math.Sin(tolat);
  192. dlon = frlon - tolon;
  193. cdlon = Math.Cos(dlon);
  194. sdlon = Math.Sin(dlon);
  195. cdist = slatf * slatt + clatf * clatt * cdlon;
  196. temp = (clatt * sdlon) * (clatt * sdlon) + (clatf * slatt - slatf * clatt * cdlon) * (clatf * slatt - slatf * clatt * cdlon);
  197. sdist = Math.Sqrt(Math.Abs(temp));
  198. if ((Math.Abs(sdist) > 1e-7) || (Math.Abs(cdist) > 1e-7))
  199. gcdist = Math.Atan2(sdist, cdist);
  200. else
  201. gcdist = 0;
  202. sbrg = sdlon * clatt;
  203. cbrg = (clatf * slatt - slatf * clatt * cdlon);
  204. if ((Math.Abs(sbrg) > 1e-7) || (Math.Abs(cbrg) > 1e-7))
  205. {
  206. temp = Math.Atan2(sbrg, cbrg);
  207. while (temp < 0)
  208. {
  209. temp = temp + 2 * PI;
  210. gcbrg = temp;
  211. }
  212. }
  213. else
  214. gcbrg = 0;
  215. hor = gcdist * rearth;
  216. xx = hor * Math.Sin(temp);
  217. yy = hor * Math.Cos(temp);
  218. double[] res = new double[2];
  219. res[0] = xx;
  220. res[1] = yy;
  221. return res;
  222. }
  223. }
  224. }