Class: GoobyGoogleMapGenerator

Inherits:
GoobyBaseObject show all
Includes:
GoobyConfiguration
Defined in:
lib/gooby_google_map_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 Attribute Summary collapse

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

#initializeGoobyGoogleMapGenerator

Returns a new instance of GoobyGoogleMapGenerator.



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

def initialize
  @content_hash, @trackpoints = Hash.new(''), []
end

Instance Attribute Details

#alt_idxObject (readonly)

Returns the value of attribute alt_idx.



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

def alt_idx
  @alt_idx
end

#cdist_idxObject (readonly)

Returns the value of attribute cdist_idx.



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

def cdist_idx
  @cdist_idx
end

#center_latitudeObject (readonly)

Returns the value of attribute center_latitude.



13
14
15
# File 'lib/gooby_google_map_generator.rb', line 13

def center_latitude
  @center_latitude
end

#center_longitudeObject (readonly)

Returns the value of attribute center_longitude.



13
14
15
# File 'lib/gooby_google_map_generator.rb', line 13

def center_longitude
  @center_longitude
end

#content_hashObject (readonly)

Returns the value of attribute content_hash.



13
14
15
# File 'lib/gooby_google_map_generator.rb', line 13

def content_hash
  @content_hash
end

#csv_fileObject (readonly)

Returns the value of attribute csv_file.



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

def csv_file
  @csv_file
end

#csv_linesObject (readonly)

Returns the value of attribute csv_lines.



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

def csv_lines
  @csv_lines
end

#dttm_idxObject (readonly)

Returns the value of attribute dttm_idx.



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

def dttm_idx
  @dttm_idx
end

#gpoint_arrayObject (readonly)

Returns the value of attribute gpoint_array.



13
14
15
# File 'lib/gooby_google_map_generator.rb', line 13

def gpoint_array
  @gpoint_array
end

#lat_idxObject (readonly)

Returns the value of attribute lat_idx.



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

def lat_idx
  @lat_idx
end

#lng_idxObject (readonly)

Returns the value of attribute lng_idx.



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

def lng_idx
  @lng_idx
end

#notesObject (readonly)

Returns the value of attribute notes.



13
14
15
# File 'lib/gooby_google_map_generator.rb', line 13

def notes
  @notes
end

#num_idxObject (readonly)

Returns the value of attribute num_idx.



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

def num_idx
  @num_idx
end

#overlay_pointsObject (readonly)

Returns the value of attribute overlay_points.



13
14
15
# File 'lib/gooby_google_map_generator.rb', line 13

def overlay_points
  @overlay_points
end

#runObject (readonly)

Returns the value of attribute run.



13
14
15
# File 'lib/gooby_google_map_generator.rb', line 13

def run
  @run
end

#tkptsObject (readonly)

Returns the value of attribute tkpts.



13
14
15
# File 'lib/gooby_google_map_generator.rb', line 13

def tkpts
  @tkpts
end

Instance Method Details

#compute_center_pointObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gooby_google_map_generator.rb', line 82

def compute_center_point
  highLat, highLong, lowLat, lowLong = -999.0, -999.0, 999.0, 999.0
  @trackpoints.each { | tkpt |
    highLat  = tkpt.latitude  if tkpt.latitude  > highLat
    lowLat   = tkpt.latitude  if tkpt.latitude  < lowLat  
    highLong = tkpt.longitude if tkpt.longitude > highLong
    lowLong  = tkpt.longitude if tkpt.longitude < lowLong  
  }
  @center_longitude = (highLong + lowLong) / 2
  @center_latitude  = (highLat  + lowLat)  / 2
  @content_hash['center_longitude'] = @center_longitude
  @content_hash['center_latitude']  = @center_latitude
end

#generate(config_file) ⇒ Object

2009_01_01_14_45_00_tcx.xml



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

def generate(config_file)
  read_configuration(config_file)
  read_csv_file    
  compute_center_point
  generate_key_js
  generate_map_div
  generate_messages_div
  generate_main_js_start
  generate_main_js_route_overlay
  generate_main_js_checkpoint_overlays
  generate_main_js_map_clicked_listeners
  generate_main_js_end

  s = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>\n<meta name=\"description\" content=\"Google Map generated by Gooby version 2.0.0\">\n<meta name=\"keywords\" content=\"Google Map Generated by Gooby version 2.0.0\">\n<title> \#{@configuration['map_title']} </title>\n\#{@content_hash['key_js']}\n\#{@content_hash['main_js_start']}\n\#{@content_hash['main_js_route_overlay']}  \n\#{@content_hash['main_js_checkpoint_overlays']}  \n\#{@content_hash['main_js_map_clicked_listeners']}  \n\#{@content_hash['main_js_end']}\n</head>\n<body onload=\"load()\" onunload=\"GUnload()\">\n<center>\n<h3> \#{@configuration['map_title']} </h3>\n\#{@content_hash['map_div']}    \n\#{@content_hash['messages_div']}   \n</center>\n</body>\n</html>\n"
  out_file = @configuration['html_file'] ||= 'html/gooby_google_map.html'
  write_file(out_file, s)
  puts "file created: #{out_file}"
