Class: Geokit::Bounds

Inherits:
Object
  • Object
show all
Defined in:
lib/geokit/mappable.rb

Overview

Bounds represents a rectangular bounds, defined by the SW and NE corners

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sw, ne) ⇒ Bounds

provide sw and ne to instantiate a new Bounds instance

Raises:

  • (ArgumentError)


443
444
445
446
# File 'lib/geokit/mappable.rb', line 443

def initialize(sw,ne)
  raise ArgumentError if !(sw.is_a?(Geokit::LatLng) && ne.is_a?(Geokit::LatLng))
  @sw,@ne=sw,ne
end

Instance Attribute Details

#neObject

sw and ne are LatLng objects



440
441
442
# File 'lib/geokit/mappable.rb', line 440

def ne
  @ne
end

#swObject

sw and ne are LatLng objects



440
441
442
# File 'lib/geokit/mappable.rb', line 440

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



500
501
502
503
504
505
506
507
508
509
# File 'lib/geokit/mappable.rb', line 500

def from_point_and_radius(point,radius,options={})
  point=LatLng.normalize(point)
  p0=point.endpoint(0,radius,options)
  p90=point.endpoint(90,radius,options)
  p180=point.endpoint(180,radius,options)
  p270=point.endpoint(270,radius,options)
  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



517
518
519
520
521
522
523
524
525
526
527
# File 'lib/geokit/mappable.rb', line 517

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
  Bounds.new(Geokit::LatLng.normalize(thing),Geokit::LatLng.normalize(other))
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.



483
484
485
# File 'lib/geokit/mappable.rb', line 483

def ==(other)
  other.is_a?(Bounds) ? self.sw == other.sw && self.ne == other.ne : false
end

#centerObject

returns the a single point which is the center of the rectangular bounds



449
450
451
# File 'lib/geokit/mappable.rb', line 449

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

Returns:

  • (Boolean)


465
466
467
468
469
470
471
472
473
474
# File 'lib/geokit/mappable.rb', line 465

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

Returns:

  • (Boolean)


477
478
479
# File 'lib/geokit/mappable.rb', line 477

def crosses_meridian?
  @sw.lng > @ne.lng 
end

#to_aObject

a two-element array of two-element arrays: sw,ne



459
460
461
# File 'lib/geokit/mappable.rb', line 459

def to_a
  [@sw.to_a, @ne.to_a]
end

#to_sObject

a simple string representation:sw,ne



454
455
456
# File 'lib/geokit/mappable.rb', line 454

def to_s
  "#{@sw.to_s},#{@ne.to_s}"   
end

#to_spanObject

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.



491
492
493
494
495
# File 'lib/geokit/mappable.rb', line 491

def to_span
  lat_span = (@ne.lat - @sw.lat).abs
  lng_span = (crosses_meridian? ? 360 + @ne.lng - @sw.lng : @ne.lng - @sw.lng).abs
  Geokit::LatLng.new(lat_span, lng_span)
end