Class: GPX::Point

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

Overview

The base class for all points. Trackpoint and Waypoint both descend from this base class.

Direct Known Subclasses

TrackPoint, Waypoint

Constant Summary collapse

D_TO_R =
Math::PI / 180.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#instantiate_with_text_elements

Constructor Details

#initialize(opts = { lat: 0.0, lon: 0.0, elevation: 0.0, time: Time.now }) ⇒ Point

When you need to manipulate individual points, you can create a Point object with a latitude, a longitude, an elevation, and a time. In addition, you can pass an XML element to this initializer, and the relevant info will be parsed out.

[View source]

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gpx/point.rb', line 14

def initialize(opts = { lat: 0.0, lon: 0.0, elevation: 0.0, time: Time.now })
  super()
  @gpx_file = opts[:gpx_file]
  if opts[:element]
    elem = opts[:element]
    @lat = elem['lat'].to_f
    @lon = elem['lon'].to_f
    @latr = (D_TO_R * @lat)
    @lonr = (D_TO_R * @lon)
    # '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?
    @time = (
    begin
      Time.xmlschema(elem.at('time').inner_text)
    rescue StandardError
      nil
    end)
    @elevation = elem.at('ele').inner_text.to_f unless elem.at('ele').nil?
    @speed = elem.at('speed').inner_text.to_f unless elem.at('speed').nil?
    @extensions = elem.at('extensions') unless elem.at('extensions').nil?
  else
    @lat = opts[:lat]
    @lon = opts[:lon]
    @elevation = opts[:elevation]
    @time = opts[:time]
    @speed = opts[:speed]
    @extensions = opts[:extensions]
  end
end

Instance Attribute Details

#elevationObject

Returns the value of attribute elevation.


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

def elevation
  @elevation
end

#extensionsObject

Returns the value of attribute extensions.


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

def extensions
  @extensions
end

#gpx_fileObject

Returns the value of attribute gpx_file.


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

def gpx_file
  @gpx_file
end

#latObject

Returns the value of attribute lat.


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

def lat
  @lat
end

#lonObject

Returns the value of attribute lon.


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

def lon
  @lon
end

#speedObject

Returns the value of attribute speed.


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

def speed
  @speed
end

#timeObject

Returns the value of attribute time.


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

def time
  @time
end

Instance Method Details

#lat_lon(delim = ', ') ⇒ Object

Returns the latitude and longitude (in that order), separated by the given delimeter. This is useful for passing a point into another API (i.e. the Google Maps javascript API).

[View source]

46
47
48
# File 'lib/gpx/point.rb', line 46

def lat_lon(delim = ', ')
  "#{lat}#{delim}#{lon}"
end

#latrObject

Latitude in radians.

[View source]

58
59
60
# File 'lib/gpx/point.rb', line 58

def latr
  @latr ||= (@lat * D_TO_R)
end

#lon_lat(delim = ', ') ⇒ Object

Returns the longitude and latitude (in that order), separated by the given delimeter. This is useful for passing a point into another API (i.e. the Google Maps javascript API).

[View source]

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

def lon_lat(delim = ', ')
  "#{lon}#{delim}#{lat}"
end

#lonrObject

Longitude in radians.

[View source]

63
64
65
# File 'lib/gpx/point.rb', line 63

def lonr
  @lonr ||= (@lon * D_TO_R)
end