/// <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); }
public DMSComponents(double degrees) { // ... Compute DMM DMMComponents dmm = new DMMComponents(degrees); _sign = dmm.get_sign(); _wholeDegrees = dmm.get_wholeDegrees(); _wholeMinutes = (int)Math.Floor(dmm.get_decimalMinutes()); double fraction = dmm.get_decimalMinutes() - _wholeMinutes; _decimalSeconds = fraction * 60; }
/// <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); }
/** * Parses a DMM string representation * @param s * @return */ public static DMMComponents Parse(String s, GeoAngleFormatOptions options) { int sign = 1; int wholeDegrees = 0; double decimalMinutes = 0; if (options == GeoAngleFormatOptions.Compact) { // ... Split value and hemisphere letter String[] tokens = s.Split(DELIMS); if (tokens.Length != 2) { throw new DMMFormatException(s); } sign = getHemisphereSign(tokens[1].Trim()[0]); double number = Double.Parse(tokens[0].Trim()); wholeDegrees = (int)Math.Floor(number / 100); decimalMinutes = number - wholeDegrees * 100; } else { // ... Split value and hemisphere letter List <String> tokens = new List <String>(s.Split(DELIMS, StringSplitOptions.RemoveEmptyEntries)); if (tokens.Count != 2) { throw new DMMFormatException(s); } wholeDegrees = Int32.Parse(tokens[0]); sign = wholeDegrees < 0 ? -1 : 1; wholeDegrees *= sign; // Abs decimalMinutes = Double.Parse(tokens[1]); } DMMComponents rc = new DMMComponents(); rc.set_sign(sign); rc.set_wholeDegrees(wholeDegrees); rc.set_decimalMinutes(decimalMinutes); return(rc); }
///// <summary> ///// Converts the current value in degrees, to DMM components ///// </summary> ///// <param name="wholeDegrees"></param> ///// <param name="decimalMinutes"></param> ///// <param name="sign"></param> //protected void computeDMM(ref int wholeDegrees, ref double decimalMinutes, ref int sign) { // double degrees = Degrees; // sign = degrees < 0 ? -1 : 1; // degrees = Math.Abs(degrees); // wholeDegrees = Convert.ToInt32( Math.Floor(degrees) ); // double fraction = degrees - wholeDegrees; // decimalMinutes = fraction * 60; //} ///// <summary> ///// Converts the current value in degrees to DMS components ///// </summary> ///// <param name="wholeDegrees"></param> ///// <param name="wholeMinutes"></param> ///// <param name="decimalSeconds"></param> ///// <param name="sign"></param> //protected void computeDMS(ref int wholeDegrees, ref int wholeMinutes, ref double decimalSeconds, ref int sign) { // double decimalMinutes = 0; // computeDMM(ref wholeDegrees, ref decimalMinutes, ref sign); // wholeMinutes = Convert.ToInt32(Math.Floor(decimalMinutes)); // double fraction = decimalMinutes - wholeMinutes; // decimalSeconds = fraction * 60; //} ///// <summary> ///// Converts from DMM to degrees ///// </summary> ///// <param name="wholeDegrees"></param> ///// <param name="decimalMinutes"></param> ///// <param name="sign"></param> ///// <returns></returns> //protected static double computeDDD(int wholeDegrees, double decimalMinutes, int sign) { // double fraction = decimalMinutes / 60; // return sign * (1.0 * wholeDegrees + fraction); //} ///// <summary> ///// Converts from DMS to degrees ///// </summary> ///// <param name="wholeDegrees"></param> ///// <param name="wholeMinutes"></param> ///// <param name="decimalSeconds"></param> ///// <param name="sign"></param> ///// <returns></returns> //protected static double computeDDD(int wholeDegrees, int wholeMinutes, double decimalSeconds, int sign) { // double fraction = decimalSeconds / 60; // fraction = (1.0 * wholeMinutes + fraction) / 60; // return sign * (1.0 * wholeDegrees + fraction); //} /// <summary> /// Parses a DMM representation and converts to degrees /// </summary> /// <param name="s"></param> /// <param name="options"></param> /// <returns></returns> protected static double ParseDMM(string s, GeoAngleFormatOptions options) { DMMComponents dmm = DMMComponents.Parse(s, options); return(dmm.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 / 100)); // double decimalMinutes = number - wholeDegrees*100; // return computeDDD(wholeDegrees, decimalMinutes, sign); //} //else { // // ... Split value and hemisphere letter // string[] tokens = s.Split(DELIMS, StringSplitOptions.RemoveEmptyEntries); // if (tokens.Length != 2) { // throw new ArgumentException(nameof(s)); // } // int wholeDegrees = Int32.Parse(tokens[0]); // int sign = wholeDegrees < 0 ? -1 : 1; // wholeDegrees *= sign; // Abs // double decimalMinutes = double.Parse(tokens[1]); // return computeDDD(wholeDegrees, decimalMinutes, sign); //} }
/// <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);