Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should definitely checkout GeoNames. They have the entire world in a standardized database. You can <a href="http://download.geonames.org/export/dump/" rel="noreferrer">download it</a> or use their <a href="http://www.geonames.org/export/web-services.html" rel="noreferrer">API</a>.</p> <p>I download the US database and use a connector I created in C# to insert States, Cities, Towns, and Zip Codes in my database.</p> <pre><code> public static class GeoNamesConnector { #region GeoName Constants private static readonly string GeoNamesPath = HttpContext.Current.Server.MapPath("~/App_Data/GeoNames/US.txt"); const int GeoNameIdColumn = 0; const int NameColumn = 1; const int LatitudeColumn = 4; const int LongitudeColumn = 5; const int FeatureCodeColumn = 7; const int CountryCodeColumn = 8; const int Admin1CodeColumn = 10; const int Admin2CodeColumn = 11; #endregion #region AlternateName Constants private static readonly string AlternateNamesPath = HttpContext.Current.Server.MapPath("~/App_Data/GeoNames/alternateNames.txt"); const int AlternateNameIdColumn = 0; const int AltNameGeoNameIdColumn = 1; const int IsoLanguageColumn = 2; const int AlternateNameColumn = 3; #endregion public static void AddAllEntities(GeoNamesEntities entities) { //Remember to turn off Intellitrace Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var geoNamesSortedList = AddGeoNames(entities); Trace.WriteLine(String.Format("Added GeoNames: {0}", stopwatch.Elapsed)); stopwatch.Restart(); SetupGeoNameChildRelationships(geoNamesSortedList, entities); Trace.WriteLine(String.Format("Setup GeoName parent/child relationships: {0}", stopwatch.Elapsed)); stopwatch.Restart(); AddPostalCodeAlternateNames(geoNamesSortedList, entities); Trace.WriteLine(String.Format("Added postal codes and relationships with parent GeoNames: {0}", stopwatch.Elapsed)); } private static SortedList&lt;int, GeoName&gt; AddGeoNames(GeoNamesEntities entities) { var lineReader = File.ReadLines(GeoNamesPath); var geoNames = from line in lineReader.AsParallel() let fields = line.Split(new char[] { '\t' }) let fieldCount = fields.Length where fieldCount &gt;= 9 let featureCode = fields[FeatureCodeColumn] where featureCode == "ADM1" || featureCode == "ADM2" || featureCode == "PPL" let name = fields[NameColumn] let id = string.IsNullOrEmpty(fields[GeoNameIdColumn]) ? 0 : int.Parse(fields[GeoNameIdColumn]) orderby id select new GeoName { Id = Guid.NewGuid(), GeoNameId = id, Name = fields[NameColumn], Latitude = string.IsNullOrEmpty(fields[LatitudeColumn]) ? 0 : Convert.ToDecimal(fields[LatitudeColumn]), Longitude = string.IsNullOrEmpty(fields[LongitudeColumn]) ? 0 : Convert.ToDecimal(fields[LongitudeColumn]), FeatureCode = featureCode, CountryCode = fields[CountryCodeColumn], Admin1Code = fieldCount &lt; 11 ? "" : fields[Admin1CodeColumn], Admin2Code = fieldCount &lt; 12 ? "" : fields[Admin2CodeColumn] }; var sortedList = new SortedList&lt;int, GeoName&gt;(); int i = 1; foreach (var geoname in geoNames) { sortedList.Add(geoname.GeoNameId, geoname); entities.GeographicAreas.AddObject(geoname); if (i++ % 20000 == 0) entities.SaveChanges(); } entities.SaveChanges(); return sortedList; } private static void SetupGeoNameChildRelationships(SortedList&lt;int, GeoName&gt; geoNamesSortedList, GeoNamesEntities entities) { foreach (var geoName in geoNamesSortedList.Where(g =&gt; g.Value.FeatureCode == "ADM2" || g.Value.FeatureCode == "ADM1")) { //Setup parent child relationship IEnumerable&lt;KeyValuePair&lt;int, GeoName&gt;&gt; children = null; switch (geoName.Value.FeatureCode) { case "ADM1": children = geoNamesSortedList.Where( g =&gt; g.Value.FeatureCode == "ADM2" &amp;&amp; g.Value.Admin1Code == geoName.Value.Admin1Code); break; case "ADM2": children = geoNamesSortedList.Where( g =&gt; g.Value.FeatureCode == "PPL" &amp;&amp; g.Value.Admin1Code == geoName.Value.Admin1Code &amp;&amp; g.Value.Admin2Code == geoName.Value.Admin2Code); break; } if (children != null) { foreach (var child in children) geoName.Value.Children.Add(child.Value); } entities.SaveChanges(); } } private static void AddPostalCodeAlternateNames(SortedList&lt;int, GeoName&gt; geoNamesSortedList, GeoNamesEntities entities) { var lineReader = File.ReadLines(AlternateNamesPath); var alternativeNames = from line in lineReader.AsParallel() let fields = line.Split(new char[] { '\t' }) let fieldCount = fields.Length where fieldCount &gt;= 4 &amp;&amp; fields[IsoLanguageColumn] == "post" let geoNameId = int.Parse(fields[AltNameGeoNameIdColumn]) orderby geoNameId select new AlternateName { Id = Guid.NewGuid(), AlternateNameId = int.Parse(fields[AlternateNameIdColumn]), ParentGeoNameId = geoNameId, Name = fields[AlternateNameColumn], IsoLanguage = fields[IsoLanguageColumn] }; //Iterate through to convert from lazy (AsParallel) so it is ready for use foreach (var alternateName in alternativeNames) { int key = alternateName.ParentGeoNameId; if (geoNamesSortedList.ContainsKey(key)) { entities.GeographicAreas.AddObject(alternateName); alternateName.Parent = geoNamesSortedList[key]; } } entities.SaveChanges(); } } </code></pre> <p>There is also Open Street Maps that you can <a href="http://wiki.openstreetmap.org/wiki/Planet.osm" rel="noreferrer">download</a> or use their <a href="http://wiki.openstreetmap.org/wiki/API" rel="noreferrer">API</a>.</p> <p>I do not suggest Yahoo's new API they are cutting products left and right and you never know how long it will be around. Also you cannot download a whole dump currently.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload