X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/215ebe59a7ee81d3e47b55cab4c3c613bb0ac901..3f31bec4a6927651bc562e8b21aa0c7354328e1b:/src/viktrack.c diff --git a/src/viktrack.c b/src/viktrack.c index 3a8a9396..be8d4786 100644 --- a/src/viktrack.c +++ b/src/viktrack.c @@ -272,6 +272,10 @@ gdouble vik_track_get_length_to_trackpoint (const VikTrack *tr, const VikTrackpo gdouble len = 0.0; if ( tr->trackpoints ) { + // Is it the very first track point? + if ( VIK_TRACKPOINT(tr->trackpoints->data) == tp ) + return len; + GList *iter = tr->trackpoints->next; while (iter) { @@ -282,7 +286,7 @@ gdouble vik_track_get_length_to_trackpoint (const VikTrack *tr, const VikTrackpo // Exit when we reach the desired point if ( tp1 == tp ) - break; + break; iter = iter->next; } @@ -551,6 +555,31 @@ void vik_track_reverse ( VikTrack *tr ) } } +/** + * vik_track_get_duration: + * @trk: The track + * + * Returns: The time in seconds that covers the whole track including gaps + * NB this may be negative particularly if the track has been reversed + */ +time_t vik_track_get_duration(const VikTrack *trk) +{ + time_t duration = 0; + if ( trk->trackpoints ) { + // Ensure times are available + if ( vik_track_get_tp_first(trk)->has_timestamp ) { + // Get trkpt only once - as using vik_track_get_tp_last() iterates whole track each time + VikTrackpoint *trkpt_last = vik_track_get_tp_last(trk); + if ( trkpt_last->has_timestamp ) { + time_t t1 = vik_track_get_tp_first(trk)->timestamp; + time_t t2 = trkpt_last->timestamp; + duration = t2 - t1; + } + } + } + return duration; +} + gdouble vik_track_get_average_speed(const VikTrack *tr) { gdouble len = 0.0; @@ -1738,11 +1767,13 @@ VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr ) while ( iter->prev ) { - if ( vik_coord_equals((VikCoord *)iter->data, (VikCoord *)iter->prev->data) ) { + VikCoord *cur_coord = &((VikTrackpoint*)iter->data)->coord; + VikCoord *prev_coord = &((VikTrackpoint*)iter->prev->data)->coord; + if ( vik_coord_equals(cur_coord, prev_coord) ) { GList *prev = iter->prev; rv = g_malloc(sizeof(VikCoord)); - *rv = *((VikCoord *) iter->data); + *rv = *cur_coord; /* truncate trackpoint list */ iter->prev = NULL; /* pretend it's the end */ @@ -1758,10 +1789,42 @@ VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr ) /* no double point found! */ rv = g_malloc(sizeof(VikCoord)); - *rv = *((VikCoord *) tr->trackpoints->data); + *rv = ((VikTrackpoint*) tr->trackpoints->data)->coord; g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL ); g_list_free( tr->trackpoints ); tr->trackpoints = NULL; return rv; } +/** + * Function to compare two tracks by their first timestamp + **/ +int vik_track_compare_timestamp (const void *x, const void *y) +{ + VikTrack *a = (VikTrack *)x; + VikTrack *b = (VikTrack *)y; + + VikTrackpoint *tpa = NULL; + VikTrackpoint *tpb = NULL; + + if ( a->trackpoints ) + tpa = VIK_TRACKPOINT(g_list_first(a->trackpoints)->data); + + if ( b->trackpoints ) + tpb = VIK_TRACKPOINT(g_list_first(b->trackpoints)->data); + + if ( tpa && tpb ) { + if ( tpa->timestamp < tpb->timestamp ) + return -1; + if ( tpa->timestamp > tpb->timestamp ) + return 1; + } + + if ( tpa && !tpb ) + return 1; + + if ( !tpa && tpb ) + return -1; + + return 0; +}