3     require "quad_tile/quad_tile_so"
 
   4   rescue MissingSourceFile
 
   5     def self.tile_for_point(lat, lon)
 
   6       x = ((lon.to_f + 180) * 65535 / 360).round
 
   7       y = ((lat.to_f + 90) * 65535 / 180).round
 
   9       return tile_for_xy(x, y)
 
  12     def self.tiles_for_area(bbox)
 
  13       minx = ((bbox.min_lon + 180) * 65535 / 360).round
 
  14       maxx = ((bbox.max_lon + 180) * 65535 / 360).round
 
  15       miny = ((bbox.min_lat + 90) * 65535 / 180).round
 
  16       maxy = ((bbox.max_lat + 90) * 65535 / 180).round
 
  19       minx.upto(maxx) do |x|
 
  20         miny.upto(maxy) do |y|
 
  21           tiles << tile_for_xy(x, y)
 
  28     def self.tile_for_xy(x, y)
 
  33         t = t | 1 unless (x & 0x8000).zero?
 
  36         t = t | 1 unless (y & 0x8000).zero?
 
  43     def self.iterate_tiles_for_area(bbox)
 
  44       tiles = tiles_for_area(bbox)
 
  47       tiles.sort.each do |tile|
 
  50         elsif tile == last + 1
 
  59       yield first, last unless last.nil?
 
  63   def self.sql_for_area(bbox, prefix)
 
  67     iterate_tiles_for_area(bbox) do |first,last|
 
  71         sql.push("#{prefix}tile BETWEEN #{first} AND #{last}")
 
  75     sql.push("#{prefix}tile IN (#{single.join(',')})") if single.size > 0
 
  77     return "( " + sql.join(" OR ") + " )"
 
  80   private_class_method :tile_for_xy, :iterate_tiles_for_area