| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- 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]}");
- Console.ReadLine();
- }
- 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;
- }
- }
- }
|