Module: Tsuga::Model::Tile::ClassMethods

Included in:
Tsuga::Model::Tile
Defined in:
lib/tsuga/model/tile.rb

Instance Method Summary collapse

Instance Method Details

#enclosing_viewport(point_ne: nil, point_sw: nil, depth: nil) ⇒ Object

Return an array of Tile instances that encloses both corner points FIXME: this is untested



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tsuga/model/tile.rb', line 87

def enclosing_viewport(point_ne:nil, point_sw:nil, depth:nil)
  # $stderr.puts "aiming to enclose:"
  # $stderr.puts "%.2f %.2f -> %.2f %.2f" % [point_ne.lat, point_ne.lng, point_sw.lat, point_sw.lng]
  # $stderr.flush

  tiles = []
  first_tile = including(point_sw, depth:depth)

  offset_lat = 0
  loop do
    offset_lng = 0
    loop do 
      # $stderr.puts("offset: #{offset_lat} #{offset_lng}")
      # $stderr.flush
      new_tile = first_tile.neighbour(lat:offset_lat, lng:offset_lng)
      tiles << new_tile

      # $stderr.puts "%.2f %.2f -> %.2f %.2f" % [new_tile.southwest.lat, new_tile.southwest.lng, new_tile.northeast.lat, new_tile.northeast.lng]
      # $stderr.flush

      offset_lng += 1
      break if tiles.last.northeast.lng >= point_ne.lng
    end
    break if tiles.last.northeast.lat >= point_ne.lat
    offset_lat += 1
    offset_lng = 0
  end

  return tiles
end

#including(point, options = {}) ⇒ Object

Returns a Tile instance. point should respond to geohash. Options:

  • :depth

Raises:

  • (ArgumentError)


78
79
80
81
82
83
# File 'lib/tsuga/model/tile.rb', line 78

def including(point, options={})
  depth = options[:depth]
  raise ArgumentError, 'bad depth' unless (0..31).include?(depth)

  new(prefix: point.prefix(depth))
end