}
+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 )
tr->comment = NULL;
}
+void vik_track_set_description(VikTrack *tr, const gchar *description)
+{
+ if ( tr->description )
+ g_free ( tr->description );
+
+ if ( description && description[0] != '\0' )
+ tr->description = g_strdup(description);
+ else
+ tr->description = NULL;
+}
+
void vik_track_ref(VikTrack *tr)
{
tr->ref_count++;
if ( tr->ref_count-- > 1 )
return;
+ if ( tr->name )
+ g_free ( tr->name );
if ( tr->comment )
g_free ( tr->comment );
+ if ( tr->description )
+ g_free ( tr->description );
g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
g_list_free( tr->trackpoints );
if (tr->property_dialog)
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);
+ vik_track_set_description(new_tr,tr->description);
return new_tr;
}
return num;
}
-void vik_track_remove_dup_points ( VikTrack *tr )
+/*
+ * Deletes adjacent points that have the same position
+ * Returns the number of points that were deleted
+ */
+gulong vik_track_remove_dup_points ( VikTrack *tr )
{
+ gulong num = 0;
GList *iter = tr->trackpoints;
while ( iter )
{
if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
&(VIK_TRACKPOINT(iter->next->data)->coord) ) )
{
- g_free ( iter->next->data );
+ num++;
+ // Maintain track segments
+ if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
+ VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
+
+ vik_trackpoint_free ( iter->next->data );
+ tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
+ }
+ else
+ iter = iter->next;
+ }
+ return num;
+}
+
+/*
+ * Get a count of trackpoints with the same defined timestamp
+ * Note is using timestamps with a resolution with 1 second
+ */
+gulong vik_track_get_same_time_point_count ( const VikTrack *tr )
+{
+ gulong num = 0;
+ GList *iter = tr->trackpoints;
+ while ( iter ) {
+ if ( iter->next &&
+ ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
+ VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
+ ( VIK_TRACKPOINT(iter->data)->timestamp ==
+ VIK_TRACKPOINT(iter->next->data)->timestamp) )
+ num++;
+ iter = iter->next;
+ }
+ return num;
+}
+
+/*
+ * Deletes adjacent points that have the same defined timestamp
+ * Returns the number of points that were deleted
+ */
+gulong vik_track_remove_same_time_points ( VikTrack *tr )
+{
+ gulong num = 0;
+ GList *iter = tr->trackpoints;
+ while ( iter ) {
+ if ( iter->next &&
+ ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
+ VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
+ ( VIK_TRACKPOINT(iter->data)->timestamp ==
+ VIK_TRACKPOINT(iter->next->data)->timestamp) ) {
+
+ num++;
+
+ // Maintain track segments
+ if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
+ VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
+
+ vik_trackpoint_free ( iter->next->data );
tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
}
else
iter = iter->next;
}
+ return num;
}
guint vik_track_get_segment_count(const VikTrack *tr)
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 );
+ if ( tr->description )
+ vik_track_set_description ( rv[i], tr->description );
rv[i]->visible = tr->visible;
rv[i]->trackpoints = iter;
i++;
return rv;
}
+/*
+ * Simply remove any subsequent segment markers in a track to form one continuous track
+ * Return the number of segments merged
+ */
+guint vik_track_merge_segments(VikTrack *tr)
+{
+ guint num = 0;
+ GList *iter = tr->trackpoints;
+ if ( !iter )
+ return num;
+
+ // Always skip the first point as this should be the first segment
+ iter = iter->next;
+
+ while ( (iter = iter->next) )
+ {
+ if ( VIK_TRACKPOINT(iter->data)->newsegment ) {
+ VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
+ num++;
+ }
+ }
+ return num;
+}
+
void vik_track_reverse ( VikTrack *tr )
{
GList *iter;
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;
gboolean okay = FALSE;
while ( iter )
{
- if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
+ // Sometimes a GPS device (or indeed any random file) can have stupid numbers for elevations
+ // Since when is 9.9999e+24 a valid elevation!!
+ // This can happen when a track (with no elevations) is uploaded to a GPS device and then redownloaded (e.g. using a Garmin Legend EtrexHCx)
+ // Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception)
+ if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE &&
+ VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) {
okay = TRUE; break;
}
iter = iter->next;
/* 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;
*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 )
iter = tr->trackpoints->next;
numpts = 0;
s[0] = 0;
- t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
+ t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
numpts++;
while (iter) {
s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
iter = tr->trackpoints->next;
numpts = 0;
s[0] = 0;
- t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
+ t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
numpts++;
while (iter) {
s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
iter = tr->trackpoints->next;
numpts = 0;
s[0] = 0;
- t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
+ t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
numpts++;
while (iter) {
s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
}
*(guint *)(b->data + intp) = ntp;
- 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);
+ // 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);
*data = b->data;
*datalen = b->len;
g_byte_array_free(b, FALSE);
}
+/*
+ * Take a byte array and convert it into a Track
+ */
VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
{
guint len;
new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
}
- len = *(guint *)data;
- data += sizeof(len);
- if (len) {
- new_tr->comment = g_strdup((gchar *)data);
- }
+#define vtu_get(s) \
+ len = *(guint *)data; \
+ data += sizeof(len); \
+ if (len) { \
+ (s) = g_strdup((gchar *)data); \
+ } else { \
+ (s) = NULL; \
+ } \
+ data += len;
+
+ vtu_get(new_tr->name);
+ vtu_get(new_tr->comment);
+ vtu_get(new_tr->description);
+
return new_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 )
{