census_shapes 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in census_geographies.gemspec
4
+ gemspec
@@ -0,0 +1,123 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ census_shapes (0.2.0)
5
+ generator_spec
6
+ pg
7
+ postgis_adapter
8
+ progress_bar
9
+ rspec
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ actionmailer (3.2.8)
15
+ actionpack (= 3.2.8)
16
+ mail (~> 2.4.4)
17
+ actionpack (3.2.8)
18
+ activemodel (= 3.2.8)
19
+ activesupport (= 3.2.8)
20
+ builder (~> 3.0.0)
21
+ erubis (~> 2.7.0)
22
+ journey (~> 1.0.4)
23
+ rack (~> 1.4.0)
24
+ rack-cache (~> 1.2)
25
+ rack-test (~> 0.6.1)
26
+ sprockets (~> 2.1.3)
27
+ activemodel (3.2.8)
28
+ activesupport (= 3.2.8)
29
+ builder (~> 3.0.0)
30
+ activerecord (3.2.8)
31
+ activemodel (= 3.2.8)
32
+ activesupport (= 3.2.8)
33
+ arel (~> 3.0.2)
34
+ tzinfo (~> 0.3.29)
35
+ activeresource (3.2.8)
36
+ activemodel (= 3.2.8)
37
+ activesupport (= 3.2.8)
38
+ activesupport (3.2.8)
39
+ i18n (~> 0.6)
40
+ multi_json (~> 1.0)
41
+ arel (3.0.2)
42
+ builder (3.0.4)
43
+ diff-lcs (1.1.3)
44
+ erubis (2.7.0)
45
+ fattr (2.2.1)
46
+ generator_spec (0.8.5)
47
+ rails (>= 3.0, < 4.0)
48
+ rspec-rails
49
+ highline (1.6.15)
50
+ hike (1.2.1)
51
+ i18n (0.6.1)
52
+ journey (1.0.4)
53
+ json (1.7.5)
54
+ mail (2.4.4)
55
+ i18n (>= 0.4.0)
56
+ mime-types (~> 1.16)
57
+ treetop (~> 1.4.8)
58
+ mime-types (1.19)
59
+ multi_json (1.5.0)
60
+ nofxx-georuby (1.9.2)
61
+ options (2.3.0)
62
+ fattr
63
+ pg (0.14.1)
64
+ polyglot (0.3.3)
65
+ postgis_adapter (0.8.1)
66
+ nofxx-georuby
67
+ progress_bar (0.4.0)
68
+ highline (~> 1.6.1)
69
+ options (~> 2.3.0)
70
+ rack (1.4.1)
71
+ rack-cache (1.2)
72
+ rack (>= 0.4)
73
+ rack-ssl (1.3.2)
74
+ rack
75
+ rack-test (0.6.2)
76
+ rack (>= 1.0)
77
+ rails (3.2.8)
78
+ actionmailer (= 3.2.8)
79
+ actionpack (= 3.2.8)
80
+ activerecord (= 3.2.8)
81
+ activeresource (= 3.2.8)
82
+ activesupport (= 3.2.8)
83
+ bundler (~> 1.0)
84
+ railties (= 3.2.8)
85
+ railties (3.2.8)
86
+ actionpack (= 3.2.8)
87
+ activesupport (= 3.2.8)
88
+ rack-ssl (~> 1.3.2)
89
+ rake (>= 0.8.7)
90
+ rdoc (~> 3.4)
91
+ thor (>= 0.14.6, < 2.0)
92
+ rake (10.0.3)
93
+ rdoc (3.12)
94
+ json (~> 1.4)
95
+ rspec (2.11.0)
96
+ rspec-core (~> 2.11.0)
97
+ rspec-expectations (~> 2.11.0)
98
+ rspec-mocks (~> 2.11.0)
99
+ rspec-core (2.11.1)
100
+ rspec-expectations (2.11.3)
101
+ diff-lcs (~> 1.1.3)
102
+ rspec-mocks (2.11.3)
103
+ rspec-rails (2.11.0)
104
+ actionpack (>= 3.0)
105
+ activesupport (>= 3.0)
106
+ railties (>= 3.0)
107
+ rspec (~> 2.11.0)
108
+ sprockets (2.1.3)
109
+ hike (~> 1.2)
110
+ rack (~> 1.0)
111
+ tilt (~> 1.1, != 1.3.0)
112
+ thor (0.16.0)
113
+ tilt (1.3.3)
114
+ treetop (1.4.12)
115
+ polyglot
116
+ polyglot (>= 0.3.1)
117
+ tzinfo (0.3.35)
118
+
119
+ PLATFORMS
120
+ ruby
121
+
122
+ DEPENDENCIES
123
+ census_shapes!
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,123 @@
1
+ census_shapes
2
+ ==================
3
+
4
+ A Ruby Gem that facilitates the importing US Census Shapefiles into a PostGIS database.
5
+
6
+ ### Prerequisites
7
+
8
+ This gem requires Postgres / PostGIS and is intended to be used with a Rails 3 application.
9
+
10
+ ### Installation
11
+
12
+ To install the 'census_shapes' ruby gem:
13
+
14
+ `gem install census_shapes`
15
+
16
+ To use the gem in your Rails 3 Application, include the gem in your Gemfile:
17
+
18
+ `gem "census_shapes"`
19
+
20
+ ### SETUP
21
+
22
+ ## 1) $ rails generate census_shapes:setup
23
+
24
+ To setup your rails application to use census_shapes, run the following generator:
25
+
26
+ `rails g census_shapes:setup MODEL_NAME`
27
+
28
+ This will create all the necessary files, including:
29
+
30
+ config/database_example.yml
31
+ db/###_create_geographies.rb
32
+ lib/tasks/postgis_template.rake
33
+ lib/tasks/census_shapes.rake
34
+
35
+ app/models/geography.rb
36
+ app/controllers/geographies_controller.rb
37
+ app/views/layouts/geographies.html.erb
38
+ app/assets/stylesheets/geographies.css.scss
39
+ app/views/geographies/partials/_map.erb
40
+
41
+ ## 2) Setup your Database
42
+
43
+ The generator will create an example database configuration file at: config/database_example.yml.
44
+
45
+ Edit the database, template, username and password values for your environment. Change the postgis_path for the appropriate environment(s). If you installed PostGIS through homebrew on OS X, the development path should be: /usr/local/share/postgis/
46
+
47
+ Rename the file database.yml.
48
+
49
+ ## 3) Create a PostGIS Template
50
+
51
+ In your application directory, run the following rake task from the command console:
52
+
53
+ `rake postgis_template:create`
54
+
55
+ The generator uses the values set in database.yml, including the postgis_path to find the necessary PostGIS SQL files, and will generate a template database if one does not already exist.
56
+
57
+ ## 3) Migrate Database
58
+
59
+ If Postgres / PostGIS is setup properly and the database.yml and template database are configured, you should now be able to migrate the database:
60
+
61
+ `rake db:migrate`
62
+
63
+ ## 4) Import Census Geographies
64
+
65
+ With the database migrated, we may import the census geographies:
66
+
67
+ `rake census_shapes:import SHAPE=DESIRED_SHAPES`
68
+
69
+ Replace DESIRED_SHAPES with the slugs of the Census Shapes you wish to import. For example, to import States and Counties, you would run the following command:
70
+
71
+ `rake census_shapes:import SHAPE=STATE,COUNTY`
72
+
73
+ The import process will download the zips of the required Census Shapes to 'tmp/shapefiles', unless a path is specified with the PATH argument:
74
+
75
+ `rake census_shapes:import SHAPE=STATE,COUNTY PATH=/usr/local/shapefiles`
76
+
77
+ Lastly, the Zip files will be deleted after import unless the ARCHIVE argument is specified:
78
+
79
+ `rake census_shapes:import SHAPE=STATE,COUNTY ARCHIVE=true`
80
+
81
+ For a full list of the 22 supported Census Geographic Summary Levels, see 'Summary Levels' of the 'Usage' section.
82
+
83
+ Note: Depending upon your internet speed and the amount of Census Shapes you require, this may take several hours or days to complete.
84
+
85
+ ## 5) Validate Geometry (Optional)
86
+
87
+ Depending upon the Census Shapes you install, validation may be required. For example:
88
+
89
+ rake census_shapes:validate_geometry SHAPE=STATE,COUNTY
90
+
91
+ The validate_geometry rake task will test and repair the geometries of all imported shapes.
92
+
93
+ ### USAGE
94
+
95
+ ## Summary Levels
96
+
97
+ Census-Geographies will import the following 22 Geographic Summary Levels:
98
+
99
+ **Sumlevel - Slug: Name**
100
+
101
+ * 040 - STATE: State
102
+ * 050 - COUNTY: County
103
+ * 060 - COUSUB: County Subdivision
104
+ * 067 - SUBMCD: Subminor Civil Subdivision
105
+ * 101 - BLOCK: Block
106
+ * 140 - TRACT: Tract
107
+ * 150 - BG: Blockgroup
108
+ * 160 - PLACE: Place
109
+ * 230 - ANRC: Alaska Native Regional Corporation
110
+ * 280 - AIANNH: American Indian Area/Alaska Native Area/Hawaiian Home Land
111
+ * 281 - AITS: American Indian Tribal Subdivision
112
+ * 320 - CBSA: Metropolitan Statistical Area/Micropolitan Statistical Area
113
+ * 323 - METDIV: Metropolitan Division
114
+ * 340 - CSA: Combined Statistical Area
115
+ * 500 - CD: Congressional District (111th)
116
+ * 610 - SLDU: State Legislative District (Upper Chamber)
117
+ * 620 - SLDL: State Legislative District (Lower Chamber)
118
+ * 700 - VTD: Voting District
119
+ * 871 - ZCTA5: ZIP Code Tabulation Area (5-Digit)
120
+ * 950 - ELSD: School District (Elementary)
121
+ * 960 - SCSD: School District (Secondary)
122
+ * 970 - UNSD: School District (Unified)
123
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require "census_shapes/version"
6
+ require "census_shapes"
7
+
8
+ Gem::Specification.new do |s|
9
+ s.name = "census_shapes"
10
+ s.version = CensusShapes::VERSION
11
+ s.authors = ["Ty Rauber"]
12
+ s.email = ["tyrauber@mac.com"]
13
+ s.homepage = "https://github.com/tyrauber/census_shapes"
14
+ s.summary = "A Ruby Gem for importing US Census Shapes into PostGIS"
15
+ s.description = "Imports all the US Census Geographies into a PostGIS database."
16
+
17
+ s.rubyforge_project = "census_shapes"
18
+
19
+ s.files = `git ls-files`.split($/)
20
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
22
+ s.require_paths = ["lib"]
23
+
24
+ s.add_dependency "rspec"
25
+ s.add_dependency "pg"
26
+ s.add_dependency "postgis_adapter"
27
+ s.add_dependency "progress_bar"
28
+ s.add_dependency "generator_spec"
29
+ end
@@ -0,0 +1,2 @@
1
+ require "rubygems"
2
+ require "census_shapes/version"
@@ -0,0 +1,3 @@
1
+ module CensusShapes
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,104 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module CensusShapes
4
+ module Generators
5
+ class SetupGenerator < ::Rails::Generators::Base
6
+
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ argument :name, :type => :string, :default => "geography"
11
+
12
+ class_option :route, :type => :boolean, :default => true, :description => "Generate Route"
13
+ class_option :model, :type => :boolean, :default => true, :description => "Generate Model"
14
+ class_option :controller, :type => :boolean, :default => true, :description => "Generate Controller"
15
+ class_option :view, :type => :boolean, :default => true, :description => "Generate View"
16
+
17
+ desc "add gem dependencies"
18
+ def add_gems
19
+ gem("pg")
20
+ gem("postgis_adapter")
21
+ end
22
+
23
+ desc "add the geographies migration"
24
+ def self.next_migration_number(path)
25
+ unless @prev_migration_nr
26
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
27
+ else
28
+ @prev_migration_nr += 1
29
+ end
30
+ @prev_migration_nr.to_s
31
+ end
32
+
33
+ def copy_files
34
+ migration_template "db/migrate/create_shapes.rb", "db/migrate/create_#{controller_name}.rb", {:controller_name => controller_name }
35
+ copy_file "config/database_example.yml", "config/database_example.yml"
36
+ copy_file "lib/tasks/postgis_template.rake", "lib/tasks/postgis_template.rake"
37
+ copy_file "lib/tasks/census_shapes.rake", "lib/tasks/census_shapes.rake"
38
+ gsub_file "lib/tasks/census_shapes.rake", "CONTROLLER_NAME", controller_name
39
+ copy_file "lib/yaml/us_shapes.yml", "lib/yaml/us_shapes.yml"
40
+ copy_file "lib/yaml/us_states.yml", "lib/yaml/us_states.yml"
41
+ copy_file "config/initializers/shapes_globals.rb", "config/initializers/#{controller_name}_globals.rb"
42
+ gsub_file "config/initializers/#{controller_name}_globals.rb", "MODEL_NAME", model_name
43
+ end
44
+
45
+ def add_geographies_route
46
+ if options.route
47
+ route("resources :#{model_name}")
48
+ route("match ':type/:z/:x/:y.:format'=> '#{controller_name}#index', :requirements => {:z => /-?\d+(\.\d+)/, :x => /-?\d+(\.\d+)/, :y => /-?\d+(\.\d+)/ }")
49
+ route("root :to => '#{controller_name}#index'")
50
+ end
51
+ end
52
+
53
+ def add_model
54
+ if options.model
55
+ copy_file "models/shape.rb", "app/models/#{model_name}.rb"
56
+ gsub_file "app/models/#{model_name}.rb", "MODEL_NAME", model_name
57
+ end
58
+ end
59
+
60
+ def add_controller
61
+ if options.controller
62
+ copy_file "controllers/shapes_controller.rb", "app/controllers/#{controller_name}_controller.rb"
63
+ gsub_file "app/controllers/#{controller_name.underscore}_controller.rb", "CONTROLLER_NAME", controller_name.camelcase
64
+ gsub_file "app/controllers/#{controller_name.underscore}_controller.rb", "MODEL_NAME", model_name
65
+ end
66
+ end
67
+
68
+ def add_views
69
+ if options.view
70
+ copy_file "views/layouts/shapes.html.erb", "app/views/layouts/#{controller_name.underscore}.html.erb"
71
+ directory "views/shapes", "app/views/#{controller_name.underscore}"
72
+ gsub_file "app/views/#{controller_name.underscore}/index.html.erb", "MODEL_NAME", model_name
73
+ gsub_file "app/views/#{controller_name.underscore}/index.html.erb", "MODEL_DOWNCASE", model_name.downcase
74
+ gsub_file "app/views/#{controller_name.underscore}/index.html.erb", "CONTROLLER_NAME", controller_name
75
+ end
76
+ end
77
+
78
+ def remove_public_index
79
+ `rm public/index.html`
80
+ end
81
+
82
+ private
83
+
84
+ def model_name
85
+ name.classify
86
+ end
87
+
88
+ def controller_name
89
+ name.tableize
90
+ end
91
+ end
92
+
93
+ class String
94
+
95
+ def underscore
96
+ self.gsub(/::/, '/').
97
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
98
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
99
+ tr("-", "_").
100
+ downcase
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,29 @@
1
+ development:
2
+ adapter: postgresql
3
+ host: localhost
4
+ encoding: utf8
5
+ database: census_development
6
+ template: template_postgis
7
+ username: USERNAME
8
+ password: PASSWORD
9
+ postgis_path: /usr/local/share/postgis/
10
+
11
+ production:
12
+ adapter: postgresql
13
+ host: localhost
14
+ encoding: utf8
15
+ database: census_production
16
+ template: template_postgis
17
+ username: USERNAME
18
+ password: PASSWORD
19
+ postgis_path: /usr/local/share/postgis/
20
+
21
+ test:
22
+ adapter: postgresql
23
+ host: localhost
24
+ encoding: utf8
25
+ database: census_test
26
+ template: template_postgis
27
+ username: USERNAME
28
+ password: PASSWORD
29
+ postgis_path: /usr/local/share/postgis/