14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/to_town/converter/us.rb', line 14
def self.run(dry = 0)
iconv = Iconv.new('UTF-8//IGNORE', 'UTF-8')
if ToTown::DataMigration.where(:filename => FILE_NAME).empty?
if dry == 0
File.open(FILE_LOCATION, "r") do |file|
file.gets
while line = file.gets
line = iconv.iconv(line) unless line.valid_encoding?
data = line.chomp.split(/\t/)
name_town_type = data[3].split(/ ((?=[[:lower:]])|CDP)/)
ToTown::Point.create(:state => data[0], :geoid => data[1], :ansicode => data[2], :name => name_town_type[0], :town_type => (name_town_type-[name_town_type[0]]).join, :aland => data[4], :awater => data[5], :latlng => [data[8].to_f, data[9].to_f] )
end end end ToTown::DataMigration.create(:filename => FILE_NAME)
end end
|