Class: SampleGeogLevel
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SampleGeogLevel
- Defined in:
- app/models/sample_geog_level.rb
Class Method Summary collapse
-
.from_nhgis_terrapop_sample(nhgis_terrapop_sample) ⇒ Object
from_nhgis_terrapop_sample presumes that the terrapop sample is associated to an NHGIS dataset; if it is not, return an empty set the number of sample geog levels returned depends upon the number of country levels associated to the terrapop sample; e.g.
- .geography_name(sample_geog_level) ⇒ Object
- .long_description(sample_geog_levels) ⇒ Object
Instance Method Summary collapse
- #api_attributes ⇒ Object
- #geog_unit ⇒ Object
- #long_description ⇒ Object
- #map ⇒ Object
-
#variables_for_area_data_extracts ⇒ Object
Use the geolinking variable from the microdata to help generate the AreaVariable stubs that are the geography instances on each row of the area data tables.
Class Method Details
.from_nhgis_terrapop_sample(nhgis_terrapop_sample) ⇒ Object
from_nhgis_terrapop_sample presumes that the terrapop sample is associated to an NHGIS dataset; if it is not, return an empty set the number of sample geog levels returned depends upon the number of country levels associated to the terrapop sample; e.g. 3: one for US-NAT (United States), one for US-HFLAD (states), and one for US-HSLAD (counties)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/models/sample_geog_level.rb', line 107 def self.from_nhgis_terrapop_sample(nhgis_terrapop_sample) #return an empty list if the terrapop sample is not linked to an NHGIS dataset return [] if nhgis_terrapop_sample.nhgis_dataset_id.nil? #TODO create a relation in Terrapop Sample? too drastic nhgis_dataset = Nhgis::Dataset.where(id: nhgis_terrapop_sample.nhgis_dataset_id).first return [] if nhgis_dataset.nil? #identify the country for the TerrapopSample country = nhgis_terrapop_sample.country raise "expecting the terrapop sample label to include the name of the country '#{country.full_name}'" unless nhgis_terrapop_sample.label.include? country.full_name # only those NHGIS datasets having nation-, state-, or county-level data are ever to be considered for terrapop samples. There are some NHGIS datasets that # participate in time series tables but do not have any nation-, state-, or county-level data. Since Terrapop only has NAT, *FLAD, *SLAD, those datasets are # to be excluded. # conversion from NHGIS geog levels/geog units to Terrapop geog units nhgis_gl_to_terrapop_gu = {"nation" => "NAT", "state" => "FLAD", "county" => "SLAD"} valid_geog_units = nhgis_dataset.data_groups.reject{ |dg| dg.relative_pathname.nil? }.map{ |dg| dg.relative_pathname.nil? ? nil : nhgis_gl_to_terrapop_gu[dg.geog_level.istads_id] }.reject{ |gl_istads_id| gl_istads_id.nil? } geog_unit_ids = GeogUnit.where(code: valid_geog_units).pluck(:id) #identify all of the country levels for the terrapop sample's country country_levels = CountryLevel.where(country_id: nhgis_terrapop_sample.country_id, geog_unit_id: geog_unit_ids).all country_levels.map do |country_level| sgl = SampleGeogLevel.new sgl.country_level = country_level sgl.terrapop_sample = nhgis_terrapop_sample gu = country_level.geog_unit sgl.internal_code = "#{country.short_name}#{nhgis_dataset.code}_#{gu.code}" harmonized = ["HFLAD", "HSLAD"].include?(gu.code) ? " (Harmonized)" : "" #National country level need not be harmonized; only states and counties are harmonized country_level_label = gu.code == "NAT" ? gu.label : country_level.label #use the geog unit label... which is expected to be 'National' (whereas United States represents extent) sgl.label = nhgis_terrapop_sample.label.gsub(country.full_name, "#{country.full_name}: #{country_level_label}#{harmonized},") sgl #do not set values for id, created_at, updated_at, code, and geolink_variable end end |
.geography_name(sample_geog_level) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/models/sample_geog_level.rb', line 138 def self.geography_name(sample_geog_level) if sample_geog_level.nil? "" else if sample_geog_level.geolink_variable.nil? sample_geog_level.terrapop_sample.short_country_name.upcase + "_" + sample_geog_level.country_level.geog_unit.code.upcase else sample_geog_level.geolink_variable.mnemonic end end end |
.long_description(sample_geog_levels) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/sample_geog_level.rb', line 31 def self.long_description(sample_geog_levels) if sample_geog_levels.count > 0 level_order = ['NAT', 'HFLAD', 'FLAD', 'HSLAD', 'SLAD'] levels = {} str = [] sample_geog_levels.each do |sgl| levels[sgl.country_level.geog_unit.code] = [] unless levels.has_key?(sgl.country_level.geog_unit.code) levels[sgl.country_level.geog_unit.code] << sgl.long_description end levels.each{ |key, lvls| levels[key] = lvls.sort } level_order.each do |lvl| if levels.has_key? lvl str << GeogUnit.where(code: lvl).first.label + " (" + lvl + ")" levels[lvl].each{ |lvl_txt| str << " " + lvl_txt } end end str.join("\n") else "No Geographic Levels" end end |
Instance Method Details
#api_attributes ⇒ Object
16 17 18 |
# File 'app/models/sample_geog_level.rb', line 16 def api_attributes "TODO" end |
#geog_unit ⇒ Object
21 22 23 |
# File 'app/models/sample_geog_level.rb', line 21 def geog_unit country_level.geog_unit end |
#long_description ⇒ Object
54 55 56 |
# File 'app/models/sample_geog_level.rb', line 54 def long_description terrapop_sample.country_name_long_year + ": " + (country_level.geog_unit.code == "NAT" ? "National" : country_level.label) end |
#map ⇒ Object
26 27 28 |
# File 'app/models/sample_geog_level.rb', line 26 def map Map.where(country_level_id: country_level_id, terrapop_sample_id: terrapop_sample_id).first end |
#variables_for_area_data_extracts ⇒ Object
Use the geolinking variable from the microdata to help generate the AreaVariable stubs that are the geography instances on each row of the area data tables. We need four:
-
geog level label (State, county, etc.)
-
geog level code: (STATEBR, STATEUS, etc.)
-
geog instance label (“Alabama”,“Hennepin”, etc.)
-
geog instance code (25,28, whatever codes are used for categories of 3
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/sample_geog_level.rb', line 65 def variables_for_area_data_extracts geog_instance_variable = "GEOG_CODE" geog_instance_variable = geolink_variable.mnemonic || "GEOG_CODE" unless geolink_variable.nil? geog_instance_label = "#{geog_instance_variable}_LABEL" adrvml_code = AreaDataRasterVariableMnemonicLookup.where(composite_mnemonic: geog_instance_variable).first if adrvml_code.nil? adrvml_code = AreaDataRasterVariableMnemonicLookup.new adrvml_code.description = 'GIS match code' unless geolink_variable.nil? adrvml_code.description += ' [' + geolink_variable.label + ']' end adrvml_code.composite_mnemonic = geog_instance_variable adrvml_code.mnemonic = geog_instance_variable adrvml_code.save end adrvml_label = AreaDataRasterVariableMnemonicLookup.where(composite_mnemonic: geog_instance_label).first if adrvml_label.nil? adrvml_label = AreaDataRasterVariableMnemonicLookup.new adrvml_label.description = 'Name of geographic instances' adrvml_label.composite_mnemonic = geog_instance_label adrvml_label.mnemonic = geog_instance_label adrvml_label.save end [ # ExtractVariableStub.new(:mnemonic => "Geog_level_label", :label=>"Geog level label", :len => 32, :data_type => "alphabetical"), # ExtractVariableStub.new(:mnemonic => "Geog_level_code", :label=>"Geog level code", :len => 10, :data_type => "alphabetical"), ExtractVariableStub.new(mnemonic: geog_instance_label, label: "Geog instance label", len: 32, data_type: "alphabetical"), ExtractVariableStub.new(mnemonic: geog_instance_variable, label: "Geog instance code", len: 10, data_type: "integer") ] end |