/free_zipcode_data

Free USA postal codes in CSV format, gleaned from US census data and other sources.

Primary LanguageRubyMIT LicenseMIT

Free Zipcode Data

Zipcode data is free from the various governments around the world. Yet so many organizations, reputable or not, want to charge money for it.

We originally pulled down all the US census data we could find, parsed it and exported it into .csv files.

In 2017 we began using GeoNames data, which is licensed under Creative Commons:

We thank GeoNames for sharing, and urge you to visit their site and support their work.

What's Included

There are three rake tasks which automatically download the latest zipcode data from the country that you specify. Not all countries are accounted for. Please check GeoNames to see a list of supported country zip files.

Each zipcode is correlated with estimated or zip-centroid, latitude and longitude coordinates. Where applicable, county/province, state and community are also correlated.

See the GeoNames readme.txt file for more information.

Usage

$ git clone https://github.com/midwire/free_zipcode_data
$ cd free_zipcode_data
$ bundle install

Determine the countries you want to use at GeoNames, or just use the rake task without any country argument to get zipcode data for all countries...

# download will pull down the zipcodes for the specified country
$ rake data:download[country]
# rake data:download[US] - For US-only zipcodes
# rake data:download[GB_full.csv] - All UK data zipcodes:
# rake data:download - ALL zipcodes
# build the .csv files
$ rake data:build[country]
# rake data:build - ALL countries
# populate_db will automatically download the zips and build the sqlite3 db
$ rake data:populate_db[country]
# rake data:populate_db - ALL countries - WARNING: takes a long time

The rake tasks cascade, from the bottom up. So if you run rake data:populate_db, it will automatically call rake data:build if the .csv files are missing, which will call rake data:download if the .zip files are missing.

SQLite3 Database

The rake task rake data:populate_db[country] will create an SQLite3 database with the following tables, and populate each one:

create table countries (
  id integer not null primary key,
  alpha2 varchar(2) not null,
  alpha3 varchar(3),
  iso varchar(3),
  name varchar(255) not null
)

create table states (
  id integer not null primary key,
  country_id integer not null,
  abbr varchar(2) not null,
  name varchar(255)
)

create table counties (
  id integer not null primary key,
  state_id integer,
  abbr varchar(255),
  name varchar(255),
  county_seat varchar(255)
)

create table zipcodes (
  id integer not null primary key,
  code varchar(10) not null,
  state_id integer,
  county_id integer,
  city varchar(255),
  area_code varchar(3),
  lat float,
  lon float,
  accuracy varchar(8)
)

Both lat and lon, geocodes, are populated for each zipcode record.

Data License

The zipcode data is licensed under a Creative Commons Attribution 3.0 Unported License, carried forward from GeoNames.
Creative Commons License

Errata

10/28/2017:

  • Removed old .csv files
  • Add template files (CODE_OF_CONDUCT.md, CONTRIBUTING.md, ISSUE_TEMPLATE.md, LICENSE.md, PULL_REQUEST_TEMPLATE.md)
  • Use new data provider - GeoNames
  • Test USA
  • Started to test allCountries.zip but it takes too long. Let me know if there are any bugs.
  • Create country_lookup_table.yml for country code lookups

05/04/2011:

  • Removed un-assigned zipcodes, which were not valid for today
  • Added a Rakefile and some rake tasks to facilitate building a SQLite relational database for the three tables (states, counties, zipcodes)
  • Zipcodes without an associated county == 0
  • Counties without a zipcode == 1 (PISCATAGUIS, Maine)

01/24/2011:

  • 670 orphaned zipcodes without an associated county
  • 1 county without any zipcodes (PISCATAGUIS, Maine)

01/13/2011:

At last check there were ...

  • 897 orphaned zipcodes without an associated county
  • 1 county without any zipcodes (PISCATAGUIS, Maine)