Class: MapPrint::ScalebarHandler

Inherits:
Object
  • Object
show all
Includes:
PngHandlers::Texts, Validations::Size
Defined in:
lib/map_print/scalebar_handler.rb

Constant Summary collapse

ZOOM_METERS_PER_PIXEL =
{
  0 => 156543.03,
  1 => 78271.52,
  2 => 39135.76,
  3 => 19567.88,
  4 => 9783.94,
  5 => 4891.97,
  6 => 2445.98,
  7 => 1222.99,
  8 => 611.50,
  9 => 305.75,
  10 => 152.87,
  11 => 76.437,
  12 => 38.219,
  13 => 19.109,
  14 => 9.5546,
  15 => 4.7773,
  16 => 2.3887,
  17 => 1.1943,
  18 => 0.5972
}

Instance Method Summary collapse

Methods included from Validations::Size

#validate_size!

Methods included from PngHandlers::Texts

#draw_text, #print_texts, #sanitize_options

Constructor Details

#initialize(scalebar, zoom) ⇒ ScalebarHandler

Returns a new instance of ScalebarHandler.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/map_print/scalebar_handler.rb', line 30

def initialize(scalebar, zoom)
  @scalebar = scalebar
  @zoom = zoom
  if @scalebar[:padding]
    @padding_left = @scalebar[:padding][:left] || 0
    @padding_right = @scalebar[:padding][:right] || 0
    @padding_top = @scalebar[:padding][:top] || 0
    @padding_bottom = @scalebar[:padding][:bottom] || 0
  else
    @padding_left = 0
    @padding_right = 0
    @padding_top = 0
    @padding_bottom = 0
  end
  validate_data!
end

Instance Method Details

#processObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/map_print/scalebar_handler.rb', line 47

def process
  size = @scalebar[:size]
  tempfile = Tempfile.new ['scalebar', '.png']
  `convert -alpha on -channel a -evaluate set #{(@scalebar[:background_opacity] || 1) *100}% -density 300 -size #{size[:width]}x#{size[:height]} xc:#{@scalebar[:background_color] || 'transparent'} #{tempfile.path}`
  image = MiniMagick::Image.new tempfile.path

  pixels_for_distance = get_distance_in_units
  quarter = (size[:width] - @padding_left - @padding_right) / 4

  y_position = size[:height] - (@scalebar[:bar_height] || 10) - @padding_bottom
  orig_y = size[:height] - @padding_bottom
  image.combine_options do |c|
    c.density 300
    c.stroke 'black'
    (0..3).each do |i|
      c.fill (i % 2 == 0 ? 'white' : 'black')
      c.draw "rectangle #{@padding_left + i*quarter},#{orig_y} #{@padding_left + (i+1)*quarter},#{y_position}"
    end
  end

  text_options = { pointsize: 4, gravity: 'NorthWest' }.merge(@scalebar[:text_style] || {})
  draw_text(image, "0", "#{@padding_left},#{@padding_top}", text_options)
  draw_text(image, (pixels_for_distance/4).round(-2).to_s, "#{-quarter + @padding_left - @padding_right},#{@padding_top}", text_options.merge(gravity: 'North'))
  draw_text(image, (pixels_for_distance/2).round(-2).to_s, "#{@padding_left - @padding_right},#{@padding_top}", text_options.merge(gravity: 'North'))
  draw_text(image, (pixels_for_distance*0.75).round(-2).to_s + distance_units_to_s, "#{@padding_right},#{@padding_top}", text_options.merge(gravity: 'NorthEast'))

  image
end