]> git.openstreetmap.org Git - rails.git/blob - lib/bounding_box.rb
Fixed formatting and stuff in fixtures.
[rails.git] / lib / bounding_box.rb
1 class BoundingBox
2   def initialize(min_lon, min_lat, max_lon, max_lat)
3     @bbox = [min_lon.to_f, min_lat.to_f, max_lon.to_f, max_lat.to_f]
4   end
5
6   def self.from_s(s)
7     BoundingBox.new(s.split(/,/))
8   end
9
10   def min_lon
11     @bbox[0]
12   end
13
14   def min_lon=(min_lon)
15     @bbox[0] = min_lon
16   end
17
18   def min_lat
19     @bbox[1]
20   end
21
22   def min_lat=(min_lat)
23     @bbox[1] = min_lat
24   end
25
26   def max_lon
27     @bbox[2]
28   end
29
30   def max_lon=(max_lon)
31     @bbox[2] = max_lon
32   end
33
34   def max_lat
35     @bbox[3]
36   end
37
38   def max_lat=(max_lat)
39     @bbox[3] = max_lat
40   end
41
42   def centre_lon
43     (@bbox[0] + @bbox[2]) / 2.0
44   end
45
46   def centre_lat
47     (@bbox[1] + @bbox[3]) / 2.0
48   end
49
50   def width
51     @bbox[2] - @bbox[0]
52   end
53
54   def height
55     @bbox[3] - @bbox[1]
56   end
57
58   def slippy_width(zoom)
59     width * 256.0 * 2.0 ** zoom / 360.0
60   end
61
62   def slippy_height(zoom)
63     min = min_lat * Math::PI / 180.0
64     max = max_lat * Math::PI / 180.0
65
66     Math.log((Math.tan(max) + 1.0 / Math.cos(max)) / (Math.tan(min) + 1.0 / Math.cos(min))) * 128.0 * 2.0 ** zoom / Math::PI
67   end
68
69   def to_s
70     return @bbox.join(",")
71   end
72 end