Sha256: 5cac91b076d360c3cc2d5ff130054396638daa1b421564540d1ec487b2ad0158

Contents?: true

Size: 1.61 KB

Versions: 7

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module USGeo
  # Mapping of ZCTA's to places they overlap with.
  class ZctaPlace < BaseRecord
    include Area

    belongs_to :zcta, foreign_key: :zipcode, inverse_of: :zcta_places
    belongs_to :place, foreign_key: :place_geoid, inverse_of: :zcta_places

    validates :zipcode, length: {is: 5}
    validates :place_geoid, length: {is: 7}
    validates :land_area, numericality: true, presence: true
    validates :water_area, numericality: true, presence: true

    class << self
      def load!(uri = nil)
        location = data_uri(uri || "zcta_places.csv")

        import! do
          load_data_file(location) do |row|
            load_record!(zipcode: row["ZCTA5"], place_geoid: row["Place GEOID"]) do |record|
              record.land_area = row["Land Area"]
              record.water_area = row["Water Area"]
            end
          end
        end
      end
    end

    # Percentage of the ZCTA population.
    def percent_zcta_population
      population.to_f / zcta.population.to_f
    end

    # Percentage of the ZCTA land area.
    def percent_zcta_land_area
      land_area / zcta.land_area
    end

    # Percentage of the ZCTA total area.
    def percent_zcta_total_area
      total_area / zcta.total_area
    end

    # Percentage of the place population.
    def percent_place_population
      population.to_f / place.population.to_f
    end

    # Percentage of the place land area.
    def percent_place_land_area
      land_area / place.land_area
    end

    # Percentage of the place total area..
    def percent_place_total_area
      total_area / place.total_area
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
us_geo-2.1.1 lib/us_geo/zcta_place.rb
us_geo-2.1.0 lib/us_geo/zcta_place.rb
us_geo-2.0.4 lib/us_geo/zcta_place.rb
us_geo-2.0.3 lib/us_geo/zcta_place.rb
us_geo-2.0.2 lib/us_geo/zcta_place.rb
us_geo-2.0.1 lib/us_geo/zcta_place.rb
us_geo-2.0.0 lib/us_geo/zcta_place.rb