end

#generate_key_jsObject



96
97
98
99
100
101
102
103
# File 'lib/gooby_google_map_generator.rb', line 96

def generate_key_js              
  key  = @configuration['gmap_key']
  key.strip!
  s  = "<script src='http://maps.google.com/maps?file=api&v=2&key=" 
  s << key
  s << "' type='text/javascript'></script>"
  @content_hash['key_js'] = s
end

#generate_main_js_checkpoint_overlaysObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/gooby_google_map_generator.rb', line 224

def generate_main_js_checkpoint_overlays
  generate_mile_markers = @configuration['mile_markers']
  s =  "\n            // Create a base icon for all of our markers that specifies the "
  s << "\n            // shadow, icon dimensions, etc."
  s << "\n            var baseIcon = new GIcon();"
  s << "\n            baseIcon.shadow = \"#{icon_url_base}shadow50.png\";"
  s << "\n            baseIcon.iconSize = new GSize(20, 34);"
  s << "\n            baseIcon.shadowSize = new GSize(37, 34);"
  s << "\n            baseIcon.iconAnchor = new GPoint(9, 34);"
  s << "\n            baseIcon.infoWindowAnchor = new GPoint(9, 2);"
  s << "\n            baseIcon.infoShadowAnchor = new GPoint(18, 25);"
  s << "\n"
  curr_idx  = -1
  last_idx  = @trackpoints.size - 1
  next_checkpoint = 0.0
  @start_dttm = nil
  @trackpoints.each { | tkpt |
    curr_idx = curr_idx + 1
    if curr_idx == 0
      @start_dttm = tkpt.dttm 
      if @trackpoints.size == 1
        label = @configuration['gmap_1_point_map_info_window_label']
        info_window_html = tkpt.as_one_point_info_window_html(label)
      else
        info_window_html = tkpt.as_quoted_info_window_html(0, @start_dttm)
      end
      s << "\n            var iconStart = new GIcon(baseIcon); "
      s << "\n            iconStart.image = \"#{icon_url_base}dd-start.png\";"
      s << "\n            var pStart = new GPoint(#{tkpt.longitude}, #{tkpt.latitude});"
      s << "\n            var mStart = new GMarker(pStart, iconStart);"
      s << "\n            GEvent.addListener(mStart, \"click\", function() { "
      s << "\n                mStart.openInfoWindowHtml(#{info_window_html});"
      s << "\n            }); "
      s << "\n            map.addOverlay(mStart);"
      s << "\n " 
      next_checkpoint = 1.0      
    elsif curr_idx == last_idx
      info_window_html = tkpt.as_quoted_info_window_html(999999, @start_dttm)          
      s << "\n            var iconFinish = new GIcon(baseIcon); "
      s << "\n            iconFinish.image = \"#{icon_url_base}dd-end.png\";"
      s << "\n            var pFinish = new GPoint(#{tkpt.longitude}, #{tkpt.latitude});"
      s << "\n            var mFinish = new GMarker(pFinish, iconFinish);"
      s << "\n            GEvent.addListener(mFinish, \"click\", function() { "
      s << "\n                mFinish.openInfoWindowHtml(#{info_window_html});"
      s << "\n            }); "
      s << "\n            map.addOverlay(mFinish);"
      s << "\n " 
      next_checkpoint = 999999
    else
      if (tkpt.distance >= next_checkpoint)
        integer = next_checkpoint.to_i
        if generate_mile_markers
          info_window_html = tkpt.as_quoted_info_window_html("#{integer}", @start_dttm)              
          s << "\n            var icon#{integer} = new GIcon(baseIcon); "
          s << "\n            icon#{integer}.image = \"#{icon_url_base}marker#{integer}.png\";"
          s << "\n            var p#{integer} = new GPoint(#{tkpt.longitude}, #{tkpt.latitude});"
          s << "\n            var m#{integer} = new GMarker(p#{integer}, icon#{integer});"
          s << "\n            GEvent.addListener(m#{integer}, \"click\", function() { "
          s << "\n              m#{integer}.openInfoWindowHtml(#{info_window_html});"
          s << "\n            }); "
          s << "\n            map.addOverlay(m#{integer});"
          s << "\n " 
        end
        next_checkpoint = next_checkpoint + 1.0   
      end                         
    end
  }
  s << "\n"    
  @content_hash['main_js_checkpoint_overlays'] = s 
end

#generate_main_js_endObject



308
309
310
311
312
313
314
# File 'lib/gooby_google_map_generator.rb', line 308

def generate_main_js_end
  s =  "\n        } "
  s << "\n    } "
  s << "\n//]]> \n" 
  s << "\n</script>" 
  @content_hash['main_js_end'] = s
end

#generate_main_js_map_clicked_listenersObject



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/gooby_google_map_generator.rb', line 295

