protected LatLonCoordinates ToLatLon() { double k0 = 0.9996; double a = Datum.a; double e2 = Datum.e2; double ep2 = (e2) / (1 - e2); double e1 = (1 - Math.Sqrt(1 - e2)) / (1 + Math.Sqrt(1 - e2)); int nUTMZoneLen = UtmZone.Length; char ZoneLetter = UtmZone[nUTMZoneLen - 1]; //int NorthernHemisphere; //1 for northern hemispher, 0 for southern double x = Easting - 500000.0; //remove 500,000 meter offset for longitude double y = Northing; int ZoneNumber = Int16.Parse(UtmZone.Substring(0, nUTMZoneLen - 1)); if ((ZoneLetter - 'N') >= 0) { //point is in northern hemisphere } else { //point is in southern hemisphere y -= 10000000.0;//remove 10,000,000 meter offset used for southern hemisphere } double LongOriginRad = ((ZoneNumber - 1) * 6 - 180 + 3) * Angle.DEG2RAD; //+3 puts origin in middle of zone double N1, T1, C1, R1, D, M; double mu, phi; M = y / k0; mu = M / (a * (1 - e2 / 4 - 3 * e2 * e2 / 64 - 5 * e2 * e2 * e2 / 256)); phi = mu + (3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * Math.Sin(2 * mu) + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * Math.Sin(4 * mu) + (151 * e1 * e1 * e1 / 96) * Math.Sin(6 * mu); N1 = a / Math.Sqrt(1 - e2 * Math.Sin(phi) * Math.Sin(phi)); T1 = Math.Tan(phi) * Math.Tan(phi); C1 = ep2 * Math.Cos(phi) * Math.Cos(phi); R1 = a * (1 - e2) / Math.Pow(1 - e2 * Math.Sin(phi) * Math.Sin(phi), 1.5); D = x / (N1 * k0); var latitude = new Angle() { Radians = (phi - (N1 * Math.Tan(phi) / R1) * (D * D / 2 - (5 + 3 * T1 + 10 * C1 - 4 * C1 * C1 - 9 * ep2) * D * D * D * D / 24 + (61 + 90 * T1 + 298 * C1 + 45 * T1 * T1 - 252 * ep2 - 3 * C1 * C1) * D * D * D * D * D * D / 720)) }; var longitude = new Angle() { Radians = LongOriginRad + ((D - (1 + 2 * T1 + C1) * D * D * D / 6 + (5 - 2 * C1 + 28 * T1 - 3 * C1 * C1 + 8 * ep2 + 24 * T1 * T1) * D * D * D * D * D / 120) / Math.Cos(phi)) }; return(new LatLonCoordinates(Datum, latitude, longitude, Altitude)); }
public override UtmCoordinates ToUtm(Datum targetDatum, int targetZoneNumber = 0) { UtmCoordinates utmc; if (Datum == targetDatum && int.Parse(UtmZone.Substring(0, 2)) == targetZoneNumber) { //already in the target datum and zone utmc = this; } else { //transform to latlon var llc = ToLatLon(); //transform to UTM (another datum) utmc = llc.ToUtm(targetDatum, targetZoneNumber); } return(utmc); }