Giter Site home page Giter Site logo

midwire / free_zipcode_data Goto Github PK

View Code? Open in Web Editor NEW
82.0 5.0 23.0 4.81 MB

Free US and world-wide postal codes in SQLite and CSV format

License: MIT License

Ruby 100.00%
zipcode census-data counties associated-county ruby sqlite3 location-picker location-data location-database location-service

free_zipcode_data's People

Contributors

cblackburn-ajla avatar dependabot[bot] avatar midwire avatar srghma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

free_zipcode_data's Issues

Incorrect Zip Codes to County data mapping

Expected Behavior

85602 zip code to have two counties.

85602,Benson,AZ,COCHISE,,32.035885,-110.262611
85602,Benson,AZ,PIMA,,...,...

There's no surjection between the set of zip codes and set of counties.

A somewhat helpful explanation of this fact in reality by census bureau:
explanation_zcta_county_rel_10.pdf

The problem is generic, not just this zip code is affected. As I said, there's no surjection between the set of zip codes and set of counties in general.

Actual Behavior

85602 only has a single county: AZ, COCHISE.

85602,Benson,AZ,COCHISE,,32.035885,-110.262611

Steps to reproduce the problem

  1. Look into the data.
  2. Look at here: https://www2.census.gov/geo/pdfs/maps-data/data/rel/explanation_zcta_county_rel_10.pdf

  • Current Version: git @ 6599105
  • Operating System: not relevant

Question on Updated Data for US

Expected Behavior

80108 / 80109 zipcodes exist in the all_us_zipcodes.csv file and can be updated via https://github.com/midwire/my_zipcode_gem scripts.

Actual Behavior

80108 / 80109 and other zipcodes (likely) do exist and can populate / keep data up-to-date for production systems.

There is -> 80104,Castle Rock,CO,DOUGLAS,,39.374779,-104.849988 in the data, which is great, but there are more zip codes that represent Castle Rock, CO.

Steps to reproduce the problem

Go to https://raw.githubusercontent.com/midwire/free_zipcode_data/master/all_us_zipcodes.csv
and search for 80108 and/or 80109 and notice that Castle Rock, CO is not in that CSV file.

If you download the data from Geonames, those are in the US.txt file in the US.zip.

I'm more than happy to work on this gem and the my_zipcode_gem to help keep things up-to-date but I find it confusing that the all_us_zipcodes.csv is generated from the my_zipcode_gem and is now 3 months out of date. What's the process for running that properly with new data and updating?

Thanks for your work on this gem, it has gotten us very far on our production application!


  • Current Version: Cloned direct from master
  • Operating System: Mac OS X 10.11.6

No such file or directory @ rb_sysopen - country_lookup_table.yml

Actual Behavior

 ~/projects  docker-ruby bash
root@78ad4e8e1709:/usr/src/app# gem install free_zipcode_data
Fetching: colored-1.2.gem (100%)
Successfully installed colored-1.2
Fetching: kiba-2.0.0.gem (100%)
Successfully installed kiba-2.0.0
Fetching: ruby-progressbar-1.9.0.gem (100%)
Successfully installed ruby-progressbar-1.9.0
Fetching: rubyzip-1.2.1.gem (100%)
Successfully installed rubyzip-1.2.1
Fetching: sqlite3-1.3.13.gem (100%)
Building native extensions. This could take a while...
Successfully installed sqlite3-1.3.13
Fetching: trollop-2.1.2.gem (100%)
Successfully installed trollop-2.1.2
Fetching: free_zipcode_data-1.0.1.gem (100%)
Successfully installed free_zipcode_data-1.0.1
7 gems installed
root@78ad4e8e1709:/usr/src/app# free_zipcode_data --work-dir /tmp/geodata --generate-files --verbose
Starting FreeZipcodeData v1.0.1...
Downloading: allCountries.zip from GeoNames...
Extracting: allCountries.zip...
Preparing: /tmp/geodata/allCountries.txt.csv for processing...
Processing 1264588 zipcodes in '/tmp/geodata/allCountries.txt.csv'...
Initializing country table: 'countries'...
Initializing state table: 'states'...                                                                                   |  ETA: ??:??:??
Initializing county table: 'counties'...                                                                                |  ETA: ??:??:??
Initializing zipcode table: 'zipcodes'...                                                                               |  ETA: ??:??:??
Processing '/tmp/geodata/allCountries.txt.csv' data, please be patient...                                               |  ETA: ??:??:??
Traceback (most recent call last):                                                                                      |  ETA: ??:??:??
        22: from /usr/local/bundle/bin/free_zipcode_data:23:in `<main>'
        21: from /usr/local/bundle/bin/free_zipcode_data:23:in `load'
        20: from /usr/local/bundle/gems/free_zipcode_data-1.0.1/bin/free_zipcode_data:9:in `<top (required)>'
        19: from /usr/local/bundle/gems/free_zipcode_data-1.0.1/lib/free_zipcode_data/runner.rb:42:in `start'
        18: from /usr/local/bundle/gems/free_zipcode_data-1.0.1/lib/free_zipcode_data/runner.rb:101:in `extract_transform_load'
        17: from /usr/local/bundle/gems/kiba-2.0.0/lib/kiba.rb:17:in `run'
        16: from /usr/local/bundle/gems/kiba-2.0.0/lib/kiba/runner.rb:15:in `run'
        15: from /usr/local/bundle/gems/kiba-2.0.0/lib/kiba/runner.rb:41:in `process_rows'
        14: from /usr/local/bundle/gems/kiba-2.0.0/lib/kiba/runner.rb:41:in `each'
        13: from /usr/local/bundle/gems/kiba-2.0.0/lib/kiba/runner.rb:42:in `block in process_rows'
        12: from /usr/local/bundle/gems/free_zipcode_data-1.0.1/lib/etl/csv_source.rb:16:in `each'
        11: from /usr/local/lib/ruby/2.5.0/csv.rb:1289:in `open'
        10: from /usr/local/bundle/gems/free_zipcode_data-1.0.1/lib/etl/csv_source.rb:21:in `block in each'
         9: from /usr/local/lib/ruby/2.5.0/csv.rb:1764:in `each'
         8: from /usr/local/bundle/gems/free_zipcode_data-1.0.1/lib/etl/csv_source.rb:22:in `block (2 levels) in each'
         7: from /usr/local/bundle/gems/kiba-2.0.0/lib/kiba/runner.rb:48:in `block (2 levels) in process_rows'
         6: from /usr/local/bundle/gems/kiba-2.0.0/lib/kiba/runner.rb:48:in `each'
         5: from /usr/local/bundle/gems/kiba-2.0.0/lib/kiba/runner.rb:49:in `block (3 levels) in process_rows'
         4: from /usr/local/bundle/gems/free_zipcode_data-1.0.1/lib/free_zipcode_data/country_table.rb:27:in `write'
         3: from /usr/local/bundle/gems/free_zipcode_data-1.0.1/lib/free_zipcode_data/db_table.rb:27:in `country_lookup_table'
         2: from /usr/local/lib/ruby/2.5.0/psych.rb:497:in `load_file'
         1: from /usr/local/lib/ruby/2.5.0/psych.rb:497:in `open'
