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 an XML 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
50
# File 'lib/gpx/point.rb', line 34

def initialize(opts = {:lat => 0.0, :lon => 0.0, :elevation => 0.0, :time => Time.now } )
   @gpx_file = opts[:gpx_file]
   if (opts[:element]) 
      elem = opts[:element]
      @lat, @lon = elem["lat"].to_f, elem["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.find("gpx:time", @gpx_file.ns).first.content) rescue nil)
      @elevation = elem.find("gpx:ele", @gpx_file.ns).first.content.to_f unless elem.find("gpx:ele", @gpx_file.ns).empty?
   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).



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

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

#latrObject

Latitude in radians.



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

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



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

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

#lonrObject

Longitude in radians.



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

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

#to_xml(elem_name = 'trkpt') ⇒ Object

Convert this point to a XML::Node.



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

def to_xml(elem_name = 'trkpt')
   pt = Node.new('trkpt')
   pt['lat'] = lat.to_s
   pt['lon'] = lon.to_s
   unless time.nil?
      time_elem = Node.new('time')
      time_elem << time.xmlschema
      pt << time_elem
   end
   elev = Node.new('ele')
   elev << elevation
   pt <<  elev
   pt
end