static void Main(string[] args) { var unlocodeCsvPaths = new List <string>() { @"C:\Projects\ReverseDNS\2017-02 UNLOCODE\2017-2 UNLOCODE CodeListPart1.csv", @"C:\Projects\ReverseDNS\2017-02 UNLOCODE\2017-2 UNLOCODE CodeListPart2.csv", @"C:\Projects\ReverseDNS\2017-02 UNLOCODE\2017-2 UNLOCODE CodeListPart3.csv" }; var outPath = @"C:\Projects\ReverseDNS\2017-02 UNLOCODE\unlocode-geonames.txt"; // Geonames data folder var geonamesDatePrefix = "2018-03-06"; var geonamesDataRootInputFolder = @"C:\Projects\ReverseDNS\Geonames\"; // Dataset with CLLI codes mapped to geonames IDs var clliPath = @"C:\Projects\ReverseDNS\first6-city-geonames.tsv"; var unlocodePath = @"C:\Projects\ReverseDNS\2017-02 UNLOCODE\unlocode-geonames.txt"; var closestCityFinder = new ClosestCityFinder(citiesPath: Path.Combine(geonamesDataRootInputFolder, geonamesDatePrefix, "cities1000.txt"), alternateNamesPath: Path.Combine(geonamesDataRootInputFolder, geonamesDatePrefix, "alternateNames.txt"), admin1Path: Path.Combine(geonamesDataRootInputFolder, geonamesDatePrefix, "admin1CodesASCII.txt"), admin2Path: Path.Combine(geonamesDataRootInputFolder, geonamesDatePrefix, "admin2Codes.txt"), countriesPath: Path.Combine(geonamesDataRootInputFolder, geonamesDatePrefix, "countryInfo.txt"), clliPath: clliPath, unlocodePath: unlocodePath); UNLOCODEGeo.GeolocateCodes(closestCityFinder, unlocodeCsvPaths, outPath); }
/* * public static void OutputLowercaseCodes(string unlocodeCsvPath, string justLocationOutputPath, string combinedCodeLocationOutputPath) * { * using (var inFile = new StreamReader(unlocodeCsvPath)) * using (var locationOutFile = new StreamWriter(justLocationOutputPath)) * using (var combinedOutFile = new StreamWriter(combinedCodeLocationOutputPath)) * { * var csv = new CsvReader(inFile); * csv.Configuration.IgnoreQuotes = false; * csv.Configuration.HasHeaderRecord = true; * csv.Configuration.IgnoreHeaderWhiteSpace = true; * * while (csv.Read()) * { * var countryCodeLower = csv.GetField<string>("Country").Trim().ToLowerInvariant(); * var locationCodeLower = csv.GetField<string>("Location").Trim().ToLowerInvariant(); * * if (countryCodeLower.Length > 0 && locationCodeLower.Length > 0) * { * locationOutFile.WriteLine(locationCodeLower); * combinedOutFile.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}{1}", countryCodeLower, locationCodeLower)); * } * } * } * } */ public static void GeolocateCodes(ClosestCityFinder closestCityFinder, List <string> unlocodeCsvPaths, string outPath) { var countWithCoordinates = 0; var countWithCoordinatesIncorrect = 0; var countWithCoordinatesFound = 0; var countWithNameOnly = 0; foreach (var unlocodeCsvPath in unlocodeCsvPaths) { using (var inFile = new StreamReader(unlocodeCsvPath)) { using (var outFile = new StreamWriter(outPath)) { var csv = new CsvReader(inFile); csv.Configuration.IgnoreQuotes = false; csv.Configuration.HasHeaderRecord = false; csv.Configuration.IgnoreHeaderWhiteSpace = true; while (csv.Read()) { // Columns: Change,Country,Location,Name,NameWoDiacritics,Subdivision,Status,Function,Date,IATA,Coordinates,Remarks // Example: ,AF,BAG,Bagram,Bagram,PAR,RL,--3-----,0307,,3457N 06915E, var entry = csv.GetRecord <UNLOCODEEntry>(); if (string.IsNullOrWhiteSpace(entry.Country) || string.IsNullOrWhiteSpace(entry.Location)) { continue; } if (!string.IsNullOrWhiteSpace(entry.Coordinates)) { countWithCoordinates++; var latitudeRaw = ParseLatitude(entry.Coordinates); var longitudeRaw = ParseLongitude(entry.Coordinates); if (latitudeRaw != null && longitudeRaw != null) { var latitude = (double)latitudeRaw; var longitude = (double)longitudeRaw; var closestCity = closestCityFinder.FindClosestCityForCoordinates(latitude, longitude); if (closestCity != null) { countWithCoordinatesFound++; outFile.WriteLine($"{entry.Country}\t{entry.Location}\t{closestCity.Id}"); } } else { countWithCoordinatesIncorrect++; } } else { countWithNameOnly++; } /* * var countryCodeLower = csv.GetField<string>("Country").Trim().ToLowerInvariant(); * var locationCodeLower = csv.GetField<string>("Location").Trim().ToLowerInvariant(); * * if (countryCodeLower.Length > 0 && locationCodeLower.Length > 0) * { * locationOutFile.WriteLine(locationCodeLower); * combinedOutFile.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}{1}", countryCodeLower, locationCodeLower)); * } */ } } } } Console.WriteLine("Done"); }