Class: USGeo::Zcta
- Inherits:
-
BaseRecord
- Object
- ActiveRecord::Base
- BaseRecord
- USGeo::Zcta
- Includes:
- Area, Population
- Defined in:
- lib/us_geo/zcta.rb
Overview
ZIP code tabulation area. These roughly map to U.S. Postal service ZIP codes, but are designed for geographic and demographic purposes instead of mail routing. In particular certain optimizations that the Postal Service makes to optimize mail routing are omitted or smoothed over (i.e. ZIP codes mapping to a single building, one-off enclaves, etc.)
ZCTA’s can span counties, but the one with the majority of the residents is identified as the primary county for when a single county is required.
ZCTA’s can span places, but the one with the majority of the residents is identified as the primary place for when a single area is required.
Constant Summary
Constants inherited from BaseRecord
BaseRecord::STATUS_IMPORTED, BaseRecord::STATUS_MANUAL, BaseRecord::STATUS_REMOVED
Instance Attribute Summary collapse
-
#zipcode ⇒ String
5-digit ZIP code.
Attributes included from Area
Attributes included from Population
Attributes inherited from BaseRecord
Class Method Summary collapse
-
.for_zipcode ⇒ ActiveRecord::Relation
This scope will search for ZCTA’s via the ZCTAMappings table.
- .load!(uri = nil) ⇒ Object
Instance Method Summary collapse
-
#combined_statistical_area ⇒ USGeo::CombinedStatisticalArea?
Combined statistical area that contains the ZCTA.
-
#core_based_statistical_area ⇒ USGeo::CoreBasedStatisticalArea?
Core-based statistical area that contains the ZCTA.
-
#counties ⇒ ActiveRecord::Relation
Counties that this ZCTA is a part of.
-
#county_subdivisions ⇒ ActiveRecord::Relation
County subdivisions that this ZCTA is a part of.
-
#metropolitan_division ⇒ USGeo::MetropolitanDivision?
Metropolitan division that contains the ZCTA.
-
#places ⇒ ActiveRecord::Relation
Places that this ZCTA is a part of.
-
#primary_county ⇒ USGeo::County
County that contains most of the ZCTA’s land area.
-
#primary_county_subdivision ⇒ USGeo::CountySubdivision
County subdivision that contains most of the ZCTA’s land area.
-
#primary_place ⇒ USGeo::Place
Place that contains most of the ZCTA’s land area.
-
#primary_urban_area ⇒ USGeo::UrbanArea
Urban area that contains most of the ZCTA’s land area.
-
#state ⇒ USGeo::State
State that contains the ZCTA.
-
#state_code ⇒ String
State code that contains the ZCTA.
-
#time_zone ⇒ ActiveSupport::TimeZone?
Get the time zone for the primary county containing the ZCTA.
-
#urban_areas ⇒ ActiveRecord::Relation
Urban areas that this ZCTA is a part of.
-
#zcta_counties ⇒ ActiveRecord::Relation
ZCTA to county mappings.
-
#zcta_county_subdivisions ⇒ ActiveRecord::Relation
ZCTA to county subdivision mappings.
-
#zcta_mappings ⇒ ActiveRecord::Relation
2010 ZCTA to current ZCTA mappings.
-
#zcta_places ⇒ ActiveRecord::Relation
ZCTA to place mappings.
-
#zcta_urban_areas ⇒ ActiveRecord::Relation
ZCTA to urban area mappings.
Methods included from Area
#land_area_km, #percent_land, #total_area, #water_area_km
Methods included from Population
#housing_density, #housing_density_km, #population_density, #population_density_km
Methods inherited from BaseRecord
#imported?, #manual?, #removed?
Instance Attribute Details
#zipcode ⇒ String
Returns 5-digit ZIP code.
|
|
# File 'lib/us_geo/zcta.rb', line 87
|
Class Method Details
.for_zipcode ⇒ ActiveRecord::Relation
This scope will search for ZCTA’s via the ZCTAMappings table. This is useful when you have a retired ZIP code and want to find the current ZCTA for that ZIP code.
27 |
# File 'lib/us_geo/zcta.rb', line 27 scope :for_zipcode, ->(zipcode) { left_outer_joins(:zcta_mappings).where(ZctaMapping.table_name => {zipcode: zipcode}).or(left_outer_joins(:zcta_mappings).where(zipcode: zipcode)).distinct } |
.load!(uri = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/us_geo/zcta.rb', line 118 def load!(uri = nil) location = data_uri(uri || "zctas.csv") import! do load_data_file(location) do |row| load_record!(zipcode: row["ZCTA5"]) do |record| record.primary_county_geoid = row["Primary County"] record.primary_urban_area_geoid = row["Primary Urban Area"] record.primary_county_subdivision_geoid = row["Primary County Subdivision"] record.primary_place_geoid = row["Primary Place"] record.population = row["Population"] record.housing_units = row["Housing Units"] record.land_area = row["Land Area"] record.water_area = row["Water Area"] record.lat = row["Latitude"] record.lng = row["Longitude"] end end end end |
Instance Method Details
#combined_statistical_area ⇒ USGeo::CombinedStatisticalArea?
Returns Combined statistical area that contains the ZCTA.
92 |
# File 'lib/us_geo/zcta.rb', line 92 delegate :combined_statistical_area, to: :primary_county, allow_nil: true |
#core_based_statistical_area ⇒ USGeo::CoreBasedStatisticalArea?
Returns Core-based statistical area that contains the ZCTA.
96 |
# File 'lib/us_geo/zcta.rb', line 96 delegate :core_based_statistical_area, to: :primary_county, allow_nil: true |
#counties ⇒ ActiveRecord::Relation
Returns Counties that this ZCTA is a part of.
35 |
# File 'lib/us_geo/zcta.rb', line 35 has_many :counties, -> { not_removed }, through: :zcta_counties |
#county_subdivisions ⇒ ActiveRecord::Relation
Returns County subdivisions that this ZCTA is a part of.
59 |
# File 'lib/us_geo/zcta.rb', line 59 has_many :county_subdivisions, -> { not_removed }, through: :zcta_county_subdivisions |
#metropolitan_division ⇒ USGeo::MetropolitanDivision?
Returns Metropolitan division that contains the ZCTA.
100 |
# File 'lib/us_geo/zcta.rb', line 100 delegate :metropolitan_division, to: :primary_county, allow_nil: true |
#places ⇒ ActiveRecord::Relation
Returns Places that this ZCTA is a part of.
71 |
# File 'lib/us_geo/zcta.rb', line 71 has_many :places, -> { not_removed }, through: :zcta_places |
#primary_county ⇒ USGeo::County
Returns County that contains most of the ZCTA’s land area.
39 |
# File 'lib/us_geo/zcta.rb', line 39 belongs_to :primary_county, foreign_key: :primary_county_geoid, optional: true, class_name: "USGeo::County" |
#primary_county_subdivision ⇒ USGeo::CountySubdivision
Returns County subdivision that contains most of the ZCTA’s land area.
63 |
# File 'lib/us_geo/zcta.rb', line 63 belongs_to :primary_county_subdivision, foreign_key: :primary_county_subdivision_geoid, optional: true, class_name: "USGeo::CountySubdivision" |
#primary_place ⇒ USGeo::Place
Returns Place that contains most of the ZCTA’s land area.
75 |
# File 'lib/us_geo/zcta.rb', line 75 belongs_to :primary_place, foreign_key: :primary_place_geoid, optional: true, class_name: "USGeo::Place" |
#primary_urban_area ⇒ USGeo::UrbanArea
Returns Urban area that contains most of the ZCTA’s land area.
51 |
# File 'lib/us_geo/zcta.rb', line 51 belongs_to :primary_urban_area, foreign_key: :primary_urban_area_geoid, optional: true, class_name: "USGeo::UrbanArea" |
#state ⇒ USGeo::State
Returns State that contains the ZCTA.
104 |
# File 'lib/us_geo/zcta.rb', line 104 delegate :state, to: :primary_county, allow_nil: true |
#state_code ⇒ String
Returns State code that contains the ZCTA.
108 |
# File 'lib/us_geo/zcta.rb', line 108 delegate :state_code, to: :primary_county, allow_nil: true |
#time_zone ⇒ ActiveSupport::TimeZone?
Get the time zone for the primary county containing the ZCTA. Note that this is not necessarily the time zone for the ZCTA itself since a handful of counties span multiple time zones.
115 |
# File 'lib/us_geo/zcta.rb', line 115 delegate :time_zone, to: :primary_county, allow_nil: true |
#urban_areas ⇒ ActiveRecord::Relation
Returns Urban areas that this ZCTA is a part of.
47 |
# File 'lib/us_geo/zcta.rb', line 47 has_many :urban_areas, through: :zcta_urban_areas |
#zcta_counties ⇒ ActiveRecord::Relation
Returns ZCTA to county mappings.
31 |
# File 'lib/us_geo/zcta.rb', line 31 has_many :zcta_counties, -> { not_removed }, foreign_key: :zipcode, inverse_of: :zcta, dependent: :destroy |
#zcta_county_subdivisions ⇒ ActiveRecord::Relation
Returns ZCTA to county subdivision mappings.
55 |
# File 'lib/us_geo/zcta.rb', line 55 has_many :zcta_county_subdivisions, -> { not_removed }, foreign_key: :zipcode, inverse_of: :zcta, dependent: :destroy |
#zcta_mappings ⇒ ActiveRecord::Relation
Returns 2010 ZCTA to current ZCTA mappings.
79 |
# File 'lib/us_geo/zcta.rb', line 79 has_many :zcta_mappings, -> { not_removed }, foreign_key: :zcta_zipcode, inverse_of: :zcta, dependent: :destroy |
#zcta_places ⇒ ActiveRecord::Relation
Returns ZCTA to place mappings.
67 |
# File 'lib/us_geo/zcta.rb', line 67 has_many :zcta_places, -> { not_removed }, foreign_key: :zipcode, inverse_of: :zcta, dependent: :destroy |
#zcta_urban_areas ⇒ ActiveRecord::Relation
Returns ZCTA to urban area mappings.
43 |
# File 'lib/us_geo/zcta.rb', line 43 has_many :zcta_urban_areas, foreign_key: :zipcode, inverse_of: :zcta, dependent: :destroy |