Class: USGeo::Zcta

Inherits:
BaseRecord
  • Object
show all
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

Attributes included from Area

#land_area, #water_area

Attributes included from Population

#housing_units, #population

Attributes inherited from BaseRecord

#status, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

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_localityString?

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.

Returns:

  • (String, nil)

    USPS city name for the ZIP code.



# File 'lib/us_geo/zcta.rb', line 95

#usps_state_codeString?

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.

Returns:

  • (String, nil)

    2-letter USPS state code for the ZIP code.



# File 'lib/us_geo/zcta.rb', line 103

#zipcodeString

Returns 5-digit ZIP code.

Returns:

  • (String)

    5-digit ZIP code.



# File 'lib/us_geo/zcta.rb', line 92

Class Method Details

.for_zipcodeActiveRecord::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.

Returns:

  • (ActiveRecord::Relation)

    ZCTA's matching the given 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_areaUSGeo::CombinedStatisticalArea?

Returns Combined statistical area that contains the ZCTA.

Returns:



111
# File 'lib/us_geo/zcta.rb', line 111

delegate :combined_statistical_area, to: :primary_county, allow_nil: true

#core_based_statistical_areaUSGeo::CoreBasedStatisticalArea?

Returns Core-based statistical area that contains the ZCTA.

Returns:



115
# File 'lib/us_geo/zcta.rb', line 115

delegate :core_based_statistical_area, to: :primary_county, allow_nil: true

#countiesActiveRecord::Relation

Returns Counties that this ZCTA is a part of.

Returns:

  • (ActiveRecord::Relation)

    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_subdivisionsActiveRecord::Relation

Returns County subdivisions that this ZCTA is a part of.

Returns:

  • (ActiveRecord::Relation)

    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_divisionUSGeo::MetropolitanDivision?

Returns Metropolitan division that contains the ZCTA.

Returns:



119
# File 'lib/us_geo/zcta.rb', line 119

delegate :metropolitan_division, to: :primary_county, allow_nil: true

#placesActiveRecord::Relation

Returns Places that this ZCTA is a part of.

Returns:

  • (ActiveRecord::Relation)

    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_countyUSGeo::County

Returns County that contains most of the ZCTA's land area.

Returns:

  • (USGeo::County)

    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_subdivisionUSGeo::CountySubdivision

Returns County subdivision that contains most of the ZCTA's land area.

Returns:



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_placeUSGeo::Place

Returns Place that contains most of the ZCTA's land area.

Returns:

  • (USGeo::Place)

    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_areaUSGeo::UrbanArea

Returns Urban area that contains most of the ZCTA's land area.

Returns:



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"

#stateUSGeo::State

Returns State that contains the ZCTA.

Returns:



123
# File 'lib/us_geo/zcta.rb', line 123

delegate :state, to: :primary_county, allow_nil: true

#state_codeString

Returns State code that contains the ZCTA.

Returns:

  • (String)

    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_zoneActiveSupport::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.

Returns:

  • (ActiveSupport::TimeZone, nil)

    Time zone for the ZCTA.



134
# File 'lib/us_geo/zcta.rb', line 134

delegate :time_zone, to: :primary_county, allow_nil: true

#urban_areasActiveRecord::Relation

Returns Urban areas that this ZCTA is a part of.

Returns:

  • (ActiveRecord::Relation)

    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_countiesActiveRecord::Relation

Returns ZCTA to county mappings.

Returns:

  • (ActiveRecord::Relation)

    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_subdivisionsActiveRecord::Relation

Returns ZCTA to county subdivision mappings.

Returns:

  • (ActiveRecord::Relation)

    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_mappingsActiveRecord::Relation

Returns 2010 ZCTA to current ZCTA mappings.

Returns:

  • (ActiveRecord::Relation)

    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_placesActiveRecord::Relation

Returns ZCTA to place mappings.

Returns:

  • (ActiveRecord::Relation)

    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_areasActiveRecord::Relation

Returns ZCTA to urban area mappings.

Returns:

  • (ActiveRecord::Relation)

    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