Class: CartoCSSHelper::RendererHandler

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

Class Method Summary collapse

Class Method Details

.base_kosmtik_command(lat, lon, zlevel, bbox_size, image_size, export_filename, additional_params: '') ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/cartocss_helper/renderer_handler.rb', line 42

def self.base_kosmtik_command(lat, lon, zlevel, bbox_size, image_size, export_filename, additional_params: '')
  bbox = get_bbox_string(lat, lon, bbox_size)
  params = "--format=png --width=#{image_size} --height=#{image_size} --bounds=\"#{bbox}\""
  project_name = CartoCSSHelper::Configuration.get_cartocss_project_name
  yaml = CartoCSSHelper::Configuration.project_file_location
  kosmtik = "#{CartoCSSHelper::Configuration.path_to_kosmtik}index.js"
  return "node #{kosmtik} export #{yaml} --output '#{export_filename}' #{additional_params} #{params}"
end

.cache_exists(export_filename, debug) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/cartocss_helper/renderer_handler.rb', line 12

def self.cache_exists(export_filename, debug)
  if File.exist?(export_filename)
    puts 'wanted file exists' if debug
    return true
  end
  return false
end

.execute_rendering_command(command, export_location, debug) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cartocss_helper/renderer_handler.rb', line 96

def self.execute_rendering_command(command, export_location, debug)
  output = nil
  begin
    output = execute_command(command, debug, ignore_stderr_presence: true)
  rescue FailedCommandException => e
    puts e
    raise RendererFailedToGenerateFile, "generation of file #{export_location} failed due to #{e}"
  end

  return export_location if cache_exists(export_location, false)
  raise RendererFailedToGenerateFile, "generation of file #{export_location} silently failed with command <#{command}> and output <#{output}>"
end

.find_command(lat, lon, zlevel, bbox_size, image_size, export_filename) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/cartocss_helper/renderer_handler.rb', line 51

def self.find_command(lat, lon, zlevel, bbox_size, image_size, export_filename)
  if CartoCSSHelper::Configuration.renderer == :tilemill
    return tilemill_command(lat, lon, zlevel, bbox_size, image_size, export_filename)
  elsif CartoCSSHelper::Configuration.renderer == :kosmtik
    return kosmtik_command(lat, lon, zlevel, bbox_size, image_size, export_filename)
  elsif CartoCSSHelper::Configuration.renderer == :magnacarto
    return magnacarto_command(lat, lon, zlevel, bbox_size, image_size, export_filename)
  end
end

.get_bbox_string(lat, lon, bbox_size) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/cartocss_helper/renderer_handler.rb', line 109

def self.get_bbox_string(lat, lon, bbox_size)
  latitude_bb_size = bbox_size[0]
  longitude_bb_size = bbox_size[1]
  #--bbox=[xmin,ymin,xmax,ymax]
  xmin = lon - longitude_bb_size / 2
  ymin = lat - latitude_bb_size / 2
  xmax = lon + longitude_bb_size / 2
  ymax = lat + latitude_bb_size / 2
  return "#{xmin},#{ymin},#{xmax},#{ymax}"
end

.get_cache_file_location(export_filename) ⇒ Object



70
71
72
73
# File 'lib/cartocss_helper/renderer_handler.rb', line 70

def self.get_cache_file_location(export_filename)
  cache_folder = CartoCSSHelper::Configuration.get_path_to_folder_for_branch_specific_cache
  return "#{cache_folder}#{renderer_marking}#{export_filename}"
end

.image_available_from_cache(lat, lon, zlevel, bbox_size, image_size, export_filename, debug = false) ⇒ Object



75
76
77
# File 'lib/cartocss_helper/renderer_handler.rb', line 75

def self.image_available_from_cache(lat, lon, zlevel, bbox_size, image_size, export_filename, debug = false)
  return File.exist?(get_cache_file_location(export_filename))
end

.kosmtik_command(lat, lon, zlevel, bbox_size, image_size, export_filename) ⇒ Object



28
29
30
31
32
33
# File 'lib/cartocss_helper/renderer_handler.rb', line 28

def self.kosmtik_command(lat, lon, zlevel, bbox_size, image_size, export_filename)
  mapnik_override = CartoCSSHelper::Configuration.mapnik_reference_version_override
  additional_params = ""
  additional_params += " --mapnik-version=#{mapnik_override}" if mapnik_override
  return base_kosmtik_command(lat, lon, zlevel, bbox_size, image_size, export_filename, additional_params: additional_params)
end

.magnacarto_command(lat, lon, zlevel, bbox_size, image_size, export_filename) ⇒ Object



35
36
37
38
39
40
# File 'lib/cartocss_helper/renderer_handler.rb', line 35

def self.magnacarto_command(lat, lon, zlevel, bbox_size, image_size, export_filename)
  mapnik_override = CartoCSSHelper::Configuration.mapnik_reference_version_override
  additional_params = " --renderer=magnacarto"
  additional_params += " --mapnik-version=#{mapnik_override}" if mapnik_override
  return base_kosmtik_command(lat, lon, zlevel, bbox_size, image_size, export_filename, additional_params: additional_params)
end

.renderer_markingObject



61
62
63
64
65
66
67
68
# File 'lib/cartocss_helper/renderer_handler.rb', line 61

def self.renderer_marking
  renderer = "#{CartoCSSHelper::Configuration.renderer}_"
  mapnik_reference = 'mapnik_reference=default_'
  if CartoCSSHelper::Configuration.mapnik_reference_version_override
    mapnik_reference = "mapnik_reference=#{CartoCSSHelper::Configuration.mapnik_reference_version_override}_"
  end
  return "#{renderer}#{mapnik_reference}"
end

.request_image_from_renderer(lat, lon, zlevel, bbox_size, image_size, export_filename, debug = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cartocss_helper/renderer_handler.rb', line 79

def self.request_image_from_renderer(lat, lon, zlevel, bbox_size, image_size, export_filename, debug = false)
  raise "filename, not path <#{export_filename}> was provided" if export_filename.include?("/")
  export_location = get_cache_file_location(export_filename)
  return export_location if cache_exists(export_location, debug)

  start = Time.now

  command_to_execute = find_command(lat, lon, zlevel, bbox_size, image_size, export_location)

  execute_rendering_command(command_to_execute, export_location, debug)

  generation_time = (Time.now - start).to_i

  puts "generated in #{generation_time}s (#{Git.get_commit_hash})"
  return export_location
end

.tilemill_command(lat, lon, zlevel, bbox_size, image_size, export_filename) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/cartocss_helper/renderer_handler.rb', line 20

def self.tilemill_command(lat, lon, zlevel, bbox_size, image_size, export_filename)
  raise 'mapnik reference overrride impossible for TileMill!' if CartoCSSHelper::Configuration.mapnik_reference_version_override
  bbox = get_bbox_string(lat, lon, bbox_size)
  params = "--format=png --width=#{image_size} --height=#{image_size} --static_zoom=#{zlevel} --bbox=\"#{bbox}\""
  project_name = CartoCSSHelper::Configuration.get_cartocss_project_name
  return "node /usr/share/tilemill/index.js export #{project_name} '#{export_filename}' #{params}"
end