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