Class: TileUp::Tiler

Inherits:
Object
  • Object
show all
Defined in:
lib/tileup/tiler.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  processor: 'rmagick', # or 'mini_magick'
  auto_zoom_level: nil,
  tile_width: 256,
  tile_height: 256,
  filename_prefix: 'map_tile',
  output_dir: '.',
  logger: 'console',
  extend_incomplete_tiles: true,
  extend_color: 'none',
  verbose: false
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_filename, options = {}) ⇒ Tiler

Returns a new instance of Tiler.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tileup/tiler.rb', line 24

def initialize image_filename, options = {}
  self.options = options = OpenStruct.new(DEFAULT_OPTIONS.merge(options))
  self.logger = TileUp::Logger.build(options.logger, :info, verbose: options.verbose)
  self.image_processor = TileUp::ImageProcessor.build(options.processor, logger)
  self.image = image_processor.open(image_filename)

  self.extension = if File.exist?(image_filename)
    File.extname(image_filename)
  else
    File.extname(URI(image_filename).path)
  end

  %w(tile_width tile_height).each{|dimension| options[dimension] = options[dimension].to_i }

  if options.auto_zoom_level
    if options.auto_zoom_level == 'auto'
      options.auto_zoom_level = recommended_auto_zoom_level
    else
      options.auto_zoom_level = options.auto_zoom_level.to_i
    end

    if options.auto_zoom_level > 20
      logger.warn 'Warning: auto zoom levels hard limited to 20.'
      options.auto_zoom_level = 20
    elsif options.auto_zoom_level <= 0
      options.auto_zoom_level = nil
    end
  end
end

Instance Attribute Details

#extensionObject

Returns the value of attribute extension.



22
23
24
# File 'lib/tileup/tiler.rb', line 22

def extension
  @extension
end

#imageObject

Returns the value of attribute image.



22
23
24
# File 'lib/tileup/tiler.rb', line 22

def image
  @image
end

#image_processorObject

Returns the value of attribute image_processor.



22
23
24
# File 'lib/tileup/tiler.rb', line 22

def image_processor
  @image_processor
end

#loggerObject

Returns the value of attribute logger.



22
23
24
# File 'lib/tileup/tiler.rb', line 22

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



22
23
24
# File 'lib/tileup/tiler.rb', line 22

def options
  @options
end

Instance Method Details

#build_base_imagesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tileup/tiler.rb', line 68

def build_base_images
  logger.info 'Building base images'

  base_images = base_image_processing_tasks.map do |task|
    {
      image: image_processor.scale(image, task[:scale]),
      image_path: task[:output_dir]
    }
  end

  if base_images.all?{|base_image| base_image[:image] }
    logger.info "Building base images finished."
    base_images
  else
    logger.info 'Building base images failed.'
    false
  end
end

#make_tiles!(base_images = build_base_images) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tileup/tiler.rb', line 54

def make_tiles! base_images = build_base_images
  if base_images
    result = base_images.map do |base_image|
      FileUtils.mkdir_p(base_image[:image_path])
      { base_image[:image_path] => make_tiles_for_base_image!(base_image[:image], File.join(base_image[:image_path], options.filename_prefix)) }
    end
    logger.verbose result
    logger.info 'Done'
    result
  else
    false
  end
end


87
88
89
90
91
92
93
94
95
96
# File 'lib/tileup/tiler.rb', line 87

def recommended_auto_zoom_level
  zoom_level = (1..20).detect do |zoom_level|
    scale = (1.to_f / 2 ** (zoom_level - 1))

    image_processor.width(image) * scale < options.tile_width ||
    image_processor.height(image) * scale < options.tile_height
  end

  [1, zoom_level.to_i - 1].max
end