Class: GeoPoint

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
GeoCalc::PrettyPrint, Calc, CoreExtension, Shared
Defined in:
lib/geo_point.rb,
lib/geo_point/calc.rb,
lib/geo_point/shared.rb,
lib/geo_point/class_methods.rb,
lib/geo_point/core_extension.rb

Overview

Sample usage:

p1 = GeoPoint.new(51.5136, -0.0983)                                                      
p2 = GeoPoint.new(51.4778, -0.0015)                                                      
dist = p1.distance_to(p2)          # in km                                             
brng = p1.bearing_to(p2)           # in degrees clockwise from north                   
... etc
                                                                                            • -

Defined Under Namespace

Modules: Calc, ClassMethods, CoreExtension, Shared

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shared

#coord_mode=, #earth_radius_km=, #unit

Methods included from CoreExtension

#geo_point

Methods included from Calc

included

Constructor Details

#initialize(*args) ⇒ GeoPoint

Optional options

  • :radius - earth radius in km

  • :mode - coordinates mode, either :lng_lat or :lat_lng, otherwise uses global setting as per GeoPoint.coord_mode



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/geo_point.rb', line 30

def initialize *args
  options = args.is_a?(GeoPoint) ? {} : args.last_option
  earth_radius_km = options[:radius] if options[:radius]
  coord_mode = options[:mode] if options[:mode]
  
  case args.size
  when 1
    create_from_one args
  when 2
    create_from_two *args
  else
    raise "GeoPoint must be initialized with either one or to arguments defining the (latitude, longitude) coordinate on the map"
  end
end

Instance Attribute Details

#latObject Also known as: to_lat

Returns the value of attribute lat.



20
21
22
# File 'lib/geo_point.rb', line 20

def lat
  @lat
end

#lonObject

Returns the value of attribute lon.



20
21
22
# File 'lib/geo_point.rb', line 20

def lon
  @lon
end

Instance Method Details

#[](key) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/geo_point.rb', line 81

def [] key
  case key
  when Fixnum   
    raise ArgumentError, "Index must be 0 or 1" if !(0..1).cover?(key)
    to_a[key] 
  when String, Symbol
    send(key) if respond_to? key
  else
    raise ArgumentError, "Key must be a Fixnum (index) or a method name"  
  end    
end

#coord_modeObject



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

def coord_mode
  @coord_mode ||= GeoPoint.coord_mode
end

#earth_radius_kmObject



53
54
55
# File 'lib/geo_point.rb', line 53

def earth_radius_km  
  @earth_radius_km ||= GeoPoint.earth_radius_km  # default
end

#reverse_pointObject



101
102
103
# File 'lib/geo_point.rb', line 101

def reverse_point
  self.dup.reverse_point!
end

#reverse_point!Object



95
96
97
98
99
# File 'lib/geo_point.rb', line 95

def reverse_point!
  self.lat = lat * -1
  self.lng = lng * -1
  self    
end

#to_aObject



113
114
115
# File 'lib/geo_point.rb', line 113

def to_a
  send(:"to_#{coord_mode}")
end

#to_lat_lngObject



105
106
107
# File 'lib/geo_point.rb', line 105

def to_lat_lng
  [lat, lng]
end

#to_lng_latObject



109
110
111
# File 'lib/geo_point.rb', line 109

def to_lng_lat
  [lng, lat]
end