# frozen_string_literal: true module Map::Gdal::Base attr_reader :files_to_clean # verifica se gdal está rodando # checa 3 vezes fazendo um pequeno sleep entre as vezes para garantia def gdal_running?(times = nil) `ps aux | grep gdal | grep -v grep` if $?.success? true elsif times.to_i < 3 sleep 0.1 gdal_running?(times.to_i + 1) end end def get_layer_name Map::Gdal::OgriInfoService.new(@file).layer_name end def store_kml @file = get_path_to_temp_file('kml', 'kml') add_to_clean(@file) IO.write(@file, @kml) end def clean @files_to_clean.to_a.compact.each do |path| FileUtils.rm_rf(path) end end def add_to_clean(path) @files_to_clean ||= [] @files_to_clean << path end def get_path_to_temp_file(prefix, extension, file_id="#{Time.current.to_i}-#{(rand * 1_000).to_i}") tmp_file("#{prefix}-#{file_id}.#{extension}") end def options_to_command_line(options, reject = []) reject = [reject] unless reject.is_a?(Array) options.map do |key, value| "-#{key} #{value}" unless reject.to_a.include?(key) end.compact.join(' ') end def run_command(command) out = `#{command} 2>&1` raise "Falha ao rodar comando [$ #{command}]\n[#{out}]" unless $?.success? out end def tmp_file(file_name) FileUtils.mkdir_p(File.join(Dir.pwd, 'tmp')) File.join(Dir.pwd, 'tmp', file_name) end def get_file_name_with_path(file) file_name = File.basename(file, '.*' ) File.join(File.dirname(file), "#{file_name}") end end