class MapPrint::Providers::Base

Attributes

north_east[RW]
provider[RW]
south_west[RW]
zoom[RW]

Public Class Methods

new(south_west, north_east, zoom, base_url=nil) click to toggle source
# File lib/map_print/providers/base.rb, line 9
def initialize(south_west, north_east, zoom, base_url=nil)
  @south_west, @north_east, @zoom = south_west, north_east, zoom
  @provider = build_provider(base_url)
end

Public Instance Methods

build_provider(base_url = nil) click to toggle source
# File lib/map_print/providers/base.rb, line 14
def build_provider(base_url = nil)
  raise 'SubClasses must override this method'
end
download() click to toggle source
# File lib/map_print/providers/base.rb, line 18
def download
  provider.download

  to_image
end

Protected Instance Methods

to_image() click to toggle source
# File lib/map_print/providers/base.rb, line 26
def to_image
  file = Tempfile.new(['map', '.png'])

  MiniMagick::Tool::Montage.new do |montage|
    montage.mode('concatenate')
    montage.density 300
    montage.tile("#{provider.x_size}x#{provider.y_size}")
    montage.merge! provider.tiles.collect(&:file_path)
    montage << file.path
  end

  result_file = Tempfile.new(['result', '.png'])

  image = MiniMagick::Image.new(file.path)
  width = image.width - provider.px_offset[:left] - provider.px_offset[:right]
  height = image.height - provider.px_offset[:top] - provider.px_offset[:bottom]

  image.crop("#{width}x#{height}+#{provider.px_offset[:left]}+#{provider.px_offset[:top]}").repage("#{width}x#{height}")
  image.write result_file.path

  file.close
  result_file
end