Exemple #1
0
        /// <summary>
        /// Tries to parse a string representing a speed. Assumes kilometers per hour by default, use explicit parsing methods for different behaviour.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryParse(string s, out Speed result)
        {
            result = null;

            if (s.IsNullOrWhiteSpace())
            {
                return(false);
            }

            // try a generic parse first, in this case assume kilometers per hour.
            double value;

            if (double.TryParse(s, out value))
            { // the value is just a numeric value.
                result = new KilometerPerHour(value);
                return(true);
            }

            // try kilometers per hour.
            if (KilometerPerHour.TryParse(s, out result))
            { // succes!
                return(true);
            }

            // try miles per hour.
            if (MilesPerHour.TryParse(s, out result))
            { // success!
                return(true);
            }

            // try knots.
            if (Knots.TryParse(s, out result))
            { // success!
                return(true);
            }

            // try meters per second.
            if (MeterPerSecond.TryParse(s, out result))
            { // success!
                return(true);
            }
            return(false);
        }
        public static bool TryParse(string s, out KilometerPerHour result)
        {
            s      = s.ToStringEmptyWhenNull().Trim().ToLower();
            result = (KilometerPerHour)null;
            double result1;

            if (double.TryParse(s, NumberStyles.Any, (IFormatProvider)CultureInfo.InvariantCulture, out result1))
            {
                result = new KilometerPerHour(result1);
                return(true);
            }
            Match match = new Regex("^\\s*(\\d+(?:\\.\\d*)?)\\s*\\s*(km/h|kmh|kph|kmph)?\\s*$", RegexOptions.IgnoreCase).Match(s);

            if (!match.Success)
            {
                return(false);
            }
            result = new KilometerPerHour(double.Parse(match.Groups[1].Value, (IFormatProvider)CultureInfo.InvariantCulture));
            return(true);
        }
Exemple #3
0
        /// <summary>
        /// Tries to parse a string representing a speed. Assumes kilometers per hour by default, use explicit parsing methods for different behaviour.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryParse(string s, out Speed result)
        {
            result = null;

            if (s.IsNullOrWhiteSpace())
                return false;

            // try a generic parse first, in this case assume kilometers per hour.
            double value;
            if (double.TryParse(s, out value))
            { // the value is just a numeric value.
                result = new KilometerPerHour(value);
                return true;
            }

            // try kilometers per hour.
            if (KilometerPerHour.TryParse(s, out result))
            { // succes!
                return true;
            }

            // try miles per hour.
            if (MilesPerHour.TryParse(s, out result))
            { // success!
                return true;
            }

            // try knots.
            if (Knots.TryParse(s, out result))
            { // success!
                return true;
            }

            // try meters per second.
            if (MeterPerSecond.TryParse(s, out result))
            { // success!
                return true;
            }
            return false;
        }
        /// <summary>
        /// Tries to parse a string containing a kilometer per hour value.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryParse(string s, out KilometerPerHour result)
        {
            s = s.ToStringEmptyWhenNull().Trim().ToLower();

            result = null;
            double value;

            if (double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out value))
            { // the value is just a numeric value.
                result = new KilometerPerHour(value);
                return(true);
            }

            // do some more parsing work.
            Regex regex = new Regex("^" + Constants.RegexDecimalWhiteSpace + KilometerPerHour.RegexUnitKilometersPerHour + "$", RegexOptions.IgnoreCase);
            Match match = regex.Match(s);

            if (match.Success)
            {
                result = new KilometerPerHour(double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture));
                return(true);
            }
            return(false);
        }
        /// <summary>
        /// Tries to parse a string containing a kilometer per hour value.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryParse(string s, out KilometerPerHour result)
        {
            s = s.ToStringEmptyWhenNull().Trim().ToLower();

            result = null;
            double value;
            if (double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out value))
            { // the value is just a numeric value.
                result = new KilometerPerHour(value);
                return true;
            }

            // do some more parsing work.
            Regex regex = new Regex("^" + Constants.RegexDecimalWhiteSpace + KilometerPerHour.RegexUnitKilometersPerHour + "$", RegexOptions.IgnoreCase);
            Match match = regex.Match(s);
            if (match.Success)
            {
                result = new KilometerPerHour(double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture));
                return true;
            }
            return false;
        }
