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