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.
The city and state that the U.S. Postal Service delivers mail to for the ZIP code are
available as usps_locality and usps_state_code. These are derived from postal data
rather than census data and can differ from the primary place and state.
Constant Summary
Constants inherited from BaseRecord
BaseRecord::STATUS_IMPORTED, BaseRecord::STATUS_MANUAL, BaseRecord::STATUS_REMOVED
Instance Attribute Summary collapse
-
#usps_locality ⇒ String?
The city the U.S.
-
#usps_state_code ⇒ String?
The state the U.S.
-
#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
#usps_locality ⇒ String?
The city the U.S. Postal Service associates with the ZIP code. This is the city that mail addressed to the ZIP code should be sent to and is not necessarily the same as the name of the primary place since the Postal Service organizes ZIP codes around mail delivery routes rather than political boundaries. It will be nil if the Postal Service does not deliver mail to the ZIP code.
|
|
# File 'lib/us_geo/zcta.rb', line 95
|
#usps_state_code ⇒ String?
The state the U.S. Postal Service associates with the ZIP code. This can differ from the state of the primary county for ZIP codes served by a post office across a state line. It will be nil if the Postal Service does not deliver mail to the ZIP code.
|
|
# File 'lib/us_geo/zcta.rb', line 103
|
#zipcode ⇒ String
Returns 5-digit ZIP code.
|
|
# File 'lib/us_geo/zcta.rb', line 92
|
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.
30 |
# File 'lib/us_geo/zcta.rb', line 30 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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/us_geo/zcta.rb', line 137 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.usps_locality = row["USPS Locality"] record.usps_state_code = row["USPS State Code"] 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.
111 |
# File 'lib/us_geo/zcta.rb', line 111 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.
115 |
# File 'lib/us_geo/zcta.rb', line 115 delegate :core_based_statistical_area, to: :primary_county, allow_nil: true |
#counties ⇒ ActiveRecord::Relation
Returns Counties that this ZCTA is a part of.
38 |
# File 'lib/us_geo/zcta.rb', line 38 has_many :counties, -> { not_removed }, through: :zcta_counties |
#county_subdivisions ⇒ ActiveRecord::Relation
Returns County subdivisions that this ZCTA is a part of.
62 |
# File 'lib/us_geo/zcta.rb', line 62 has_many :county_subdivisions, -> { not_removed }, through: :zcta_county_subdivisions |
#metropolitan_division ⇒ USGeo::MetropolitanDivision?
Returns Metropolitan division that contains the ZCTA.
119 |
# File 'lib/us_geo/zcta.rb', line 119 delegate :metropolitan_division, to: :primary_county, allow_nil: true |
#places ⇒ ActiveRecord::Relation
Returns Places that this ZCTA is a part of.
74 |
# File 'lib/us_geo/zcta.rb', line 74 has_many :places, -> { not_removed }, through: :zcta_places |
#primary_county ⇒ USGeo::County
Returns County that contains most of the ZCTA's land area.
42 |
# File 'lib/us_geo/zcta.rb', line 42 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.
66 |
# File 'lib/us_geo/zcta.rb', line 66 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.
78 |
# File 'lib/us_geo/zcta.rb', line 78 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.
54 |
# File 'lib/us_geo/zcta.rb', line 54 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.
123 |
# File 'lib/us_geo/zcta.rb', line 123 delegate :state, to: :primary_county, allow_nil: true |
#state_code ⇒ String
Returns State code that contains the ZCTA.
127 |
# File 'lib/us_geo/zcta.rb', line 127 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.
134 |
# File 'lib/us_geo/zcta.rb', line 134 delegate :time_zone, to: :primary_county, allow_nil: true |
#urban_areas ⇒ ActiveRecord::Relation
Returns Urban areas that this ZCTA is a part of.
50 |
# File 'lib/us_geo/zcta.rb', line 50 has_many :urban_areas, through: :zcta_urban_areas |
#zcta_counties ⇒ ActiveRecord::Relation
Returns ZCTA to county mappings.
34 |
# File 'lib/us_geo/zcta.rb', line 34 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.
58 |
# File 'lib/us_geo/zcta.rb', line 58 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.
82 |
# File 'lib/us_geo/zcta.rb', line 82 has_many :zcta_mappings, -> { not_removed }, foreign_key: :zcta_zipcode, inverse_of: :zcta, dependent: :destroy |
#zcta_places ⇒ ActiveRecord::Relation
Returns ZCTA to place mappings.
70 |
# File 'lib/us_geo/zcta.rb', line 70 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.
46 |
# File 'lib/us_geo/zcta.rb', line 46 has_many :zcta_urban_areas, foreign_key: :zipcode, inverse_of: :zcta, dependent: :destroy |