public static UtmVal ConvertLatLonToUtm(string temputmZone, string templat, string temnlon) { ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; string url = "https://geodesy.noaa.gov/api/ncat/llh?lat=" + templat + "&lon=" + temnlon + "&inDatum=nad83(1986)&outDatum=nad83(2011)&utmZone=" + temputmZone; var request = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); dynamic json = System.Web.Helpers.Json.Decode(responseString); Console.WriteLine(json.srcLon); UtmVal ut = new UtmVal { easting = json.utmEasting, northing = json.utmNorthing, utmZone = json.utmZone }; return(ut); }
//public static LattLong UTM2GEO(string temputmZone, string temnorthing, string temeasting) //{ //} public static UtmVal GEO2UTM(double Longitude, double Latitude, bool North) { //Initial var A = 6378137; var B = 6356752.314; var e = (Math.Pow((Math.Pow(A, 2) - Math.Pow(B, 2)), (0.5))) / (A); var ep = (Math.Pow((Math.Pow(A, 2) - Math.Pow(B, 2)), (0.5))) / (B); //var e = 0.081819191; //var ep = 0.082094438; var C = Math.Pow(A, 2) / B; var RLng = (Longitude * Math.PI) / 180; var RLat = (Latitude * Math.PI) / 180; //First Calculate var Spl = (Math.Truncate((Longitude / 6))) + 31; var Mer = (6 * Spl) - 183; var DL = RLng - ((Mer * Math.PI) / 180); var D = Math.Cos(RLat) * Math.Sin(DL); var XI = Math.Log((1 + D) / (1 - D)) / 2; //ξ var eta = Math.Atan(Math.Tan(RLat) / Math.Cos(DL)) - RLat; //η var ni = (C / (Math.Pow((1 + (Math.Pow(ep, 2)) * (Math.Pow((Math.Cos(RLat)), 2))), (0.5)))) * 0.9996; //∋ var zeta = ((Math.Pow(ep, 2)) / 2) * (Math.Pow(XI, 2)) * (Math.Pow((Math.Cos(RLat)), 2)); //ζ //second Calculate var D1 = Math.Sin(2 * RLat); var D2 = D1 * (Math.Pow((Math.Cos(RLat)), 2)); var J2 = RLat + (D1 / 2); var J4 = (3 * J2 + D2) / 4; var J6 = (5 * J4 + D2 * (Math.Pow((Math.Cos(RLat)), 2))) / 3; var alfa = (3 * Math.Pow(ep, 2)) / 4; var beta = (5 * Math.Pow(alfa, 2)) / 3; var gama = (35 * Math.Pow(alfa, 3)) / 27; var bf = 0.9996 * C * (RLat - (alfa * J2) + (beta * J4) - (gama * J6)); //Final Calculate var Easting = XI * ni * (1 + (zeta / 3)) + 500000; var Northing = eta * ni * (1 + zeta) + bf; if (North) { Northing = Northing + 10000000; } double tempcalc = 0; string latdegreestemp = (Math.Truncate(Latitude)).ToString(); tempcalc = Latitude - Math.Truncate(Latitude); tempcalc = tempcalc * 60; string latminutestemp = (Math.Truncate(tempcalc)).ToString(); tempcalc = tempcalc - Math.Truncate(tempcalc); tempcalc = tempcalc * 60; string latsecondstemp = tempcalc.ToString(); string londegreestemp = (Math.Truncate(Longitude)).ToString(); tempcalc = Latitude - Math.Truncate(Longitude); tempcalc = tempcalc * 60; string lonminutestemp = (Math.Truncate(tempcalc)).ToString(); tempcalc = tempcalc - Math.Truncate(tempcalc); tempcalc = tempcalc * 60; string lonsecondstemp = tempcalc.ToString(); UtmVal uv = new UtmVal { easting = Easting.ToString(), northing = Northing.ToString(), utmZone = Spl.ToString(), lat = Latitude.ToString(), latdegrees = latdegreestemp, latminutes = latminutestemp, latseconds = latsecondstemp, lon = Longitude.ToString(), londegrees = londegreestemp, lonminutes = lonminutestemp, lonseconds = lonsecondstemp }; return(uv); }
public static UtmVal UTM2GEO(double X, double Y, int temputmZone, bool North) { //Initial var A = 6378137; var B = 6356752.314; var e = (Math.Pow((Math.Pow(A, 2) - Math.Pow(B, 2)), (0.5))) / (A); var ep = (Math.Pow((Math.Pow(A, 2) - Math.Pow(B, 2)), (0.5))) / (B); var ep2 = Math.Pow(ep, 2); var C = Math.Pow(A, 2) / B; var Mer = (6 * temputmZone) - 183; var Ypolarity = Y; if (North == false) { Ypolarity = Y - 10000000; } var Fip = Ypolarity / (6366197.724 * 0.9996); var ni = (C / (Math.Pow((1 + (Math.Pow(ep, 2)) * (Math.Pow((Math.Cos(Fip)), 2))), (0.5)))) * 0.9996;//∋ var a = (X - 500000) / ni; //second Calculate var D1 = Math.Sin(2 * Fip); var D2 = D1 * (Math.Pow((Math.Cos(Fip)), 2)); var J2 = Fip + (D1 / 2); var J4 = (3 * J2 + D2) / 4; var J6 = (5 * J4 + D2 * (Math.Pow((Math.Cos(Fip)), 2))) / 3; var alfa = (3 * ep2) / 4; var beta = (5 * Math.Pow(alfa, 2)) / 3; var gama = (35 * Math.Pow(alfa, 3)) / 27; var bf = 0.9996 * C * (Fip - (alfa * J2) + (beta * J4) - (gama * J6)); var b = (Ypolarity - bf) / ni; var zeta = ((ep2 * (Math.Pow(a, 2))) / 2) * (Math.Pow((Math.Cos(Fip)), 2)); //ζ var XI = a * (1 - (zeta / 3)); //ξ var eta = b * (1 - zeta) + Fip; //η var SenXi = (Math.Exp(XI) - Math.Exp((-1) * (XI))) / 2; var delta = Math.Atan((SenXi / (Math.Cos(eta)))); var tau = Math.Atan(((Math.Cos(delta)) * (Math.Tan(eta)))); var Fi = Fip + (1 + Math.Pow((ep2 * Math.Cos(Fip)), 2) - ((3 * Math.Sin(Fip)) / 2) * Math.Cos(Fip) * (tau - Fip)) * (tau - Fip); var lattemp = (Fi / (Math.PI)) * 180; var longtemp = ((delta / (Math.PI)) * 180) + Mer; double tempcalc = 0; string latdegreestemp = (Math.Truncate(lattemp)).ToString(); tempcalc = lattemp - Math.Truncate(lattemp); tempcalc = tempcalc * 60; string latminutestemp = (Math.Truncate(tempcalc)).ToString(); tempcalc = tempcalc - Math.Truncate(tempcalc); tempcalc = tempcalc * 60; string latsecondstemp = tempcalc.ToString(); string londegreestemp = (Math.Truncate(longtemp)).ToString(); tempcalc = longtemp - Math.Truncate(longtemp); tempcalc = tempcalc * 60; string lonminutestemp = (Math.Truncate(tempcalc)).ToString(); tempcalc = tempcalc - Math.Truncate(tempcalc); tempcalc = tempcalc * 60; string lonsecondstemp = tempcalc.ToString(); UtmVal uv = new UtmVal { easting = X.ToString(), northing = Y.ToString(), utmZone = temputmZone.ToString(), lat = lattemp.ToString(), latdegrees = latdegreestemp, latminutes = latminutestemp, latseconds = latsecondstemp, lon = longtemp.ToString(), londegrees = londegreestemp, lonminutes = lonminutestemp, lonseconds = lonsecondstemp }; return(uv); }