public PopUpMap(List<Airport> airports)
            : this(ImageSize * 3)
        {
            this.ShowingAirports = true;
            this.KeyDown += new KeyEventHandler(PopUpMap_KeyDown);
            this.AirportsList = airports;
            this.Width = MapSize + 200;
            this.ZoomCoordinates = new Coordinates(new Coordinate(0, 0, 0, Coordinate.Directions.N), new Coordinate(0, 0, 0, Coordinate.Directions.E));
            this.Zoom = 1;

            showMap(airports, this.Zoom, this.ZoomCoordinates);
        }
        //converts coordinates to a map position
        public static Point WorldToTilePos(Coordinates coordinates, int zoom)
        {
            double lon = coordinates.Longitude.toDecimal();
            double lat = coordinates.Latitude.toDecimal();

            Point p = new Point();
            p.X = (float)((lon + 180.0) / 360.0 * (1 << zoom));
            p.Y = (float)((1.0 - Math.Log(Math.Tan(lat * Math.PI / 180.0) +
                1.0 / Math.Cos(lat * Math.PI / 180.0)) / Math.PI) / 2.0 * (1 << zoom));

            double maxXValue = Math.Pow(2, zoom);

            if (p.X < 0)
                p.X = maxXValue + p.X;

            if (p.X > maxXValue)
                p.X = p.X - maxXValue;

            return p;
        }
        public AirportProfile(string name, string code, string icaocode, AirportType type, Period<DateTime> period, Town town, TimeSpan offsetGMT, TimeSpan offsetDST, Coordinates coordinates, GeneralHelpers.Size cargo, double cargovolume, Weather.Season season)
        {
            this.PaxValues = new List<PaxValue>();

            this.Name = name;
            this.Period = period;
            this.IATACode = code;
            this.ICAOCode = icaocode;
            this.Type = type;
            this.Town = town;
            this.Coordinates = coordinates;
            this.CargoVolume = cargovolume;
            this.MajorDestionations = new Dictionary<string, int>();
            this.Cargo = cargo;
            this.Logo = "";
            this.OffsetDST = offsetDST;
            this.OffsetGMT = offsetGMT;
            this.Season = season;
            this.ID = string.Format("{0:00}-{1:00}-{2:00}-{3:00}-{4:00}-{5:00}", char.ConvertToUtf32(this.IATACode, 0), char.ConvertToUtf32(this.IATACode, 1), char.ConvertToUtf32(this.IATACode, 2), name.Length, char.ConvertToUtf32(this.Name, this.Name.Length / 2), (int)this.Cargo);
        }
        public PopUpMap(List<Route> routes)
            : this(ImageSize * 3)
        {
            this.KeyDown += new KeyEventHandler(PopUpMap_KeyDown);
            this.Zoom = 1;
            this.ZoomCoordinates = new Coordinates(new Coordinate(0, 0, 0, Coordinate.Directions.N), new Coordinate(0, 0, 0, Coordinate.Directions.E));
            this.Width = MapSize + 200;
            this.RoutesList = routes;

            showMap(this.RoutesList, this.Zoom, this.ZoomCoordinates);
        }
        //creates the map for coordinates
        private void showMap(Coordinates coordinates, Boolean isAirport)
        {
            this.Zoom = 3;

            Canvas c = new Canvas();
            c.Width = this.Width;
            c.Height = this.Height;

            StringReader stringReader = new StringReader(GeneralHelpers.BigMapXaml);
            XmlReader xmlReader = XmlReader.Create(stringReader);

            Canvas panelMap = (Canvas)XamlReader.Load(xmlReader);

            Point pos = GraphicsHelpers.WorldToTilePos(coordinates, this.Zoom);

            Point p = new Point(pos.X * ImageSize, pos.Y * ImageSize);

            double tMapSize = Math.Sqrt(panelMap.Children.Count) * ImageSize;

            double x = p.X - this.Width / 2;
            double y = p.Y - this.Height / 2;

            if (x < 0) x = 0;
            if (y < 0) y = 0;
            if (x + this.Width > tMapSize) x = tMapSize - this.Width;
            if (y + this.Height > tMapSize) y = tMapSize - this.Height;

            //panelMap.ClipToBounds = true;

            panelMap.Width = this.Width;
            panelMap.Height = this.Height;

            if (isAirport)
                panelMap.Children.Add(createPin(p, Airports.GetAirport(coordinates)));
            else
                panelMap.Children.Add(createPin(p));

            panelMap.Clip = new RectangleGeometry(new Rect(x, y, this.Width, this.Height));
            // panelMap.ClipToBounds = false;

            Canvas.SetLeft(panelMap, -x);
            Canvas.SetTop(panelMap, -y);
            c.Children.Add(panelMap);
            c.ClipToBounds = true;

            this.Content = c;
        }
        //shows the map for a list of airport with specific coordinates in focus
        private void showMap(List<Airport> airports, int zoom, Coordinates focused)
        {
            double px, py;

            if (focused != null)
            {
                Point pos = GraphicsHelpers.WorldToTilePos(focused, zoom);

                px = Math.Max(1, pos.X);
                py = Math.Max(1, pos.Y);
            }
            else
            {
                px = 1;
                py = 1;
            }

            Canvas panelMap = new Canvas();

            Canvas panelMainMap = new Canvas();
            panelMainMap.Width = 2 * ImageSize;

            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    string name = string.Format(@"{0}\{1}\{2}.png", zoom, x - 1 + (int)px, y - 1 + (int)py);

                    Image imgMap = new Image();
                    imgMap.Width = ImageSize;
                    imgMap.Height = ImageSize;
                    imgMap.Source = new BitmapImage(new Uri(AppSettings.getDataPath() + "\\graphics\\maps\\" + name, UriKind.RelativeOrAbsolute));
                    RenderOptions.SetBitmapScalingMode(imgMap, BitmapScalingMode.HighQuality);

                    Canvas.SetTop(imgMap, y * ImageSize);
                    Canvas.SetLeft(imgMap, x * ImageSize);

                    panelMainMap.Children.Add(imgMap);

                }
            }
            ScaleTransform transform = new ScaleTransform();
            transform.ScaleX = 1.5;
            transform.ScaleY = 1.5;
            panelMainMap.RenderTransform = transform;

            StackPanel sidePanel = createAirportSizeSidePanel();
            Canvas.SetTop(sidePanel, 0);
            Canvas.SetLeft(sidePanel, this.MapSize);

            panelMap.Children.Add(panelMainMap);
            panelMap.Children.Add(sidePanel);

            foreach (Airport airport in airports)
                showAirport(airport, panelMainMap, zoom, new Point((int)px - 1, (int)py - 1));

            this.Content = panelMap;
        }
        private void PopUpMap_KeyDown(object sender, KeyEventArgs e)
        {
            int zoomer = 4 - this.Zoom;

            if (e.Key == Key.OemPlus || e.Key == Key.OemMinus)
            {
                zoomMap(e.Key == Key.OemPlus);
            }
            if (e.Key == Key.Left && this.ZoomCoordinates.Longitude.Degrees > -180 + (40 * zoomer))
            {
                this.ZoomCoordinates = new Coordinates(this.ZoomCoordinates.Latitude, new Coordinate(this.ZoomCoordinates.Longitude.Degrees - 40 * zoomer, this.ZoomCoordinates.Longitude.Minutes, this.ZoomCoordinates.Longitude.Seconds, this.ZoomCoordinates.Longitude.Direction));
                if (this.ShowingAirports)
                    showMap(this.AirportsList, this.Zoom, this.ZoomCoordinates);
                else
                    showMap(this.RoutesList, this.Zoom, this.ZoomCoordinates);

            }
            if (e.Key == Key.Right && this.ZoomCoordinates.Longitude.Degrees < 180 - (40 * zoomer))
            {
                this.ZoomCoordinates = new Coordinates(this.ZoomCoordinates.Latitude, new Coordinate(this.ZoomCoordinates.Longitude.Degrees + 40 * zoomer, this.ZoomCoordinates.Longitude.Minutes, this.ZoomCoordinates.Longitude.Seconds, this.ZoomCoordinates.Longitude.Direction));
                if (this.ShowingAirports)
                    showMap(this.AirportsList, this.Zoom, this.ZoomCoordinates);
                else
                    showMap(this.RoutesList, this.Zoom, this.ZoomCoordinates);

            }
            if (e.Key == Key.Up && this.ZoomCoordinates.Latitude.Degrees < 90 - (30 * zoomer))
            {
                this.ZoomCoordinates = new Coordinates(new Coordinate(this.ZoomCoordinates.Latitude.Degrees + 30 * zoomer, this.ZoomCoordinates.Latitude.Minutes, this.ZoomCoordinates.Latitude.Seconds, this.ZoomCoordinates.Latitude.Direction), this.ZoomCoordinates.Longitude);
                if (this.ShowingAirports)
                    showMap(this.AirportsList, this.Zoom, this.ZoomCoordinates);
                else
                    showMap(this.RoutesList, this.Zoom, this.ZoomCoordinates);

            }
            if (e.Key == Key.Down && this.ZoomCoordinates.Latitude.Degrees > -90 + (30 * zoomer))
            {
                this.ZoomCoordinates = new Coordinates(new Coordinate(this.ZoomCoordinates.Latitude.Degrees - 30 * zoomer, this.ZoomCoordinates.Latitude.Minutes, this.ZoomCoordinates.Latitude.Seconds, this.ZoomCoordinates.Latitude.Direction), this.ZoomCoordinates.Longitude);
                if (this.ShowingAirports)
                    showMap(this.AirportsList, this.Zoom, this.ZoomCoordinates);
                else
                    showMap(this.RoutesList, this.Zoom, this.ZoomCoordinates);

            }
        }
