public void GenerateAirportCsvList(AirportList airportList) { var csvFile = XP2AFSConverterManager.Settings.XP2AFSConverterFolder + @"airports.csv"; var writer = new StreamWriter(csvFile); var csv = new CsvWriter(writer); csv.WriteHeader <AirportCsvListItem>(); csv.NextRecord(); int i = 0; foreach (var airport in airportList.Airports) { // Exclude sea runways, closed runways and helipads if (!airport.AirportName.StartsWith("[S]") && !airport.AirportName.StartsWith("[X]") && !airport.AirportName.StartsWith("[H]")) { var airportXPFullDirectory = DirectoryHelper.GetAirportXPFullDirectory(airport.AirportCode, XP2AFSConverterManager.Settings); var airportZipFilename = airportXPFullDirectory + @"\" + airport.AirportCode + ".zip"; var airportFilename = airportXPFullDirectory + @"\airport.xml"; var airportSceneryFilename = airportXPFullDirectory + @"\scenery.xml"; if (File.Exists(airportZipFilename)) { //if (airport.AirportCode == "") log.InfoFormat("Adding airport {0} of {1}", i + 1, airportList.Airports.Count); // Parse the DST and DAT files var datFileLoader = new DATFileLoader(); var datFile = datFileLoader.GetDATFileFromXPZip(airport.AirportCode, airportZipFilename); //var dsfFile = this.GetDSFFileFromXPZip(airport.AirportCode, airportZipFilename); var sceneryFile = DeserializeSceneryFile(airportSceneryFilename); // Create empty AFS files var tscFile = new TSCFile(); var tocFile = new TOCFile(); var datConverter = new DATConverter(); // Convert the X-Plane files datConverter.Convert(datFile, tscFile, tocFile); if (datFile.AirportHeader.AirportType == AirportType.Airport && datFile.LandRunways != null) { TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; AirportCsvListItem airportListItem = new AirportCsvListItem(); airportListItem.Code = airport.AirportCode; airportListItem.Name = textInfo.ToTitleCase(airport.AirportName.ToLower()); airportListItem.IsHelipad = false; if (datFile.MetadataLookup.ContainsKey("datum_lat")) { double lat; if (Double.TryParse(datFile.MetadataLookup["datum_lat"], out lat)) { airportListItem.Latitude = lat; } } if (datFile.MetadataLookup.ContainsKey("datum_lon")) { double lon; if (Double.TryParse(datFile.MetadataLookup["datum_lon"], out lon)) { airportListItem.Longitude = lon; } } if (datFile.MetadataLookup.ContainsKey("country")) { airportListItem.Country = datFile.MetadataLookup["country"]; } if (datFile.MetadataLookup.ContainsKey("state")) { airportListItem.Region = datFile.MetadataLookup["state"]; } if (datFile.MetadataLookup.ContainsKey("city")) { airportListItem.City = datFile.MetadataLookup["city"]; } if (datFile.MetadataLookup.ContainsKey("region_code")) { airportListItem.RegionCode = datFile.MetadataLookup["region_code"]; } if (datFile.MetadataLookup.ContainsKey("icao_code")) { airportListItem.ICAO = datFile.MetadataLookup["icao_code"]; } if (datFile.MetadataLookup.ContainsKey("iata_code")) { airportListItem.IATA = datFile.MetadataLookup["iata_code"]; } if (datFile.MetadataLookup.ContainsKey("faa_code")) { airportListItem.FAACode = datFile.MetadataLookup["faa_code"]; } if (datFile.Pavements == null || datFile.Pavements.Count == 0) { airportListItem.HasPavements = false; } else { airportListItem.HasPavements = true; } if (sceneryFile.Type == "3D") { airportListItem.Is3D = true; } else { airportListItem.Is3D = false; } foreach (var runway in datFile.LandRunways) { airportListItem.RunwaySurfaceType = (int)runway.SurfaceType; } airportListItem.RunwayCount = datFile.LandRunways.Count; // If we couldn't get a value from the metadata we need to get it elsewhere if (!airportListItem.Latitude.HasValue || !airportListItem.Longitude.HasValue) { airportListItem.Latitude = tscFile.Location.Latitude; airportListItem.Longitude = tscFile.Location.Longitude; } csv.WriteRecord(airportListItem); csv.NextRecord(); } } else { log.ErrorFormat("Could not find the data for airport {0} make sure it is downloaded", airport.AirportCode); } } i++; } writer.Flush(); writer.Close(); writer = null; }
private void ConvertAirport(string icaoCode) { log.InfoFormat("Converting {0}", icaoCode); var airportXPFullDirectory = DirectoryHelper.GetAirportXPFullDirectory(icaoCode, XP2AFSConverterManager.Settings); var airportZipFilename = airportXPFullDirectory + @"\" + icaoCode + ".zip"; var airportFilename = airportXPFullDirectory + @"\airport.xml"; var airportSceneryFilename = airportXPFullDirectory + @"\scenery.xml"; var airportAFSFullDirectory = DirectoryHelper.GetAirportAFSFullDirectory(icaoCode, XP2AFSConverterManager.Settings); // Make sure the 'input' folder exists if (!Directory.Exists(airportAFSFullDirectory + @"\Input")) { Directory.CreateDirectory(airportAFSFullDirectory + @"\Input"); } var tscFilename = airportAFSFullDirectory + @"\Input\" + icaoCode + ".tsc"; var tocFilename = airportAFSFullDirectory + @"\Input\" + icaoCode + ".toc"; var tmcFilename = airportAFSFullDirectory + @"\Input\" + icaoCode + ".tmc"; if (File.Exists(airportZipFilename)) { // Parse the DST and DAT files var datFileLoader = new DATFileLoader(); var dsfFileLoader = new DSFFileLoader(); var datFile = datFileLoader.GetDATFileFromXPZip(icaoCode, airportZipFilename); var dsfFile = dsfFileLoader.GetDSFFileFromXPZip(icaoCode, airportZipFilename); // Create empty AFS files var tscFile = new TSCFile(); var tocFile = new TOCFile(); var tmcFile = new TMCFile(); // Convert the X-Plane files this.datConverter.Convert(datFile, tscFile, tocFile); this.dsfConverter.Convert(dsfFile, tscFile, tocFile); // Update the TMC file this.UpdateTMCFile(tmcFile, airportAFSFullDirectory); // Get strings of the AFS files var tscFileString = tscFile.ToString(); var tocFileString = tocFile.ToString(); var tmcFileString = tmcFile.ToString(); if (!Directory.Exists(airportAFSFullDirectory)) { Directory.CreateDirectory(airportAFSFullDirectory); } // Write the AFS files File.WriteAllText(tscFilename, tscFileString); File.WriteAllText(tocFilename, tocFileString); File.WriteAllText(tmcFilename, tmcFileString); var maxScriptGenerator = new MaxScriptGenerator(); maxScriptGenerator.GenerateScripts(icaoCode, datFile, dsfFile, tscFile, airportAFSFullDirectory, DirectoryHelper.GetTexturesDirectory(XP2AFSConverterManager.Settings)); log.Info("Airport conversion done"); } else { log.ErrorFormat("Could not find the data for airport {0} make sure it is downloaded", icaoCode); } }