midwire / free_zipcode_data Goto Github PK
View Code? Open in Web Editor NEWFree US and world-wide postal codes in SQLite and CSV format
License: MIT License
Free US and world-wide postal codes in SQLite and CSV format
License: MIT License
Also, I havent found a way to disable sql files generation and leave only csv generation
Originally posted by @srghma in #13 (comment)
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.
85602 only has a single county: AZ, COCHISE.
85602,Benson,AZ,COCHISE,,32.035885,-110.262611
80108 / 80109 zipcodes exist in the all_us_zipcodes.csv file and can be updated via https://github.com/midwire/my_zipcode_gem scripts.
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.
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!
~/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#
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:
Under the etl
or lib/etl
directory:
GeoNames
dataNOTE: I’m open to other suggestions.
All countries states are not mixed up
Some countries states are mixed up (For instance 82 for state_abbr)
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
A possible workaround
Instead of
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
We have to time this carefully and coordinate with my_zipcode_gem
because it currently depends on these 3 files:
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.
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.
Internationalize the command line interface so that non-English users can also enjoy free zipcode data.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.