Class: Fdc::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/fdc/compiler.rb

Overview

Compile KML documents from a Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ Compiler

Create a new instance

Parameters:

  • parser (Fdc::Parser)

    A parser from which the KML document should be compiled.


15
16
17
# File 'lib/fdc/compiler.rb', line 15

def initialize(parser)
  @parser = parser
end

Instance Attribute Details

#kmlObject (readonly)

The compiled KML document


9
10
11
# File 'lib/fdc/compiler.rb', line 9

def kml
  @kml
end

#parserObject

Returns the value of attribute parser


10
11
12
# File 'lib/fdc/compiler.rb', line 10

def parser
  @parser
end

Instance Method Details

#compile(track_name, clamp = false, extrude = false, gps = false) ⇒ Object

Compile the KML document from the parsed IGC file.

Parameters:

  • track_name (String)

    The name of the KML track

  • clamp (Boolean) (defaults to: false)

    Whether the track should be clamped to the ground

  • extrude (Boolean) (defaults to: false)

    Whether the track should be extruded to the ground

  • gps (Boolean) (defaults to: false)

    Whether GPS altitude information should be used

Raises:

  • (RuntimeError)

    If the supplied parser is not ready


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fdc/compiler.rb', line 26

def compile(track_name, clamp=false, extrude=false, gps=false)
  
  raise RuntimeError, "Parser not ready" unless @parser.ready?
  
  # Build HTML for balloon description
  html = Builder::XmlMarkup.new(:indent => 2)
  html.div :style => "width: 250;" do
    html.p do
      unless @parser.a_record[3].nil? then 
        html.strong "Device:"
        html.dfn @parser.a_record[3].strip
        html.br 
      end
    end
    html.p do
      @parser.h_records.each do |h|
        if h.include? "PLT" and not h[2].strip.empty? then 
          html.strong "Pilot:"
          html.dfn h[2].strip
          html.br
        end
        if h.include? "CID" and not h[2].strip.empty? then 
          html.strong "Competition ID:"
          html.dfn h[2].strip
          html.br
        end
        if h.include? "GTY" and not h[2].strip.empty? then 
          html.strong "Glider:"
          html.dfn h[2].strip
          html.br
        end
        if h.include? "GID" and not h[2].strip.empty? then
          html.strong "Glider ID:"
          html.dfn h[2].strip
          html.br
        end
        if h.include? "CCL" and not h[2].strip.empty? then 
          html.strong "Competition class:"
          html.dfn h[2].strip
          html.br 
        end
        if h.include? "SIT" and not h[2].strip.empty? then 
          html.strong "Site:"
          html.dfn h[2].strip
          html.br
        end
      end
    
      html.strong "Date:"
      html.dfn @parser.date_record[3..5].join(".")
      html.br
    end
  
    # Manufacturer-dependent L records
    case @parser.a_record[1]
    when "XSX"
      @parser.l_records.each do |l|
        if matches = l[1].scan(/(\w*):(-?\d+.?\d+)/) then 
          html.p do
            matches.each do |match|
              case match[0]
              when "MC"
                html.strong "Max. climb:"
                html.dfn match[1] << " m/s"
                html.br
              when "MS"
                html.strong "Max. sink:"
                html.dfn match[1] << " m/s"
                html.br
              when "MSP"
                html.strong "Max. speed:"
                html.dfn match[1] << " km/h"
                html.br
              when "Dist"
                html.strong "Track distance:"
                html.dfn match[1] << " km"
                html.br
              end
            end
          end
        end
      end
    end
  
  end

  # Build KML
  xml = Builder::XmlMarkup.new(:indent => 2)
  xml.instruct!
  xml.kml "xmlns" => "http://www.opengis.net/kml/2.2", "xmlns:gx" => "http://www.google.com/kml/ext/2.2" do
    xml.Placemark {
      xml.name track_name
      xml.Snippet :maxLines => "2" do
        xml.text! snippet
      end
      xml.description do
        xml.cdata! html.target!
      end
      xml.Style do
        xml.IconStyle do
          xml.Icon do 
            xml.href "http://earth.google.com/images/kml-icons/track-directional/track-0.png"
          end
        end
        xml.LineStyle do
          xml.color "99ffac59"
          xml.width "4"
        end
      end
      xml.gx:Track do
      
        clamp ? xml.altitudeMode("clampToGround") : xml.altitudeMode("absolute")
        extrude ? xml.extrude("1") : xml.extrude("0")
      
        @parser.b_records.each do |b_record|
           time = DateTime.new(2000 + @parser.date_record[5].to_i, @parser.date_record[4].to_i, @parser.date_record[3].to_i, 
            b_record[1].to_i, b_record[2].to_i, b_record[3].to_i)
           xml.when time
        end
        @parser.b_records.each do |b_record|
          coords = Fdc::GeoLocation.to_dec(b_record[5], b_record[4])
          gps ? coords << b_record[8].to_f : coords << b_record[7].to_f
          xml.gx :coord, coords.join(" ")
        end
      end
    }
  end

  @kml = xml.target!
end