Class: Philiprehberger::GeoPoint::BoundingBox

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_latObject (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_lonObject (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_latObject (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_lonObject (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_km2Float

Approximate surface area of the bounding box in square kilometers

Returns:

  • (Float)

    area in km²



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

Returns:

  • (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

Returns:

  • (Boolean)


60
61
62
# File 'lib/philiprehberger/geo_point/bounding_box.rb', line 60

def eql?(other)
  self == other
end

#hashObject



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

#inspectObject



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_aObject



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_hObject



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_sObject



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