]> git.street.me.uk Git - andy/viking.git/blobdiff - src/viktrack.c
[QA] Shift Gtk dependent utility functions into separate file.
[andy/viking.git] / src / viktrack.c
index 04e75f0260515d69e485ff0592e60a7086f117ac..2c25f061dc669fb0ac70719f13f3baa6cfb2e209 100644 (file)
@@ -258,8 +258,12 @@ static void track_recalculate_bounds_last_tp ( VikTrack *trk )
  */
 void vik_track_add_trackpoint ( VikTrack *tr, VikTrackpoint *tp, gboolean recalculate )
 {
+  // When it's the first trackpoint need to ensure the bounding box is initialized correctly
+  gboolean adding_first_point = tr->trackpoints ? FALSE : TRUE;
   tr->trackpoints = g_list_append ( tr->trackpoints, tp );
-  if ( recalculate )
+  if ( adding_first_point )
+    vik_track_calculate_bounds ( tr );
+  else if ( recalculate )
     track_recalculate_bounds_last_tp ( tr );
 }
 
@@ -272,6 +276,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 +290,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 +559,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 +1771,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,7 +1793,7 @@ 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;