Class: GoobyGoogleKmlGenerator

Inherits:
GoobyBaseObject show all
Includes:
GoobyConfiguration
Defined in:
lib/gooby_google_kml_generator.rb

Overview

Gooby = Google APIs + Ruby. Copyright 2009 by Chris Joakim. Gooby is available under GNU General Public License (GPL) license.

Constant Summary

Constants inherited from GoobyBaseObject

GoobyBaseObject::KILOMETERS_PER_MILE, GoobyBaseObject::METERS_PER_FOOT, GoobyBaseObject::MILES_PER_KILOMETER, GoobyBaseObject::SECONDS_PER_HOUR, GoobyBaseObject::UOM_KILOMETERS, GoobyBaseObject::UOM_MILES, GoobyBaseObject::UOM_YARDS, GoobyBaseObject::YARDS_PER_KILOMETER, GoobyBaseObject::YARDS_PER_MILE

Instance Method Summary collapse

Methods included from GoobyConfiguration

#gooby_home, #read_configuration

Methods inherited from GoobyBaseObject

boolean_config_value, config_value, get_config, set_config

Methods included from GoobyIO

#read_file_as_lines, #write_file, #write_lines

Methods included from GoobyIntrospection

#classname, included

Methods included from GoobyEnvironment

#array_param, #boolean_param, #command_line_arg, #float_param, #integer_param, #string_param

Constructor Details

#initializeGoobyGoogleKmlGenerator

Returns a new instance of GoobyGoogleKmlGenerator.



12
13
14
# File 'lib/gooby_google_kml_generator.rb', line 12

def initialize
 @trackpoints = []
end

Instance Method Details

#collect_coordinatesObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gooby_google_kml_generator.rb', line 93

def collect_coordinates
  tkpt_count = @trackpoints.size.to_f
  last_idx   = tkpt_count - 1
  next_idx   = 0
  app_max    = @configuration['approx_max_points'].to_f
  ratio = tkpt_count / app_max
  if ratio > 1.0
    increment = (tkpt_count / app_max).to_i
  else
    increment = 1
  end
  @coordinates = ''
  @trackpoints.each_with_index { | tkpt, idx |
    if (idx == 0) || (idx == last_idx) || (idx == next_idx)
      @coordinates << "          #{tkpt.longitude},#{tkpt.latitude},1\n" 
      next_idx = idx + increment
    end
  }
end

#collect_placemarksObject



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
# File 'lib/gooby_google_kml_generator.rb', line 62

def collect_placemarks
  generate_mile_markers = @configuration['mile_markers']    
  @placemarks, next_checkpoint_f, next_checkpoint_i = [], 0.0, 0
  first_tkpt, last_index = nil, @trackpoints.size - 1
  @trackpoints.each_with_index { | tkpt, index |
    if index == 0
      first_tkpt = tkpt
    end
    if (tkpt.distance >= next_checkpoint_f) || (index == 0) || (index == last_index)
      if next_checkpoint_i == 0
        tkpt.set('PlacemarkName', 'Start')
        tkpt.set('PlacemarkIcon', 'images/dd-start.png')
      elsif index == last_index
        tkpt.set('PlacemarkName', 'Finish')
        tkpt.set('PlacemarkIcon', 'images/dd-end.png') 
        next_checkpoint_i = 999999
      else
        if generate_mile_markers
          tkpt.set('PlacemarkName', '') 
          tkpt.set('PlacemarkIcon', "images/marker#{next_checkpoint_i.to_s}.png")
        end
      end
      tkpt.set('PlacemarkDescription', tkpt.as_info_window_html(next_checkpoint_i, first_tkpt.dttm))
      tkpt.set('PlacemarkCoordinates', "#{tkpt.longitude},#{tkpt.latitude},1")
      @placemarks << tkpt
      next_checkpoint_i = next_checkpoint_i + 1
      next_checkpoint_f = next_checkpoint_f + 1.0
    end
  }
end

#generate(config_file) ⇒ Object



16
17
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
# File 'lib/gooby_google_kml_generator.rb', line 16

def generate(config_file)
  read_configuration(config_file)
  read_csv_file 
  collect_placemarks
  collect_coordinates     
  template = ERB.new(kml_document_template, 0, "<>")
  result   = template.result(binding)
  kmz_build = @configuration['kmz_build']
  kmz_build_command = @configuration['kmz_build_command']
  if kmz_build && kmz_build_command
    out_file = 'out/doc.kml'
    out = File.new out_file, "w+"
    out.write result
    out.flush
    out.close
    puts "file created: #{out_file}"
    puts "executing kmz build command: #{kmz_build_command}"
    `#{kmz_build_command}`
    puts "kmz build completed"
  else
    out_file = @configuration['kml_file'] ||= 'kml/gooby_google_earth_map.kml'    
    out = File.new out_file, "w+"
    out.write result
    out.flush
    out.close
    puts "file created: #{out_file}"
  end
end

#kml_document_templateObject



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
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/gooby_google_kml_generator.rb', line 113

def kml_document_template
  template = <<HEREDOC
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
  <!-- kml file generated by Gooby version 2.0.0 -->
  <name><%= @configuration['map_title'] %></name>
  <Style id="goobyStyle">
    <BalloonStyle> 
      <text>$[description]</text> 
    </BalloonStyle>
    <LineStyle>
      <color>7f0000ff</color>
      <width>4</width>
    </LineStyle>
    <PolyStyle>
      <color>7f0000ff</color>
    </PolyStyle>
  </Style>
  <% @placemarks.each do | tkpt | %>
  <Placemark>
    <name><%= tkpt.get('PlacemarkName') %></name>
    <description><%= tkpt.get('PlacemarkDescription') %></description>
    <Style>
      <IconStyle>
        <Icon>
          <href><%= tkpt.get('PlacemarkIcon') %></href>
        </Icon>
      </IconStyle>
    </Style>      
    <styleUrl>#goobyStyle</styleUrl>      
    <Point>
      <coordinates><%= tkpt.get('PlacemarkCoordinates') %></coordinates>
    </Point>
  </Placemark>
  <% end %>
  <Placemark>
    <name>Route</name>
    <visibility>1</visibility>
    <description>Route</description>
    <styleUrl>#goobyStyle</styleUrl>
    <LineString>
      <tessellate>1</tessellate>
      <altitudeMode>relativeToGround</altitudeMode>
      <coordinates>
<%= @coordinates %>
      </coordinates>
    </LineString>
  </Placemark>
</Document>
</kml>
HEREDOC
  template
end

#read_csv_fileObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gooby_google_kml_generator.rb', line 45

def read_csv_file
  csv_file    = @configuration['csv_file']
  first_point = @configuration['first_point'].to_i
  last_point  = @configuration['last_point'].to_i
  puts "reading csv_file #{csv_file}  first_point: #{first_point}  last_point: #{last_point}"
  row_number  = 0
  FasterCSV.foreach(csv_file) do | row |
    row_number = row_number + 1
    tkpt = GoobyTrackpoint.new
    tkpt.from_csv(row)
    if (row_number >= first_point) && (row_number <= last_point)
      @trackpoints << tkpt
    end 
  end
  puts "csv_file #{csv_file} has been read; #{@trackpoints.size} trackpoints included"
end