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