public TripLength CalculateDistance(Location startLocation, Location endLocation, TimeSpan startTime) { var startLocationDomain = _mapperService.MapModelToDomain(startLocation); startLocationDomain.Latitude = startLocation.Latitude; startLocationDomain.Longitude = startLocation.Longitude; var endLocationDomain = _mapperService.MapModelToDomain(endLocation); endLocationDomain.Latitude = endLocation.Latitude; endLocationDomain.Longitude = endLocation.Longitude; var locationDistance = _locationDistanceService.Get(startLocationDomain, endLocationDomain); TripLength result; if (locationDistance != null) { var hourIndex = (int)startTime.TotalHours; while (hourIndex >= 24) { hourIndex = hourIndex - 24; } long travelTime = locationDistance.Hours[hourIndex].TravelTime.Value; if (travelTime == 0 && locationDistance.TravelTime.HasValue) { travelTime = locationDistance.TravelTime.Value; } long distance = 0; if (locationDistance.Distance.HasValue) { distance = (long)locationDistance.Distance.Value; } result = new TripLength(distance, TimeSpan.FromSeconds(travelTime)); } else { // Use fallback service result = _fallbackDistanceService.CalculateDistance(startLocation, endLocation, startTime); } return(result); }
/// <summary> /// Calculates the toal route statistics a list of route stops /// </summary> /// <param name="stops"></param> /// <returns></returns> public TripLength CalculateTripLength(IList <RouteStop> stops) { var result = new TripLength(); for (int i = 0; i < stops.Count - 1; i++) { // calculate the trip between the current and next stop var currentStop = stops[i]; var nextStop = stops[i + 1]; var tripLength = CalculateTripLength(currentStop, nextStop); result += tripLength; } return(result); }
/// <summary> /// Calculates the taxi cab trip length /// </summary> /// <param name="startLocation"></param> /// <param name="endLocation"></param> /// <returns></returns> public TripLength CalculateTaxicabDistance(Location startLocation, Location endLocation) { double lon1 = startLocation.Longitude; double lat1 = startLocation.Latitude; double lon2 = endLocation.Longitude; double lat2 = endLocation.Latitude; double maxLat = Math.Max(lat1, lat2); double k = Math.Cos(Radians(maxLat)); var distance = 69.172 * (Math.Abs(lat2 - lat1) + k * Math.Abs(lon2 - lon1)); var result = new TripLength() { Distance = (decimal)distance, Time = _travelTimeEstimator.CalculateTravelTime(distance) }; return(result); }
/// <summary> /// calculates the euclidian trip length /// </summary> /// <param name="startLocation"></param> /// <param name="endLocation"></param> /// <returns></returns> public TripLength CalculateEuclidianDistance(Location startLocation, Location endLocation) { double lon1 = startLocation.Longitude; double lat1 = startLocation.Latitude; double lon2 = endLocation.Longitude; double lat2 = endLocation.Latitude; double dlon = Radians(lon2 - lon1); double dlat = Radians(lat2 - lat1); double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(Radians(lat1)) * Math.Cos(Radians(lat2)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2)); double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); var distance = angle * EarthRadius; var result = new TripLength() { Distance = (decimal)distance, Time = _travelTimeEstimator.CalculateTravelTime(distance) }; return(result); }