Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /**
         * 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);
        }
Beispiel #3
0
        /// <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);
            //}
        }
Beispiel #4
0
        /// <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);
        }
Beispiel #5
0
 /// <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);