Class: GeoKit::Bounds

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_kit/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:

  • (ArguementError)


353
354
355
356
# File 'lib/geo_kit/mappable.rb', line 353

def initialize(sw,ne)
  raise ArguementError 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



350
351
352
# File 'lib/geo_kit/mappable.rb', line 350

def ne
  @ne
end

#swObject

sw and ne are LatLng objects



350
351
352
# File 'lib/geo_kit/mappable.rb', line 350

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



400
401
402
403
404
405
406
407
408
409
# File 'lib/geo_kit/mappable.rb', line 400

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 arguements 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



417
418
419
420
421
422
423
424
425
426
427
# File 'lib/geo_kit/mappable.rb', line 417

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.



393
394
395
# File 'lib/geo_kit/mappable.rb', line 393

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



359
360
361
# File 'lib/geo_kit/mappable.rb', line 359

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)


375
376
377
378
379
380
381
382
383
384
# File 'lib/geo_kit/mappable.rb', line 375

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)


387
388
389
# File 'lib/geo_kit/mappable.rb', line 387

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

#to_aObject

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



369
370
371
# File 'lib/geo_kit/mappable.rb', line 369

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

#to_sObject

a simple string representation:sw,ne



364
365
366
# File 'lib/geo_kit/mappable.rb', line 364

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