1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2013 Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ***********************************************************
28 #include <glib/gprintf.h>
29 #include <glib/gi18n.h>
32 #include "viktrwlayer_analysis.h"
35 // Units of each item are in SI Units
36 // (as returned by the appropriate internal viking track functions)
53 // Early incarnations of the code had facilities to print output for multiple files
54 // but has been rescoped to work on a single list of tracks for the GUI
60 static track_stats tracks_stats[1];
62 // cf with vik_track_get_minmax_alt internals
63 #define VIK_VAL_MIN_ALT 25000.0
64 #define VIK_VAL_MAX_ALT -5000.0
67 * Reset the specified block
68 * Call this when starting to processing multiple items
70 static void val_reset ( track_stat_block block )
72 tracks_stats[block].min_alt = VIK_VAL_MIN_ALT;
73 tracks_stats[block].max_alt = VIK_VAL_MAX_ALT;
74 tracks_stats[block].elev_gain = 0.0;
75 tracks_stats[block].elev_loss = 0.0;
76 tracks_stats[block].length = 0.0;
77 tracks_stats[block].length_gaps = 0.0;
78 tracks_stats[block].max_speed = 0.0;
79 tracks_stats[block].trackpoints = 0;
80 tracks_stats[block].segments = 0;
81 tracks_stats[block].duration = 0;
82 tracks_stats[block].start_time = 0;
83 tracks_stats[block].end_time = 0;
84 tracks_stats[block].count = 0;
89 * @trk: The track to be analyse
91 * Function to collect statistics, using the internal track functions
93 static void val_analyse_track ( VikTrack *trk )
95 //val_reset ( TS_TRACK );
101 gdouble length = 0.0;
102 gdouble length_gaps = 0.0;
103 gdouble max_speed = 0.0;
104 gulong trackpoints = 0;
107 tracks_stats[TS_TRACKS].count++;
109 trackpoints = vik_track_get_tp_count (trk);
110 segments = vik_track_get_segment_count (trk);
111 length = vik_track_get_length (trk);
112 length_gaps = vik_track_get_length_including_gaps (trk);
113 max_speed = vik_track_get_max_speed (trk);
116 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
117 tracks_stats[ii].trackpoints += trackpoints;
118 tracks_stats[ii].segments += segments;
119 tracks_stats[ii].length += length;
120 tracks_stats[ii].length_gaps += length_gaps;
121 if ( max_speed > tracks_stats[ii].max_speed )
122 tracks_stats[ii].max_speed = max_speed;
125 if ( vik_track_get_minmax_alt (trk, &min_alt, &max_alt) ) {
126 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
127 if ( min_alt < tracks_stats[ii].min_alt )
128 tracks_stats[ii].min_alt = min_alt;
129 if ( max_alt > tracks_stats[ii].max_alt )
130 tracks_stats[ii].max_alt = max_alt;
134 vik_track_get_total_elevation_gain (trk, &up, &down );
136 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
137 tracks_stats[ii].elev_gain += up;
138 tracks_stats[ii].elev_loss += down;
141 if ( trk->trackpoints && VIK_TRACKPOINT(trk->trackpoints->data)->timestamp ) {
143 t1 = VIK_TRACKPOINT(g_list_first(trk->trackpoints)->data)->timestamp;
144 t2 = VIK_TRACKPOINT(g_list_last(trk->trackpoints)->data)->timestamp;
146 // Assume never actually have a track with a time of 0 (1st Jan 1970)
147 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
148 if ( tracks_stats[ii].start_time == 0)
149 tracks_stats[ii].start_time = t1;
150 if ( tracks_stats[ii].end_time == 0)
151 tracks_stats[ii].end_time = t2;
154 // Initialize to the first value
155 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
156 if (t1 < tracks_stats[ii].start_time)
157 tracks_stats[ii].start_time = t1;
158 if (t2 > tracks_stats[ii].end_time)
159 tracks_stats[ii].end_time = t2;
162 for (ii = 0; ii < G_N_ELEMENTS(tracks_stats); ii++) {
163 tracks_stats[ii].duration = tracks_stats[ii].duration + (int)(t2-t1);
168 // Could use GtkGrids but that is Gtk3+
169 static GtkWidget *create_table (int cnt, char *labels[], GtkWidget *contents[])
174 table = GTK_TABLE(gtk_table_new (cnt, 2, FALSE));
175 gtk_table_set_col_spacing (table, 0, 10);
176 for (i=0; i<cnt; i++) {
178 label = gtk_label_new(NULL);
179 gtk_misc_set_alignment ( GTK_MISC(label), 1, 0.5 ); // Position text centrally in vertical plane
180 // All text labels are set to be in bold
181 char *markup = g_markup_printf_escaped ("<b>%s:</b>", _(labels[i]) );
182 gtk_label_set_markup ( GTK_LABEL(label), markup );
184 gtk_table_attach ( table, label, 0, 1, i, i+1, GTK_FILL, GTK_EXPAND, 4, 2 );
185 if (GTK_IS_MISC(contents[i])) {
186 gtk_misc_set_alignment ( GTK_MISC(contents[i]), 0, 0.5 );
188 gtk_table_attach_defaults ( table, contents[i], 1, 2, i, i+1 );
190 return GTK_WIDGET (table);
193 static gchar *label_texts[] = {
194 N_("Number of Tracks"),
197 N_("Average Length"),
200 N_("Minimum Altitude"),
201 N_("Maximum Altitude"),
202 N_("Total Elevation Gain/Loss"),
203 N_("Avg. Elevation Gain/Loss"),
204 N_("Total Duration"),
211 * Returns a widget to hold the stats information in a table grid layout
213 static GtkWidget *create_layout ( GtkWidget *content[] )
216 for ( cnt = 0; cnt < G_N_ELEMENTS(label_texts); cnt++ )
217 content[cnt] = ui_label_new_selectable ( NULL );
219 return create_table (cnt, label_texts, content);
225 * Update the given widgets table with the values from the track stats
227 static void table_output ( track_stats ts, GtkWidget *content[] )
232 g_snprintf ( tmp_buf, sizeof(tmp_buf), "%d", ts.count );
233 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
235 if ( ts.count == 0 ) {
236 // Blank all other fields
237 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
238 for ( cnt = 1; cnt < G_N_ELEMENTS(label_texts); cnt++ )
239 gtk_label_set_text ( GTK_LABEL(content[cnt]), tmp_buf );
243 // Check for potential date range
244 // Test if the same day by comparing the date string of the timestamp
245 GDate* gdate_start = g_date_new ();
246 g_date_set_time_t ( gdate_start, ts.start_time );
247 gchar time_start[32];
248 g_date_strftime ( time_start, sizeof(time_start), "%x", gdate_start );
249 g_date_free ( gdate_start );
251 GDate* gdate_end = g_date_new ();
252 g_date_set_time_t ( gdate_end, ts.end_time );
254 g_date_strftime ( time_end, sizeof(time_end), "%x", gdate_end );
255 g_date_free ( gdate_end );
257 if ( ts.start_time == ts.end_time )
258 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("No Data") );
259 else if ( strncmp(time_start, time_end, 32) )
260 g_snprintf ( tmp_buf, sizeof(tmp_buf), "%s --> %s", time_start, time_end );
262 g_snprintf ( tmp_buf, sizeof(tmp_buf), "%s", time_start );
264 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
266 switch (a_vik_get_units_distance ()) {
267 case VIK_UNITS_DISTANCE_MILES:
268 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.1f miles"), VIK_METERS_TO_MILES(ts.length) );
271 //VIK_UNITS_DISTANCE_KILOMETRES
272 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.1f km"), ts.length/1000.0 );
275 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
277 switch (a_vik_get_units_distance ()) {
278 case VIK_UNITS_DISTANCE_MILES:
279 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f miles"), (VIK_METERS_TO_MILES(ts.length)/ts.count) );
282 //VIK_UNITS_DISTANCE_KILOMETRES
283 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f km"), ts.length/(1000.0*ts.count) );
286 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
288 // I'm sure this could be cleaner...
289 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
290 switch (a_vik_get_units_speed()) {
291 case VIK_UNITS_SPEED_MILES_PER_HOUR:
292 if ( ts.max_speed > 0 )
293 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.1f mph"), (double)VIK_MPS_TO_MPH(ts.max_speed) );
294 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
295 if ( ts.duration > 0 )
296 g_snprintf ( tmp_buf, sizeof(tmp_buf), ("%.1f mph"), (double)VIK_MPS_TO_MPH(ts.length/ts.duration) );
298 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
300 case VIK_UNITS_SPEED_METRES_PER_SECOND:
301 if ( ts.max_speed > 0 )
302 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f m/s"), (double)ts.max_speed );
303 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
304 if ( ts.duration > 0 )
305 g_snprintf ( tmp_buf, sizeof(tmp_buf), ("%.2f m/s"), (double)(ts.length/ts.duration) );
307 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
309 case VIK_UNITS_SPEED_KNOTS:
310 if ( ts.max_speed > 0 )
311 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f knots\n"), (double)VIK_MPS_TO_KNOTS(ts.max_speed) );
312 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
313 if ( ts.duration > 0 )
314 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f knots"), (double)VIK_MPS_TO_KNOTS(ts.length/ts.duration) );
316 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
319 //VIK_UNITS_SPEED_KILOMETRES_PER_HOUR:
320 if ( ts.max_speed > 0 )
321 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f km/h"), (double)VIK_MPS_TO_KPH(ts.max_speed) );
322 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
323 if ( ts.duration > 0 )
324 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%.2f km/h"), (double)VIK_MPS_TO_KPH(ts.length/ts.duration) );
326 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
329 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
331 switch ( a_vik_get_units_height() ) {
332 // Note always round off height value output since sub unit accuracy is overkill
333 case VIK_UNITS_HEIGHT_FEET:
334 if ( ts.min_alt != VIK_VAL_MIN_ALT )
335 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet"), (int)round(VIK_METERS_TO_FEET(ts.min_alt)) );
337 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
338 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
340 if ( ts.max_alt != VIK_VAL_MAX_ALT )
341 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet"), (int)round(VIK_METERS_TO_FEET(ts.max_alt)) );
343 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
344 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
346 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet / %d feet"), (int)round(VIK_METERS_TO_FEET(ts.elev_gain)), (int)round(VIK_METERS_TO_FEET(ts.elev_loss)) );
347 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
348 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d feet / %d feet"), (int)round(VIK_METERS_TO_FEET(ts.elev_gain/ts.count)), (int)round(VIK_METERS_TO_FEET(ts.elev_loss/ts.count)) );
351 //VIK_UNITS_HEIGHT_METRES
352 if ( ts.min_alt != VIK_VAL_MIN_ALT )
353 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m"), (int)round(ts.min_alt) );
355 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
356 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
358 if ( ts.max_alt != VIK_VAL_MAX_ALT )
359 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m"), (int)round(ts.max_alt) );
361 g_snprintf ( tmp_buf, sizeof(tmp_buf), "--" );
362 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
364 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m / %d m"), (int)round(ts.elev_gain), (int)round(ts.elev_loss) );
365 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
366 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d m / %d m"), (int)round(ts.elev_gain/ts.count), (int)round(ts.elev_loss/ts.count) );
369 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
375 days = (gint)(ts.duration / (60*60*24));
376 hours = (gint)floor((ts.duration - (days*60*60*24)) / (60*60));
377 minutes = (gint)((ts.duration - (days*60*60*24) - (hours*60*60)) / 60);
378 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d:%02d:%02d days:hrs:mins"), days, hours, minutes );
379 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
382 gint avg_dur = ts.duration / ts.count;
383 hours = (gint)floor(avg_dur / (60*60));
384 minutes = (gint)((avg_dur - (hours*60*60)) / 60);
385 g_snprintf ( tmp_buf, sizeof(tmp_buf), _("%d:%02d hrs:mins"), hours, minutes );
386 gtk_label_set_text ( GTK_LABEL(content[cnt++]), tmp_buf );
390 * val_analyse_item_maybe:
391 * @vtlist: A track and the associated layer to consider for analysis
392 * @data: Whether to include invisible items
394 * Analyse this particular track
395 * considering whether it should be included depending on it's visibility
397 static void val_analyse_item_maybe ( vik_trw_track_list_t *vtlist, const gpointer data )
399 gboolean include_invisible = GPOINTER_TO_INT(data);
400 VikTrack *trk = vtlist->trk;
401 VikTrwLayer *vtl = vtlist->vtl;
403 // Safety first - items shouldn't be deleted...
404 if ( !IS_VIK_TRW_LAYER(vtl) ) return;
407 if ( !include_invisible ) {
408 // Skip invisible layers or sublayers
409 if ( !VIK_LAYER(vtl)->visible ||
410 (trk->is_route && !vik_trw_layer_get_routes_visibility(vtl)) ||
411 (!trk->is_route && !vik_trw_layer_get_tracks_visibility(vtl)) )
414 // Skip invisible tracks
419 val_analyse_track ( trk );
424 * @widgets: The widget layout
425 * @tracks_and_layers: A list of #vik_trw_track_list_t
426 * @include_invisible: Whether to include invisible layers and tracks
428 * Analyse each item in the @tracks_and_layers list
431 void val_analyse ( GtkWidget *widgets[], GList *tracks_and_layers, gboolean include_invisible )
433 val_reset ( TS_TRACKS );
435 GList *gl = g_list_first ( tracks_and_layers );
437 g_list_foreach ( gl, (GFunc) val_analyse_item_maybe, GINT_TO_POINTER(include_invisible) );
440 table_output ( tracks_stats[TS_TRACKS], widgets );
446 GtkWidget *check_button;
447 GList *tracks_and_layers;
450 VikTrwlayerGetTracksAndLayersFunc get_tracks_and_layers_cb;
451 VikTrwlayerAnalyseCloseFunc on_close_cb;
454 static void include_invisible_toggled_cb ( GtkToggleButton *togglebutton, analyse_cb_t *acb )
456 gboolean value = FALSE;
457 if ( gtk_toggle_button_get_active ( togglebutton ) )
460 // Delete old list of items
461 if ( acb->tracks_and_layers ) {
462 g_list_foreach ( acb->tracks_and_layers, (GFunc) g_free, NULL );
463 g_list_free ( acb->tracks_and_layers );
466 // Get the latest list of items to analyse
467 acb->tracks_and_layers = acb->get_tracks_and_layers_cb ( acb->vl, acb->user_data );
469 val_analyse ( acb->widgets, acb->tracks_and_layers, value );
470 gtk_widget_show_all ( acb->layout );
473 #define VIK_SETTINGS_ANALYSIS_DO_INVISIBLE "track_analysis_do_invisible"
478 * Multi stage closure - as we need to clear allocations made here
479 * before passing on to the callee so they know then the dialog is closed too
481 static void analyse_close ( GtkWidget *dialog, gint resp, analyse_cb_t *data )
483 // Save current invisible value for next time
484 gboolean do_invisible = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON(data->check_button) );
485 a_settings_set_boolean ( VIK_SETTINGS_ANALYSIS_DO_INVISIBLE, do_invisible );
487 //g_free ( data->layout );
488 g_free ( data->widgets );
489 g_list_foreach ( data->tracks_and_layers, (GFunc) g_free, NULL );
490 g_list_free ( data->tracks_and_layers );
492 if ( data->on_close_cb )
493 data->on_close_cb ( dialog, resp, data->vl );
499 * vik_trw_layer_analyse_this:
500 * @window: A window from which the dialog will be derived
501 * @name: The name to be shown
502 * @vl: The #VikLayer passed on into get_tracks_and_layers_cb()
503 * @user_data: Data passed on into get_tracks_and_layers_cb()
504 * @get_tracks_and_layers_cb: The function to call to construct items to be analysed
506 * Display a dialog with stats across many tracks
508 * Returns: The dialog that is created to display the analyse information
510 GtkWidget* vik_trw_layer_analyse_this ( GtkWindow *window,
514 VikTrwlayerGetTracksAndLayersFunc get_tracks_and_layers_cb,
515 VikTrwlayerAnalyseCloseFunc on_close_cb )
517 //VikWindow *vw = VIK_WINDOW(window);
520 dialog = gtk_dialog_new_with_buttons ( _("Statistics"),
522 GTK_DIALOG_DESTROY_WITH_PARENT,
523 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL,
526 GtkWidget *name_l = gtk_label_new ( NULL );
527 gchar *myname = g_markup_printf_escaped ( "<b>%s</b>", name );
528 gtk_label_set_markup ( GTK_LABEL(name_l), myname );
531 GtkWidget *content = gtk_dialog_get_content_area ( GTK_DIALOG(dialog) );
532 gtk_box_pack_start ( GTK_BOX(content), name_l, FALSE, FALSE, 10);
534 // Get previous value (if any) from the settings
535 gboolean include_invisible;
536 if ( ! a_settings_get_boolean ( VIK_SETTINGS_ANALYSIS_DO_INVISIBLE, &include_invisible ) )
537 include_invisible = TRUE;
539 analyse_cb_t *acb = g_malloc (sizeof(analyse_cb_t));
541 acb->user_data = user_data;
542 acb->get_tracks_and_layers_cb = get_tracks_and_layers_cb;
543 acb->on_close_cb = on_close_cb;
544 acb->tracks_and_layers = get_tracks_and_layers_cb ( vl, user_data );
545 acb->widgets = g_malloc ( sizeof(GtkWidget*) * G_N_ELEMENTS(label_texts) );
546 acb->layout = create_layout ( acb->widgets );
548 gtk_box_pack_start ( GTK_BOX(content), acb->layout, FALSE, FALSE, 0 );
550 // Analysis seems reasonably quick
551 // unless you have really large numbers of tracks (i.e. many many thousands or a really slow computer)
552 // One day might store stats in the track itself....
553 val_analyse ( acb->widgets, acb->tracks_and_layers, include_invisible );
555 GtkWidget *cb = gtk_check_button_new_with_label ( _("Include Invisible Items") );
556 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON(cb), include_invisible );
557 gtk_box_pack_start ( GTK_BOX(content), cb, FALSE, FALSE, 10);
558 acb->check_button = cb;
560 gtk_widget_show_all ( dialog );
562 g_signal_connect ( G_OBJECT(cb), "toggled", G_CALLBACK(include_invisible_toggled_cb), acb );
563 g_signal_connect ( G_OBJECT(dialog), "response", G_CALLBACK(analyse_close), acb );