]> git.openstreetmap.org Git - rails.git/blobdiff - lib/quad_tile.rb
Don't try and monkey patch database drivers that aren't loaded
[rails.git] / lib / quad_tile.rb
index 6e4fb6d2248c243bfc15c245fa0e643501229abf..258fb9fd58a1e660e22283a2f4905404301603ff 100644 (file)
@@ -3,17 +3,17 @@ module QuadTile
     require "quad_tile/quad_tile_so"
   rescue MissingSourceFile
     def self.tile_for_point(lat, lon)
-      x = ((lon + 180) * 65535 / 360).round
-      y = ((lat + 90) * 65535 / 180).round
+      x = ((lon.to_f + 180) * 65535 / 360).round
+      y = ((lat.to_f + 90) * 65535 / 180).round
 
       return tile_for_xy(x, y)
     end
 
-    def self.tiles_for_area(minlat, minlon, maxlat, maxlon)
-      minx = ((minlon + 180) * 65535 / 360).round
-      maxx = ((maxlon + 180) * 65535 / 360).round
-      miny = ((minlat + 90) * 65535 / 180).round
-      maxy = ((maxlat + 90) * 65535 / 180).round
+    def self.tiles_for_area(bbox)
+      minx = ((bbox.min_lon + 180) * 65535 / 360).round
+      maxx = ((bbox.max_lon + 180) * 65535 / 360).round
+      miny = ((bbox.min_lat + 90) * 65535 / 180).round
+      maxy = ((bbox.max_lat + 90) * 65535 / 180).round
       tiles = []
 
       minx.upto(maxx) do |x|
@@ -39,13 +39,32 @@ module QuadTile
 
       return t
     end
+
+    def self.iterate_tiles_for_area(bbox)
+      tiles = tiles_for_area(bbox)
+      first = last = nil
+
+      tiles.sort.each do |tile|
+        if last.nil?
+          first = last = tile
+        elsif tile == last + 1
+          last = tile
+        else
+          yield first, last
+
+          first = last = tile
+        end
+      end
+
+      yield first, last unless last.nil?
+    end
   end
 
-  def self.sql_for_area(minlat, minlon, maxlat, maxlon, prefix)
+  def self.sql_for_area(bbox, prefix)
     sql = Array.new
     single = Array.new
 
-    iterate_tiles_for_area(minlat, minlon, maxlat, maxlon) do |first,last|
+    iterate_tiles_for_area(bbox) do |first,last|
       if first == last
         single.push(first)
       else
@@ -58,24 +77,5 @@ module QuadTile
     return "( " + sql.join(" OR ") + " )"
   end
 
-  def self.iterate_tiles_for_area(minlat, minlon, maxlat, maxlon)
-    tiles = tiles_for_area(minlat, minlon, maxlat, maxlon)
-    first = last = nil
-
-    tiles.sort.each do |tile|
-      if last.nil?
-        first = last = tile
-      elsif tile == last + 1
-        last = tile
-      else
-        yield first, last
-
-        first = last = tile
-      end
-    end
-
-    yield first, last unless last.nil?
-  end
-
   private_class_method :tile_for_xy, :iterate_tiles_for_area
 end