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 + 2
  tile_height  = (height / Tile::SIZE).to_i + 2
  
  # 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/smappy/static_map.rb', line 69

def to_image
  canvas = ChunkyPNG::Image.new(width, height)
  
  tiles.each do |tile|
    x, y  = tile.position_on_map(self)
    image = tile.to_image
    
    if x < 0 || y < 0
      crop_x = x < 0 ? -x : 0
      crop_y = y < 0 ? -y : 0
      x, y   = [0, x].max, [0, y].max
      
      image.crop!(crop_x, crop_y, image.width - crop_x, image.height - crop_y)
    end
    
    if x + image.width > canvas.width || y + image.height > height
      crop_width  = [image.width, canvas.width - x].min
      crop_height = [image.height, canvas.height - y].min
      
      image.crop!(0, 0, crop_width, crop_height)
    end
    
    canvas.compose!(image, x, y)
  end
  
  canvas
end