def generate_main_js_map_clicked_listeners
  s =  "\n"
  s << "\n            GEvent.addListener(map, \"click\", function() { "
  s << "\n                var center = map.getCenter(); \n"
  s << "\n                document.getElementById(\"messages\").innerHTML = 'click: ' + center.toString(); "
  s << "\n            });"
  s << "\n            GEvent.addListener(map, \"moveend\", function() { "
  s << "\n                var center = map.getCenter(); \n"
  s << "\n                document.getElementById(\"messages\").innerHTML = 'moveend: ' + center.toString(); "
  s << "\n            });"
  @content_hash['main_js_map_clicked_listeners'] = s 
end

#generate_main_js_route_overlayObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/gooby_google_map_generator.rb', line 189

def generate_main_js_route_overlay
  tkpt_count   = @trackpoints.size.to_f
  app_max      = @configuration['approx_max_points'].to_f 
  gen_comments = @configuration['gen_comments']  
  ratio        = tkpt_count / app_max
  @start_dttm  = nil
  if ratio > 1.0
    increment = (tkpt_count / app_max).to_i
  else
    increment = 1
  end
  curr_idx, next_idx, gpoint_count, last_idx = -1, 0, 0, @trackpoints.size - 1
  s = "          var points = new Array(); " 
  @trackpoints.each { |tkpt|
    curr_idx = curr_idx + 1
    if curr_idx == 0
      @start_dttm, @start_pos = tkpt.dttm, tkpt
    end
    if ((curr_idx == next_idx) || (curr_idx == last_idx) || (tkpt.split?))
      gpoint_count = gpoint_count + 1
      s << tkpt.as_glatlng(false, gen_comments, tkpt_count, curr_idx, @start_dttm)
      next_idx = curr_idx + increment
    else
      s << tkpt.as_glatlng(true, gen_comments, tkpt_count, curr_idx, @start_dttm)
    end
  }
  s << "\n"
  s << "\n            // app_max=#{app_max}  ratio=#{ratio}  increment=#{increment}"
  s << "\n"    
  s << "\n            var routePolyline = new GPolyline(points); "
  s << "\n            map.addOverlay(routePolyline); "
  @content_hash['main_js_route_overlay'] = s 
  @content_hash['main_js_route_overlay_increment'] = increment
end

#generate_main_js_startObject



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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/gooby_google_map_generator.rb', line 126

def generate_main_js_start
  id       = @configuration['map_element_id']  
  size     = @configuration['gmap_size_control']
  type     = @configuration['gmap_type']
  zoom     = @configuration['gmap_zoom_level']
  title    = @configuration["#{@csv_file}"]
  title    = '' if title == nil
  zoom_tab = @configuration['gmap_zoom_tab'] 
  if size
    if size == 'smallmap'
      size = 'GSmallMapControl'
    elsif size == 'smallzoom'
      size = 'GSmallMapControl'          
    else
      size = 'GLargeMapControl'          
    end          
  end
    
  if type
    if type == 'satellite'
      type = 'G_SATELLITE_MAP'
    elsif type == 'hybrid'
      type = 'G_HYBRID_MAP'
    else
      type = 'G_NORMAL_MAP'
    end
  else
    type = 'G_NORMAL_MAP'
  end          
  s  =  '<script type="text/javascript">'
  s << "\n"
  s << "//<![CDATA[ \n"
  s << "    function load() { \n"
  s << "        if (GBrowserIsCompatible()) { \n"
  s << '            var map = new GMap2(document.getElementById("'
  s << id
  s << '")); '
  s << "\n"

  if size
    s << '            map.addControl(new '
    s << size
    s << '());'
    s << "\n"
  end
  
  if type
    s << '            map.addControl(new GMapTypeControl());'
    s << "\n"
    # s << '            map.setMapType('
    # s << type
    # s << ');'
    s << "\n"
  end   
  s << "            var centerPoint = new GLatLng(#{@center_latitude}, #{@center_longitude}); // #{project_embedded_comment} \n"
  s << "            map.setCenter(centerPoint, #{zoom}); \n"         
  @content_hash['main_js_start'] = s 
end

#generate_map_divObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/gooby_google_map_generator.rb', line 105

def generate_map_div
  width  = @configuration['gmap_width']
  height = @configuration['gmap_height']
  id     = @configuration['map_element_id']
  s = '<div id="'
  s << id
  s << '" style="width: '
  s << width
  s << '; height: '
  s << height
  s << '"></div>'
  @content_hash['map_width']  = width  
  @content_hash['map_height'] = height          
  @content_hash['map_div']    = s            
end

#generate_messages_divObject



121
122
123
124
# File 'lib/gooby_google_map_generator.rb', line 121

def generate_messages_div
  s = "<div id=\"messages\"></div>"
  @content_hash['messages_div'] = s            
end

#icon_url_baseObject



316
317
318
# File 'lib/gooby_google_map_generator.rb', line 316

def icon_url_base
  @configuration['gmap_icon_url_base']
end

#project_embedded_commentObject



185
186
187
# File 'lib/gooby_google_map_generator.rb', line 185

def project_embedded_comment
  "Map generated by Gooby"
end

#read_csv_fileObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gooby_google_map_generator.rb', line 65

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