示例#1
0
        public Double Parse(String text)
        {
            double  d = 0, m = 0, s = 0;
            double  result;
            Boolean negate = false;
            int     length = text.Length;

            if (length > 0)
            {
                char c = text[length - 1];
                switch (c)
                {
                case 'W':
                case 'S':
                    negate = true;
                    text   = text.Substring(0, text.Length - 1);
                    break;

                case 'E':
                case 'N':
                    text = text.Substring(0, text.Length - 1);
                    break;
                }
            }

            int i = text.IndexOf('d');

            if (i == -1)
            {
                i = text.IndexOf('\u00b0');
            }
            if (i != -1)
            {
                String dd   = text.Substring(0, i);
                String mmss = text.Substring(i + 1);
                d = Convert.ToDouble(dd, CultureInfo.InvariantCulture);
                i = mmss.IndexOf('m');
                if (i == -1)
                {
                    i = mmss.IndexOf('\'');
                }
                if (i != -1)
                {
                    if (i != 0)
                    {
                        String mm = mmss.Substring(0, i);
                        m = Convert.ToDouble(mm, CultureInfo.InvariantCulture);
                    }
                    if (mmss.EndsWith("s") || mmss.EndsWith("\""))
                    {
                        mmss = mmss.Substring(0, mmss.Length - 1);
                    }
                    if (i != mmss.Length - 1)
                    {
                        String ss = mmss.Substring(i + 1);
                        s = Convert.ToDouble(ss, CultureInfo.InvariantCulture);
                    }
#if SILVERLIGHT
                    if (m < 0 || m > 59)
                    {
                        throw new ArgumentOutOfRangeException("m", "Minutes must be between 0 and 59");
                    }
                    if (s < 0 || s >= 60)
                    {
                        throw new ArgumentOutOfRangeException("s", "Seconds must be between 0 and 59");
                    }
#else
                    if (m < 0 || m > 59)
                    {
                        throw new ArgumentOutOfRangeException("m", m, "Minutes must be between 0 and 59");
                    }
                    if (s < 0 || s >= 60)
                    {
                        throw new ArgumentOutOfRangeException("s", s, "Seconds must be between 0 and 59");
                    }
#endif
                }
                else if (i != 0)
                {
                    m = Convert.ToDouble(mmss, CultureInfo.InvariantCulture);
                }
                if (_isDegrees)
                {
                    result = ProjectionMath.DegreesMinutesSecondsToDegrees(d, m, s);
                }
                else
                {
                    result = ProjectionMath.DegreesMinutesSecondsToRadians(d, m, s);
                }
            }
            else
            {
                result = Convert.ToDouble(text, CultureInfo.InvariantCulture);
                if (!_isDegrees)
                {
                    result = ProjectionMath.ToRadians(result);
                }
            }

            if (negate)
            {
                result = -result;
            }

            return(result);
        }