/// <summary> /// Stringify, with format /// </summary> /// <param name="fmt"></param> /// <returns></returns> public string ToString(GeoAngleFormat fmt, GeoAngleFormatOptions options = GeoAngleFormatOptions.ShowUnits) { string rc = null; switch (fmt) { default: throw new ArgumentException("Unsupported format: " + fmt); case GeoAngleFormat.DDD: if (options == GeoAngleFormatOptions.Compact) { rc = Degrees.ToString("0.0000000"); } else { rc = Degrees.ToString("0.0000000") + Strings.degrees; } break; case GeoAngleFormat.DMM: { DMMComponents dmmComponents = new DMMComponents(Degrees); rc = ToStringDMM(dmmComponents, options); } break; case GeoAngleFormat.DMS: { DMSComponents dmsComponents = new DMSComponents(Degrees); rc = ToStringDMS(dmsComponents, options); } break; } return(rc); }
/// <summary> /// Parses a string representation of longitude /// </summary> /// <param name="s"></param> /// <param name="fmt"></param> /// <param name="options"></param> /// <returns></returns> public static Longitude Parse(string s, GeoAngleFormat fmt, GeoAngleFormatOptions options) { return(new Longitude() { Degrees = ParseDegrees(s, fmt, options) }); }
/** * Parses a DMS string representation * @param s * @return */ public static DMSComponents Parse(String s, GeoAngleFormatOptions options) { int sign = 1; int wholeDegrees = 0; int wholeMinutes = 0; double decimalSeconds = 0; if (options == GeoAngleFormatOptions.Compact) { // ... Split value and hemisphere letter String[] tokens = s.Split(DELIMS); if (tokens.Length != 2) { throw new DMSFormatException(s); } sign = getHemisphereSign(tokens[1].Trim()[0]); double number = Double.Parse(tokens[0].Trim()); wholeDegrees = (int)Math.Floor(number / 10000); double decimalMinutes = number - wholeDegrees * 10000; wholeMinutes = (int)Math.Floor(decimalMinutes / 100); decimalSeconds = decimalMinutes - wholeMinutes * 100; } else { // ... Split value and hemisphere letter List <String> tokens = new List <String>(s.Split(DELIMS, StringSplitOptions.RemoveEmptyEntries)); wholeDegrees = Int32.Parse(tokens[0]); wholeMinutes = Int32.Parse(tokens[1]); decimalSeconds = Double.Parse(tokens[2]); switch (tokens.Count) { case 3: sign = wholeDegrees < 0 ? -1 : 1; wholeDegrees *= sign; // Abs break; case 4: sign = getHemisphereSign(tokens[3].Trim()[0]); break; default: throw new DMSFormatException(s); } } DMSComponents rc = new DMSComponents(); rc.set_sign(sign); rc.set_wholeDegrees(wholeDegrees); rc.set_wholeMinutes(wholeMinutes); rc.set_decimalSeconds(decimalSeconds); return(rc); }
/// <summary> /// Parses a DMS representation and converts to degrees /// </summary> /// <param name="s"></param> /// <param name="options"></param> /// <returns></returns> protected static double ParseDMS(string s, GeoAngleFormatOptions options) { DMSComponents dms = DMSComponents.Parse(s, options); return(dms.ToDDD()); //if (options == GeoAngleFormatOptions.Compact) { // // ... Split value and hemisphere letter // string[] tokens = s.Split(DELIMS); // if (tokens.Length != 2) { // throw new ArgumentException(nameof(s)); // } // int sign = 0; // switch (tokens[1].ToUpper().Trim()[0] ) { // case 'N': case 'E': sign = 1;break; // case 'S': case 'W': sign = -1; break; // default: // throw new ArgumentException(nameof(s)); // } // double number = double.Parse(tokens[0].Trim()); // int wholeDegrees = Convert.ToInt32(Math.Floor(number / 10000)); // double decimalMinutes = number - wholeDegrees*10000; // int wholeMinutes = Convert.ToInt32(Math.Floor(decimalMinutes / 100)); // double decimalSeconds = decimalMinutes - wholeMinutes*100; // return computeDDD(wholeDegrees, wholeMinutes, decimalSeconds, sign); //} //else { // // ... Split value and hemisphere letter // string[] tokens = s.Split(DELIMS, StringSplitOptions.RemoveEmptyEntries); // int wholeDegrees = Int32.Parse(tokens[0]); // int wholeMinutes = Int32.Parse(tokens[1]); // double decimalSeconds = double.Parse(tokens[2]); // int sign = 1; // switch (tokens.Length) { // case 3: // sign = wholeDegrees < 0 ? -1 : 1; // wholeDegrees *= sign; // Abs // break; // case 4: // switch (tokens[1].ToUpper().Trim()[0]) { // case 'N': case 'E': sign = 1; break; // case 'S': case 'W': sign = -1; break; // default: // throw new ArgumentException(nameof(s)); // } // break; // default: // throw new ArgumentException(nameof(s)); // } // return computeDDD(wholeDegrees, wholeMinutes, decimalSeconds, sign); //} }
/// <summary> /// Implements string representation of DMM for longitudes /// </summary> /// <param name="wholeDegrees"></param> /// <param name="decimalMinutes"></param> /// <param name="sign"></param> /// <param name="options"></param> /// <returns></returns> protected override string ToStringDMM(DMMComponents dmm, GeoAngleFormatOptions options) { string rc = null; char letter = dmm.get_sign() < 0 ? 'W' : 'E'; if (options == GeoAngleFormatOptions.Compact) { rc = string.Format("{0:000}{1:00.0000},{2}", dmm.get_wholeDegrees(), dmm.get_decimalMinutes(), letter); } else { rc = string.Format("{0}{1} {2:0.######}'", dmm.get_sign() * dmm.get_wholeDegrees(), Strings.degrees, dmm.get_decimalMinutes()); } return(rc); }
/// <summary> /// Implements string representation of DMS for longitudes /// </summary> /// <param name="wholeDegrees"></param> /// <param name="wholeMinutes"></param> /// <param name="decimalSeconds"></param> /// <param name="sign"></param> /// <param name="options"></param> /// <returns></returns> protected override string ToStringDMS(DMSComponents dms, GeoAngleFormatOptions options) { string rc = null; char letter = dms.get_sign() < 0 ? 'W' : 'E'; if (options == GeoAngleFormatOptions.Compact) { rc = string.Format("{0:000}{1:00}{2:00.0000},{3}", dms.get_wholeDegrees(), dms.get_wholeMinutes(), dms.get_decimalSeconds(), letter); } else { rc = string.Format("{0}{1} {2}' {3:0.####}\" {4}", dms.get_wholeDegrees(), Strings.degrees, dms.get_wholeMinutes(), dms.get_decimalSeconds(), letter); } return(rc); }
/// <summary> /// Converts a string representation back to numeric degrees /// </summary> /// <param name="s"></param> /// <param name="fmt"></param> /// <param name="options"></param> /// <returns></returns> protected static double ParseDegrees(string s, GeoAngleFormat fmt, GeoAngleFormatOptions options) { double rc = 0; switch (fmt) { case GeoAngleFormat.DDD: rc = double.Parse(s); break; case GeoAngleFormat.DMM: rc = ParseDMM(s, options); break; case GeoAngleFormat.DMS: rc = ParseDMS(s, options); break; } return(rc); }
protected override String ToStringDMS(DMSComponents dms, GeoAngleFormatOptions options) { return(null); }
/// <summary> /// Converts from DMS components to its string representation /// </summary> /// <param name="wholeDegrees"></param> /// <param name="wholeMinutes"></param> /// <param name="decimalSeconds"></param> /// <param name="sign"></param> /// <param name="options"></param> /// <returns></returns> protected abstract string ToStringDMS(DMSComponents dms, GeoAngleFormatOptions options);
/// <summary> /// Converts from DMM components to its string representation /// </summary> /// <param name="wholeDegrees"></param> /// <param name="decimalMinutes"></param> /// <param name="sign"></param> /// <param name="options"></param> /// <returns></returns> protected abstract string ToStringDMM(DMMComponents dmm, GeoAngleFormatOptions options);