예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }