Class: Smappy::Tile

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

Constant Summary collapse

SIZE =
256
DEFAULT_OPTIONS =
{
  x:            0,
  y:            0,
  zoomlevel:    0,
  url_template: 'http://tile.openstreetmap.org/%{zoomlevel}/%{x}/%{y}.png'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tile

Returns a new instance of Tile.



17
18
19
20
21
22
# File 'lib/smappy/tile.rb', line 17

def initialize(options = {})
  options = DEFAULT_OPTIONS.merge(options)
  options.each do |key, value|
    send("#{key}=", value) if respond_to?("#{key}=")
  end
end

Instance Attribute Details

#url_templateObject

Returns the value of attribute url_template.



15
16
17
# File 'lib/smappy/tile.rb', line 15

def url_template
  @url_template
end

#xObject

Returns the value of attribute x.



15
16
17
# File 'lib/smappy/tile.rb', line 15

def x
  @x
end

#yObject

Returns the value of attribute y.



15
16
17
# File 'lib/smappy/tile.rb', line 15

def y
  @y
end

#zoomlevelObject

Returns the value of attribute zoomlevel.



15
16
17
# File 'lib/smappy/tile.rb', line 15

def zoomlevel
  @zoomlevel
end

Instance Method Details

#position_on_map(map) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/smappy/tile.rb', line 24

def position_on_map(map)
  center_tile_x = (map.width  / 2) - (Tile::SIZE / 2) - map.tile_offset[0]
  center_tile_y = (map.height / 2) - (Tile::SIZE / 2) - map.tile_offset[1]
  
  offset_x = (map.center_tile.x - x) * Tile::SIZE
  offset_y = (map.center_tile.y - y) * Tile::SIZE
  
  [center_tile_x - offset_x,
   center_tile_y - offset_y]
end

#to_imageObject



35
36
37
38
39
# File 'lib/smappy/tile.rb', line 35

def to_image
  @tile_image ||= ChunkyPNG::Image.from_datastream(
    ChunkyPNG::Datastream.from_io(open(to_url))
  )
end

#to_urlObject



49
50
51
# File 'lib/smappy/tile.rb', line 49

def to_url
  url_template % { zoomlevel: zoomlevel, x: x, y: y }
end