Class: GpxRuby::Gpx::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/gpx_ruby/gpx/point.rb

Direct Known Subclasses

Track::Point

Constant Summary collapse

RADIUS_KM =
6371

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_hash) ⇒ Point

Returns a new instance of Point.



11
12
13
14
# File 'lib/gpx_ruby/gpx/point.rb', line 11

def initialize(a_hash)
  @lat = a_hash[:lat]
  @lon = a_hash[:lon]
end

Instance Attribute Details

#latObject

Returns the value of attribute lat.



7
8
9
# File 'lib/gpx_ruby/gpx/point.rb', line 7

def lat
  @lat
end

#lonObject

Returns the value of attribute lon.



7
8
9
# File 'lib/gpx_ruby/gpx/point.rb', line 7

def lon
  @lon
end

Instance Method Details

#+(a_point) ⇒ Object



28
29
30
31
32
# File 'lib/gpx_ruby/gpx/point.rb', line 28

def +(a_point)
  nlat = lat + a_point.lat
  nlon = lon + a_point.lon
  Point.new lat: nlat, lon: nlon
end

#/(a_scalar) ⇒ Object



34
35
36
37
38
# File 'lib/gpx_ruby/gpx/point.rb', line 34

def /(a_scalar)
  nlat = lat / a_scalar.to_f
  nlon = lon / a_scalar.to_f
  Point.new lat: nlat, lon: nlon
end

#[](index) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/gpx_ruby/gpx/point.rb', line 17

def [](index)
  case index
    when 0
      lat
    when 1
      lon
    else
      raise 'Invalid index!'
  end
end

#distance(a_point) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gpx_ruby/gpx/point.rb', line 55

def distance(a_point)
  p_point = self
  rad_per_deg, radius_meter = Math::PI/180, RADIUS_KM * 1000

  dlon_rad = (a_point.lon-p_point.lon) * rad_per_deg
  dlat_rad = (a_point.lat-p_point.lat) * rad_per_deg

  lat1_rad = p_point.lat * rad_per_deg
  lat2_rad = a_point.lat * rad_per_deg

  p_point = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)**2
  c = 2 * Math::atan2(Math::sqrt(p_point), Math::sqrt(1-p_point))

  radius_meter * c
end

#to_aObject



44
45
46
# File 'lib/gpx_ruby/gpx/point.rb', line 44

def to_a
  [lat, lon]
end

#to_hashObject Also known as: to_h



48
49
50
# File 'lib/gpx_ruby/gpx/point.rb', line 48

def to_hash
  { lat: lat, lon: lon }
end

#to_sObject



40
41
42
# File 'lib/gpx_ruby/gpx/point.rb', line 40

def to_s
  "lat: #{lat}, lon: #{lon}"
end