]> git.openstreetmap.org Git - rails.git/blob - lib/bounding_box.rb
Replace deprecated xxx_filter methods with xxx_action
[rails.git] / lib / bounding_box.rb
1 class BoundingBox
2   attr_reader :min_lon, :min_lat, :max_lon, :max_lat
3
4   LON_LIMIT = 180.0
5   LAT_LIMIT = 90.0
6   SCALED_LON_LIMIT = LON_LIMIT * GeoRecord::SCALE
7   SCALED_LAT_LIMIT = LAT_LIMIT * GeoRecord::SCALE
8
9   def initialize(min_lon, min_lat, max_lon, max_lat)
10     @min_lon = min_lon.to_f unless min_lon.nil?
11     @min_lat = min_lat.to_f unless min_lat.nil?
12     @max_lon = max_lon.to_f unless max_lon.nil?
13     @max_lat = max_lat.to_f unless max_lat.nil?
14   end
15
16   def self.from_s(s)
17     BoundingBox.new(*s.split(/,/)) if s.count(",") == 3
18   end
19
20   def self.from_bbox_params(params)
21     if params[:bbox] && params[:bbox].count(",") == 3
22       bbox_array = params[:bbox].split(",")
23     end
24     from_bbox_array(bbox_array)
25   end
26
27   def self.from_lon_lat_params(params)
28     if params[:minlon] && params[:minlat] && params[:maxlon] && params[:maxlat]
29       bbox_array = [params[:minlon], params[:minlat], params[:maxlon], params[:maxlat]]
30     end
31     from_bbox_array(bbox_array)
32   end
33
34   def self.from_lrbt_params(params)
35     if params[:l] && params[:b] && params[:t] && params[:t]
36       bbox_array = [params[:l], params[:b], params[:r], params[:t]]
37     end
38     from_bbox_array(bbox_array)
39   end
40
41   def expand!(bbox, margin = 0)
42     update!(bbox) unless complete?
43     # only try to expand the bbox if there is a value for every coordinate
44     # which there will be from the previous line as long as array does not contain a nil
45     if bbox.complete?
46       if bbox.min_lon < min_lon
47         @min_lon = [-SCALED_LON_LIMIT,
48                     bbox.min_lon + margin * (min_lon - max_lon)].max
49       end
50       if bbox.min_lat < min_lat
51         @min_lat = [-SCALED_LAT_LIMIT,
52                     bbox.min_lat + margin * (min_lat - max_lat)].max
53       end
54       if bbox.max_lon > max_lon
55         @max_lon = [+SCALED_LON_LIMIT,
56                     bbox.max_lon + margin * (max_lon - min_lon)].min
57       end
58       if bbox.max_lat > max_lat
59         @max_lat = [+SCALED_LAT_LIMIT,
60                     bbox.max_lat + margin * (max_lat - min_lat)].min
61       end
62     end
63     self
64   end
65
66   def check_boundaries
67     # check the bbox is sane
68     if min_lon > max_lon
69       raise OSM::APIBadBoundingBox.new(
70         "The minimum longitude must be less than the maximum longitude, but it wasn't"
71       )
72     end
73     if min_lat > max_lat
74       raise OSM::APIBadBoundingBox.new(
75         "The minimum latitude must be less than the maximum latitude, but it wasn't"
76       )
77     end
78     if min_lon < -LON_LIMIT || min_lat < -LAT_LIMIT || max_lon > +LON_LIMIT || max_lat > +LAT_LIMIT
79       raise OSM::APIBadBoundingBox.new("The latitudes must be between #{-LAT_LIMIT} and #{LAT_LIMIT}," +
80                                        " and longitudes between #{-LON_LIMIT} and #{LON_LIMIT}")
81     end
82     self
83   end
84
85   def check_size(max_area = MAX_REQUEST_AREA)
86     # check the bbox isn't too large
87     if area > max_area
88       raise OSM::APIBadBoundingBox.new("The maximum bbox size is " + max_area.to_s +
89         ", and your request was too large. Either request a smaller area, or use planet.osm")
90     end
91     self
92   end
93
94   ##
95   # returns area of the bbox as a rough comparative quantity
96   def area
97     if complete?
98       (max_lon - min_lon) * (max_lat - min_lat)
99     else
100       0
101     end
102   end
103
104   def complete?
105     !to_a.include?(nil)
106   end
107
108   def centre_lon
109     (min_lon + max_lon) / 2.0
110   end
111
112   def centre_lat
113     (min_lat + max_lat) / 2.0
114   end
115
116   def width
117     max_lon - min_lon
118   end
119
120   def height
121     max_lat - min_lat
122   end
123
124   def slippy_width(zoom)
125     width * 256.0 * 2.0**zoom / 360.0
126   end
127
128   def slippy_height(zoom)
129     min = min_lat * Math::PI / 180.0
130     max = max_lat * Math::PI / 180.0
131
132     Math.log((Math.tan(max) + 1.0 / Math.cos(max)) /
133              (Math.tan(min) + 1.0 / Math.cos(min))) *
134       (128.0 * 2.0**zoom / Math::PI)
135   end
136
137   # there are two forms used for bounds with and without an underscore,
138   # cater for both forms eg minlon and min_lon
139   def add_bounds_to(hash, underscore = "")
140     hash["min#{underscore}lat"] = min_lat.to_s
141     hash["min#{underscore}lon"] = min_lon.to_s
142     hash["max#{underscore}lat"] = max_lat.to_s
143     hash["max#{underscore}lon"] = max_lon.to_s
144     hash
145   end
146
147   def to_scaled
148     BoundingBox.new((min_lon * GeoRecord::SCALE),
149                     (min_lat * GeoRecord::SCALE),
150                     (max_lon * GeoRecord::SCALE),
151                     (max_lat * GeoRecord::SCALE))
152   end
153
154   def to_unscaled
155     BoundingBox.new((min_lon / GeoRecord::SCALE),
156                     (min_lat / GeoRecord::SCALE),
157                     (max_lon / GeoRecord::SCALE),
158                     (max_lat / GeoRecord::SCALE))
159   end
160
161   def to_a
162     [min_lon, min_lat, max_lon, max_lat]
163   end
164
165   def to_s
166     "#{min_lon},#{min_lat},#{max_lon},#{max_lat}"
167   end
168
169   class << self
170     private
171
172     def from_bbox_array(bbox_array)
173       unless bbox_array
174         raise OSM::APIBadUserInput.new(
175           "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat"
176         )
177       end
178       # Take an array of length 4, create a bounding box with min_lon, min_lat, max_lon and
179       # max_lat within their respective boundaries.
180       min_lon = [[bbox_array[0].to_f, -LON_LIMIT].max, +LON_LIMIT].min
181       min_lat = [[bbox_array[1].to_f, -LAT_LIMIT].max, +LAT_LIMIT].min
182       max_lon = [[bbox_array[2].to_f, +LON_LIMIT].min, -LON_LIMIT].max
183       max_lat = [[bbox_array[3].to_f, +LAT_LIMIT].min, -LAT_LIMIT].max
184       BoundingBox.new(min_lon, min_lat, max_lon, max_lat)
185     end
186   end
187
188   def update!(bbox)
189     # ensure that bbox has no nils in it. if there are any
190     # nils, just use the bounding box update to write over them.
191     @min_lon = bbox.min_lon if min_lon.nil?
192     @min_lat = bbox.min_lat if min_lat.nil?
193     @max_lon = bbox.max_lon if max_lon.nil?
194     @max_lat = bbox.max_lat if max_lat.nil?
195   end
196 end