Safety in Numbers
Brighter Planet's blog
The wonderful convenience of Earth
When I was whipping up Yaktrak I needed to get a (richly annotated) list of zip codes into the app. This is a familiar problem for web app developers: what’s the easiest way to get auxiliary data from canonical sources loaded and ready to use?
Lucky for me we have earth
. Look at how easy this is:
$ rails new zippy
In the Gemfile:
gem 'earth'
gem 'fastercsv' # only if you're on Ruby 1.8
And in config/environment.rb
or an initializer:
Earth.init :locality
Back at the shell:
$ bundle install
$ rails console
1.9.3-p0 :001 > ZipCode.run_data_miner!
(7.2ms) DROP TABLE "zip_codes"
Receiving schema
Schema: 0% | | ETA: --:--:--
Schema: 100% |==========================================| Time: 00:00:01
Receiving indexes
Receiving data
1 tables, 200 records
zip_codes: 100% |==========================================| Time: 00:00:09
Resetting sequences
[...it will also add some related tables...]
1.9.3-p0 :002 > ZipCode.find 53703
ZipCode Load (22.4ms) SELECT "zip_codes".* FROM "zip_codes" WHERE "zip_codes"."name" = ? LIMIT 1 [["name", 53703]]
=> #<ZipCode name: "53703", state_postal_abbreviation: "WI", description: "Madison", latitude: "43.078646", longitude: "-89.37727", egrid_subregion_abbreviation: "MROE", climate_division_name: "WI8">
Voila! Fresh from the transparent data crawlers at data.brighterplanet.com.
What blog is this?
Safety in Numbers is Brighter Planet's blog about climate science, Ruby, Rails, data, transparency, and, well, us.
Who's behind this?
We're Brighter Planet, the world's leading computational sustainability platform.