2   attr_reader :min_lon, :min_lat, :max_lon, :max_lat
 
   7   SCALED_LON_LIMIT = LON_LIMIT *  GeoRecord::SCALE
 
   8   SCALED_LAT_LIMIT = LAT_LIMIT *  GeoRecord::SCALE
 
  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?
 
  20       BoundingBox.new(*s.split(/,/))
 
  26   def self.from_bbox_params(params)
 
  27     if params[:bbox] and params[:bbox].count(',') == 3
 
  28       bbox_array = params[:bbox].split(',')
 
  30     from_bbox_array(bbox_array)
 
  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]]
 
  37     from_bbox_array(bbox_array)
 
  40   def self.from_lrbt_params(params)
 
  41     if params[:l] and params[:b] and params[:t] and params[:t]
 
  42       bbox_array = [params[:l], params[:b], params[:r], params[:t]]
 
  44     from_bbox_array(bbox_array)
 
  47   def expand!(bbox, margin = 0)
 
  48     update!(bbox) unless complete?
 
  49     # only try to expand the bbox if there is a value for every coordinate
 
  50     # which there will be from the previous line as long as array does not contain a nil
 
  52       @min_lon = [-SCALED_LON_LIMIT,
 
  53                   bbox.min_lon + margin * (min_lon - max_lon)].max if bbox.min_lon < min_lon
 
  54       @min_lat = [-SCALED_LAT_LIMIT,
 
  55                   bbox.min_lat + margin * (min_lat - max_lat)].max if bbox.min_lat < min_lat
 
  56       @max_lon = [+SCALED_LON_LIMIT,
 
  57                   bbox.max_lon + margin * (max_lon - min_lon)].min if bbox.max_lon > max_lon
 
  58       @max_lat = [+SCALED_LAT_LIMIT,
 
  59                   bbox.max_lat + margin * (max_lat - min_lat)].min if bbox.max_lat > max_lat
 
  65     # check the bbox is sane
 
  67       raise OSM::APIBadBoundingBox.new(
 
  68         "The minimum longitude must be less than the maximum longitude, but it wasn't")
 
  71       raise OSM::APIBadBoundingBox.new(
 
  72         "The minimum latitude must be less than the maximum latitude, but it wasn't")
 
  74     if min_lon < -LON_LIMIT || min_lat < -LAT_LIMIT || max_lon > +LON_LIMIT || max_lat > +LAT_LIMIT
 
  75       raise OSM::APIBadBoundingBox.new("The latitudes must be between #{-LAT_LIMIT} and #{LAT_LIMIT}," +
 
  76                                        " and longitudes between #{-LON_LIMIT} and #{LON_LIMIT}")
 
  81   def check_size(max_area = MAX_REQUEST_AREA)
 
  82     # check the bbox isn't too large
 
  84       raise OSM::APIBadBoundingBox.new("The maximum bbox size is " + max_area.to_s +
 
  85         ", and your request was too large. Either request a smaller area, or use planet.osm")
 
  91   # returns area of the bbox as a rough comparative quantity
 
  94       (max_lon - min_lon) * (max_lat - min_lat)
 
 101     not to_a.include?(nil)
 
 105     (min_lon + max_lon) / 2.0
 
 109     (min_lat + max_lat) / 2.0
 
 120   def slippy_width(zoom)
 
 121     width * 256.0 * 2.0 ** zoom / 360.0
 
 124   def slippy_height(zoom)
 
 125     min = min_lat * Math::PI / 180.0
 
 126     max = max_lat * Math::PI / 180.0
 
 128     Math.log((Math.tan(max) + 1.0 / Math.cos(max)) /
 
 129              (Math.tan(min) + 1.0 / Math.cos(min))) *
 
 130              (128.0 * 2.0 ** zoom / Math::PI)
 
 133   # there are two forms used for bounds with and without an underscore,
 
 134   # cater for both forms eg minlon and min_lon
 
 135   def add_bounds_to(hash, underscore = "")
 
 136     hash["min#{underscore}lat"] = min_lat.to_s
 
 137     hash["min#{underscore}lon"] = min_lon.to_s
 
 138     hash["max#{underscore}lat"] = max_lat.to_s
 
 139     hash["max#{underscore}lon"] = max_lon.to_s
 
 144     BoundingBox.new((min_lon * GeoRecord::SCALE),
 
 145                     (min_lat * GeoRecord::SCALE),
 
 146                     (max_lon * GeoRecord::SCALE),
 
 147                     (max_lat * GeoRecord::SCALE))
 
 151     BoundingBox.new((min_lon / GeoRecord::SCALE),
 
 152                     (min_lat / GeoRecord::SCALE),
 
 153                     (max_lon / GeoRecord::SCALE),
 
 154                     (max_lat / GeoRecord::SCALE))
 
 158     [min_lon, min_lat, max_lon, max_lat]
 
 162     "#{min_lon},#{min_lat},#{max_lon},#{max_lat}"
 
 166   def self.from_bbox_array(bbox_array)
 
 168       raise OSM::APIBadUserInput.new(
 
 169         "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat")
 
 171     # Take an array of length 4, create a bounding box with min_lon, min_lat, max_lon and
 
 172     # max_lat within their respective boundaries.
 
 173     min_lon = [[bbox_array[0].to_f, -LON_LIMIT].max, +LON_LIMIT].min
 
 174     min_lat = [[bbox_array[1].to_f, -LAT_LIMIT].max, +LAT_LIMIT].min
 
 175     max_lon = [[bbox_array[2].to_f, +LON_LIMIT].min, -LON_LIMIT].max
 
 176     max_lat = [[bbox_array[3].to_f, +LAT_LIMIT].min, -LAT_LIMIT].max
 
 177     BoundingBox.new(min_lon, min_lat, max_lon, max_lat)
 
 181     # ensure that bbox has no nils in it. if there are any
 
 182     # nils, just use the bounding box update to write over them.
 
 183     @min_lon = bbox.min_lon if min_lon.nil?
 
 184     @min_lat = bbox.min_lat if min_lat.nil?
 
 185     @max_lon = bbox.max_lon if max_lon.nil?
 
 186     @max_lat = bbox.max_lat if max_lat.nil?