Class: Philiprehberger::GeoPoint::BoundingBox
- Inherits:
-
Object
- Object
- Philiprehberger::GeoPoint::BoundingBox
- Defined in:
- lib/philiprehberger/geo_point/bounding_box.rb
Overview
Represents a geographic bounding box defined by min/max latitude and longitude.
Instance Attribute Summary collapse
-
#max_lat ⇒ Object
readonly
Returns the value of attribute max_lat.
-
#max_lon ⇒ Object
readonly
Returns the value of attribute max_lon.
-
#min_lat ⇒ Object
readonly
Returns the value of attribute min_lat.
-
#min_lon ⇒ Object
readonly
Returns the value of attribute min_lon.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#area_km2 ⇒ Float
Approximate surface area of the bounding box in square kilometers.
- #contains?(point) ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(min_lat, max_lat, min_lon, max_lon) ⇒ BoundingBox
constructor
A new instance of BoundingBox.
- #inspect ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(min_lat, max_lat, min_lon, max_lon) ⇒ BoundingBox
Returns a new instance of BoundingBox.
9 10 11 12 13 14 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 9 def initialize(min_lat, max_lat, min_lon, max_lon) @min_lat = Float(min_lat) @max_lat = Float(max_lat) @min_lon = Float(min_lon) @max_lon = Float(max_lon) end |
Instance Attribute Details
#max_lat ⇒ Object (readonly)
Returns the value of attribute max_lat.
7 8 9 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 7 def max_lat @max_lat end |
#max_lon ⇒ Object (readonly)
Returns the value of attribute max_lon.
7 8 9 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 7 def max_lon @max_lon end |
#min_lat ⇒ Object (readonly)
Returns the value of attribute min_lat.
7 8 9 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 7 def min_lat @min_lat end |
#min_lon ⇒ Object (readonly)
Returns the value of attribute min_lon.
7 8 9 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 7 def min_lon @min_lon end |
Class Method Details
.around(point, radius, unit: :km) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 16 def self.around(point, radius, unit: :km) km_radius = radius / Point::UNIT_MULTIPLIERS.fetch(unit) lat_delta = km_radius / Point::EARTH_RADIUS_KM * (180.0 / Math::PI) lon_delta = km_radius / (Point::EARTH_RADIUS_KM * Math.cos(point.lat * Math::PI / 180.0)) * (180.0 / Math::PI) new( point.lat - lat_delta, point.lat + lat_delta, point.lon - lon_delta, point.lon + lon_delta ) end |
Instance Method Details
#==(other) ⇒ Object
54 55 56 57 58 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 54 def ==(other) other.is_a?(self.class) && @min_lat == other.min_lat && @max_lat == other.max_lat && @min_lon == other.min_lon && @max_lon == other.max_lon end |
#area_km2 ⇒ Float
Approximate surface area of the bounding box in square kilometers
38 39 40 41 42 43 44 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 38 def area_km2 lat_dist = Point::EARTH_RADIUS_KM * ((@max_lat - @min_lat) * Math::PI / 180.0) mid_lat = (@min_lat + @max_lat) / 2.0 lon_dist = Point::EARTH_RADIUS_KM * Math.cos(mid_lat * Math::PI / 180.0) * ((@max_lon - @min_lon) * Math::PI / 180.0) lat_dist.abs * lon_dist.abs end |
#contains?(point) ⇒ Boolean
31 32 33 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 31 def contains?(point) point.lat.between?(@min_lat, @max_lat) && point.lon.between?(@min_lon, @max_lon) end |
#eql?(other) ⇒ Boolean
60 61 62 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 60 def eql?(other) self == other end |
#hash ⇒ Object
64 65 66 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 64 def hash [@min_lat, @max_lat, @min_lon, @max_lon].hash end |
#inspect ⇒ Object
72 73 74 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 72 def inspect "#<#{self.class} min_lat=#{@min_lat} max_lat=#{@max_lat} min_lon=#{@min_lon} max_lon=#{@max_lon}>" end |
#to_a ⇒ Object
46 47 48 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 46 def to_a [@min_lat, @max_lat, @min_lon, @max_lon] end |
#to_h ⇒ Object
50 51 52 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 50 def to_h { min_lat: @min_lat, max_lat: @max_lat, min_lon: @min_lon, max_lon: @max_lon } end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 68 def to_s "(#{@min_lat}..#{@max_lat}, #{@min_lon}..#{@max_lon})" end |