Class: GPX::Bounds

Inherits:
Base
  • Object
show all
Defined in:
lib/gpx/bounds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#instantiate_with_text_elements

Constructor Details

#initialize(opts = { min_lat: 90.0, max_lat: -90.0, min_lon: 180.0, max_lon: -180.0 }) ⇒ Bounds

Creates a new bounds object with the passed-in min and max longitudes and latitudes.


7
8
9
10
11
12
# File 'lib/gpx/bounds.rb', line 7

def initialize(opts = { min_lat: 90.0, max_lat: -90.0, min_lon: 180.0, max_lon: -180.0 })
  @min_lat = opts[:min_lat].to_f
  @max_lat = opts[:max_lat].to_f
  @min_lon = opts[:min_lon].to_f
  @max_lon = opts[:max_lon].to_f
end

Instance Attribute Details

#max_latObject

Returns the value of attribute max_lat


3
4
5
# File 'lib/gpx/bounds.rb', line 3

def max_lat
  @max_lat
end

#max_lonObject

Returns the value of attribute max_lon


3
4
5
# File 'lib/gpx/bounds.rb', line 3

def max_lon
  @max_lon
end

#min_latObject

Returns the value of attribute min_lat


3
4
5
# File 'lib/gpx/bounds.rb', line 3

def min_lat
  @min_lat
end

#min_lonObject

Returns the value of attribute min_lon


3
4
5
# File 'lib/gpx/bounds.rb', line 3

def min_lon
  @min_lon
end

Instance Method Details

#add(item) ⇒ Object

Adds an item to itself, expanding its min/max lat/lon as needed to contain the given item. The item can be either another instance of Bounds or a Point.


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gpx/bounds.rb', line 34

def add(item)
  if item.respond_to?(:lat) && item.respond_to?(:lon)
    @min_lat = item.lat if item.lat < @min_lat
    @min_lon = item.lon if item.lon < @min_lon
    @max_lat = item.lat if item.lat > @max_lat
    @max_lon = item.lon if item.lon > @max_lon
  else
    @min_lat = item.min_lat if item.min_lat < @min_lat
    @min_lon = item.min_lon if item.min_lon < @min_lon
    @max_lat = item.max_lat if item.max_lat > @max_lat
    @max_lon = item.max_lon if item.max_lon > @max_lon
  end
end

#center_latObject

Returns the middle latitude.


15
16
17
18
# File 'lib/gpx/bounds.rb', line 15

def center_lat
  distance = (max_lat - min_lat) / 2.0
  (min_lat + distance)
end

#center_lonObject

Returns the middle longitude.


21
22
23
24
# File 'lib/gpx/bounds.rb', line 21

def center_lon
  distance = (max_lon - min_lon) / 2.0
  (min_lon + distance)
end

#contains?(pt) ⇒ Boolean

Returns true if the pt is within these bounds.

Returns:

  • (Boolean)

27
28
29
# File 'lib/gpx/bounds.rb', line 27

def contains?(pt)
  ((pt.lat >= min_lat) && (pt.lat <= max_lat) && (pt.lon >= min_lon) && (pt.lon <= max_lon))
end

#to_sObject

Returns the min_lat, min_lon, max_lat, and max_lon in a labeled string.


49
50
51
# File 'lib/gpx/bounds.rb', line 49

def to_s
  "min_lat: #{min_lat} min_lon: #{min_lon} max_lat: #{max_lat} max_lon: #{max_lon}"
end