Class: GpxUtils::WaypointsExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/gpx_utils/waypoints_exporter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWaypointsExporter

Returns a new instance of WaypointsExporter.



10
11
12
13
14
# File 'lib/gpx_utils/waypoints_exporter.rb', line 10

def initialize
  @pois = Array.new
  @etrex_model = "eTrex 30"
  @check_min_threshold = 0.01
end

Instance Attribute Details

#check_min_thresholdObject

Returns the value of attribute check_min_threshold.



16
17
18
# File 'lib/gpx_utils/waypoints_exporter.rb', line 16

def check_min_threshold
  @check_min_threshold
end

#etrex_modelObject

Returns the value of attribute etrex_model.



17
18
19
# File 'lib/gpx_utils/waypoints_exporter.rb', line 17

def etrex_model
  @etrex_model
end

#poisObject (readonly)

Returns the value of attribute pois.



101
102
103
# File 'lib/gpx_utils/waypoints_exporter.rb', line 101

def pois
  @pois
end

Class Method Details

.symbolsObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gpx_utils/waypoints_exporter.rb', line 103

def self.symbols
  # http://freegeographytools.com/2008/garmin-gps-unit-waypoint-icons-table
  [
    "Flag, Blue",
    "Flag, Green",
    "Flag, Red",

    "Pin, Blue",
    "Pin, Green",
    "Pin, Red",

    "Block, Blue",
    "Block, Green",
    "Block, Red",

    "Summit",
    "Trail Head", # other trail parts, not summits
    "Lodging", # rooms

    "Ground Transportation" # all public ground transportation
  # ...

  ]
end

Instance Method Details

#add(lat, lon, name, cmt = nil, time = nil, ele = nil, sym = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gpx_utils/waypoints_exporter.rb', line 23

def add(lat, lon, name, cmt = nil, time = nil, ele = nil, sym = nil)
  @pois << {
    :lat => lat,
    :lon => lon,
    :name => name,
    :cmt => cmt,
    :time => time,
    :ele => ele,
    :sym => sym
  }
end

#add_yaml_file(y) ⇒ Object



19
20
21
# File 'lib/gpx_utils/waypoints_exporter.rb', line 19

def add_yaml_file(y)
  @pois += YAML::load_file(y)
end

#checkObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gpx_utils/waypoints_exporter.rb', line 35

def check
  puts "Distance conflicts:"

  sorted = @pois.sort { |p, q| p[:lat] <=> q[:lat] }
  (1...sorted.size).each do |i|
    l = (sorted[i-1][:lat] - sorted[i][:lat]) ** 2 +
      (sorted[i-1][:lon] - sorted[i][:lon]) ** 2
    l = Math.sqrt(l)

    if l < @check_min_threshold
      puts "* #{sorted[i-1][:name]} - #{sorted[i][:name]} = #{l}"
    end
  end
end

#process_time(time) ⇒ Object



128
129
130
# File 'lib/gpx_utils/waypoints_exporter.rb', line 128

def process_time(time)
  time.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
end

#to_xmlObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gpx_utils/waypoints_exporter.rb', line 50

def to_xml
  #xml = Builder::XmlMarkup.new(:indent => 2)
  string = ""
  xml = Builder::XmlMarkup.new(:target => string, :indent => 0)
  xml.instruct! :xml, :encoding => "UTF-8", :standalone => 'no'
  xml.gpx(
    'xmlns' => "http://www.topografix.com/GPX/1/1",
    'xmlns:gpxx' => "http://www.garmin.com/xmlschemas/GpxExtensions/v3",
    'xmlns:wptx1' => "http://www.garmin.com/xmlschemas/WaypointExtension/v1",
    'xmlns:gpxtpx' => "http://www.garmin.com/xmlschemas/TrackPointExtension/v1",
    'creator' => @etrex_model,
    'version' => "1.1",
    'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
    'xsi:schemaLocation' => "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd"


  ) do |g|
    g. do |meta|
      meta.link('href' => "http://www.garmin.com") do |link|
        link.text 'Garmin International'
      end
      meta.time process_time(Time.now) # 2012-03-24T15:41:34Z
    end

    # coords
    # <wpt lat="52.484444" lon="16.893056"><ele>113.286499</ele><time>2012-03-18T16:42:47Z</time><name>GORA MORASKO</name><cmt>DUZY</cmt><sym>Flag, Blue</sym></wpt>
    @pois.each do |poi|
      g.wpt('lat' => poi[:lat], 'lon' => poi[:lon]) do |wp|
        wp.ele poi[:elevation] unless poi[:elevation].nil?
        wp.ele poi[:ele] unless poi[:ele].nil?

        unless poi[:time].nil?
          wp.time process_time(poi[:time])
        else
          wp.time process_time(Time.now)
        end
        wp.name poi[:name]

        wp.cmt poi[:comment] unless poi[:comment].nil?
        wp.cmt poi[:cmt] unless poi[:cmt].nil?

        wp.sym poi[:sym] || "Flag, Blue" # default garmin symbol
      end
    end
  end

  #return string
  return string.gsub(/\n/, '').gsub(/\r/, '')

end