public static List <WindForecastInstance> GetWindForecastInstances5Days3HourInterval(double latitude, double longitude) { StringBuilder urlRequest = new StringBuilder(); urlRequest.Append("http://api.openweathermap.org/data/2.5/forecast?"); urlRequest.Append($"lat={latitude}&lon={longitude}"); urlRequest.Append("&APPID=9fbb2cbde0201181c427ff24405bfdeb&units=metric"); string jsonResponse = HttpRequestsUtility.Get(urlRequest.ToString()); JsonRoot forecast = JsonConvert.DeserializeObject <JsonRoot>(jsonResponse); if (forecast.httpReponseCode != "200") { return(null); } List <WindForecastInstance> windForecastInstances = new List <WindForecastInstance>(); foreach (var forecastInstance in forecast.forecastInstances) { WindForecastInstance windForecastInstance = new WindForecastInstance(); windForecastInstance.forecastTime = DateTimeUtility.UnixTimeStampToDateTime(forecastInstance.forecastTime); windForecastInstance.windStrengthMps = forecastInstance.wind.windStrengthMps; windForecastInstance.WindDirectionDegrees = forecastInstance.wind.windDirectionDegrees; windForecastInstances.Add(windForecastInstance); } return(windForecastInstances); }
private static WindForecastInstance GetClothestForecastInstance(List <WindForecastInstance> windForecastInstances, DateTime forecastTime) { TimeSpan shortestInstanceTimeDifference = TimeSpan.MaxValue; WindForecastInstance clothestWindForecastInstance = null; foreach (var windForecastInstance in windForecastInstances) { TimeSpan instanceTimeDifference = (windForecastInstance.forecastTime - forecastTime).Duration(); if (instanceTimeDifference < shortestInstanceTimeDifference) { shortestInstanceTimeDifference = instanceTimeDifference; clothestWindForecastInstance = windForecastInstance; } } return(clothestWindForecastInstance); }
public static WindForecastInstance GetWindForecast(double latitude, double longitude, DateTime forecastTime) { if (forecastTime < DateTime.Now.AddHours(-1) || forecastTime > DateTime.Now.AddDays(5)) { Console.WriteLine("Cannot get forecast for " + forecastTime.ToString()); return(null); } var windForecastInstances = GetWindForecastInstances5Days3HourInterval(latitude, longitude); if (windForecastInstances == null) { Console.WriteLine("GetWindForecastInstances5Days3HourInterval returned null"); return(null); } WindForecastInstance clothestWindForecastInstance = GetClothestForecastInstance(windForecastInstances, forecastTime); return(clothestWindForecastInstance); }
public static CommuteQuality GetCommuteQualityRating(string startAddress, string destinationAddress, DateTime forecastTime) { GoogleMapsAPI googleMapsAPI = new GoogleMapsAPI(startAddress, destinationAddress); if (googleMapsAPI.GetPathDistance() > 100000) // > 100 km { Console.WriteLine("Distance between addresses is too long. Results may be inaccurate."); } double pathDirection = googleMapsAPI.GetPathDirection(); WindForecastInstance windForecastInstance = WindForecastAPI.GetWindForecast(googleMapsAPI.GetMedianLatitude(), googleMapsAPI.GetMedianLongitude(), forecastTime); // Wind direction is the direction wind comes from, therefore before doing vector calculations it needs to be rotated 180 degrees. double forecastWindVectorDirection = TrigonometryUtility.RotateAngle180(windForecastInstance.WindDirectionDegrees); double windAngleDifference = TrigonometryUtility.GetDegreeBetweenVectors(pathDirection, forecastWindVectorDirection); double windHelpMagnitude = TrigonometryUtility.DotProductWithUnitVector(windForecastInstance.windStrengthMps, windAngleDifference); CommuteQuality commuteQuality = new CommuteQuality(); commuteQuality.Rating = new CommuteRating(CalculateHelpIndex(windHelpMagnitude)); commuteQuality.windPositiveWorkPercentage = GetPositiveWindWorkPercentage(googleMapsAPI, forecastWindVectorDirection); return(commuteQuality); }