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()); }
static void Main(string[] args) { //Test GeoName dumps var dumpdownloader = GeoFileDownloader.CreateGeoFileDownloader(); var dumpfiles = GetDumps(dumpdownloader); foreach (var geofile in dumpfiles) { Console.Write("Download: {0}", geofile.Filename); dumpdownloader.DownloadFile(geofile.Filename, Dump_DownloadDirectory); Console.Write(" Testing: "); Console.WriteLine("{0}", geofile.Test(Path.Combine(Dump_DownloadDirectory, geofile.Filename))); } //Test Postalcode dumps var postalcodedownloader = GeoFileDownloader.CreatePostalcodeDownloader(); var postalcodefiles = GetCountryPostalcodes(postalcodedownloader); foreach (var geofile in postalcodefiles) { Console.Write("Download: {0}", geofile.Filename); postalcodedownloader.DownloadFile(geofile.Filename, Postal_DownloadDirectory); Console.Write(" Testing: "); Console.WriteLine("{0}", geofile.Test(Path.Combine(Postal_DownloadDirectory, geofile.Filename))); } //DumpASCIILies(Dump_DownloadDirectory); Console.WriteLine("All done!"); }
static void Main(string[] args) { //Test GeoName dumps var dumpdownloader = GeoFileDownloader.CreateGeoFileDownloader(); var dumpfiles = GetDumps(dumpdownloader); dumpfiles.AsParallel().ForAll(g => { Console.WriteLine("Download: {0}", g.Filename); dumpdownloader.DownloadFile(g.Filename, Dump_DownloadDirectory); Console.WriteLine("Testing {0}: {1}", g.Filename, g.Test(Path.Combine(Dump_DownloadDirectory, g.Filename))); }); //Test Postalcode dumps var postalcodedownloader = GeoFileDownloader.CreatePostalcodeDownloader(); var postalcodefiles = GetCountryPostalcodes(postalcodedownloader); postalcodefiles.AsParallel().ForAll(g => { Console.WriteLine("Download: {0}", g.Filename); postalcodedownloader.DownloadFile(g.Filename, Postal_DownloadDirectory); Console.WriteLine("Testing {0}: {1}", g.Filename, g.Test(Path.Combine(Postal_DownloadDirectory, g.Filename))); }); Console.WriteLine("Testing ASCII fields"); DumpASCIILies(Dump_DownloadDirectory); Console.WriteLine("All done!"); }
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 static void Populate(IConfiguration configuration) { Console.WriteLine("Getting ready to populate countries…"); var connectionString = configuration["ConnectionString"]; var dataPath = configuration["DataSourcePath"]; var countriesPath = Path.Combine(dataPath, @"allCountries.txt"); if (!File.Exists(countriesPath)) { Console.WriteLine("Downloading GeoNames data for all countries…"); var downloader = GeoFileDownloader.CreateGeoFileDownloader(); downloader.DownloadFile("allCountries.zip", dataPath); } var results = GeoFileReader.ReadExtendedGeoNames(countriesPath).OrderBy(p => p.Id); using (var connection = new SqliteConnection(connectionString)) { connection.Open(); Console.WriteLine("Populating countries…"); var allowIdentityInsert = connection.CreateCommand(); allowIdentityInsert.CommandText = @"SET IDENTITY_INSERT GeoNames ON"; try { allowIdentityInsert.ExecuteNonQuery(); } catch (SqliteException exception) { Console.WriteLine("SQL Exception occurred. Error Code: " + exception.ErrorCode); } const string sql = @"INSERT INTO GeoNames (ID, Name, NameASCII, Latitude, Longitude, FeatureClass, FeatureCode, CountryCode, Population, Elevation, Dem, Timezone, ModificationDate) VALUES (@ID, @Name, @NameASCII, @Latitude, @Longitude, @FeatureClass, @FeatureCode, @CountryCode, @Population, @Elevation, @Dem, @Timezone, @ModificationDate)"; var command = connection.CreateCommand(); command.CommandText = sql; string[] parameterNames = new[] { "@ID", "@Name", "@NameASCII", "@Latitude", "@Longitude", "@FeatureClass", "@FeatureCode", "@CountryCode", "@Population", "@Elevation", "@Dem", "@Timezone", "@ModificationDate" }; 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.Id; parameters[1].Value = r.Name.HasValueOrDBNull(); parameters[2].Value = r.NameASCII.HasValueOrDBNull(); parameters[3].Value = r.Latitude; parameters[4].Value = r.Longitude; parameters[5].Value = r.FeatureClass.HasValueOrDBNull(); parameters[6].Value = r.FeatureCode.HasValueOrDBNull(); parameters[7].Value = r.CountryCode.HasValueOrDBNull(); parameters[8].Value = r.Population; parameters[9].Value = r.Elevation.HasValueOrDBNull(); parameters[10].Value = r.Dem; parameters[11].Value = r.Timezone.HasValueOrDBNull(); parameters[12].Value = r.ModificationDate; command.ExecuteNonQuery(); Console.WriteLine("GeoName ID: " + r.Id); } var disallowIdentityInsert = connection.CreateCommand(); disallowIdentityInsert.CommandText = @"SET IDENTITY_INSERT GeoNames OFF"; try { disallowIdentityInsert.ExecuteNonQuery(); } catch (SqliteException exception) { Console.WriteLine("SQL Exception occurred. Error Code: " + exception.ErrorCode); } Console.WriteLine(); } }
public static void Populate(IConfiguration configuration) { Console.WriteLine("Getting ready to populate GeoNames…"); var connectionString = configuration["ConnectionString"]; var dataPath = configuration["DataSourcePath"]; var minimumPopulation = configuration["GeoNames:CitiesMinimumPopulation"]; var includeCountriesData = configuration["GeoNames:IncludeCountriesData"]; string citiesFileName; switch (minimumPopulation) { case "1000": citiesFileName = "cities1000"; break; case "5000": citiesFileName = "cities5000"; break; case "15000": citiesFileName = "cities15000"; break; default: citiesFileName = "cities15000"; break; } var citiesPath = Path.Combine(dataPath, citiesFileName + ".txt"); if (!File.Exists(citiesPath)) { Console.WriteLine("Downloading specified GeoNames data…"); var downloader = GeoFileDownloader.CreateGeoFileDownloader(); downloader.DownloadFile(citiesFileName + ".zip", dataPath); } var results = GeoFileReader.ReadExtendedGeoNames(citiesPath).ToList(); if (includeCountriesData == "true") { var countriesPath = Path.Combine(dataPath, @"allCountries.txt"); if (!File.Exists(countriesPath)) { Console.WriteLine("Downloading GeoNames data for all countries…"); var downloader = GeoFileDownloader.CreateGeoFileDownloader(); downloader.DownloadFile("allCountries.zip", dataPath); } var countriesNames = GeoFileReader.ReadExtendedGeoNames(countriesPath) .Where(p => p.FeatureCode == "PCLI" || p.FeatureCode == "ADM1"); results.AddRange(countriesNames); } results.OrderBy(p => p.Id); using (var connection = new SqliteConnection(connectionString)) { connection.Open(); Console.WriteLine("Populating GeoNames…"); const string sql = @"INSERT INTO GeoNames (ID, Name, NameASCII, Latitude, Longitude, FeatureClass, FeatureCode, CountryCode, Population, Elevation, Dem, Timezone, ModificationDate) VALUES (@ID, @Name, @NameASCII, @Latitude, @Longitude, @FeatureClass, @FeatureCode, @CountryCode, @Population, @Elevation, @Dem, @Timezone, @ModificationDate)"; var command = connection.CreateCommand(); command.CommandText = sql; string[] parameterNames = new[] { "@ID", "@Name", "@NameASCII", "@Latitude", "@Longitude", "@FeatureClass", "@FeatureCode", "@CountryCode", "@Population", "@Elevation", "@Dem", "@Timezone", "@ModificationDate" }; 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.Id; parameters[1].Value = r.Name.HasValueOrDBNull(); parameters[2].Value = r.NameASCII.HasValueOrDBNull(); parameters[3].Value = r.Latitude; parameters[4].Value = r.Longitude; parameters[5].Value = r.FeatureClass.HasValueOrDBNull(); parameters[6].Value = r.FeatureCode.HasValueOrDBNull(); parameters[7].Value = r.CountryCode.HasValueOrDBNull(); parameters[8].Value = r.Population; parameters[9].Value = r.Elevation.HasValueOrDBNull(); parameters[10].Value = r.Dem; parameters[11].Value = r.Timezone.HasValueOrDBNull(); parameters[12].Value = r.ModificationDate; command.ExecuteNonQuery(); Console.WriteLine("GeoName ID: " + r.Id); } Console.WriteLine(); } }
public static void Populate(IConfiguration configuration) { Console.WriteLine("Getting ready to populate alternate names…"); var connectionString = configuration["ConnectionString"]; var dataPath = configuration["DataSourcePath"]; var alternatesPath = Path.Combine(dataPath, @"alternateNamesV2.txt"); var alternateLanguages = configuration["GeoNames:AlternateNamesLanguageCodes"] ?? String.Empty; var targetLanguages = alternateLanguages.Split(','); if (!File.Exists(alternatesPath)) { Console.WriteLine("Downloading alternate names data…"); var downloader = GeoFileDownloader.CreateGeoFileDownloader(); downloader.DownloadFile("alternateNamesV2.zip", dataPath); } var results = GeoFileReader.ReadAlternateNamesV2(alternatesPath).OrderBy(p => p.Id); using (var connection = new SqliteConnection(connectionString)) { connection.Open(); Console.WriteLine("Populating alternate names…"); const string sql = @"INSERT INTO AlternateNames VALUES (@ID, @GeoNameId, @ISOLanguage, @AlternateName, @IsPreferredName, @IsShortName, @IsColloquial, @IsHistoric, @FromDate, @ToDate)"; var command = connection.CreateCommand(); command.CommandText = sql; string[] parameterNames = new[] { "@ID", "@GeoNameId", "@ISOLanguage", "@AlternateName", "@IsPreferredName", "@IsShortName", "@IsColloquial", "@IsHistoric", "@FromDate", "@ToDate" }; DbParameter[] parameters = parameterNames.Select(pn => { DbParameter parameter = command.CreateParameter(); parameter.ParameterName = pn; command.Parameters.Add(parameter); return(parameter); }) .ToArray(); foreach (var r in results) { if (targetLanguages.Length > 0 && !targetLanguages.Contains(r.ISOLanguage)) { continue; } parameters[0].Value = r.Id; parameters[1].Value = r.GeoNameId; parameters[2].Value = r.ISOLanguage.HasValueOrDBNull(); parameters[3].Value = r.Name.HasValueOrDBNull(); parameters[4].Value = r.IsPreferredName; parameters[5].Value = r.IsShortName; parameters[6].Value = r.IsColloquial; parameters[7].Value = r.IsHistoric; parameters[8].Value = r.From.HasValueOrDBNull(); parameters[9].Value = r.To.HasValueOrDBNull(); command.ExecuteNonQuery(); Console.WriteLine("Alternate Name ID: " + r.Id); } Console.WriteLine(); } }
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(); } }