Class: Smappy::Tile
- Inherits:
-
Object
- Object
- Smappy::Tile
- 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
-
#url_template ⇒ Object
Returns the value of attribute url_template.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#zoomlevel ⇒ Object
Returns the value of attribute zoomlevel.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Tile
constructor
A new instance of Tile.
- #position_on_map(map) ⇒ Object
- #to_image ⇒ Object
- #to_url ⇒ Object
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( = {}) = DEFAULT_OPTIONS.merge() .each do |key, value| send("#{key}=", value) if respond_to?("#{key}=") end end |
Instance Attribute Details
#url_template ⇒ Object
Returns the value of attribute url_template.
15 16 17 |
# File 'lib/smappy/tile.rb', line 15 def url_template @url_template end |
#x ⇒ Object
Returns the value of attribute x.
15 16 17 |
# File 'lib/smappy/tile.rb', line 15 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
15 16 17 |
# File 'lib/smappy/tile.rb', line 15 def y @y end |
#zoomlevel ⇒ Object
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_image ⇒ Object
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_url ⇒ Object
49 50 51 |
# File 'lib/smappy/tile.rb', line 49 def to_url url_template % { zoomlevel: zoomlevel, x: x, y: y } end |