Class: TileUp::Tiler
- Inherits:
-
Object
- Object
- TileUp::Tiler
- 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
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#image ⇒ Object
Returns the value of attribute image.
-
#image_processor ⇒ Object
Returns the value of attribute image_processor.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #build_base_images ⇒ Object
-
#initialize(image_filename, options = {}) ⇒ Tiler
constructor
A new instance of Tiler.
- #make_tiles!(base_images = build_base_images) ⇒ Object
- #recommended_auto_zoom_level ⇒ Object
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, = {} self. = = OpenStruct.new(DEFAULT_OPTIONS.merge()) self.logger = TileUp::Logger.build(.logger, :info, verbose: .verbose) self.image_processor = TileUp::ImageProcessor.build(.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| [dimension] = [dimension].to_i } if .auto_zoom_level if .auto_zoom_level == 'auto' .auto_zoom_level = recommended_auto_zoom_level else .auto_zoom_level = .auto_zoom_level.to_i end if .auto_zoom_level > 20 logger.warn 'Warning: auto zoom levels hard limited to 20.' .auto_zoom_level = 20 elsif .auto_zoom_level <= 0 .auto_zoom_level = nil end end end |
Instance Attribute Details
#extension ⇒ Object
Returns the value of attribute extension.
22 23 24 |
# File 'lib/tileup/tiler.rb', line 22 def extension @extension end |
#image ⇒ Object
Returns the value of attribute image.
22 23 24 |
# File 'lib/tileup/tiler.rb', line 22 def image @image end |
#image_processor ⇒ Object
Returns the value of attribute image_processor.
22 23 24 |
# File 'lib/tileup/tiler.rb', line 22 def image_processor @image_processor end |
#logger ⇒ Object
Returns the value of attribute logger.
22 23 24 |
# File 'lib/tileup/tiler.rb', line 22 def logger @logger end |
#options ⇒ Object
Returns the value of attribute options.
22 23 24 |
# File 'lib/tileup/tiler.rb', line 22 def @options end |
Instance Method Details
#build_base_images ⇒ Object
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], .filename_prefix)) } end logger.verbose result logger.info 'Done' result else false end end |
#recommended_auto_zoom_level ⇒ Object
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 < .tile_width || image_processor.height(image) * scale < .tile_height end [1, zoom_level.to_i - 1].max end |