Class: Smappy::StaticMap
- Inherits:
-
Object
- Object
- Smappy::StaticMap
- Defined in:
- lib/smappy/static_map.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ width: 500, height: 350, zoomlevel: 0, center: [0.0, 0.0] }
Instance Attribute Summary collapse
-
#center ⇒ Object
Returns the value of attribute center.
-
#height ⇒ Object
Returns the value of attribute height.
-
#tile_url_template ⇒ Object
Returns the value of attribute tile_url_template.
-
#width ⇒ Object
Returns the value of attribute width.
-
#zoomlevel ⇒ Object
Returns the value of attribute zoomlevel.
Instance Method Summary collapse
- #center_tile ⇒ Object
-
#initialize(options = {}) ⇒ StaticMap
constructor
A new instance of StaticMap.
- #tile_offset ⇒ Object
- #tile_options ⇒ Object
- #tiles ⇒ Object
- #to_image ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ StaticMap
Returns a new instance of StaticMap.
14 15 16 17 18 19 |
# File 'lib/smappy/static_map.rb', line 14 def initialize( = {}) = DEFAULT_OPTIONS.merge() .each do |key, value| send("#{key}=", value) if respond_to?("#{key}=") end end |
Instance Attribute Details
#center ⇒ Object
Returns the value of attribute center.
12 13 14 |
# File 'lib/smappy/static_map.rb', line 12 def center @center end |
#height ⇒ Object
Returns the value of attribute height.
12 13 14 |
# File 'lib/smappy/static_map.rb', line 12 def height @height end |
#tile_url_template ⇒ Object
Returns the value of attribute tile_url_template.
12 13 14 |
# File 'lib/smappy/static_map.rb', line 12 def tile_url_template @tile_url_template end |
#width ⇒ Object
Returns the value of attribute width.
12 13 14 |
# File 'lib/smappy/static_map.rb', line 12 def width @width end |
#zoomlevel ⇒ Object
Returns the value of attribute zoomlevel.
12 13 14 |
# File 'lib/smappy/static_map.rb', line 12 def zoomlevel @zoomlevel end |
Instance Method Details
#center_tile ⇒ Object
29 30 31 |
# File 'lib/smappy/static_map.rb', line 29 def center_tile center.to_tile zoomlevel: zoomlevel end |
#tile_offset ⇒ Object
33 34 35 36 37 |
# File 'lib/smappy/static_map.rb', line 33 def tile_offset center.position_on_tile(center_tile).map do |position| position - (Tile::SIZE / 2) end end |
#tile_options ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/smappy/static_map.rb', line 39 def { zoomlevel: zoomlevel, url_template: tile_url_template }.delete_if do |key, value| value.nil? end end |
#tiles ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/smappy/static_map.rb', line 48 def tiles # How many tiles do we need to fill the image: tile_width = (width / Tile::SIZE).to_i + 1 tile_height = (height / Tile::SIZE).to_i + 1 # What are our first and last tiles: tile_x_start = (center_tile.x - (tile_width / 2)).to_i tile_x_end = tile_x_start + tile_width tile_y_start = (center_tile.y - (tile_height / 2)).to_i tile_y_end = tile_y_start + tile_height (tile_x_start..tile_x_end).map do |x| (tile_y_start..tile_y_end).map do |y| Tile.new(.merge(x: x, y: y)) end end.flatten.reject do |tile| x,y = tile.position_on_map(self) (x + Tile::SIZE < 0 || x > width) || (y + Tile::SIZE < 0 || y > height) end end |
#to_image ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/smappy/static_map.rb', line 69 def to_image canvas = Magick::Image.new(width, height) drawing = Magick::Draw.new tiles.each do |tile| position = tile.position_on_map(self) drawing.composite(position[0], position[1], Tile::SIZE, Tile::SIZE, tile.to_image) end drawing.draw(canvas) canvas end |