Class: OSM::Calculator
- Inherits:
-
Object
- Object
- OSM::Calculator
- Defined in:
- lib/osm/calculator.rb
Constant Summary collapse
- TILE_SIZE =
256
Instance Attribute Summary collapse
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#target_size ⇒ Object
readonly
Returns the value of attribute target_size.
-
#zoom ⇒ Object
readonly
Returns the value of attribute zoom.
Class Method Summary collapse
Instance Method Summary collapse
- #bottom_right_tile(path) ⇒ Object
- #center_pixel(path) ⇒ Object
- #crop_dimensions(path) ⇒ Object
-
#initialize(zoom, padding, target_size) ⇒ Calculator
constructor
A new instance of Calculator.
- #path_pixel_dimensions(path) ⇒ Object
- #pixels(path) ⇒ Object
- #tile_dimensions(path) ⇒ Object
- #tiles(path) ⇒ Object
- #top_left_tile(path) ⇒ Object
Constructor Details
#initialize(zoom, padding, target_size) ⇒ Calculator
Returns a new instance of Calculator.
8 9 10 11 12 |
# File 'lib/osm/calculator.rb', line 8 def initialize(zoom, padding, target_size) @zoom = zoom @padding = padding @target_size = target_size end |
Instance Attribute Details
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
6 7 8 |
# File 'lib/osm/calculator.rb', line 6 def padding @padding end |
#target_size ⇒ Object (readonly)
Returns the value of attribute target_size.
6 7 8 |
# File 'lib/osm/calculator.rb', line 6 def target_size @target_size end |
#zoom ⇒ Object (readonly)
Returns the value of attribute zoom.
6 7 8 |
# File 'lib/osm/calculator.rb', line 6 def zoom @zoom end |
Class Method Details
.get_zoom_level(path, padding, target_size) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/osm/calculator.rb', line 14 def self.get_zoom_level(path, padding, target_size) 18.downto(1) do |level| d = Calculator.new(level, padding, target_size) path_size = d.path_pixel_dimensions(path) if path_size.x + padding.x <= target_size.x && path_size.y + padding.y <= target_size.y return level end end return 1 end |
Instance Method Details
#bottom_right_tile(path) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/osm/calculator.rb', line 53 def bottom_right_tile(path) center = center_pixel(path) x = center.x + (target_size.x / 2) y = center.y + (target_size.y / 2) OpenStruct.new(x: (x / TILE_SIZE).floor, y: (y / TILE_SIZE).floor) end |
#center_pixel(path) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/osm/calculator.rb', line 60 def center_pixel(path) top_left = path_top_left_pixel(path) bottom_right = path_bottom_right_pixel(path) delta = OpenStruct.new(x: bottom_right.x - top_left.x, y: bottom_right.y - top_left.y) OpenStruct.new(x: top_left.x + (delta.x / 2), y: top_left.y + (delta.y / 2)) end |
#crop_dimensions(path) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/osm/calculator.rb', line 80 def crop_dimensions(path) center = center_pixel(path) top_left = top_left_tile(path) left = center.x - (top_left.x * TILE_SIZE) - (target_size.x / 2) top = center.y - (top_left.y * TILE_SIZE) - (target_size.y / 2) OpenStruct.new(x: left, y: top) end |
#path_pixel_dimensions(path) ⇒ Object
74 75 76 77 78 |
# File 'lib/osm/calculator.rb', line 74 def path_pixel_dimensions(path) top_left = path_top_left_pixel(path) bottom_right = path_bottom_right_pixel(path) OpenStruct.new(x: bottom_right.x - top_left.x, y: bottom_right.y - top_left.y) end |
#pixels(path) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/osm/calculator.rb', line 25 def pixels(path) crop = crop_dimensions(path) top_left = top_left_tile(path) path.map do |point| pixel = lat_lng_to_pixel(point) OpenStruct.new(x: pixel.x - (top_left.x * TILE_SIZE) - crop.x, y: pixel.y - (top_left.y * TILE_SIZE) - crop.y) end end |
#tile_dimensions(path) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/osm/calculator.rb', line 67 def tile_dimensions(path) t = tiles(path) width = t.map(&:x).max - t.map(&:x).min height = t.map(&:y).max - t.map(&:y).min OpenStruct.new(x: width + 1, y: height + 1) end |
#tiles(path) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/osm/calculator.rb', line 34 def tiles(path) top_left = top_left_tile(path) bottom_right = bottom_right_tile(path) tiles = [] (top_left.y..bottom_right.y).each do |y| (top_left.x..bottom_right.x).each do |x| tiles << OpenStruct.new(x: x, y: y) end end tiles end |