Sha256: ddb8bf1832306d90da0083a02f163045a970a4bee637b513a091e762cd29be1a

Contents?: true

Size: 1.54 KB

Versions: 8

Compression:

Stored size: 1.54 KB

Contents

# 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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
aqila-mapas-0.4.12 lib/map/gdal/base.rb
aqila-mapas-0.4.11 lib/map/gdal/base.rb
aqila-mapas-0.4.9 lib/map/gdal/base.rb
aqila-mapas-0.4.8 lib/map/gdal/base.rb
aqila-mapas-0.4.7 lib/map/gdal/base.rb
aqila-mapas-0.4.6 lib/map/gdal/base.rb
aqila-mapas-0.4.5 lib/map/gdal/base.rb
aqila-mapas-0.4.4 lib/map/gdal/base.rb