/usr/local/lib/ruby/2.5.0/psych.rb:497:in `initialize': No such file or directory @ rb_sysopen - country_lookup_table.yml (Errno::ENOENT)
root@78ad4e8e1709:/usr/src/app#

Steps to reproduce the problem

docker-ruby bash

Use Kiba ETL to provide a cleaner solution for building sqlite tables and csv files.

I’ve been using Kiba ETL for a client project and it is a great way to separate ETL concerns.

NOTES: I still envision using a rake task to download GeoNames data, but then to use the following ETL hierarchy to move that data into both SQLite3 tables and *.csv files for each of the following:

  • Country
  • State
  • County
  • Zipcode

Potential ETL Hierarchy

Under the etl or lib/etl directory:

  • csv_source.rb - to read the downloaded GeoNames data
  • csv_destination.rb - to write discrete CSV files
  • country_table_destination.rb - SQLite table destination
  • state_table_destination.rb - SQLite table destination
  • county_table_destination.rb - SQLite table destination
  • zipcode_table_destination.rb - SQLite table destination
  • free_zipcode_data.etl - the ETL source file

NOTE: I’m open to other suggestions.

state abbreviations are not globally unique

Expected Behavior

All countries states are not mixed up

Actual Behavior

Some countries states are mixed up (For instance 82 for state_abbr)

Steps to reproduce the problem

Run without specifying a country

cut -d'       ' -f4,5 allCountries.txt | grep 82 | cut -d'    ' -f1 | sort | uniq

insert

-d'       '

with CTRL+V TAB

These adm1 share 82 as abbr: (As example)
Çankiri
Khulna Division
Královéhradecký kraj
Phang Nga
Pomerania
Region Midtjylland


  • Current Version: 1.0.2
  • Operating System: FreeBSD

A possible workaround
Instead of

WHERE abbr = '#{state_abbr}' OR name = '#{escape_single_quotes(state_name)}'"

    def get_state_id(state_abbr, state_name)
      sql = "SELECT id FROM states
        WHERE abbr = '#{state_abbr}' and  name = '#{escape_single_quotes(state_name)}'"
      res = select_first(sql)
      if(res == nil) 
         sql = "SELECT id FROM states WHERE name = '#{escape_single_quotes(state_name)}'"
         res=select_first(sql)
      end
      return res
    end

The fallback to test only for name is required because of mixed up state_abbr in Denmark's data (One State (Region) has two abbr's for the same state name i.e the state has one name but falsely in the dataset two abbr's, (More of data a date quality issue) and perhaps other countries as well.
EDIT: Perhaps using country_id in the state_id lookup would be better...
BTW South Africa does not have a adm1 (state) so I used country for state like this

 def write(row)
      if(row[:short_state] == nil || row[:short_state] == '')
        row[:short_state] = row[:country]
        row[:state] = country_lookup_table[row[:country]][:name]
      end
      return nil unless row[:short_state]
      row[:state] = 'Marshall Islands' if row[:short_state] == 'MH' && row[:state].nil?
      country_id = get_country_id(row[:country])
      sql = <<-SQL
        INSERT INTO states (abbr, name, country_id)
        VALUES ('#{row[:short_state]}',
          '#{escape_single_quotes(row[:state])}',
          #{country_id}
        )
      SQL
      begin
        database.execute(sql)
      rescue SQLite3::ConstraintException
        # Swallow duplicates
      end

Deprecate the legacy *.csv files.

We have to time this carefully and coordinate with my_zipcode_gem because it currently depends on these 3 files:

  • all_us_counties.csv
  • all_us_states.csv
  • all_us_zipcodes.csv

Once we get a stable solution in place here using the GeoNames data, we can update the my_zipcode_gem rake tasks to use that solution and remove these files.

New feature request: Dockerfile

It would be considerably easier to run this very useful tool if there was a Dockerfile in the repository which could be used to create and populate the sqlite database.

Internationalization

Internationalize the command line interface so that non-English users can also enjoy free zipcode data.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.