Class: Map::Gdal::ColorizeService

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/map/gdal/colorize_service.rb

Constant Summary collapse

REFERENCE_TABLE_COLOR_RED_TO_GREEN =
[
  '215,25,28,255',
  '246,144,83,255',
  '255,223,154,255',
  '222,242,180,255',
  '145,203,169,255',
  '43,131,186,255'
].freeze

Instance Attribute Summary

Attributes included from Base

#files_to_clean

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#add_to_clean, #clean, #gdal_running?, #get_file_name_with_path, #get_layer_name, #get_path_to_temp_file, #options_to_command_line, #run_command, #store_kml, #tmp_file

Constructor Details

#initialize(file) ⇒ ColorizeService

Returns a new instance of ColorizeService.



15
16
17
# File 'lib/map/gdal/colorize_service.rb', line 15

def initialize(file)
  @file = file
end

Class Method Details

.format_color_table(color_table) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/map/gdal/colorize_service.rb', line 56

def self.format_color_table(color_table)
  lines = IO.read(color_table)
    .split("\n")
    .reject{ |line| line.start_with?('#') }

  formated = lines.map do |color_line|
    splited = color_line.split(',').map(&:strip)
    {
      min: splited.first.to_f,
      max: nil,
      color_rgba: "#{splited.second},#{splited.third},#{splited.fourth},#{splited.fifth}"
    }
  end

  formated.each_with_index.map do |item, index|
    next_element = formated[index.next]
    item[:max] = next_element[:min] if next_element
    item
  end
end

Instance Method Details

#call(options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/map/gdal/colorize_service.rb', line 19

def call(options = {})
  color_table = options[:color_table]
  color_table ||= File.join(Gem.loaded_specs['aqila-mapas'].full_gem_path, 'lib', 'map', 'gdal', 'table_colors.txt')
  out = get_path_to_temp_file('color', 'tif')

  color_table = generate_color_table(options[:inverse]) if color_table == :auto

  run_command("gdaldem color-relief -alpha #{@file} #{color_table} #{out}")

  add_to_clean(out)
  out
end

#generate_color_table(inverse = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/map/gdal/colorize_service.rb', line 32

def generate_color_table(inverse = false)
  service_contrast = Map::Gdal::ConstrastStretchService.new(@file)
  result_contrast = service_contrast.call
  min = result_contrast[:min]
  max = result_contrast[:max]
  service_contrast.clean

  intervalo = (max - min) / (REFERENCE_TABLE_COLOR_RED_TO_GREEN.length - 1)
  temp_file = get_path_to_temp_file('table-color', 'txt')
  add_to_clean(temp_file)

  File.open(temp_file, 'a') do |file|
    # TransparĂȘncia (alpha) para zero
    file.puts '0,255,255,255,0,0'

    Array.new(REFERENCE_TABLE_COLOR_RED_TO_GREEN.length) do |index|
      value = min + (intervalo * index)
      file.puts line_color_table(value, inverse ? (REFERENCE_TABLE_COLOR_RED_TO_GREEN.length - 1 - index) : index)
    end
  end

  temp_file
end