Class: Geonames::Dump
- Inherits:
-
Object
- Object
- Geonames::Dump
- Defined in:
- lib/geonames_local/data/dump.rb
Constant Summary collapse
- URL =
Geonames base URL
"http://download.geonames.org/export/"
- TMP =
Work temporary files
"/tmp/geonames/"
Instance Method Summary collapse
- #data ⇒ Object
- #download(file) ⇒ Object
- #get_file(nation) ⇒ Object
-
#initialize(target, kind) ⇒ Dump
constructor
A new instance of Dump.
- #nations ⇒ Object
- #parse(file) ⇒ Object
- #parse_line(l) ⇒ Object
- #uncompress(file) ⇒ Object
- #work(nation) ⇒ Object
Constructor Details
#initialize(target, kind) ⇒ Dump
Returns a new instance of Dump.
8 9 10 11 12 13 14 15 16 |
# File 'lib/geonames_local/data/dump.rb', line 8 def initialize(target, kind) @kind = kind @data = [] if target.respond_to? :each target.each { |n| work(n) } elsif target == :all nations end end |
Instance Method Details
#data ⇒ Object
33 34 35 |
# File 'lib/geonames_local/data/dump.rb', line 33 def data @data end |
#download(file) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/geonames_local/data/dump.rb', line 41 def download(file) Dir.mkdir(TMP) unless File.exists?(TMP) Dir.mkdir(TMP + @kind.to_s) unless File.exists?(TMP + @kind.to_s) fname = TMP + "#{@kind}/#{file}" return if File.exists?(fname) `curl #{URL}/#{@kind}/#{file} -o #{fname}` end |
#get_file(nation) ⇒ Object
37 38 39 |
# File 'lib/geonames_local/data/dump.rb', line 37 def get_file(nation) nation == "nation" ? "countryInfo.txt" : "#{nation.upcase}.zip" end |
#nations ⇒ Object
18 19 20 21 22 23 |
# File 'lib/geonames_local/data/dump.rb', line 18 def nations info "\nDumping nation database" file = get_file('nation') download file parse file end |
#parse(file) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/geonames_local/data/dump.rb', line 68 def parse(file) red = 0 start = Time.now File.open("/tmp/geonames/#{@kind}/#{file.gsub("zip", "txt")}") do |f| while line = f.gets if record = parse_line(line) @data << record red += 1 end end total = Time.now - start info "#{red} #{@kind} spots parsed #{total}s (#{(red / total).to_i}/s)" end rescue Errno::ENOENT => e info "Failed to download #{file}, skipping. #{e}" end |
#parse_line(l) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/geonames_local/data/dump.rb', line 54 def parse_line(l) return if l =~ /^#|^iso/i if @kind == :dump if l =~ /^\D/ return l else if Opt[:level] != "all" return unless l =~ /ADM\d/ # ADM2 => cities end end end Spot.new(l, @kind) end |
#uncompress(file) ⇒ Object
49 50 51 52 |
# File 'lib/geonames_local/data/dump.rb', line 49 def uncompress(file) info "Uncompressing #{file}" `unzip -quo /tmp/geonames/#{@kind}/#{file} -d /tmp/geonames/#{@kind}` end |
#work(nation) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/geonames_local/data/dump.rb', line 25 def work nation info "\nWorking on #{@kind} for #{nation}" file = get_file(nation) download file uncompress file parse file end |