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