diff options
-rw-r--r-- | lib/postrunner/ActivitiesDB.rb | 17 | ||||
-rw-r--r-- | lib/postrunner/TrackView.rb | 162 |
2 files changed, 43 insertions, 136 deletions
diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb index 9264f55..608a0e4 100644 --- a/lib/postrunner/ActivitiesDB.rb +++ b/lib/postrunner/ActivitiesDB.rb @@ -31,7 +31,7 @@ module PostRunner @cfg = cfg @fit_dir = File.join(@db_dir, 'fit') @archive_file = File.join(@db_dir, 'archive.yml') - @auxilliary_dirs = %w( icons jquery flot openlayers ) + @auxilliary_dirs = %w( icons jquery flot openlayers postrunner ) create_directories begin @@ -352,14 +352,13 @@ module PostRunner unless Dir.exists?(src_dir) Log.fatal "Cannot find '#{src_dir}': #{$!}" end - dst_dir = File.join(@cfg[:html_dir], dir) - unless File.exists?(dst_dir) - begin - #FileUtils.ln_s(src_dir, dst_dir) - FileUtils.cp_r(src_dir, dst_dir) - rescue IOError - Log.fatal "Cannot create auxilliary directory '#{dst_dir}': #{$!}" - end + dst_dir = @cfg[:html_dir] + + begin + #FileUtils.ln_s(src_dir, dst_dir) + FileUtils.cp_r(src_dir, dst_dir) + rescue IOError + Log.fatal "Cannot copy auxilliary data directory '#{dst_dir}': #{$!}" end end diff --git a/lib/postrunner/TrackView.rb b/lib/postrunner/TrackView.rb index fb902d4..56f97c6 100644 --- a/lib/postrunner/TrackView.rb +++ b/lib/postrunner/TrackView.rb @@ -16,14 +16,20 @@ require 'postrunner/ViewFrame' module PostRunner + # A TrackView object uses OpenLayers to draw a map with the waypoints of the + # activity. Lap end are marked with enumerated lap markers. class TrackView + # Create a new TrackView object for a given Activity. + # @param activity [Activity] The activity to render def initialize(activity) @activity = activity @session = @activity.fit_activity.sessions[0] @has_geo_data = @session.has_geo_data? end + # Render the map widget with the track and marker overlay as HTML code. + # @param doc [HTMLBuilder] HTML document to add to. def to_html(doc) return unless @has_geo_data @@ -34,6 +40,7 @@ module PostRunner 'href' => 'openlayers/ol.css', 'type' => 'text/css' }) doc.script({ 'src' => 'openlayers/ol.js' }) + doc.script({ 'src' => 'postrunner/trackview.js' }) } doc.script(java_script) } @@ -60,147 +67,48 @@ EOT end def java_script - js = <<EOT -function init() { -EOT - center_long = @session.swc_long + (@session.nec_long - @session.swc_long) / 2.0 center_lat = @session.swc_lat + (@session.nec_lat - @session.swc_lat) / 2.0 - track_points(js) - lap_markers(js) - js << <<"EOT" - lm_w = 40; - lm_h = 100; - var map = new ol.Map({ - view: new ol.View({ - center: ol.proj.transform([ #{center_long}, #{center_lat} ], - 'EPSG:4326', 'EPSG:900913'), - zoom: 14, - }), - layers: [ - new ol.layer.Tile({ - source: new ol.source.MapQuest({layer: 'osm'}) - }), - new ol.layer.Vector({ - source: source_vector = new ol.source.Vector({ - features: [ - new ol.Feature({ - geometry: new ol.geom.LineString(track_points) - }) - ] - }), - style: [ - new ol.style.Style({ - stroke: new ol.style.Stroke({ - color: 'red', - width: 5 - }), - fill: new ol.style.Fill({ - color: 'white' - }) - }) - ] - }), - new ol.layer.Vector({ - source: lap_marker_source = new ol.source.Vector(), - style: function(feature, resolution) { - return [ - new ol.style.Style({ - stroke: new ol.style.Stroke({ - color: 'black', - width: 2 - }), - fill: new ol.style.Fill({ - color: 'red' - }), - text: new ol.style.Text({ - font: '' + (lm_w / resolution) + 'px helvetica,sans-serif', - text: resolution < (lm_w / 8.0) ? feature.get('name') : '', - fill: new ol.style.Fill({ - color: 'black' - }) - }) - }) - ]; - } - }) - ], - target: "map" - }); - for (var i in lap_markers) { - x = lap_markers[i][0]; - y = lap_markers[i][1]; - lap_marker_source.addFeature( - new ol.Feature({ - geometry: new ol.geom.Polygon([[[ x - lm_w, y + lm_h ], - [ x, y ], - [ x + lm_w, y + lm_h ], - [ x - lm_w, y + lm_h ]]]) - }) - ); - lap_marker_source.addFeature( - new ol.Feature({ - geometry: new ol.geom.Circle([ x, y + lm_h + 3 ], lm_w), - name: (i == 0 ? 'S' : i == lap_markers.length - 1 ? 'F' : i), - }) - ); - }; -}; + <<"EOT" +#{track_points} +#{lap_markers} +function init() { + pr_trackview_init(#{center_long}, #{center_lat}); +} EOT - - js end - def track_points(script) - first = true - script << "var track_points = [\n" - - @activity.fit_activity.sessions.each do |session| - session.records.each do |record| + # Generate a javascript variable with an Array with the coordinates of the + # track points. Each coordinate is an Array with a longitude and latitude + # in EPSG:4326. Generate a javascript variable with an Array of track + # points. + def track_points + "var pr_track_points = [\n" + + @activity.fit_activity.sessions.map do |session| + session.records.map do |record| long = record.position_long lat = record.position_lat next unless long && lat - - if first - first = false - else - script << ', ' - end - script << "[ #{long}, #{lat} ]" - end - end - script << <<"EOT" -]; -track_points.forEach( - function(coords, i, arr) { - arr[i] = ol.proj.transform(coords, 'EPSG:4326', 'EPSG:900913'); - } -); -EOT + "[ #{long}, #{lat} ]" + end.join(', ') + end.join(', ') + + "\n];\n" end - def lap_markers(script) - script << "var lap_markers = [\n" + + # Generate a javascript variable with an Array with the coordinates of the + # start point and the lap end points. Each coordinate is an Array with a + # longitude and latitude in EPSG:4326. + def lap_markers + "var pr_lap_markers = [\n" + "[ #{@session.start_position_long}," + - " #{@session.start_position_lat} ], " - @activity.fit_activity.laps.each do |lap| - script << "[ #{lap.end_position_long}, #{lap.end_position_lat} ], " - end - last_lap = @activity.fit_activity.laps[-1] - script << "[ #{last_lap.end_position_long}, - #{last_lap.end_position_lat} ]" - script << <<"EOT" -]; -lap_markers.forEach( - function(coords, i, arr) { - arr[i] = ol.proj.transform(coords, 'EPSG:4326', 'EPSG:900913'); - } -); -EOT - + " #{@session.start_position_lat} ], " + + @activity.fit_activity.laps.map do |lap| + "[ #{lap.end_position_long}, #{lap.end_position_lat} ]" + end.join(', ') + + "\n];\n" end end |