Class: Smappy::StaticMap

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options = {})
  options = DEFAULT_OPTIONS.merge(options)
  options.each do |key, value|
    send("#{key}=", value) if respond_to?("#{key}=")
  end
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



12
13
14
# File 'lib/smappy/static_map.rb', line 12

def center
  @center
end

#heightObject

Returns the value of attribute height.



12
13
14
# File 'lib/smappy/static_map.rb', line 12

def height
  @height
end

#tile_url_templateObject

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

#widthObject

Returns the value of attribute width.



12
13
14
# File 'lib/smappy/static_map.rb', line 12

def width
  @width
end

#zoomlevelObject

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_tileObject



29
30
31
# File 'lib/smappy/static_map.rb', line 29

def center_tile
  center.to_tile zoomlevel: zoomlevel
end

#tile_offsetObject



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_optionsObject



39
40
41
42
43
44
45
46
# File 'lib/smappy/static_map.rb', line 39

def tile_options
  {
    zoomlevel:    zoomlevel,
    url_template: tile_url_template
  }.delete_if do |key, value|
    value.nil?
  end
end

#tilesObject



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(tile_options.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_imageObject



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