Example #8
0
        private static void LoadAirports(string filename)
        {
            string id = "";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filename);
                XmlElement root = doc.DocumentElement;

                XmlNodeList airportsList = root.SelectNodes("//airport");

                foreach (XmlElement airportElement in airportsList)
                {

                    string name = airportElement.Attributes["name"].Value;
                    string icao = airportElement.Attributes["icao"].Value;
                    string iata = airportElement.Attributes["iata"].Value;

                    id = name + " iata: " + iata;

                    AirportProfile.AirportType type = (AirportProfile.AirportType)Enum.Parse(typeof(AirportProfile.AirportType), airportElement.Attributes["type"].Value);
                    Weather.Season season = (Weather.Season)Enum.Parse(typeof(Weather.Season), airportElement.Attributes["season"].Value);

                    XmlElement periodElement = (XmlElement)airportElement.SelectSingleNode("period");

                    Period<DateTime> airportPeriod;
                    if (periodElement != null)
                    {
                        DateTime airportFrom = Convert.ToDateTime(periodElement.Attributes["from"].Value, new CultureInfo("en-US", false));
                        DateTime airportTo = Convert.ToDateTime(periodElement.Attributes["to"].Value, new CultureInfo("en-US", false));

                        airportPeriod = new Period<DateTime>(airportFrom, airportTo);
                    }
                    else
                        airportPeriod = new Period<DateTime>(new DateTime(1959, 12, 31), new DateTime(2199, 12, 31));

                    XmlElement townElement = (XmlElement)airportElement.SelectSingleNode("town");
                    string town = townElement.Attributes["town"].Value;
                    string country = townElement.Attributes["country"].Value;
                    TimeSpan gmt = TimeSpan.Parse(townElement.Attributes["GMT"].Value);
                    TimeSpan dst = TimeSpan.Parse(townElement.Attributes["DST"].Value);

                    XmlElement latitudeElement = (XmlElement)airportElement.SelectSingleNode("coordinates/latitude");
                    XmlElement longitudeElement = (XmlElement)airportElement.SelectSingleNode("coordinates/longitude");
                    string[] latitude = latitudeElement.Attributes["value"].Value.Split(new Char[] { '°', '\'' }, StringSplitOptions.RemoveEmptyEntries);
                    string[] longitude = longitude = longitudeElement.Attributes["value"].Value.Split(new Char[] { '°', '\'' }, StringSplitOptions.RemoveEmptyEntries);
                    int[] coords = new int[6];

                    //latitude
                    coords[0] = int.Parse(latitude[0]);
                    coords[1] = int.Parse(latitude[1]);
                    coords[2] = int.Parse(latitude[2]);

                    if (latitude[3] == "S")
                        coords[0] = -coords[0];

                    //longitude
                    coords[3] = int.Parse(longitude[0]);
                    coords[4] = int.Parse(longitude[1]);
                    coords[5] = int.Parse(longitude[2]);

                    if (longitude[3] == "W")
                        coords[3] = -coords[3];

                    /*
                    foreach(string l in latitude )
                    {
                        //int.TryParse(l, out coords[c]);
                        coords[c] = int.Parse(l);
                        c++;
                    }
                    c = 3;

                    foreach (string l in longitude)
                    {
                        //int.TryParse(l, out coords[c]);
                        coords[c] = int.Parse(l);

                        c++;
                    }*/

                    //cleaning up
                    latitude = null;
                    longitude = null;

                    //GeoCoordinate pos = new GeoCoordinate(MathHelpers.DMStoDeg(coords[0], coords[1], coords[2]),MathHelpers.DMStoDeg(coords[3],coords[4],coords[5]));
                    Coordinates pos = new Coordinates(new Coordinate(coords[0], coords[1], coords[2]), new Coordinate(coords[3], coords[4], coords[5]));

                    //double longitude = Coordinate.Parse(longitudeElement.Attributes["value"].Value);

                    XmlElement sizeElement = (XmlElement)airportElement.SelectSingleNode("size");

                    List<PaxValue> paxValues = new List<PaxValue>();

                    if (!sizeElement.HasChildNodes)
                    {
                        GeneralHelpers.Size size = (GeneralHelpers.Size)Enum.Parse(typeof(GeneralHelpers.Size), sizeElement.Attributes["value"].Value);
                        int pax = sizeElement.HasAttribute("pax") ? Convert.ToInt32(sizeElement.Attributes["pax"].Value) : 0;

                        paxValues.Add(new PaxValue(airportPeriod.From.Year, airportPeriod.To.Year, size, pax));
                    }
                    else
                    {
                        XmlNodeList yearsList = sizeElement.SelectNodes("yearvalues/yearvalue");

                        foreach (XmlElement yearElement in yearsList)
                        {
                            int fromYear = Convert.ToInt16(yearElement.Attributes["from"].Value);
                            int toYear = Convert.ToInt16(yearElement.Attributes["to"].Value);
                            GeneralHelpers.Size size = (GeneralHelpers.Size)Enum.Parse(typeof(GeneralHelpers.Size), yearElement.Attributes["value"].Value);
                            int pax = Convert.ToInt32(yearElement.Attributes["pax"].Value);

                            PaxValue paxValue = new PaxValue(fromYear, toYear, size, pax);

                            if (yearElement.HasAttribute("inflationafter"))
                                paxValue.InflationAfterYear = Convert.ToDouble(yearElement.Attributes["inflationafter"].Value, CultureInfo.GetCultureInfo("en-US").NumberFormat);
                            if (yearElement.HasAttribute("inflationbefore"))
                                paxValue.InflationBeforeYear = Convert.ToDouble(yearElement.Attributes["inflationbefore"].Value, CultureInfo.GetCultureInfo("en-US").NumberFormat);

                            paxValues.Add(paxValue);
                        }
                    }

                    GeneralHelpers.Size cargoSize = GeneralHelpers.Size.Very_small;
                    double cargovolume = sizeElement.HasAttribute("cargovolume") ? Convert.ToDouble(sizeElement.Attributes["cargovolume"].Value, CultureInfo.GetCultureInfo("en-US").NumberFormat) : 0;

                    if (sizeElement.HasAttribute("cargo"))
                        cargoSize = (GeneralHelpers.Size)Enum.Parse(typeof(GeneralHelpers.Size), sizeElement.Attributes["cargo"].Value);
                    else
                    {
                        //calculates the cargo size
                        GeneralHelpers.Size[] cargoSizes = (GeneralHelpers.Size[])Enum.GetValues(typeof(GeneralHelpers.Size));

                        int i=0;

                       Dictionary<GeneralHelpers.Size,int> list = new Dictionary<GeneralHelpers.Size,int>();

                        while (i<cargoSizes.Length && cargoSizes[i] <= paxValues.First().Size)
                        {
                            list.Add(cargoSizes[i], 10 - i);
                            i++;
                        }

                        cargoSize = AIHelpers.GetRandomItem(list);

                       }

                    Town eTown = null;
                    if (town.Contains(","))
                    {
                        State state = States.GetState(Countries.GetCountry(country), town.Split(',')[1].Trim());

                        if (state == null)
                            eTown = new Town(town.Split(',')[0], Countries.GetCountry(country));
                        else
                            eTown = new Town(town.Split(',')[0], Countries.GetCountry(country), state);
                    }
                    else
                        eTown = new Town(town, Countries.GetCountry(country));

                    AirportProfile profile = new AirportProfile(name, iata, icao, type, airportPeriod, eTown, gmt, dst, pos, cargoSize, cargovolume, season);
                    profile.PaxValues = paxValues;

                    Airport airport = new Airport(profile);

                    XmlElement destinationsElement = (XmlElement)airportElement.SelectSingleNode("destinations");

                    if (destinationsElement != null)
                    {
                        XmlNodeList majorDestinationsList = destinationsElement.SelectNodes("destination");

                        Dictionary<string, int> majorDestinations = new Dictionary<string, int>();

                        foreach (XmlElement majorDestinationNode in majorDestinationsList)
                        {
                            string majorDestination = majorDestinationNode.Attributes["airport"].Value;
                            int majorDestinationPax = Convert.ToInt32(majorDestinationNode.Attributes["pax"].Value);

                            majorDestinations.Add(majorDestination, majorDestinationPax);
                        }

                        airport.Profile.MajorDestionations = majorDestinations;

                    }

                    XmlNodeList terminalList = airportElement.SelectNodes("terminals/terminal");

                    foreach (XmlElement terminalNode in terminalList)
                    {
                        string terminalName = terminalNode.Attributes["name"].Value;
                        int terminalGates = XmlConvert.ToInt32(terminalNode.Attributes["gates"].Value);

                        airport.Terminals.addTerminal(new Terminal(airport, null, terminalName, terminalGates, new DateTime(1950, 1, 1)));
                    }

                    XmlNodeList runwaysList = airportElement.SelectNodes("runways/runway");

                    foreach (XmlElement runwayNode in runwaysList)
                    {
                        string runwayName = runwayNode.Attributes["name"].Value;
                        long runwayLength = XmlConvert.ToInt32(runwayNode.Attributes["length"].Value);
                        Runway.SurfaceType surface = (Runway.SurfaceType)Enum.Parse(typeof(Runway.SurfaceType), runwayNode.Attributes["surface"].Value);

                        airport.Runways.Add(new Runway(runwayName, runwayLength, surface, new DateTime(1900, 1, 1), true));

                    }

                    if (Airports.GetAirport(a => a.Profile.ID == airport.Profile.ID) == null)
                        Airports.AddAirport(airport);

                }
            }
            catch (Exception e)
            {
                /*
                System.IO.StreamWriter file = new System.IO.StreamWriter(AppSettings.getCommonApplicationDataPath() + "\\theairlinestartup.log", true);
                file.WriteLine("Airport failing: " + id);
                file.WriteLine(e.ToString());
                file.WriteLine(e.StackTrace);
                file.Close();
                 * */
                string i = id;
                string s = e.ToString();
            }
        }
        //returns the next entry after a specific date and not to a specific coordinates (airport)
        public RouteTimeTableEntry getNextEntry(DateTime time, Coordinates coordinates)
        {
            DayOfWeek day = time.DayOfWeek;

            int counter = 0;

            while (counter < 8)
            {

                List<RouteTimeTableEntry> entries = getEntries(day);

                foreach (RouteTimeTableEntry dEntry in (from e in entries orderby e.Time select e))
                {
                    if (!((dEntry.Day == time.DayOfWeek && dEntry.Time <= time.TimeOfDay)) && dEntry.Destination.Airport.Profile.Coordinates.CompareTo(coordinates) != 0)
                        return dEntry;
                }
                day++;

                if (day == (DayOfWeek)7)
                    day = (DayOfWeek)0;

                counter++;
            }

            return null;
        }
 //returns a possible match for coordinates
 public static Airport GetAirport(Coordinates coordinates)
 {
     return GetAllActiveAirports().Find(a => a.Profile.Coordinates.CompareTo(coordinates) == 0);
 }