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 =
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 a REXML element to this initializer, and the relevant info will be parsed out.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gpx/point.rb', line 34

def initialize(opts = {:lat => 0.0, :lon => 0.0, :elevation => 0.0, :time => Time.now } )
   if (opts[:element]) 
      elem = opts[:element]
      @lat, @lon = elem.attributes["lat"].to_f, elem.attributes["lon"].to_f
      @latr, @lonr = (D_TO_R * @lat), (D_TO_R * @lon)
      #'-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?
      @time = (Time.xmlschema(elem.elements["time"].text) rescue nil)
      @elevation = elem.elements["ele"].text.to_f if elem.elements["ele"]
   else
      @lat = opts[:lat]
      @lon = opts[:lon]
      @elevation = opts[:elevation]
      @time = opts[:time]
   end

end

Instance Attribute Details

#elevationObject

Returns the value of attribute elevation.



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

def elevation
  @elevation
end

#latObject

Returns the value of attribute lat.



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

def lat
  @lat
end

#lonObject

Returns the value of attribute lon.



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

def lon
  @lon
end

#timeObject

Returns the value of attribute time.



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

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).



55
56
57
# File 'lib/gpx/point.rb', line 55

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

#latrObject

Latitude in radians.



67
68
69
# File 'lib/gpx/point.rb', line 67

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).



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

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

#lonrObject

Longitude in radians.



72
73
74
# File 'lib/gpx/point.rb', line 72

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

#to_xml(elem_name = 'trkpt') ⇒ Object

Convert this point to a REXML::Element.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gpx/point.rb', line 89

def to_xml(elem_name = 'trkpt')
   pt = Element.new('trkpt')
   pt.attributes['lat'] = lat
   pt.attributes['lon'] = lon
   time_elem = Element.new('time')
   time_elem.text = time.xmlschema
   pt.elements << time_elem
   elev = Element.new('ele')
   elev.text = elevation
   pt.elements << elev
   pt
end