Class: GeoPoint

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
GeoCalc::PrettyPrint, Calc, 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 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]
  coord_mode = 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



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

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



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

def coord_mode
  @coord_mode ||= GeoPoint.coord_mode
end

#earth_radius_kmObject



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

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

#reverse_pointObject



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

def reverse_point
  self.dup.reverse_point!
end

#reverse_point!Object



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

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

#to_aObject



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

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

#to_lat_lngObject



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

def to_lat_lng
  [lat, lng]
end

#to_lng_latObject



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

def to_lng_lat
  [lng, lat]
end