class Tily::TileSystem

Implements a simple tile calculating system according to A dev guide named “Bing Maps Tile System”. Link here: msdn.microsoft.com/en-us/library/bb259689.aspx

Attributes

raw_height[RW]
raw_width[RW]
unit_size[RW]

Public Class Methods

new(unit_size = 256) click to toggle source

Initialize with customizable unit tile size Default value: 256

   # File lib/tily/utils/tile_system.rb
18 def initialize unit_size = 256 # defaults to 256
19         @unit_size = unit_size
20 end

Public Instance Methods

each_level() { |level| ... } click to toggle source

Returns the list of possible levels

   # File lib/tily/utils/tile_system.rb
70 def each_level
71         (1..max_level).each do |level|
72                 yield level if block_given?
73         end
74 end
each_tile(level) { |x, y| ... } click to toggle source

Builds the “x, y” pairs, and returns these pairs in block

   # File lib/tily/utils/tile_system.rb
77 def each_tile level
78         size = tile_size level
79         (0...size).each do |y|
80                 (0...size).each do |x|
81                         yield(x, y) if block_given?
82                 end
83         end
84 end
each_tile_with_index(level) { |x, y, idx| ... } click to toggle source

Builds the “x, y, index” pairs, and returns these pairs in block

   # File lib/tily/utils/tile_system.rb
87 def each_tile_with_index level
88         idx = 0
89         size = tile_size level
90         (0...size).each do |y|
91                 (0...size).each do |x|
92                         yield(x, y, idx) if block_given?
93                         idx += 1
94                 end
95         end
96 end
level_size(level) click to toggle source

Returns the size of the level image

   # File lib/tily/utils/tile_system.rb
48 def level_size level
49         @unit_size * tile_size(level)
50 end
max_level() click to toggle source

Calculates level size of the raw dimensions Basically, it calculates level from width and height respectly, and return the bigger number

   # File lib/tily/utils/tile_system.rb
31 def max_level
32         [to_level(@raw_width), to_level(@raw_height)].max
33 end
meta() click to toggle source

Generates meta info for current TileSystem

    # File lib/tily/utils/tile_system.rb
 99 def meta
100         {
101                 total_level: max_level, 
102                 unit_size:   @unit_size,
103                 raw_width:   @raw_width, 
104                 raw_height:  @raw_height
105         }
106 end
normalized_size() click to toggle source

Returns the normalized number Sometimes, the image is not so well prepared in dimension so, we need to calculate the normalized size

   # File lib/tily/utils/tile_system.rb
38 def normalized_size
39         tile_size(max_level) * @unit_size
40 end
quadkey(level, x, y) click to toggle source

Calculates quadkey for tile (<x>, <y>) on level <level_number> The process is kind of complex, here's what it does:

  1. Converts x, y to binary string

  2. Interleave the bits of y and x coordinates

  3. Interprate the result as a base-4 number (with leading zeros maintained)

   # File lib/tily/utils/tile_system.rb
62 def quadkey level, x, y
63         x_chars = x.bin_str(level).split ""
64         y_chars = y.bin_str(level).split ""
65 
66         y_chars.zip(x_chars).flatten.join("").to_i(2).to_s(4).rjust(level, "0")
67 end
read_raw_dimension(width, height) click to toggle source

Read in raw dimension to be processed

   # File lib/tily/utils/tile_system.rb
23 def read_raw_dimension width, height
24         @raw_width = width
25         @raw_height = height
26 end
tile_offset(index) click to toggle source

Returns the pixel offset in the image

   # File lib/tily/utils/tile_system.rb
53 def tile_offset index
54         @unit_size * index
55 end
tile_size(level) click to toggle source

Returns the number of tile as for rows/columns in a level

   # File lib/tily/utils/tile_system.rb
43 def tile_size level
44         2 ** level
45 end

Private Instance Methods

to_level(raw_size) click to toggle source

Calculate level size from raw size with the following fomula:

level = ceil(log2(raw size / unit_size))
    # File lib/tily/utils/tile_system.rb
111 def to_level raw_size
112         Math.log2(1.0 * raw_size / @unit_size).ceil
113 end