Class: Fdc::Converter

Inherits:
Object
  • Object
show all
Includes:
FileLoader, FileWriter
Defined in:
lib/fdc/converter.rb

Overview

Convert IGC Files to KML

Examples:

converter = Converter.new
converter.parse(path/to/file.igc)
converter.compile
converter.export(output/dir)

Instance Attribute Summary collapse

Attributes included from FileLoader

#encoding, #file, #path

Instance Method Summary collapse

Constructor Details

#initializeConverter

Returns a new instance of Converter.



24
25
26
27
# File 'lib/fdc/converter.rb', line 24

def initialize
  @parser = Fdc::Parser.new
  @compiler = Fdc::Compiler.new @parser
end

Instance Attribute Details

#kmlString (readonly)

Get the compiled KML document

Returns:

  • (String)

    The compiled KML document



18
19
20
21
22
23
24
25
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
# File 'lib/fdc/converter.rb', line 18

class Fdc::Converter

  # Mixins
  include Fdc::FileLoader
  include Fdc::FileWriter
  
  def initialize
    @parser = Fdc::Parser.new
    @compiler = Fdc::Compiler.new @parser
  end

  # Load and parse an IGC file from the supplied path.
  # 
  # @param [String] file The path to the IGC file
  # @param [String] encoding The encoding of the input file
  # @raise [Fdc::FileReadError] If file could not be loaded
  # @raise [Fdc::FileFormatError] If the file format is invalid
  def parse(file, encoding="ISO-8859-1")
    
    path = Pathname.new(file)
    load(path, encoding)
    @parser.parse @file
  
  end

  # Compile the KML document from the parsed IGC file.
  # 
  # @param [Boolean] clamp Whether the track should be clamped to the ground
  # @param [Boolean] extrude Whether the track should be extruded to the ground
  # @param [Boolean] gps Whether GPS altitude information should be used
  # @raise [RuntimeError] If {#parse} was not called before
  def compile(clamp=false, extrude=false, gps=false)
  
    # State assertion
    raise RuntimeError, "Cannot compile without preceding parse" unless @parser.ready?
    
    name = @path.basename(@path.extname)
    @compiler.compile name, clamp, extrude, gps
  
  end

  # Export the compiled KML document
  # 
  # @param [String] dir The alternative output directory. 
  #   If nothing is supplied the files are written to the same location 
  #   as the IGC input file.
  # @raise [RuntimeError] If {#parse} and {#compile} were not called before
  # @raise [Fdc::FileWriteError] If dirname is not a directory or write protected
  def export(dir = nil)
  
    # Assert state
    raise RuntimeError, "Cannot export before compile was called" unless @compiler.kml
  
    dir = @path.dirname.to_s unless dir
    write Pathname.new(dir) + (@path.basename(@path.extname).to_s << ".kml"), @compiler.kml
  
  end
  
  # Get the compiled KML document
  # @return [String] The compiled KML document
  def kml
    @compiler.kml
  end

end

Instance Method Details

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

Compile the KML document from the parsed IGC file.

Parameters:

  • 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 #parse was not called before



49
50
51
52
53
54
55
56
57
# File 'lib/fdc/converter.rb', line 49

def compile(clamp=false, extrude=false, gps=false)

  # State assertion
  raise RuntimeError, "Cannot compile without preceding parse" unless @parser.ready?
  
  name = @path.basename(@path.extname)
  @compiler.compile name, clamp, extrude, gps

end

#export(dir = nil) ⇒ Object

Export the compiled KML document

Parameters:

  • dir (String) (defaults to: nil)

    The alternative output directory. If nothing is supplied the files are written to the same location as the IGC input file.

Raises:



66
67
68
69
70
71
72
73
74
# File 'lib/fdc/converter.rb', line 66

def export(dir = nil)

  # Assert state
  raise RuntimeError, "Cannot export before compile was called" unless @compiler.kml

  dir = @path.dirname.to_s unless dir
  write Pathname.new(dir) + (@path.basename(@path.extname).to_s << ".kml"), @compiler.kml

end

#parse(file, encoding = "ISO-8859-1") ⇒ Object

Load and parse an IGC file from the supplied path.

Parameters:

  • file (String)

    The path to the IGC file

  • encoding (String) (defaults to: "ISO-8859-1")

    The encoding of the input file

Raises:



35
36
37
38
39
40
41
# File 'lib/fdc/converter.rb', line 35

def parse(file, encoding="ISO-8859-1")
  
  path = Pathname.new(file)
  load(path, encoding)
  @parser.parse @file

end