]> git.street.me.uk Git - andy/viking.git/blobdiff - src/viktrack.c
Make the track name a property of the track.
[andy/viking.git] / src / viktrack.c
index 2a02b1e48cc6b39ebba64fdfcc3d5f861349090b..ec13707f5f6a68de8981110ee7b388f472c05f55 100644 (file)
@@ -53,6 +53,17 @@ void vik_track_set_comment_no_copy(VikTrack *tr, gchar *comment)
 }
 
 
+void vik_track_set_name(VikTrack *tr, const gchar *name)
+{
+  if ( tr->name )
+    g_free ( tr->name );
+
+  if ( name && name[0] != '\0' )
+    tr->name = g_strdup(name);
+  else
+    tr->name = NULL;
+}
+
 void vik_track_set_comment(VikTrack *tr, const gchar *comment)
 {
   if ( tr->comment )
@@ -85,6 +96,8 @@ void vik_track_free(VikTrack *tr)
   if ( tr->ref_count-- > 1 )
     return;
 
+  if ( tr->name )
+    g_free ( tr->name );
   if ( tr->comment )
     g_free ( tr->comment );
   g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
@@ -109,6 +122,7 @@ VikTrack *vik_track_copy ( const VikTrack *tr )
     new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp );
     tp_iter = tp_iter->next;
   }
+  vik_track_set_name(new_tr,tr->name);
   vik_track_set_comment(new_tr,tr->comment);
   return new_tr;
 }
@@ -253,6 +267,9 @@ VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
       iter->prev->next = NULL;
       iter->prev = NULL;
       rv[i] = vik_track_new();
+      // TODO: consider new naming strategy here
+      if ( tr->name )
+        vik_track_set_name ( rv[i], tr->name );
       if ( tr->comment )
         vik_track_set_comment ( rv[i], tr->comment );
       rv[i]->visible = tr->visible;
@@ -309,6 +326,42 @@ gdouble vik_track_get_average_speed(const VikTrack *tr)
   return (time == 0) ? 0 : ABS(len/time);
 }
 
+/**
+ * Based on a simple average speed, but with a twist - to give a moving average.
+ *  . GPSs often report a moving average in their statistics output
+ *  . bicycle speedos often don't factor in time when stopped - hence reporting a moving average for speed
+ *
+ * Often GPS track will record every second but not when stationary
+ * This method doesn't use samples that differ over the specified time limit - effectively skipping that time chunk from the total time
+ *
+ * Suggest to use 60 seconds as the stop length (as the default used in the TrackWaypoint draw stops factor)
+ */
+gdouble vik_track_get_average_speed_moving (const VikTrack *tr, int stop_length_seconds)
+{
+  gdouble len = 0.0;
+  guint32 time = 0;
+  if ( tr->trackpoints )
+  {
+    GList *iter = tr->trackpoints->next;
+    while (iter)
+    {
+      if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
+          VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
+          (! VIK_TRACKPOINT(iter->data)->newsegment) )
+      {
+       if ( ( VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp ) < stop_length_seconds ) {
+         len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
+                                 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
+       
+         time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
+       }
+      }
+      iter = iter->next;
+    }
+  }
+  return (time == 0) ? 0 : ABS(len/time);
+}
+
 gdouble vik_track_get_max_speed(const VikTrack *tr)
 {
   gdouble maxspeed = 0.0, speed = 0.0;
@@ -412,7 +465,8 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
        **/
 
       if ( ignore_it )
-        pts[current_chunk] = VIK_DEFAULT_ALTITUDE;
+       // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero)
+        pts[current_chunk] = altitude1;
       else
         pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
 
@@ -445,7 +499,7 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
 
       /* final seg */
       dist_along_seg = chunk_length - current_dist;
-      if ( ignore_it || !iter->next ) {
+      if ( ignore_it || ( iter && !iter->next ) ) {
         pts[current_chunk] = current_area_under_curve / current_dist;
         if (!iter->next) {
           int i;
@@ -488,6 +542,43 @@ void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble
     *up = *down = VIK_DEFAULT_ALTITUDE;
 }
 
+gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks )
+{
+  gdouble *pts;
+  gdouble *altitudes;
+  gdouble total_length, chunk_length, current_gradient;
+  gdouble altitude1, altitude2;
+  guint16 current_chunk;
+
+  g_assert ( num_chunks < 16000 );
+
+  total_length = vik_track_get_length_including_gaps ( tr );
+  chunk_length = total_length / num_chunks;
+
+  /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
+  if (chunk_length <= 0) {
+    return NULL;
+  }
+
+  altitudes = vik_track_make_elevation_map (tr, num_chunks);
+  if (altitudes == NULL) {
+    return NULL;
+  }
+
+  current_gradient = 0.0;
+  pts = g_malloc ( sizeof(gdouble) * num_chunks );
+  for (current_chunk = 0; current_chunk < (num_chunks - 1); current_chunk++) {
+    altitude1 = altitudes[current_chunk];
+    altitude2 = altitudes[current_chunk + 1];
+    current_gradient = 100.0 * (altitude2 - altitude1) / chunk_length;
+
+    pts[current_chunk] = current_gradient;
+  }
+
+  pts[current_chunk] = current_gradient;
+
+  return pts;
+}
 
 /* by Alex Foobarian */
 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
@@ -1011,6 +1102,10 @@ void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
   }
   *(guint *)(b->data + intp) = ntp;
 
+  len = (tr->name) ? strlen(tr->name)+1 : 0;
+  g_byte_array_append(b, (guint8 *)&len, sizeof(len));
+  if (tr->name) g_byte_array_append(b, (guint8 *)tr->name, len);
+
   len = (tr->comment) ? strlen(tr->comment)+1 : 0; 
   g_byte_array_append(b, (guint8 *)&len, sizeof(len)); 
   if (tr->comment) g_byte_array_append(b, (guint8 *)tr->comment, len);
@@ -1042,6 +1137,13 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
     new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
   }
 
+  len = *(guint *)data;
+  data += sizeof(len);
+  if (len) {
+    new_tr->name = g_strdup((gchar *)data);
+  }
+  data += len;
+
   len = *(guint *)data;
   data += sizeof(len);
   if (len) {
@@ -1066,6 +1168,20 @@ void vik_track_apply_dem_data ( VikTrack *tr )
   }
 }
 
+/*
+ * Apply DEM data (if available) - to only the last trackpoint
+ */
+void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr )
+{
+  gint16 elev;
+  if ( tr->trackpoints ) {
+    /* As in vik_track_apply_dem_data above - use 'best' interpolation method */
+    elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->coord), VIK_DEM_INTERPOL_BEST );
+    if ( elev != VIK_DEM_INVALID_ELEVATION )
+      VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->altitude = elev;
+  }
+}
+
 /* appends t2 to t1, leaving t2 with no trackpoints */
 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
 {
@@ -1081,9 +1197,8 @@ void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
 }
 
 /* starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
- * this is indicative of magic scissors continued use. If there is no double point,
- * deletes all the trackpoints. Returns the new end of the track (or the start if
- * there are no double points
+ * If there is no double point, deletes all the trackpoints.
+ * Returns the new end of the track (or the start if there are no double points)
  */
 VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr )
 {