]> git.street.me.uk Git - andy/viking.git/blobdiff - src/viktrack.c
Open files in selected layer
[andy/viking.git] / src / viktrack.c
index 3ac95a98f56ce8833aa3f82d867ff1fac5b48baf..0a5dd73bc9931a23f82ad72ad7ef90bfb87e51cd 100644 (file)
@@ -105,6 +105,28 @@ void vik_track_set_description(VikTrack *tr, const gchar *description)
     tr->description = NULL;
 }
 
+void vik_track_set_source(VikTrack *tr, const gchar *source)
+{
+  if ( tr->source )
+    g_free ( tr->source );
+
+  if ( source && source[0] != '\0' )
+    tr->source = g_strdup(source);
+  else
+    tr->source = NULL;
+}
+
+void vik_track_set_type(VikTrack *tr, const gchar *type)
+{
+  if ( tr->type )
+    g_free ( tr->type );
+
+  if ( type && type[0] != '\0' )
+    tr->type = g_strdup(type);
+  else
+    tr->type = NULL;
+}
+
 void vik_track_ref(VikTrack *tr)
 {
   tr->ref_count++;
@@ -132,7 +154,11 @@ void vik_track_free(VikTrack *tr)
     g_free ( tr->comment );
   if ( tr->description )
     g_free ( tr->description );
-  g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
+  if ( tr->source )
+    g_free ( tr->source );
+  if ( tr->type )
+    g_free ( tr->type );
+  g_list_foreach ( tr->trackpoints, (GFunc) vik_trackpoint_free, NULL );
   g_list_free( tr->trackpoints );
   if (tr->property_dialog)
     if ( GTK_IS_WIDGET(tr->property_dialog) )
@@ -153,8 +179,6 @@ void vik_track_free(VikTrack *tr)
 VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points )
 {
   VikTrack *new_tr = vik_track_new();
-  VikTrackpoint *new_tp;
-  GList *tp_iter = tr->trackpoints;
   new_tr->name = g_strdup(tr->name);
   new_tr->visible = tr->visible;
   new_tr->is_route = tr->is_route;
@@ -166,17 +190,20 @@ VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points )
   new_tr->trackpoints = NULL;
   if ( copy_points )
   {
+    GList *tp_iter = tr->trackpoints;
     while ( tp_iter )
     {
-      new_tp = g_malloc ( sizeof ( VikTrackpoint ) );
-      *new_tp = *((VikTrackpoint *)(tp_iter->data));
-      new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp );
+      VikTrackpoint *new_tp = vik_trackpoint_copy ( (VikTrackpoint*)(tp_iter->data) );
+      new_tr->trackpoints = g_list_prepend ( new_tr->trackpoints, new_tp );
       tp_iter = tp_iter->next;
     }
+    if ( new_tr->trackpoints )
+      new_tr->trackpoints = g_list_reverse ( new_tr->trackpoints );
   }
   vik_track_set_name(new_tr,tr->name);
   vik_track_set_comment(new_tr,tr->comment);
   vik_track_set_description(new_tr,tr->description);
+  vik_track_set_source(new_tr,tr->source);
   return new_tr;
 }
 