Exemple #6
0
        /// <summary>
        /// Tries to parse a speed value from a given tag-value.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryParseSpeed(string s, out KilometerPerHour result)
        {
            result = double.MaxValue;

            if (Utilities.IsNullOrWhiteSpace(s))
                return false;

            if (s[0] != '0' && s[0] != '1' && s[0] != '2' && s[0] != '3' && s[0] != '4' &&
                s[0] != '5' && s[0] != '6' && s[0] != '7' && s[0] != '8' && s[0] != '9')
            { // performance inprovement, quick negative answer.
                return false;
            }

            if(s.Contains(","))
            { // refuse comma as a decimal seperator or anywhere else in the number.
                return false;
            }

            // try regular speed: convention in OSM is km/h in this case.
            double kmphDouble;
            if (double.TryParse(s, NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out kmphDouble))
            {
                result = kmphDouble;
            }

            // try km/h
            if (KilometerPerHour.TryParse(s, out result))
            {
                return true;
            }

            // try mph.
            MilesPerHour resultMph;
            if(MilesPerHour.TryParse(s, out resultMph))
            {
                result = resultMph;
                return true;
            }

            // try knots.
            Knots resultKnots;
            if (Knots.TryParse(s, out resultKnots))
            {
                result = resultKnots;
                return true;
            }

            return false;
        }
Exemple #7
0
 /// <summary>
 /// Searches for a maxspeed tag and returns the associated value.
 /// 
 ///  http://wiki.openstreetmap.org/wiki/Key:maxspeed
 /// </summary>
 /// <param name="tags">The tags to search.</param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static bool TryGetMaxSpeed(this TagsCollectionBase tags, out KilometerPerHour result)
 {
     result = double.MaxValue;
     string tagValue;
     if (tags == null || !tags.TryGetValue("maxspeed", out tagValue) || Utilities.IsNullOrWhiteSpace(tagValue) ||
         tagValue == "none" || tagValue == "signals" || tagValue == "signs" || tagValue == "no")
         return false;
     return TagExtensions.TryParseSpeed(tagValue, out result);
 }
Exemple #8
0
        /// <summary>
        /// Tries to parse a speed value from a given tag-value.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool TryParseSpeed(string s, out KilometerPerHour result)
        {
            result = double.MaxValue;

            if (string.IsNullOrWhiteSpace(s))
                return false;

            if(s.Contains(','))
            { // refuse comma as a decimal seperator or anywhere else in the number.
                return false;
            }

            // try regular speed: convention in OSM is km/h in this case.
            double kmphDouble;
            if (double.TryParse(s, NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out kmphDouble))
            {
                result = kmphDouble;
            }

            // try km/h
            if (KilometerPerHour.TryParse(s, out result))
            {
                return true;
            }

            // try mph.
            MilesPerHour resultMph;
            if(MilesPerHour.TryParse(s, out resultMph))
            {
                result = resultMph;
                return true;
            }

            // try knots.
            Knots resultKnots;
            if (Knots.TryParse(s, out resultKnots))
            {
                result = resultKnots;
                return true;
            }

            return false;
        }
Exemple #9
0
 /// <summary>
 /// Searches for a maxspeed tag and returns the associated value.
 /// 
 ///  http://wiki.openstreetmap.org/wiki/Key:maxspeed
 /// </summary>
 /// <param name="tags">The tags to search.</param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static bool TryGetMaxSpeed(this TagsCollection tags, out KilometerPerHour result)
 {
     result = double.MaxValue;
     string tagValue;
     if (tags == null || !tags.TryGetValue("maxspeed", out tagValue) || string.IsNullOrWhiteSpace(tagValue))
         return false;
     return TagExtensions.TryParseSpeed(tagValue, out result);
 }