Class: Travertine

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

Constant Summary collapse

DEFAULT_TILE_SIZE =

This is the correct size for Google maps tiles

256

Class Method Summary collapse

Class Method Details

.cut_tile(img, x, y, tile_size = DEFAULT_TILE_SIZE) ⇒ Object

Cuts a tile from the source image.

Parameters:

img: A Magick::Image object for the source image. x: The x coordinate of the tile to cut. Coordinates are specified in terms of the tile grid, not pixels. y: The y coordinate of the tile to cut. Coordinates are specified in terms of the tile grid, not pixels. tile_size: The size of each tile in pixels.

Returns:

A Magick::Image tile.



43
44
45
46
47
48
49
50
# File 'lib/travertine.rb', line 43

def self.cut_tile(img, x, y, tile_size = DEFAULT_TILE_SIZE)
  tile = Magick::Image.new(tile_size, tile_size)

  img_x = x * tile_size
  img_y = y * tile_size

  tile.import_pixels(0, 0, tile_size, tile_size, "RGBA",img.export_pixels(img_x, img_y, tile_size, tile_size, "RGBA"))
end

.cut_tiles(img, max_zoom, tile_size = DEFAULT_TILE_SIZE) ⇒ Object

Cuts tiles from a source image at various zoom levels.

Parameters:

img: A Magick::Image object for the source image. For proper tiling it should be exactly square. max_zoom: The maximum level of zoom. max_zoom of 0 has 1 tile. Each successive level has 4 times as many tiles as the previous. tile_size: The size of each tile in pixels.

Returns:

An Array of Arrays, where each inner array contains the tiles for a given zoom level. The first Array will have 1 tile, for zoom level 0. The second will have 4 for zoom level 1, and so on.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/travertine.rb', line 16

def self.cut_tiles(img, max_zoom, tile_size = DEFAULT_TILE_SIZE)
  tile_sets = []
  (0..max_zoom).collect.reverse.each do |zoom|
    tiles = []
    resize_to_zoom_level(img, zoom, tile_size)
    tile_coordinates_for_zoom(zoom).each do |x,y|
      tiles << cut_tile(img, x, y, tile_size)
    end

    tile_sets << tiles
  end

  tile_sets.reverse
end

.resize_to_zoom_level(img, zoom_level, tile_size = DEFAULT_TILE_SIZE) ⇒ Object

Resizes the image to the correct size for cutting tiles at the given zoom level. It does an in-place resize which modifies the passed-in image.

Note: If the image is not square, tiles will probably be incorrect.

Parameters:

img: The source image as Magick::Image. zoom_level: The level of zoom to compute the tile grid for. tile_size: The size of each tile in pixels.

Returns:

nil - The resize is done in-place, modifying the passed in image.



97
98
99
100
# File 'lib/travertine.rb', line 97

def self.resize_to_zoom_level(img, zoom_level, tile_size = DEFAULT_TILE_SIZE)
  size = zoom_image_size(zoom_level, tile_size)
  img.resize!(size, size)
end

.tile_coordinates_for_zoom(zoom_level) ⇒ Object

Calculates the tile grid for a given zoom level.

Parameters:

zoom_level: The level of zoom to compute the tile grid for.

Returns:

An Array of 2-element Arrays representing the tile x,y coordinates. For example, the tile grid at zoom 1 is [[0,0,], [0,1], [1,0], [1,1]]



61
62
63
64
65
66
67
68
69
# File 'lib/travertine.rb', line 61

def self.tile_coordinates_for_zoom(zoom_level)
  [].tap do |coords|
    (0..((2 ** zoom_level) - 1)).each do |x|
      (0..((2 ** zoom_level) - 1)).each do |y|
        coords << [x,y]
      end
    end
  end
end

.tile_count_for_zoom(zoom_level) ⇒ Object

Calculates the total tile count for a given zoom level.

Parameters:

zoom_level: The level of zoom to compute the tile grid for.

Returns:

An Integer count of the number of tiles for a given zoom level.



80
81
82
# File 'lib/travertine.rb', line 80

def self.tile_count_for_zoom(zoom_level)
  4 ** zoom_level
end

.zoom_image_size(zoom_level, tile_size = DEFAULT_TILE_SIZE) ⇒ Object

Calculates the correct size in pixels for a source image at the given zoom level. This assumes the image to resize will be square.

For example, at zoom_level 2 and tile_size 256, the image size is 2048px on a side.

Parameters:

zoom_level: The level of zoom to compute the image size for. tile_size: The size of each tile in pixels.

Returns:

The size in pixels of each side of the square image for a source image at the given zoom.



115
116
117
# File 'lib/travertine.rb', line 115

def self.zoom_image_size(zoom_level, tile_size = DEFAULT_TILE_SIZE)
  tile_size * (2 ** zoom_level )
end