public SpeedMessage(byte[] payload)
        {
            payload.RequireBytes(RequireBytes);

            SignedSpeed = new KilometerPerHour(BitArrayConverter.ToUInt16(payload, 12, 12) * 0.08m - 40m);
            UISpeed     = new KilometerPerHour(BitArrayConverter.ToUInt16(payload, 24, 8));
        }
Example #2
0
        public void TestKnots()
        {
            // initialize value.
            Knots ten = 10;

            // convert from.
            KilometerPerHour tenInKilometerPerHour = 18.52;
            MeterPerSecond   tenInMeterPerSecond   = 5.14444444444;
            MilesPerHour     tenInMilesPerHour     = 11.50779;

            // test converts to.
            Assert.AreEqual(ten.Value, ((Knots)tenInKilometerPerHour).Value, 0.000001);
            Assert.AreEqual(ten.Value, ((Knots)tenInMeterPerSecond).Value, 0.000001);
            Assert.AreEqual(ten.Value, ((Knots)tenInMilesPerHour).Value, 0.000001);

            // tests division of distance with time resulting in speed.
            Kilometer distance = ten * (Hour)2;

            Assert.AreEqual(18.52 * 2.0, distance.Value);

            // tests some parsing functions.
            Knots tenPointFive = 10.5;
            Knots tenPointFiveParsed;

            Assert.IsTrue(Knots.TryParse(tenPointFive.ToString(), out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(Knots.TryParse("10.5", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(Knots.TryParse("10.5 knots", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);

            Assert.IsFalse(Knots.TryParse("10.5 m/s", out tenPointFiveParsed));
            Assert.IsFalse(Knots.TryParse("10.5 mph", out tenPointFiveParsed));
            Assert.IsFalse(Knots.TryParse("10.5 k/h", out tenPointFiveParsed));
        }
Example #3
0
        public static bool TryParseSpeed(string s, out KilometerPerHour result)
        {
            result = (KilometerPerHour)double.MaxValue;
            if (string.IsNullOrWhiteSpace(s) || (int)s[0] != 48 && (int)s[0] != 49 && ((int)s[0] != 50 && (int)s[0] != 51) && ((int)s[0] != 52 && (int)s[0] != 53 && ((int)s[0] != 54 && (int)s[0] != 55)) && ((int)s[0] != 56 && (int)s[0] != 57) || s.Contains(","))
            {
                return(false);
            }
            double result1;

            if (double.TryParse(s, NumberStyles.Any, (IFormatProvider)CultureInfo.InvariantCulture, out result1))
            {
                result = (KilometerPerHour)result1;
            }
            if (KilometerPerHour.TryParse(s, out result))
            {
                return(true);
            }
            MilesPerHour result2;

            if (MilesPerHour.TryParse(s, out result2))
            {
                result = (KilometerPerHour)result2;
                return(true);
            }
            Knots result3;

            if (!Knots.TryParse(s, out result3))
            {
                return(false);
            }
            result = (KilometerPerHour)result3;
            return(true);
        }
Example #4
0
        /// <summary>
        /// Estimates the probable speed of this vehicle on a way with the given tags.
        /// </summary>
        /// <param name="tags"></param>
        /// <returns></returns>
        public virtual KilometerPerHour ProbableSpeed(TagsCollectionBase tags)
        {
            KilometerPerHour maxSpeedAllowed = this.MaxSpeedAllowed(tags);
            KilometerPerHour maxSpeed        = this.MaxSpeed();

            if (maxSpeed.Value < maxSpeedAllowed.Value)
            {
                return(maxSpeed);
            }
            return(maxSpeedAllowed);
        }
Example #5
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[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);
        }
Example #6
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));
        }
Example #7
0
        public virtual KilometerPerHour ProbableSpeed(TagsCollectionBase tags)
        {
            KilometerPerHour kilometerPerHour1 = this.MaxSpeedAllowed(tags);
            KilometerPerHour kilometerPerHour2 = this.MaxSpeed();

            if (kilometerPerHour2.Value < kilometerPerHour1.Value)
            {
                return(kilometerPerHour2);
            }
            return(kilometerPerHour1);
        }
Example #8
0
        public static bool TryGetMaxSpeed(this TagsCollectionBase tags, out KilometerPerHour result)
        {
            result = (KilometerPerHour)double.MaxValue;
            string s;

            if (tags == null || !tags.TryGetValue("maxspeed", out s) || (string.IsNullOrWhiteSpace(s) || s == "none") || (s == "signals" || s == "signs" || s == "no"))
            {
                return(false);
            }
            return(TagExtensions.TryParseSpeed(s, out result));
        }
Example #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 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));
        }
Example #10
0
        public virtual KilometerPerHour MaxSpeedAllowed(TagsCollectionBase tags)
        {
            KilometerPerHour kilometerPerHour = (KilometerPerHour)5.0;
            string           highwayType;

            if (TagExtensions.TryGetMaxSpeed(tags, out kilometerPerHour) || !this.TryGetHighwayType(tags, out highwayType))
            {
                return(kilometerPerHour);
            }
            kilometerPerHour = this.MaxSpeedAllowed(highwayType);
            return(kilometerPerHour);
        }
Example #11
0
        /// <summary>
        /// Calculate metrics for a given arc.
        /// </summary>
        /// <param name="vehicle"></param>
        /// <param name="result"></param>
        /// <param name="arc"></param>
        private void CalculateArcMetrics(Vehicle vehicle, Dictionary <string, double> result, AggregatedArc arc)
        {
            // update the distance.
            result[DISTANCE_KEY] = result[DISTANCE_KEY] + arc.Distance.Value;

            // update the time.
            KilometerPerHour speed = vehicle.ProbableSpeed(arc.Tags);
            Second           time  = arc.Distance / speed;

            // FOR NOW USE A METRIC OF 75% MAX SPEED.
            // TODO: improve this for a more realistic estimated based on the type of road.
            result[TIME_KEY] = result[TIME_KEY] + time.Value;
        }
