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 Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #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
10 11 12 13 14 15 16 |
# File 'lib/geonames_local/data/dump.rb', line 10 def initialize(target, kind) @kind = kind @data = [] target.each { |n| work(n) } if target.respond_to? :each nations if target == :all end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
3 4 5 |
# File 'lib/geonames_local/data/dump.rb', line 3 def data @data end |
Instance Method Details
#download(file) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/geonames_local/data/dump.rb', line 37 def download(file) Dir.mkdir(TMP) unless File.exist?(TMP) Dir.mkdir(TMP + @kind.to_s) unless File.exist?(TMP + @kind.to_s) fname = TMP + "#{@kind}/#{file}" return if File.exist?(fname) `curl #{URL}/#{@kind}/#{file} -o #{fname}` end |
#get_file(nation) ⇒ Object
33 34 35 |
# File 'lib/geonames_local/data/dump.rb', line 33 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/geonames_local/data/dump.rb', line 61 def parse(file) 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 end end total = Time.now - start info "#{@data.size} #{@kind} spots parsed #{total}s (#{(@data.size / total).to_i}/s)" end rescue Errno::ENOENT => e info "Failed to download #{file}, skipping. #{e}" end |
#parse_line(l) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/geonames_local/data/dump.rb', line 50 def parse_line(l) return if l =~ /^#|^iso/i if @kind == :dump return l if l =~ /^\D/ if Opt[:level] != 'all' return unless l =~ /ADM\d/ # ADM2 => cities end end Spot.new(l, @kind) end |
#uncompress(file) ⇒ Object
45 46 47 48 |
# File 'lib/geonames_local/data/dump.rb', line 45 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 |