Class: GPX::Bounds
Instance Attribute Summary collapse
-
#max_lat ⇒ Object
Returns the value of attribute max_lat.
-
#max_lon ⇒ Object
Returns the value of attribute max_lon.
-
#min_lat ⇒ Object
Returns the value of attribute min_lat.
-
#min_lon ⇒ Object
Returns the value of attribute min_lon.
Instance Method Summary collapse
-
#add(item) ⇒ Object
Adds an item to itself, expanding its min/max lat/lon as needed to contain the given item.
-
#center_lat ⇒ Object
Returns the middle latitude.
-
#center_lon ⇒ Object
Returns the middle longitude.
-
#contains?(pt) ⇒ Boolean
Returns true if the pt is within these bounds.
-
#initialize(opts = { min_lat: 90.0, max_lat: -90.0, min_lon: 180.0, max_lon: -180.0 }) ⇒ Bounds
constructor
Creates a new bounds object with the passed-in min and max longitudes and latitudes.
-
#to_s ⇒ Object
Returns the min_lat, min_lon, max_lat, and max_lon in a labeled string.
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_lat ⇒ Object
Returns the value of attribute max_lat.
3 4 5 |
# File 'lib/gpx/bounds.rb', line 3 def max_lat @max_lat end |
#max_lon ⇒ Object
Returns the value of attribute max_lon.
3 4 5 |
# File 'lib/gpx/bounds.rb', line 3 def max_lon @max_lon end |
#min_lat ⇒ Object
Returns the value of attribute min_lat.
3 4 5 |
# File 'lib/gpx/bounds.rb', line 3 def min_lat @min_lat end |
#min_lon ⇒ Object
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_lat ⇒ Object
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_lon ⇒ Object
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.
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_s ⇒ Object
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 |