static void Main(string[] args) { var downloader = GeoFileDownloader.CreateGeoFileDownloader(); string destinationPath = Environment.CurrentDirectory; string countryPath = downloader.DownloadFile("countryInfo.txt", destinationPath).Single(); IEnumerable <CountryInfo> countries = GeoFileReader.ReadCountryInfo(countryPath); var sb = new StringBuilder(); sb.AppendLine("using System.ComponentModel.DataAnnotations;"); sb.AppendLine(); sb.AppendLine("public enum Countries"); sb.AppendLine("{"); foreach (CountryInfo country in countries) { sb.AppendLine(format( name: country.Country, groupName: country.Continent, description: country.ISO_Numeric, shortName: country.ISO_Alpha2, memberName: country.ISO_Alpha3, value: country.GeoNameId )); } sb.AppendLine("}"); File.WriteAllText("..\\..\\..\\Countries.cs", sb.ToString()); }
public void CountryInfoComposer_ComposesFileCorrectly() { // We use a *slightly* different file (test_countryInfo2 instead of test_countryInfo) because the // CountryInfoParser "fixes" phonenumbers with a missing + (e.g. 31 vs. +31); this way the files // would always differ; test_countryInfo2 has these values fixed var src = @"testdata\test_countryInfo2.txt"; var dst = @"testdata\test_countryInfo2.out.txt"; GeoFileWriter.WriteCountryInfo(dst, GeoFileReader.ReadCountryInfo(src)); FileUtil.EnsureFilesAreFunctionallyEqual(src, dst, 19, 0, new[] { '\t' }, Encoding.UTF8, true); }
public void CountryInfoParser_ParsesFileCorrectly() { var target = GeoFileReader.ReadCountryInfo(@"testdata\test_countryInfo.txt").ToArray(); Assert.AreEqual(2, target.Length); //Should've skipped comment lines Assert.AreEqual("BZ", target[0].ISO_Alpha2); Assert.AreEqual("BLZ", target[0].ISO_Alpha3); Assert.AreEqual("084", target[0].ISO_Numeric); //Check if leading zeroes are preserved Assert.AreEqual("BH", target[0].FIPS); Assert.AreEqual("Belize", target[0].Country); Assert.AreEqual("Belmopan", target[0].Capital); Assert.AreEqual(22966, target[0].Area); Assert.AreEqual(314522, target[0].Population); Assert.AreEqual("NA", target[0].Continent); Assert.AreEqual(".bz", target[0].Tld); Assert.AreEqual("BZD", target[0].CurrencyCode); Assert.AreEqual("Dollar", target[0].CurrencyName); Assert.AreEqual("+501", target[0].Phone); //Intl. dialingcodes should be prefixed with a + even though they're not in the file Assert.AreEqual(string.Empty, target[0].PostalCodeFormat); Assert.AreEqual(string.Empty, target[0].PostalCodeRegex); CollectionAssert.AreEqual(new[] { "en-BZ", "es" }, target[0].Languages); Assert.AreEqual(3582678, target[0].GeoNameId); CollectionAssert.AreEqual(new[] { "GT", "MX" }, target[0].Neighbours); Assert.AreEqual(string.Empty, target[0].EquivalentFipsCode); Assert.AreEqual("XX", target[1].ISO_Alpha2); Assert.AreEqual("XXX", target[1].ISO_Alpha3); Assert.AreEqual("999", target[1].ISO_Numeric); Assert.AreEqual(string.Empty, target[1].FIPS); Assert.AreEqual("MADE UP COUNTRY", target[1].Country); Assert.AreEqual(string.Empty, target[1].Capital); Assert.IsNull(target[1].Area); //Can be unspecified/null Assert.AreEqual(0, target[1].Population); Assert.AreEqual("EU", target[1].Continent); Assert.AreEqual(".XX", target[1].Tld); Assert.AreEqual("XXX", target[1].CurrencyCode); Assert.AreEqual("X-Dollar", target[1].CurrencyName); Assert.AreEqual("+1", target[1].Phone); //Intl. dialingcodes should be prefixed with a + even though they're not in the file Assert.AreEqual("@# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA", target[1].PostalCodeFormat); Assert.AreEqual(@"^(([A-Z]\d{2}[A-Z]{2})|([A-Z]\d{3}[A-Z]{2})|([A-Z]{2}\d{2}[A-Z]{2})|([A-Z]{2}\d{3}[A-Z]{2})|([A-Z]\d[A-Z]\d[A-Z]{2})|([A-Z]{2}\d[A-Z]\d[A-Z]{2})|(GIR0AA))$", target[1].PostalCodeRegex); Assert.AreEqual(0, target[1].Languages.Length); Assert.IsNull(target[1].GeoNameId); Assert.AreEqual(0, target[1].Neighbours.Length); Assert.AreEqual("XXX", target[1].EquivalentFipsCode); }
public List <Country> PopulateGeoNameIDs(List <Country> countries) { if (!File.Exists(_countryInfoPath)) { Console.WriteLine("Downloading country info data…"); var downloader = GeoFileDownloader.CreateGeoFileDownloader(); downloader.DownloadFile(_fileName, _dataPath); Console.WriteLine("Country info downloaded…"); } var nonCountries = new List <Country>(); foreach (var country in countries) { var geoNameID = GeoFileReader.ReadCountryInfo(_countryInfoPath) .Where(c => c.ISO_Alpha2 == country.Code) .FirstOrDefault()? .GeoNameId; if (geoNameID is int id) { country.GeoNameID = id; } else { nonCountries.Add(country); } } foreach (var nonCountry in nonCountries) { countries.Remove(nonCountry); } return(countries); }
public void FileReader_CountryInfo_StreamOverload() { using (var s = File.OpenRead(@"testdata\test_CountryInfo.txt")) GeoFileReader.ReadCountryInfo(s).Count(); }
public void FileReader_ThrowsOnIncompatibleOrInvalidGZipFiles() { var target = GeoFileReader.ReadCountryInfo(@"testdata\countryInfo_not_gzip_compat.txt.gz").ToArray(); }
public void FileReader_HandlesGZippedFilesCorrectly() { var target = GeoFileReader.ReadCountryInfo(@"testdata\countryInfo_gzip_compat.txt.gz").ToArray(); Assert.AreEqual(2, target.Length); }
private static GeoFile[] GetDumps(GeoFileDownloader downloader) { return(new[] { new GeoFile { Filename = "admin1CodesASCII.txt", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadAdmin1Codes(fn).Count(); }) }, new GeoFile { Filename = "admin2Codes.txt", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadAdmin2Codes(fn).Count(); }) }, new GeoFile { Filename = "allCountries.zip", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadExtendedGeoNames(fn).Count(); }) }, new GeoFile { Filename = "alternateNames.zip", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadAlternateNames(fn).Count(); }) }, new GeoFile { Filename = "cities1000.zip", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadExtendedGeoNames(fn).Count(); }) }, new GeoFile { Filename = "cities15000.zip", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadExtendedGeoNames(fn).Count(); }) }, new GeoFile { Filename = "cities5000.zip", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadExtendedGeoNames(fn).Count(); }) }, new GeoFile { Filename = "countryInfo.txt", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadCountryInfo(fn).Count(); }) }, //Featurecodes are downloaded by GetCountryDumps() new GeoFile { Filename = "hierarchy.zip", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadHierarchy(fn).Count(); }) }, new GeoFile { Filename = "iso-languagecodes.txt", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadISOLanguageCodes(fn).Count(); }) }, new GeoFile { Filename = "no-country.zip", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadExtendedGeoNames(fn).Count(); }) }, new GeoFile { Filename = "timeZones.txt", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadTimeZones(fn).Count(); }) }, new GeoFile { Filename = "userTags.zip", Test = (f) => ExecuteTest(f, (fn) => { return GeoFileReader.ReadUserTags(fn).Count(); }) }, }.Union(GetCountryDumps(downloader)).ToArray()); }
public static void Populate(IConfiguration configuration) { Console.WriteLine("Getting ready to populate country info…"); var connectionString = configuration["ConnectionString"]; var dataPath = configuration["DataSourcePath"]; var countriesPath = Path.Combine(dataPath, @"countryInfo.txt"); if (!File.Exists(countriesPath)) { Console.WriteLine("Downloading country info…"); var downloader = GeoFileDownloader.CreateGeoFileDownloader(); downloader.DownloadFile("countryInfo.txt", dataPath); } var results = GeoFileReader.ReadCountryInfo(countriesPath).OrderBy(p => p.GeoNameId); using (var connection = new SqlConnection(connectionString)) { connection.Open(); Console.WriteLine("Populating country info…"); const string sql = @"INSERT INTO CountryInfo VALUES (@ISO_Alpha2, @ISO_Alpha3, @ISO_Numeric, @FIPS, @Country, @Capital, @Area, @Population, @Continent, @Tld, @CurrencyCode, @CurrencyName, @Phone, @PostalCodeFormat, @PostalCodeRegex, @GeoNameId, @EquivalentFipsCode)"; var command = connection.CreateCommand(); command.CommandText = sql; string[] parameterNames = new[] { "@ISO_Alpha2", "@ISO_Alpha3", "@ISO_Numeric", "@FIPS", "@Country", "@Capital", "@Area", "@Population", "@Continent", "@Tld", "@CurrencyCode", "@CurrencyName", "@Phone", "@PostalCodeFormat", "@PostalCodeRegex", "@GeoNameId", "@EquivalentFipsCode" }; DbParameter[] parameters = parameterNames.Select(pn => { DbParameter parameter = command.CreateParameter(); parameter.ParameterName = pn; command.Parameters.Add(parameter); return(parameter); }) .ToArray(); foreach (var r in results) { parameters[0].Value = r.ISO_Alpha2; parameters[1].Value = r.ISO_Alpha3; parameters[2].Value = r.ISO_Numeric; parameters[3].Value = r.FIPS; parameters[4].Value = r.Country; parameters[5].Value = r.Capital; parameters[6].Value = r.Area; parameters[7].Value = r.Population; parameters[8].Value = r.Continent; parameters[9].Value = r.Tld; parameters[10].Value = r.CurrencyCode; parameters[11].Value = r.CurrencyName; parameters[12].Value = r.Phone; parameters[13].Value = r.PostalCodeFormat; parameters[14].Value = r.PostalCodeRegex; parameters[15].Value = r.GeoNameId; parameters[16].Value = r.EquivalentFipsCode; Console.WriteLine("Country: " + r.Country); command.ExecuteNonQuery(); } Console.WriteLine(); } }