class Inegi::Geo::Transformer
Public Class Methods
call()
click to toggle source
# File lib/inegi/geo/transformer.rb, line 5 def self.call new.call end
new()
click to toggle source
# File lib/inegi/geo/transformer.rb, line 16 def initialize @directory = File.expand_path('.', 'datasets') end
Public Instance Methods
call()
click to toggle source
# File lib/inegi/geo/transformer.rb, line 9 def call unzip_datasets unless already_unzipped? dbf_to_csv unless already_csv? end
Private Instance Methods
already_csv?()
click to toggle source
# File lib/inegi/geo/transformer.rb, line 81 def already_csv? files_with(extension: 'csv').any? end
already_unzipped?()
click to toggle source
# File lib/inegi/geo/transformer.rb, line 35 def already_unzipped? files_with(extension: 'dbf').any? end
dbf_to_csv()
click to toggle source
# File lib/inegi/geo/transformer.rb, line 39 def dbf_to_csv files_with(extension: 'dbf').each do |filename| basename = File.basename(filename) destination = filename.sub('.dbf', '.csv') table = DBF::Table.new(filename) CSV.open(destination, 'w', force_quotes: true) do |csv| csv << translate_header(table.column_names) table.each { |record| csv << record.to_a.map(&latin1_to_utf8) } end puts "CONVERTED: #{basename} -> #{basename.sub('dbf', 'csv')}" end end
files_with(extension:)
click to toggle source
# File lib/inegi/geo/transformer.rb, line 89 def files_with(extension:) Dir[File.join(@directory, "*.#{extension}")] end
latin1_to_utf8()
click to toggle source
# File lib/inegi/geo/transformer.rb, line 85 def latin1_to_utf8 proc { |str| str.force_encoding('ISO-8859-1').encode('UTF-8') } end
translate_header(header)
click to toggle source
# File lib/inegi/geo/transformer.rb, line 54 def translate_header(header) dictionary = { AMBITO: 'area_type', ALTITUD: 'altitude', LATITUD: 'latitude', LONGITUD: 'longitude', CVE_CAB: 'head_code', CVE_CAP: 'capital_code', CVE_CARTA: 'map_code', CVE_ENT: 'state_code', CVE_LOC: 'locality_code', CVE_MUN: 'municipality_code', NOM_ABR: 'state_abbreviation', NOM_CAB: 'head_name', NOM_CAP: 'capital_name', NOM_ENT: 'state_name', NOM_LOC: 'locality_name', NOM_MUN: 'municipality_name', PFEM: 'femenine_population', PMAS: 'masculine_population', PTOT: 'total_population', VTOT: 'inhabited_residences', } header.map { |column| dictionary[column.to_sym] } end
unzip(filename)
click to toggle source
# File lib/inegi/geo/transformer.rb, line 26 def unzip(filename) destination = filename.sub('.zip', '.dbf') puts "UNZIP: #{File.basename(filename)} -> #{File.basename(destination)}" Zip::File.open(filename) do |zip_file| zip_file.each { |entry| entry.extract(destination) } end end
unzip_datasets()
click to toggle source
# File lib/inegi/geo/transformer.rb, line 22 def unzip_datasets files_with(extension: 'zip').each { |filename| unzip(filename) } end