Class: CartoCSSHelper::DataFileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/cartocss_helper/data_file_handling.rb

Constant Summary collapse

@@loaded_filename =
nil

Class Method Summary collapse

Class Method Details

.get_command_to_load_using_osmpgsql(data_filename) ⇒ Object



14
15
16
# File 'lib/cartocss_helper/data_file_handling.rb', line 14

def self.get_command_to_load_using_osmpgsql(data_filename)
  return "osm2pgsql --create --slim --drop --cache 10 --number-processes 1 --hstore --unlogged --style #{Configuration.get_style_file_location} --multi-geometry '#{data_filename}'"
end

.get_filename_of_recently_loaded_fileObject



9
10
11
12
# File 'lib/cartocss_helper/data_file_handling.rb', line 9

def self.get_filename_of_recently_loaded_file
  return nil if @@loaded_filename == Configuration.get_data_filename
  return @@loaded_filename
end

.is_already_loaded(data_filename) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/cartocss_helper/data_file_handling.rb', line 18

def self.is_already_loaded(data_filename)
  if get_filename_of_recently_loaded_file == data_filename
    puts "\tavoided reloading the same file! <#{data_filename}>"
    return true
  end
  return false
end

.load_data_into_database(data_filename, debug = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cartocss_helper/data_file_handling.rb', line 26

def self.load_data_into_database(data_filename, debug = false)
  return if is_already_loaded(data_filename)
  start_time = Time.now
  puts "\tloading data into database <#{data_filename}>"
  @@loaded_filename = nil
  begin
    execute_command(get_command_to_load_using_osmpgsql(data_filename), debug, ignore_stderr_presence: true) # osm2pgsql outputs everything to stderr
  rescue FailedCommandException => e
    puts 'loading data into database failed'
    raise e if debug
    puts 'retry with enabled debug'
    load_data_into_database(data_filename, true)
  end
  @@loaded_filename = data_filename
  puts "\tloading lasted #{(Time.now - start_time).to_i}s"
end