/// <summary> /// Attempts to parse a string into a CoordinatePart. /// </summary> /// <param name="value">CoordinatePart string</param> /// <param name="coordinatePart">CoordinatePart object to populate</param> /// <returns>boolean</returns> /// <example> /// The following example demonstrates how to parse a latitude from a string. /// <code> /// CoordinatePart cp; /// if(CoordinatePart.TryParse("N 32.891º", out cp)) /// { /// Console.WriteLine(cp); //N 32º 53' 28.212" /// } /// </code> /// </example> public static bool TryParse(string value, out CoordinatePart coordinatePart) { coordinatePart = null; if (FormatFinder_CoordPart.TryParse(value, out coordinatePart)) { return(true); } return(false); }
/// <summary> /// Parses a string into a CoordinatePart with specified part type (latitude/longitude). /// </summary> /// <param name="value">CoordinatePart string</param> /// <param name="cType">CoordinateType</param> /// <returns>CoordinatePart</returns> /// <example> /// The following example demonstrates how to parse a latitude from a string. /// Latitude is specified so that the parser knows to attempt parse in latitude. /// <code> /// CoordinatePart cp = CoordinatePart.Parse("-32.891º", CoordinateType.Long); /// </code> /// </example> public static CoordinatePart Parse(string value, CoordinateType cType) { CoordinatePart coordinatePart = null; if (TryParse(value, cType, out coordinatePart)) { return(coordinatePart); } throw new FormatException(string.Format("Input CoordinatePart \"{0}\" was not in a correct format.", value)); }
/// <summary> /// Creates a Coordinate object with default values and a custom datum. /// </summary> /// <remarks> /// Default Coordinate objects will initialize with a latitude and longitude of 0 degrees, /// a GeoDate of 1900-1-1 00:00:00. All properties will be set to EagerLoaded. /// </remarks> internal Coordinate(double equatorialRadius, double inverseFlattening, bool t) { FormatOptions = new CoordinateFormatOptions(); geoDate = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc); latitude = new CoordinatePart(CoordinateType.Lat); longitude = new CoordinatePart(CoordinateType.Long); latitude.parent = this; longitude.parent = this; celestialInfo = new Celestial(); utm = new UniversalTransverseMercator(latitude.ToDouble(), longitude.ToDouble(), this, equatorialRadius, inverseFlattening); mgrs = new MilitaryGridReferenceSystem(utm); cartesian = new Cartesian(this); ecef = new ECEF(this); EagerLoadSettings = new EagerLoad(); Set_Datum(equatorialRadius, inverseFlattening); }
/// <summary> /// Attempts to parse a string into a CoordinatePart. /// </summary> /// <param name="value">CoordinatePart string</param> /// <param name="cType">CoordinateType</param> /// <param name="coordinatePart">CoordinatePart object to populate</param> /// <returns>boolean</returns> /// <example> /// The following example demonstrates how to parse a latitude from a string. /// <code> /// CoordinatePart cp; /// if(CoordinatePart.TryParse("-32.891º", CoordinateType.Long, out cp)) /// { /// Console.WriteLine(cp); //W 32º 53' 27.6" /// } /// </code> /// </example> public static bool TryParse(string value, CoordinateType cType, out CoordinatePart coordinatePart) { coordinatePart = null; //Comma at beginning parses to long //Asterisk forces lat if (cType == CoordinateType.Long) { value = "," + value; } else { value = "*" + value; } if (FormatFinder_CoordPart.TryParse(value, out coordinatePart)) { return(true); } return(false); }
private static Coordinate UTMtoLatLong(double x, double y, double zone, double equatorialRadius, double flattening) { //x easting //y northing //http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html double phif, Nf, Nfpow, nuf2, ep2, tf, tf2, tf4, cf; double x1frac, x2frac, x3frac, x4frac, x5frac, x6frac, x7frac, x8frac; double x2poly, x3poly, x4poly, x5poly, x6poly, x7poly, x8poly; double sm_a = equatorialRadius; double sm_b = equatorialRadius * (1 - (1.0 / flattening)); //Polar Radius /* Get the value of phif, the footpoint latitude. */ phif = FootpointLatitude(y, equatorialRadius, flattening); /* Precalculate ep2 */ ep2 = (Math.Pow(sm_a, 2.0) - Math.Pow(sm_b, 2.0)) / Math.Pow(sm_b, 2.0); /* Precalculate cos (phif) */ cf = Math.Cos(phif); /* Precalculate nuf2 */ nuf2 = ep2 * Math.Pow(cf, 2.0); /* Precalculate Nf and initialize Nfpow */ Nf = Math.Pow(sm_a, 2.0) / (sm_b * Math.Sqrt(1 + nuf2)); Nfpow = Nf; /* Precalculate tf */ tf = Math.Tan(phif); tf2 = tf * tf; tf4 = tf2 * tf2; /* Precalculate fractional coefficients for x**n in the equations * below to simplify the expressions for latitude and longitude. */ x1frac = 1.0 / (Nfpow * cf); Nfpow *= Nf; /* now equals Nf**2) */ x2frac = tf / (2.0 * Nfpow); Nfpow *= Nf; /* now equals Nf**3) */ x3frac = 1.0 / (6.0 * Nfpow * cf); Nfpow *= Nf; /* now equals Nf**4) */ x4frac = tf / (24.0 * Nfpow); Nfpow *= Nf; /* now equals Nf**5) */ x5frac = 1.0 / (120.0 * Nfpow * cf); Nfpow *= Nf; /* now equals Nf**6) */ x6frac = tf / (720.0 * Nfpow); Nfpow *= Nf; /* now equals Nf**7) */ x7frac = 1.0 / (5040.0 * Nfpow * cf); Nfpow *= Nf; /* now equals Nf**8) */ x8frac = tf / (40320.0 * Nfpow); /* Precalculate polynomial coefficients for x**n. * -- x**1 does not have a polynomial coefficient. */ x2poly = -1.0 - nuf2; x3poly = -1.0 - 2 * tf2 - nuf2; x4poly = 5.0 + 3.0 * tf2 + 6.0 * nuf2 - 6.0 * tf2 * nuf2 - 3.0 * (nuf2 * nuf2) - 9.0 * tf2 * (nuf2 * nuf2); x5poly = 5.0 + 28.0 * tf2 + 24.0 * tf4 + 6.0 * nuf2 + 8.0 * tf2 * nuf2; x6poly = -61.0 - 90.0 * tf2 - 45.0 * tf4 - 107.0 * nuf2 + 162.0 * tf2 * nuf2; x7poly = -61.0 - 662.0 * tf2 - 1320.0 * tf4 - 720.0 * (tf4 * tf2); x8poly = 1385.0 + 3633.0 * tf2 + 4095.0 * tf4 + 1575 * (tf4 * tf2); /* Calculate latitude */ double nLat = phif + x2frac * x2poly * (x * x) + x4frac * x4poly * Math.Pow(x, 4.0) + x6frac * x6poly * Math.Pow(x, 6.0) + x8frac * x8poly * Math.Pow(x, 8.0); /* Calculate longitude */ double nLong = zone + x1frac * x + x3frac * x3poly * Math.Pow(x, 3.0) + x5frac * x5poly * Math.Pow(x, 5.0) + x7frac * x7poly * Math.Pow(x, 7.0); double dLat = RadToDeg(nLat); double dLong = RadToDeg(nLong); if (dLat > 90) { dLat = 90; } if (dLat < -90) { dLat = -90; } if (dLong > 180) { dLong = 180; } if (dLong < -180) { dLong = -180; } Coordinate c = new Coordinate(equatorialRadius, flattening, true); CoordinatePart cLat = new CoordinatePart(dLat, CoordinateType.Lat, c); CoordinatePart cLng = new CoordinatePart(dLong, CoordinateType.Long, c); c.Latitude = cLat; c.Longitude = cLng; return(c); }
//Add main to Coordinate and tunnel to Format class. Add private methods to format. //WHEN PARSING NO EXCPETIONS FOR OUT OF RANGE ARGS WILL BE THROWN public static bool TryParse(string coordString, out CoordinatePart cp) { //Turn of eagerload for efficiency EagerLoad eg = new EagerLoad(); int type = 0; //0 = unspecifed, 1 = lat, 2 = long; eg.Cartesian = false; eg.Celestial = false; eg.UTM_MGRS = false; cp = null; Coordinate c = new Coordinate(eg); string s = coordString; s = s.Trim(); //Trim all spaces before and after string double[] d; if (s[0] == ',') { type = 2; s = s.Replace(",", ""); s = s.Trim(); } if (s[0] == '*') { type = 1; s = s.Replace("*", ""); s = s.Trim(); } if (TrySignedDegree(s, type, out d)) { try { switch (type) { case 0: //Attempt Lat first (default for signed) try { cp = new CoordinatePart(d[0], CoordinateType.Lat); c.Parse_Format = Parse_Format_Type.Signed_Degree; return(true); } catch { cp = new CoordinatePart(d[0], CoordinateType.Long); c.Parse_Format = Parse_Format_Type.Signed_Degree; return(true); } case 1: //Attempt Lat cp = new CoordinatePart(d[0], CoordinateType.Lat); c.Parse_Format = Parse_Format_Type.Signed_Degree; return(true); case 2: //Attempt long cp = new CoordinatePart(d[0], CoordinateType.Long); c.Parse_Format = Parse_Format_Type.Signed_Degree; return(true); } } catch { //silent fail } } //SIGNED DEGREE FAILED, REMOVE DASHES FOR OTHER FORMATS s = s.Replace("-", " "); //All other formats should contain 1 letter. if (Regex.Matches(s, @"[a-zA-Z]").Count != 1) { return(false); } //Should only contain 1 letter. //Get Coord Direction int direction = Find_Position(s); if (direction == -1) { return(false); //No direction found } //If Coordinate type int specified, look for mismatch if (type == 1 && (direction == 1 || direction == 3)) { return(false); //mismatch } if (type == 2 && (direction == 0 || direction == 2)) { return(false); //mismatch } CoordinateType t; if (direction == 0 || direction == 2) { t = CoordinateType.Lat; } else { t = CoordinateType.Long; } s = Regex.Replace(s, "[^0-9. ]", ""); //Remove directional character s = s.Trim(); //Trim all spaces before and after string //Try Decimal Degree with Direction if (TryDecimalDegree(s, direction, out d)) { try { cp = new CoordinatePart(d[0], t); c.Parse_Format = Parse_Format_Type.Decimal_Degree; return(true); } catch {//Parser failed try next method } } //Try DDM if (TryDegreeDecimalMinute(s, out d)) { try { //0 Degree //1 Minute //2 Direction (0 = N, 1 = E, 2 = S, 3 = W) cp = new CoordinatePart((int)d[0], d[1], (CoordinatesPosition)direction); c.Parse_Format = Parse_Format_Type.Degree_Decimal_Minute; return(true); } catch { //Parser failed try next method } } //Try DMS if (TryDegreeMinuteSecond(s, out d)) { try { //0 Degree //1 Minute //2 Second //3 Direction (0 = N, 1 = E, 2 = S, 3 = W) cp = new CoordinatePart((int)d[0], (int)d[1], d[2], (CoordinatesPosition)direction); c.Parse_Format = Parse_Format_Type.Degree_Minute_Second; return(true); } catch {//Parser failed try next method } } return(false); }
//Add main to Coordinate and tunnel to Format class. Add private methods to format. //WHEN PARSING NO EXCPETIONS FOR OUT OF RANGE ARGS WILL BE THROWN public static bool TryParse(string coordString, CartesianType ct, out Coordinate c) { try { //Turn of eagerload for efficiency EagerLoad eg = new EagerLoad(); eg.Cartesian = false; eg.Celestial = false; eg.UTM_MGRS = false; c = new Coordinate(eg); if (string.IsNullOrEmpty(coordString)) { return(false); } string s = coordString; s = s.Trim(); //Trim all spaces before and after string double[] d; //Try Signed Degree if (TrySignedDegree(s, out d)) { try { c = new Coordinate(d[0], d[1], eg); c.Parse_Format = Parse_Format_Type.Signed_Degree; return(true); } catch {//Parser failed try next method } } //Try Decimal Degree if (TryDecimalDegree(s, out d)) { try { c = new Coordinate(d[0], d[1], eg); c.Parse_Format = Parse_Format_Type.Decimal_Degree; return(true); } catch {//Parser failed try next method } } //Try DDM if (TryDegreeDecimalMinute(s, out d)) { try { //0 Lat Degree //1 Lat Minute //2 Lat Direction (0 = N, 1 = S) //3 Long Degree //4 Long Minute //5 Long Direction (0 = E, 1 = W) CoordinatesPosition latP = CoordinatesPosition.N; CoordinatesPosition lngP = CoordinatesPosition.E; if (d[2] != 0) { latP = CoordinatesPosition.S; } if (d[5] != 0) { lngP = CoordinatesPosition.W; } CoordinatePart lat = new CoordinatePart((int)d[0], d[1], latP); CoordinatePart lng = new CoordinatePart((int)d[3], d[4], lngP); c = new Coordinate(eg); c.Latitude = lat; c.Longitude = lng; c.Parse_Format = Parse_Format_Type.Degree_Decimal_Minute; return(true); } catch {//Parser failed try next method } } //Try DMS if (TryDegreeMinuteSecond(s, out d)) { try { //0 Lat Degree //1 Lat Minute //2 Lat Second //3 Lat Direction (0 = N, 1 = S) //4 Long Degree //5 Long Minute //6 Long Second //7 Long Direction (0 = E, 1 = W) CoordinatesPosition latP = CoordinatesPosition.N; CoordinatesPosition lngP = CoordinatesPosition.E; if (d[3] != 0) { latP = CoordinatesPosition.S; } if (d[7] != 0) { lngP = CoordinatesPosition.W; } CoordinatePart lat = new CoordinatePart((int)d[0], (int)d[1], d[2], latP); CoordinatePart lng = new CoordinatePart((int)d[4], (int)d[5], d[6], lngP); c = new Coordinate(eg); c.Latitude = lat; c.Longitude = lng; c.Parse_Format = Parse_Format_Type.Degree_Minute_Second; return(true); } catch {//Parser failed try next method } } string[] um; //Try MGRS if (TryMGRS(s, out um) || TryMGRS_Polar(s, out um)) { try { double zone = Convert.ToDouble(um[0]); double easting = Convert.ToDouble(um[3]); double northing = Convert.ToDouble(um[4]); MilitaryGridReferenceSystem mgrs = new MilitaryGridReferenceSystem(um[1], (int)zone, um[2], easting, northing); c = MilitaryGridReferenceSystem.MGRStoLatLong(mgrs); c.Parse_Format = Parse_Format_Type.MGRS; return(true); } catch {//Parser failed try next method } } //Try UTM if (TryUTM(s, out um) || TryUPS(s, out um)) { try { double zone = Convert.ToDouble(um[0]); double easting = Convert.ToDouble(um[2]); double northing = Convert.ToDouble(um[3]); UniversalTransverseMercator utm = new UniversalTransverseMercator(um[1], (int)zone, easting, northing); c = UniversalTransverseMercator.ConvertUTMtoLatLong(utm); c.Parse_Format = Parse_Format_Type.UTM; return(true); } catch {//Parser failed try next method } } //Try Cartesian if (TryCartesian(s.ToUpper().Replace("KM", "").Replace("X", "").Replace("Y", "").Replace("Z", ""), out d)) { if (ct == CartesianType.Cartesian) { try { Cartesian cart = new Cartesian(d[0], d[1], d[2]); c = Cartesian.CartesianToLatLong(cart); c.Parse_Format = Parse_Format_Type.Cartesian_Spherical; return(true); } catch {//Parser failed try next method } } if (ct == CartesianType.ECEF) { try { ECEF ecef = new ECEF(d[0], d[1], d[2]); c = ECEF.ECEFToLatLong(ecef); c.Parse_Format = Parse_Format_Type.Cartesian_ECEF; return(true); } catch {//Parser failed try next method } } } } catch (Exception ex) { //Parser exception has occurred Debug.WriteLine("PARSER EXCEPTION HANDLED: " + ex.ToString()); } c = null; return(false); }
/// <summary> /// Coordinate build logic goes here. /// </summary> /// <param name="lat">Signed latitude</param> /// <param name="longi">Signed longitude</param> /// <param name="date">Date at location</param> /// <param name="eagerLoad">Eagerloading settings</param> private void Coordinate_Builder(double lat, double longi, DateTime date, EagerLoad eagerLoad) { FormatOptions = new CoordinateFormatOptions(); //Use default constructor if signed degree is 0 for performance. if (lat == 0) { latitude = new CoordinatePart(CoordinateType.Lat); } else { latitude = new CoordinatePart(lat, CoordinateType.Lat); } if (longi == 0) { longitude = new CoordinatePart(CoordinateType.Long); } else { longitude = new CoordinatePart(longi, CoordinateType.Long); } //Set CoordinatePart parents latitude.parent = this; longitude.parent = this; //Set UTC date at location geoDate = date; //LOAD NEW COORDINATE SYSTEMS HERE //Load Celestial if (eagerLoad.Celestial) { celestialInfo = new Celestial(lat, longi, date); } //Load UTM MGRS if (eagerLoad.UTM_MGRS) { utm = new UniversalTransverseMercator(lat, longi, this); mgrs = new MilitaryGridReferenceSystem(utm); } //Load CARTESIAN if (eagerLoad.Cartesian) { cartesian = new Cartesian(this); } //Load ECEF if (eagerLoad.ECEF) { ecef = new ECEF(this); } //SET EagerLoading Setting EagerLoadSettings = eagerLoad; //Set Ellipsoid equatorial_radius = 6378137.0; inverse_flattening = 298.257223563; }
//Add main to Coordinate and tunnel to Format class. Add private methods to format. //WHEN PARSING NO EXCPETIONS FOR OUT OF RANGE ARGS WILL BE THROWN public static bool TryParse(string coordString, out Coordinate c) { //Turn of eagerload for efficiency EagerLoad eg = new EagerLoad(); eg.Cartesian = false; eg.Celestial = false; eg.UTM_MGRS = false; c = new Coordinate(eg); string s = coordString; s = s.Trim(); //Trim all spaces before and after string double[] d; //Try Signed Degree if (TrySignedDegree(s, out d)) { try { c = new Coordinate(d[0], d[1], eg); return(true); } catch {//Parser failed try next method } } //Try Decimal Degree if (TryDecimalDegree(s, out d)) { try { c = new Coordinate(d[0], d[1], eg); return(true); } catch {//Parser failed try next method } } //Try DDM if (TryDegreeDecimalMinute(s, out d)) { try { //0 Lat Degree //1 Lat Minute //2 Lat Direction (0 = N, 1 = S) //3 Long Degree //4 Long Minute //5 Long Direction (0 = E, 1 = W) CoordinatesPosition latP = CoordinatesPosition.N; CoordinatesPosition lngP = CoordinatesPosition.E; if (d[2] != 0) { latP = CoordinatesPosition.S; } if (d[5] != 0) { lngP = CoordinatesPosition.W; } CoordinatePart lat = new CoordinatePart((int)d[0], d[1], latP, c); CoordinatePart lng = new CoordinatePart((int)d[3], d[4], lngP, c); c = new Coordinate(eg); c.Latitude = lat; c.Longitude = lng; return(true); } catch {//Parser failed try next method } } //Try DMS if (TryDegreeMinuteSecond(s, out d)) { try { //0 Lat Degree //1 Lat Minute //2 Lat Second //3 Lat Direction (0 = N, 1 = S) //4 Long Degree //5 Long Minute //6 Long Second //7 Long Direction (0 = E, 1 = W) CoordinatesPosition latP = CoordinatesPosition.N; CoordinatesPosition lngP = CoordinatesPosition.E; if (d[3] != 0) { latP = CoordinatesPosition.S; } if (d[7] != 0) { lngP = CoordinatesPosition.W; } CoordinatePart lat = new CoordinatePart((int)d[0], (int)d[1], d[2], latP, c); CoordinatePart lng = new CoordinatePart((int)d[4], (int)d[5], d[6], lngP, c); c = new Coordinate(eg); c.Latitude = lat; c.Longitude = lng; return(true); } catch {//Parser failed try next method } } string[] um; //Try MGRS if (TryMGRS(s, out um)) { try { double zone = Convert.ToDouble(um[0]); double easting = Convert.ToDouble(um[3]); double northing = Convert.ToDouble(um[4]); MilitaryGridReferenceSystem mgrs = new MilitaryGridReferenceSystem(um[1], (int)zone, um[2], easting, northing); c = MilitaryGridReferenceSystem.MGRStoLatLong(mgrs); return(true); } catch {//Parser failed try next method } } //Try UTM if (TryUTM(s, out um)) { try { double zone = Convert.ToDouble(um[0]); double easting = Convert.ToDouble(um[2]); double northing = Convert.ToDouble(um[3]); UniversalTransverseMercator utm = new UniversalTransverseMercator(um[1], (int)zone, easting, northing); c = UniversalTransverseMercator.ConvertUTMtoLatLong(utm); return(true); } catch {//Parser failed try next method } } if (TryCartesian(s, out d)) { try { Cartesian cart = new Cartesian(d[0], d[1], d[2]); c = Cartesian.CartesianToLatLong(cart); return(true); } catch {//Parser failed try next method } } //Try Cartesian return(false); }