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.

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

#zipcodeString

Returns 5-digit ZIP code.

Returns:

  • (String)

    5-digit ZIP code.



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

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.

Parameters:

  • zipcode (String)

    ZIP code to search for.

Returns:

  • (ActiveRecord::Relation)

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

Returns Combined statistical area that contains the ZCTA.

Returns:



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

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:



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

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.



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

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.



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

has_many :county_subdivisions, -> { not_removed }, through: :zcta_county_subdivisions

#metropolitan_divisionUSGeo::MetropolitanDivision?

Returns Metropolitan division that contains the ZCTA.

Returns:



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

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.



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

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.



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

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

Returns:



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_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.



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

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

Returns:

  • (USGeo::UrbanArea)

    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"

#stateUSGeo::State

Returns State that contains the ZCTA.

Returns:



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

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.



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

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.



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

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.



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

has_many :urban_areas, through: :zcta_urban_areas

#zcta_countiesActiveRecord::Relation

Returns ZCTA to county mappings.

Returns:

  • (ActiveRecord::Relation)

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

Returns ZCTA to county subdivision mappings.

Returns:

  • (ActiveRecord::Relation)

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

Returns 2010 ZCTA to current ZCTA mappings.

Returns:

  • (ActiveRecord::Relation)

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

Returns ZCTA to place mappings.

Returns:

  • (ActiveRecord::Relation)

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

Returns ZCTA to urban area mappings.

Returns:

  • (ActiveRecord::Relation)

    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