Example #12
0
        public void TestKilometerPerHour()
        {
            // initialize value.
            KilometerPerHour ten = 10;

            // convert from.
            Knots          tenInKnots          = 5.39956803;
            MeterPerSecond tenInMeterPerSecond = 2.77777777778;
            MilesPerHour   tenInMilesPerHour   = 6.21371192;

            // test converts to.
            Assert.AreEqual(ten.Value, ((KilometerPerHour)tenInKnots).Value, 0.000001);
            Assert.AreEqual(ten.Value, ((KilometerPerHour)tenInMeterPerSecond).Value, 0.000001);
            Assert.AreEqual(ten.Value, ((KilometerPerHour)tenInMilesPerHour).Value, 0.000001);

            // tests division of distance with time resulting in speed.
            Kilometer twenty = ten * (Hour)2;

            Assert.AreEqual(20, twenty.Value);

            // tests some parsing functions.
            KilometerPerHour tenPointFive = 10.5;
            KilometerPerHour tenPointFiveParsed;

            Assert.IsTrue(KilometerPerHour.TryParse(tenPointFive.ToString(), out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(KilometerPerHour.TryParse("10.5", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(KilometerPerHour.TryParse("10.5 km/h", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(KilometerPerHour.TryParse("10.5 kmh", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(KilometerPerHour.TryParse("10.5 kph", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(KilometerPerHour.TryParse("10.5 kmph", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(KilometerPerHour.TryParse("10.5km/h", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(KilometerPerHour.TryParse("10.5kmh", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(KilometerPerHour.TryParse("10.5kph", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(KilometerPerHour.TryParse("10.5kmph", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);

            Assert.IsFalse(KilometerPerHour.TryParse("10.5 m/s", out tenPointFiveParsed));
            Assert.IsFalse(KilometerPerHour.TryParse("10.5 mph", out tenPointFiveParsed));
            Assert.IsFalse(KilometerPerHour.TryParse("10.5 knots", out tenPointFiveParsed));
        }
Example #13
0
        /// <summary>
        /// Returns the maximum speed.
        /// </summary>
        /// <returns></returns>
        public KilometerPerHour MaxSpeedAllowed(TagsCollectionBase tags)
        {
            // THESE ARE THE MAX SPEEDS FOR BELGIUM.
            // TODO: Find a way to make this all configurable.
            KilometerPerHour speed = 5;

            // get max-speed tag if any.
            if (tags.TryGetMaxSpeed(out speed))
            {
                return(speed);
            }

            string highwayType;

            if (TryGetHighwayType(tags, out highwayType))
            {
                speed = this.MaxSpeedAllowed(highwayType);
            }

            return(speed);
        }
Example #14
0
        /// <summary>
        ///     Returns the maximum speed.
        /// </summary>
        /// <returns></returns>
        public KilometerPerHour MaxSpeed(TagsCollection tags)
        {
            // THESE ARE THE MAX SPEEDS FOR BELGIUM.
            // TODO: Find a way to make this all configurable.
            KilometerPerHour speed = 5;

            // get max-speed tag if any.
            var maxSpeedValue = tags.GetNumericValue("maxspeed");

            if (maxSpeedValue.HasValue)
            {
                return(maxSpeedValue.Value);
            }

            string highwayType;

            if (TryGetHighwayType(tags, out highwayType))
            {
                speed = MaxSpeed(highwayType);
            }

            return(speed);
        }
Example #15
0
        public void TestMeterPerSecond()
        {
            // initialize value.
            MeterPerSecond ten = 10;

            // convert from.
            Knots            tenInKnots            = 19.43844492440605;
            KilometerPerHour tenInKilometerPerHour = 36;
            MilesPerHour     tenInMilesPerHour     = 22.36936292054402;

            // test converts to.
            Assert.AreEqual(ten.Value, ((MeterPerSecond)tenInKnots).Value, 0.000001);
            Assert.AreEqual(ten.Value, ((MeterPerSecond)tenInKilometerPerHour).Value, 0.000001);
            Assert.AreEqual(ten.Value, ((MeterPerSecond)tenInMilesPerHour).Value, 0.000001);

            // tests division of distance with time resulting in speed.
            Kilometer twenty = ten * (Hour)2;

            Assert.AreEqual(tenInKilometerPerHour.Value * 2.0, twenty.Value);

            // tests some parsing functions.
            MeterPerSecond tenPointFive = 10.5;
            MeterPerSecond tenPointFiveParsed;

            Assert.IsTrue(MeterPerSecond.TryParse(tenPointFive.ToString(), out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(MeterPerSecond.TryParse("10.5", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(MeterPerSecond.TryParse("10.5 m/s", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);
            Assert.IsTrue(MeterPerSecond.TryParse("10.5m/s", out tenPointFiveParsed));
            Assert.AreEqual(tenPointFive.Value, tenPointFiveParsed.Value);

            Assert.IsFalse(MeterPerSecond.TryParse("10.5 km/h", out tenPointFiveParsed));
            Assert.IsFalse(MeterPerSecond.TryParse("10.5 mph", out tenPointFiveParsed));
            Assert.IsFalse(MeterPerSecond.TryParse("10.5 knots", out tenPointFiveParsed));
        }