// Input : FlightPlan of type server (with DB id and other fields)
        //Output : None
        // Function initilazies fields of instance
        public void Build(FlightPlan plan)
        {
            LocationClient locationClient = new LocationClient();

            flight_id                = plan.flight_id;
            passengers               = plan.passengers;
            company_name             = plan.company_name;
            locationClient.longitude = plan.initial_location_longitude;
            locationClient.latitude  = plan.initial_location_latitude;
            locationClient.date_time = plan.initial_location_date_time;
            initial_location         = locationClient;
            segments = SegmentClient.ParseSegments(plan.segments_string);
        }
        //Input : Segments string
        //Output : SegmentClient list
        public static IEnumerable <SegmentClient> ParseSegments(string segments_string)
        {
            int i = 0;
            List <SegmentClient> segmentsClient = new List <SegmentClient>();
            SegmentClient        segmentClient  = null;

            if (segments_string == null || segments_string == "")
            {
                return(segmentsClient);
            }
            segments_string = segments_string.Replace("[", "").Replace("]", "");
            segments_string = segments_string.Replace("{", "").Replace("}", "");
            string[] words = segments_string.Split(',');
            if (words.Length == 1)
            {
                return(segmentsClient);
            }
            foreach (string word in words)
            {
                if (i % 3 == 0) //Longitude (start of new segment)
                {
                    segmentClient           = new SegmentClient();
                    segmentClient.longitude = Convert.ToDouble(word);
                }
                else if (i % 3 == 1) //Latitude
                {
                    segmentClient.latitude = Convert.ToDouble(word);
                }
                else if (i % 3 == 2) //time_span (end of segment)
                {
                    segmentClient.timespan_seconds = Convert.ToDouble(word);
                    segmentsClient.Add(segmentClient);
                }
                i++;
            }
            return(segmentsClient);
        }
Exemplo n.º 3
0
        //Input: Time in string format (yyyy-mm-ddThh-mm-ssZ)
        //Output: Tuple (longitude,latitude), at time given
        public Tuple <double, double> getCoordinatesAtTime(string time)
        {
            IEnumerable <SegmentClient> segments = SegmentClient.ParseSegments(segments_string);
            Tuple <double, double>      coordinates;
            DateTime start_time   = Tools.FormatDateTime(initial_location_date_time);
            DateTime current_time = Tools.FormatDateTime(time);
            double   time_passed  = (current_time - start_time).TotalSeconds;

            if (time_passed <= 0)
            {
                coordinates = new Tuple <double, double>(initial_location_longitude, initial_location_latitude);
                return(coordinates);
            }
            SegmentClient start_segment = new SegmentClient();

            start_segment.longitude = initial_location_longitude;
            start_segment.latitude  = initial_location_latitude;
            SegmentClient next_segment  = new SegmentClient();
            bool          hasNoSegments = true;

            foreach (SegmentClient segment in segments)
            {
                hasNoSegments = false;
                next_segment  = segment;
                if (next_segment.timespan_seconds == time_passed)
                {
                    coordinates = new Tuple <double, double>(next_segment.longitude, next_segment.latitude);
                    return(coordinates);
                }
                else if (time_passed > next_segment.timespan_seconds)
                {
                    time_passed  -= segment.timespan_seconds;
                    start_segment = next_segment;
                }
                else
                {
                    double d = Tools.getDistance(next_segment, start_segment); //Positive
                    if (d == 0)
                    {
                        coordinates = new Tuple <double, double>
                                          (start_segment.longitude, start_segment.latitude);
                        return(coordinates);
                    }
                    double speed = d / next_segment.timespan_seconds;
                    double k     = speed * time_passed;
                    double l     = d - k;

                    double longitude = (l * start_segment.longitude +
                                        k * next_segment.longitude) / d;

                    double latitude = (l * start_segment.latitude +
                                       k * next_segment.latitude) / d;
                    coordinates = new Tuple <double, double>(longitude, latitude);
                    return(coordinates);
                }
            }
            if (hasNoSegments)
            {
                coordinates = new Tuple <double, double>
                                  (initial_location_longitude, initial_location_latitude);
            }
            else
            {
                coordinates = new Tuple <double, double>
                                  (start_segment.longitude, start_segment.latitude);
            }
            return(coordinates);
        }