Class: Geokit::Bounds
Overview
Bounds represents a rectangular bounds, defined by the SW and NE corners
Instance Attribute Summary collapse
-
#ne ⇒ Object
sw and ne are LatLng objects.
-
#sw ⇒ Object
sw and ne are LatLng objects.
Class Method Summary collapse
-
.from_point_and_radius(point, radius, options = {}) ⇒ Object
returns an instance of bounds which completely encompases the given circle.
-
.normalize(thing, other = nil) ⇒ Object
Takes two main combinations of arguments to create a bounds: point,point (this is the only one which takes two arguments [point,point] . . . where a point is anything LatLng#normalize can handle (which is quite a lot).
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns true if the candidate object is logically equal.
-
#center ⇒ Object
returns the a single point which is the center of the rectangular bounds.
-
#contains?(point) ⇒ Boolean
Returns true if the bounds contain the passed point.
-
#crosses_meridian? ⇒ Boolean
returns true if the bounds crosses the international dateline.
-
#initialize(sw, ne) ⇒ Bounds
constructor
provide sw and ne to instantiate a new Bounds instance.
-
#to_a ⇒ Object
a two-element array of two-element arrays: sw,ne.
-
#to_s ⇒ Object
a simple string representation:sw,ne.
-
#to_span ⇒ Object
Equivalent to Google Maps API’s .toSpan() method on GLatLng’s.
Constructor Details
Instance Attribute Details
#ne ⇒ Object
sw and ne are LatLng objects
5 6 7 |
# File 'lib/geokit/bounds.rb', line 5 def ne @ne end |
#sw ⇒ Object
sw and ne are LatLng objects
5 6 7 |
# File 'lib/geokit/bounds.rb', line 5 def sw @sw end |
Class Method Details
.from_point_and_radius(point, radius, options = {}) ⇒ Object
returns an instance of bounds which completely encompases the given circle
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/geokit/bounds.rb', line 69 def from_point_and_radius(point, radius, = {}) point = LatLng.normalize(point) p0 = point.endpoint(0, radius, ) p90 = point.endpoint(90, radius, ) p180 = point.endpoint(180, radius, ) p270 = point.endpoint(270, radius, ) sw = Geokit::LatLng.new(p180.lat, p270.lng) ne = Geokit::LatLng.new(p0.lat, p90.lng) Geokit::Bounds.new(sw, ne) end |
.normalize(thing, other = nil) ⇒ Object
Takes two main combinations of arguments to create a bounds: point,point (this is the only one which takes two arguments
- point,point
-
. . . where a point is anything LatLng#normalize can handle
(which is quite a lot)
NOTE: everything combination is assumed to pass points in the order sw, ne
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/geokit/bounds.rb', line 88 def normalize (thing, other = nil) # maybe this will be simple -- an actual bounds object is passed, and # we can all go home return thing if thing.is_a? Bounds # no? OK, if there's no "other," the thing better be a two-element array thing, other = thing if !other && thing.is_a?(Array) && thing.size == 2 # Now that we're set with a thing and another thing, let LatLng do the # heavy lifting. # Exceptions may be thrown thing_ll = Geokit::LatLng.normalize(thing) other_ll = Geokit::LatLng.normalize(other) Bounds.new(thing_ll, other_ll) end |
Instance Method Details
#==(other) ⇒ Object
Returns true if the candidate object is logically equal. Logical equivalence is true if the lat and lng attributes are the same for both objects.
51 52 53 54 |
# File 'lib/geokit/bounds.rb', line 51 def ==(other) return false unless other.is_a?(Bounds) sw == other.sw && ne == other.ne end |
#center ⇒ Object
returns the a single point which is the center of the rectangular bounds
16 17 18 |
# File 'lib/geokit/bounds.rb', line 16 def center @sw.midpoint_to(@ne) end |
#contains?(point) ⇒ Boolean
Returns true if the bounds contain the passed point. allows for bounds which cross the meridian
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/geokit/bounds.rb', line 32 def contains?(point) point = Geokit::LatLng.normalize(point) res = point.lat > @sw.lat && point.lat < @ne.lat if crosses_meridian? res &= point.lng < @ne.lng || point.lng > @sw.lng else res &= point.lng < @ne.lng && point.lng > @sw.lng end res end |
#crosses_meridian? ⇒ Boolean
returns true if the bounds crosses the international dateline
44 45 46 |
# File 'lib/geokit/bounds.rb', line 44 def crosses_meridian? @sw.lng > @ne.lng end |
#to_a ⇒ Object
a two-element array of two-element arrays: sw,ne
26 27 28 |
# File 'lib/geokit/bounds.rb', line 26 def to_a [@sw.to_a, @ne.to_a] end |
#to_s ⇒ Object
a simple string representation:sw,ne
21 22 23 |
# File 'lib/geokit/bounds.rb', line 21 def to_s "#{@sw},#{@ne}" end |
#to_span ⇒ Object
Equivalent to Google Maps API’s .toSpan() method on GLatLng’s.
Returns a LatLng object, whose coordinates represent the size of a rectangle defined by these bounds.
60 61 62 63 64 |
# File 'lib/geokit/bounds.rb', line 60 def to_span lat_span = @ne.lat - @sw.lat lng_span = crosses_meridian? ? 360 + @ne.lng - @sw.lng : @ne.lng - @sw.lng Geokit::LatLng.new(lat_span.abs, lng_span.abs) end |