Class: Geospatial::Tiles

Inherits:
Object
  • Object
show all
Defined in:
lib/geospatial/tiles.rb

Constant Summary collapse

R2D =

Radians to degrees multiplier

(180.0 / Math::PI)
D2R =
(Math::PI / 180.0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(box, zoom = 5) ⇒ Tiles



29
30
31
32
# File 'lib/geospatial/tiles.rb', line 29

def initialize(box, zoom = 5)
  @box = box
  @zoom = zoom
end

Instance Attribute Details

#boxObject (readonly)

Returns the value of attribute box.



34
35
36
# File 'lib/geospatial/tiles.rb', line 34

def box
  @box
end

#zoomObject (readonly)

Returns the value of attribute zoom.



35
36
37
# File 'lib/geospatial/tiles.rb', line 35

def zoom
  @zoom
end

Instance Method Details

#each(zoom = @zoom) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/geospatial/tiles.rb', line 54

def each(zoom = @zoom)
  return to_enum(:each, zoom) unless block_given?
  
  min = map(*@box.min, zoom)
  max = map(*@box.max, zoom)
  
  (min[0].floor...max[0].ceil).each do |x|
    # The y axis is reversed... (i.e. the origin is in the top left)
    (max[1].floor...min[1].ceil).each do |y|
      yield zoom, x, y
    end
  end
end

#map(longitude, latitude, zoom = @zoom) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/geospatial/tiles.rb', line 37

def map(longitude, latitude, zoom = @zoom)
  n = 2 ** zoom
  
  x = n * ((longitude + 180.0) / 360.0)
  y = n * (1.0 - (Math::log(Math::tan(latitude * D2R) + (1.0 / Math::cos(latitude * D2R))) / Math::PI)) / 2.0
  
  return x, y
end

#unmap(x, y, zoom = @zoom) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/geospatial/tiles.rb', line 46

def unmap(x, y, zoom = @zoom)
  n = 2 ** zoom
  longitude = x / n * 360.0 - 180.0
  latitude = Math::arctan(Math::sinh(Math::PI * (1.0 - 2.0 * y / n))) * R2D
  
  return longitude, latitude
end