@@ -214,9 +241,11 @@ void vik_trackpoint_set_name(VikTrackpoint *tp, const gchar *name)
 
 VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
 {
-  VikTrackpoint *rv = vik_trackpoint_new();
-  *rv = *tp;
-  return rv;
+  VikTrackpoint *new_tp = vik_trackpoint_new();
+  memcpy ( new_tp, tp, sizeof(VikTrackpoint) );
+  if ( tp->name )
+    new_tp->name = g_strdup (tp->name);
+  return new_tp;
 }
 
 /**
@@ -256,8 +285,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 );
 }
 
@@ -270,6 +303,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)
     {
@@ -280,7 +317,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;
     }
@@ -549,6 +586,47 @@ void vik_track_reverse ( VikTrack *tr )
   }
 }
 
+/**
+ * vik_track_get_duration:
+ * @trk: The track
+ * @segment_gaps: Whether the duration should include gaps between segments
+ *
+ * Returns: The time in seconds
+ *  NB this may be negative particularly if the track has been reversed
+ */
+time_t vik_track_get_duration(const VikTrack *trk, gboolean segment_gaps)
+{
+  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
+      if (segment_gaps) {
+        // Simple duration
+        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;
+        }
+      }
+      else {
+        // Total within segments
+        GList *iter = trk->trackpoints->next;
+        while (iter) {
+          if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
+               VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
+              (!VIK_TRACKPOINT(iter->data)->newsegment) ) {
+            duration += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
+          }
+          iter = iter->next;
+        }
+      }
+    }
+  }
+  return duration;
+}
+
 gdouble vik_track_get_average_speed(const VikTrack *tr)
 {
   gdouble len = 0.0;
@@ -1230,7 +1308,7 @@ VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdoub
     }
     /* we've gone past the dist already, was prev trackpoint closer? */
     /* should do a vik_coord_average_weighted() thingy. */
