Class: GeoPoint

Inherits:
Object
  • Object
show all
Includes:
GeoCalc, NumericCheckExt
Defined in:
lib/geo_calc/geo_point.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
                                                                                            • -

                                                                                            • -

Note that minimal error checking is performed in this example code!
                                                                                            • -

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NumericCheckExt

#check_numeric!, #is_numeric?

Methods included from GeoCalc

#bearing_to, #destination_point, #distance_to, #final_bearing_to, intersection, #midpoint_to, #rhumb_bearing_to, #rhumb_destination_point, #rhumb_distance_to, #to_lon, #to_s

Constructor Details

#initialize(*args) ⇒ GeoPoint

Returns a new instance of GeoPoint.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/geo_calc/geo_point.rb', line 27

def initialize *args
  rad = args.delete(args.size) if is_numeric?(args.last) && args.last.is_between?(6350, 6380)
  rad ||= 6371 # default
  case args.size
  when 1
    create_from_one *args, rad
  when 2
    create_from_two *args, rad
  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

Creates a point on the earth’s surface at the supplied latitude / longitude

Constructor

  • Numeric lat: latitude in numeric degrees

  • Numeric lon: longitude in numeric degrees

  • Numeric [rad=6371]: radius of earth if different value is required from standard 6,371km



24
25
26
# File 'lib/geo_calc/geo_point.rb', line 24

def lat
  @lat
end

#lonObject

Creates a point on the earth’s surface at the supplied latitude / longitude

Constructor

  • Numeric lat: latitude in numeric degrees

  • Numeric lon: longitude in numeric degrees

  • Numeric [rad=6371]: radius of earth if different value is required from standard 6,371km



24
25
26
# File 'lib/geo_calc/geo_point.rb', line 24

def lon
  @lon
end

#radiusObject

Returns the value of attribute radius.



25
26
27
# File 'lib/geo_calc/geo_point.rb', line 25

def radius
  @radius
end

Instance Method Details

#[](key) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/geo_calc/geo_point.rb', line 68

def [] key
  case key
  when Fixnum   
    raise ArgumentError, "Index must be 0 or 1" if !(0..1).cover?(key)
    to_arr[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

#normal_arr!Object



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

def normal_arr!
  @reverse_arr = false
end

#reverse_arr!Object



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

def reverse_arr!
  @reverse_arr = true
end

#reverse_arr?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/geo_calc/geo_point.rb', line 91

def reverse_arr?
  @reverse_arr
end

#to_arrObject



86
87
88
89
# File 'lib/geo_calc/geo_point.rb', line 86

def to_arr
  a = to_lat_lng
  reverse_arr? ? a.reverse : a
end

#to_lat_lngObject



82
83
84
# File 'lib/geo_calc/geo_point.rb', line 82

def to_lat_lng
  [lat, lng]
end

#unitObject



40
41
42
# File 'lib/geo_calc/geo_point.rb', line 40

def unit
  :degrees
end