Class: MapPrint::PngHandler

Inherits:
Object
  • Object
show all
Includes:
MapPrint::PngHandlers::Images, MapPrint::PngHandlers::Texts
Defined in:
lib/map_print/png_handler.rb

Instance Method Summary collapse

Methods included from MapPrint::PngHandlers::Texts

#draw_text, #print_texts, #sanitize_options

Methods included from MapPrint::PngHandlers::Images

#print_images

Constructor Details

#initialize(context) ⇒ PngHandler

Returns a new instance of PngHandler.



9
10
11
# File 'lib/map_print/png_handler.rb', line 9

def initialize(context)
  @context = context
end

Instance Method Details

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/map_print/png_handler.rb', line 13

def print
  raise ParameterError.new('Missing png_options width attribute') unless @context.png_options && @context.png_options[:width]
  raise ParameterError.new('Missing png_options height attribute') unless @context.png_options[:height]
  `convert -density 300 -size #{@context.png_options[:width]}x#{@context.png_options[:height]} xc:#{@context.png_options[:background_color] || 'transparent'} #{@context.output_path}`
  @png = MiniMagick::Image.new @context.output_path

  print_map

  print_images(@context.images, @png)
  print_texts(@context.texts, @png)

  scalebar_image = @context.print_scalebar
  if scalebar_image
    overlay_image(MiniMagick::Image.new(scalebar_image.path), @context.scalebar[:position])
  end

  legend_image = @context.print_legend
  if legend_image
    overlay_image(MiniMagick::Image.new(legend_image.path), @context.legend[:position])
  end
end


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/map_print/png_handler.rb', line 35

def print_map
  map_image = @context.print_layers

  size = @context.map[:size]
  geometry = ''
  if size && (size[:width] || size[:height])
    geometry += size[:width].to_s if size[:width]
    geometry += 'x'
    geometry += size[:height].to_s if size[:height]

    image = MiniMagick::Image.new(map_image.path)
    image.combine_options do |c|
      c.density 300
      c.resize geometry
      c.unsharp '1.5x1+0.7+0.02'
    end
    image.write map_image.path
  end

  map_image = @context.print_geojson(MiniMagick::Image.new(map_image.path))

  overlay_image(map_image, @context.map[:position])
end