Exemplo n.º 1
0
        // ReSharper disable once MethodTooLong
        // ReSharper disable once TooManyArguments
        private static void GetDistanceAndDuration(List <Position> positions, Position lastPosition, Position firstPosition,
                                                   TargetViewModel trgt)
        {
            // ReSharper disable once ComplexConditionExpression
            if (lastPosition != null && firstPosition != null)
            {
                var index = GetPreviousPositionIndexx(positions, lastPosition);
                if (index == -1)
                {
                    index = 0;
                }
                var p1 = new GeofenceHelper.Position();
                p1.Latitude  = positions.ElementAt(index).Lat;
                p1.Longitude = positions.ElementAt(index).Long;
                var p2 = new GeofenceHelper.Position();
                p2.Latitude  = firstPosition.Lat;
                p2.Longitude = firstPosition.Long;

                trgt.Distance = Math.Round(GeofenceHelper.HaversineFormula(p1, p2, GeofenceHelper.DistanceType.Kilometers), 2);
                var avgSpeed = GetAvgSpeed(positions, firstPosition, lastPosition);
                trgt.AvgSpeed = Math.Round(avgSpeed, 2);
                trgt.MaxSpeed = Math.Round(GetMaxSpeed(positions, firstPosition, lastPosition), 2);
            }

            if (double.IsNaN(trgt.Distance))
            {
                trgt.Distance = 0;
            }
            trgt.Duration = (DateTime.Parse(trgt.EndPeriod) - DateTime.Parse(trgt.StartPeriod)).TotalSeconds;
        }
Exemplo n.º 2
0
        double CalculateDistance(double lat1, double log, double lat2, double log2)
        {
            var p1 = new GeofenceHelper.Position();

            p1.Latitude  = lat1;
            p1.Longitude = log;
            var p2 = new GeofenceHelper.Position();

            p2.Latitude  = lat2;
            p2.Longitude = log2;

            return(Math.Round(GeofenceHelper.HaversineFormula(p1, p2, GeofenceHelper.DistanceType.Kilometers), 2));
        }
Exemplo n.º 3
0
        // ReSharper disable once MethodTooLong
        // ReSharper disable once TooManyArguments
        private static void GetDistanceAndDuration(List <Position> positions, TargetViewModel trgt, DateTime currenPosition, DateTime positionEnd)
        {
            // ReSharper disable once ComplexConditionExpression
            var points   = positions.Where(x => x.Timestamp >= currenPosition && x.Timestamp <= positionEnd).ToList();
            var avgSpeed = points.Average(x => x.Speed);

            trgt.AvgSpeed = Math.Round(avgSpeed, 2);
            trgt.MaxSpeed = Math.Round(points.Max(x => x.Speed), 2);

            trgt.Distance = 0;
            // ReSharper disable once ComplexConditionExpression
            if (points.Any() && trgt.MotionStatus != "Stopped")
            {
                var firstPos = points.First();
                foreach (var p in points.OrderBy(x => x.Timestamp).Skip(1))
                {
                    var dis = Math.Round(GeofenceHelper.HaversineFormula(new GeofenceHelper.Position {
                        Latitude = firstPos.Lat, Longitude = firstPos.Long
                    }, new GeofenceHelper.Position {
                        Latitude = p.Lat, Longitude = p.Long
                    }, GeofenceHelper.DistanceType.Kilometers), 2);
                    if (!double.IsNaN(dis))
                    {
                        trgt.Distance += dis;
                    }
                    firstPos = p;
                }
            }

            if (double.IsNaN(trgt.Distance))
            {
                trgt.Distance = 0;
            }
            trgt.Distance = Math.Round(trgt.Distance, 2);
            trgt.Duration = (DateTime.Parse(trgt.EndPeriod) - DateTime.Parse(trgt.StartPeriod)).TotalSeconds;
        }