-    if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) {
+    if ( iter->prev && fabs(current_dist-current_inc-dist) < fabs(current_dist-dist) ) {
       if (meters_from_start)
         *meters_from_start = last_dist;
       iter = iter->prev;
@@ -1247,6 +1325,9 @@ VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdoub
 
 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
 {
+  if ( !tr->trackpoints )
+    return NULL;
+
   time_t t_pos, t_start, t_end, t_total;
   t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
   t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
@@ -1254,9 +1335,6 @@ VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdoub
 
   t_pos = t_start + t_total * reltime;
 
-  if ( !tr->trackpoints )
-    return NULL;
-
   GList *iter = tr->trackpoints;
 
   while (iter) {
@@ -1361,6 +1439,43 @@ VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
   return min_alt_tp;
 }
 
+VikTrackpoint *vik_track_get_tp_first( const VikTrack *tr )
+{
+  if ( !tr->trackpoints )
+    return NULL;
+
+  return (VikTrackpoint*)g_list_first(tr->trackpoints)->data;
+}
+
+VikTrackpoint *vik_track_get_tp_last ( const VikTrack *tr )
+{
+  if ( !tr->trackpoints )
+    return NULL;
+
+  return (VikTrackpoint*)g_list_last(tr->trackpoints)->data;
+}
+
+VikTrackpoint *vik_track_get_tp_prev ( const VikTrack *tr, VikTrackpoint *tp )
+{
+  if ( !tr->trackpoints )
+    return NULL;
+
+  GList *iter = tr->trackpoints;
+  VikTrackpoint *tp_prev = NULL;
+
+  while (iter) {
+    if (iter->prev) {
+      if ( VIK_TRACKPOINT(iter->data) == tp ) {
+        tp_prev = VIK_TRACKPOINT(iter->prev->data);
+        break;
+      }
+    }
+    iter = iter->next;
+  }
+
+  return tp_prev;
+}
+
 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
 {
   *min_alt = 25000;
@@ -1395,25 +1510,27 @@ void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
   intp = b->len;
   g_byte_array_append(b, (guint8 *)&len, sizeof(len));
 
+  // This allocates space for variant sized strings
+  //  and copies that amount of data from the track to byte array
+#define vtm_append(s) \
+  len = (s) ? strlen(s)+1 : 0; \
+  g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
+  if (s) g_byte_array_append(b, (guint8 *)s, len);
+
   tps = tr->trackpoints;
   ntp = 0;
   while (tps) {
     g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
+    vtm_append(VIK_TRACKPOINT(tps->data)->name);
     tps = tps->next;
     ntp++;
   }
   *(guint *)(b->data + intp) = ntp;
 
-  // This allocates space for variant sized strings
-  //  and copies that amount of data from the track to byte array
-#define vtm_append(s) \
-  len = (s) ? strlen(s)+1 : 0; \
-  g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
-  if (s) g_byte_array_append(b, (guint8 *)s, len);
-
   vtm_append(tr->name);
   vtm_append(tr->comment);
   vtm_append(tr->description);
+  vtm_append(tr->source);
 
   *data = b->data;
   *datalen = b->len;
@@ -1445,13 +1562,6 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
   ntp = *(guint *)data;
   data += sizeof(ntp);
 
-  for (i=0; i<ntp; i++) {
-    new_tp = vik_trackpoint_new();
-    memcpy(new_tp, data, sizeof(*new_tp));
-    data += sizeof(*new_tp);
-    new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
-  }
-
 #define vtu_get(s) \
   len = *(guint *)data; \
   data += sizeof(len); \
@@ -1462,9 +1572,20 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
   } \
   data += len;
 
+  for (i=0; i<ntp; i++) {
+    new_tp = vik_trackpoint_new();
+    memcpy(new_tp, data, sizeof(*new_tp));
+    data += sizeof(*new_tp);
+    vtu_get(new_tp->name);
+    new_tr->trackpoints = g_list_prepend(new_tr->trackpoints, new_tp);
+  }
+  if ( new_tr->trackpoints )
+    new_tr->trackpoints = g_list_reverse(new_tr->trackpoints);
+
   vtu_get(new_tr->name);
   vtu_get(new_tr->comment);
   vtu_get(new_tr->description);
+  vtu_get(new_tr->source);
 
   return new_tr;
 }
@@ -1516,7 +1637,11 @@ void vik_track_calculate_bounds ( VikTrack *trk )
 void vik_track_anonymize_times ( VikTrack *tr )
 {
   GTimeVal gtv;
-  g_time_val_from_iso8601 ( "1901-01-01T00:00:00Z", &gtv );
+  // Check result just to please Coverity - even though it shouldn't fail as it's a hard coded value here!
+  if ( !g_time_val_from_iso8601 ( "1901-01-01T00:00:00Z", &gtv ) ) {
+    g_critical ( "Calendar time value failure" );
+    return;
+  }
 
   time_t anon_timestamp = gtv.tv_sec;
   time_t offset = 0;
@@ -1538,6 +1663,55 @@ void vik_track_anonymize_times ( VikTrack *tr )
   }
 }
 
+/**
+ * vik_track_interpolate_times:
+ *
+ * Interpolate the timestamps between first and last trackpoint,
+ * so that the track is driven at equal speed, regardless of the
+ * distance between individual trackpoints.
+ *
+ * NB This will overwrite any existing trackpoint timestamps
+ */
+void vik_track_interpolate_times ( VikTrack *tr )
+{
+  gdouble tr_dist, cur_dist;
+  time_t tsdiff, tsfirst;
+
+  GList *iter;
+  iter = tr->trackpoints;
+
+  VikTrackpoint *tp = VIK_TRACKPOINT(iter->data);
+  if ( tp->has_timestamp ) {
+    tsfirst = tp->timestamp;
+
+    // Find the end of the track and the last timestamp
+    while ( iter->next ) {
+      iter = iter->next;
+    }
+    tp = VIK_TRACKPOINT(iter->data);
+    if ( tp->has_timestamp ) {
+      tsdiff = tp->timestamp - tsfirst;
+
+      tr_dist = vik_track_get_length_including_gaps ( tr );
+      cur_dist = 0.0;
+
+      if ( tr_dist > 0 ) {
+        iter = tr->trackpoints;
+        // Apply the calculated timestamp to all trackpoints except the first and last ones
+        while ( iter->next && iter->next->next ) {
+          iter = iter->next;
+          tp = VIK_TRACKPOINT(iter->data);
+          cur_dist += vik_coord_diff ( &(tp->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) );
+
+          tp->timestamp = (cur_dist / tr_dist) * tsdiff + tsfirst;
+          tp->has_timestamp = TRUE;
+        }
+        // Some points may now have the same time so remove them.
+        vik_track_remove_same_time_points ( tr );
+      }
+    }
+  }
+}
 
 /**
  * vik_track_apply_dem_data:
@@ -1716,11 +1890,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 */
@@ -1736,10 +1912,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;
+}