X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/4d333042e52f65517983d73fb70252319e6a112d..3adb68271b3969f524ebbba61ec49d6661e8979d:/src/viktrack.c diff --git a/src/viktrack.c b/src/viktrack.c index 6cf610e9..64079c16 100644 --- a/src/viktrack.c +++ b/src/viktrack.c @@ -38,6 +38,7 @@ #include "viktrack.h" #include "globals.h" #include "dems.h" +#include "settings.h" VikTrack *vik_track_new() { @@ -46,6 +47,26 @@ VikTrack *vik_track_new() return tr; } +#define VIK_SETTINGS_TRACK_NAME_MODE "track_draw_name_mode" +#define VIK_SETTINGS_TRACK_NUM_DIST_LABELS "track_number_dist_labels" + +/** + * vik_track_set_defaults: + * + * Set some default values for a track. + * ATM This uses the 'settings' method to get values, + * so there is no GUI way to control these yet... + */ +void vik_track_set_defaults(VikTrack *tr) +{ + gint tmp; + if ( a_settings_get_integer ( VIK_SETTINGS_TRACK_NAME_MODE, &tmp ) ) + tr->draw_name_mode = tmp; + + if ( a_settings_get_integer ( VIK_SETTINGS_TRACK_NUM_DIST_LABELS, &tmp ) ) + tr->max_number_dist_labels = tmp; +} + void vik_track_set_comment_no_copy(VikTrack *tr, gchar *comment) { if ( tr->comment ) @@ -111,7 +132,7 @@ 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 ); + 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) ) @@ -132,23 +153,26 @@ 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; + new_tr->draw_name_mode = tr->draw_name_mode; + new_tr->max_number_dist_labels = tr->max_number_dist_labels; new_tr->has_color = tr->has_color; new_tr->color = tr->color; new_tr->bbox = tr->bbox; 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); @@ -170,14 +194,31 @@ VikTrackpoint *vik_trackpoint_new() void vik_trackpoint_free(VikTrackpoint *tp) { + g_free(tp->name); g_free(tp); } +void vik_trackpoint_set_name(VikTrackpoint *tp, const gchar *name) +{ + if ( tp->name ) + g_free ( tp->name ); + + // If the name is blank then completely remove it + if ( name && name[0] == '\0' ) + tp->name = NULL; + else if ( name ) + tp->name = g_strdup(name); + else + tp->name = NULL; +} + 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; } /** @@ -217,11 +258,46 @@ 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 ); } +/** + * vik_track_get_length_to_trackpoint: + * + */ +gdouble vik_track_get_length_to_trackpoint (const VikTrack *tr, const VikTrackpoint *tp) +{ + 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) + { + VikTrackpoint *tp1 = VIK_TRACKPOINT(iter->data); + if ( ! tp1->newsegment ) + len += vik_coord_diff ( &(tp1->coord), + &(VIK_TRACKPOINT(iter->prev->data)->coord) ); + + // Exit when we reach the desired point + if ( tp1 == tp ) + break; + + iter = iter->next; + } + } + return len; +} + gdouble vik_track_get_length(const VikTrack *tr) { gdouble len = 0.0; @@ -483,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; @@ -1082,6 +1183,55 @@ gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks return v; } +/** + * vik_track_get_tp_by_dist: + * @trk: The Track on which to find a Trackpoint + * @meters_from_start: The distance along a track that the trackpoint returned is near + * @get_next_point: Since there is a choice of trackpoints, this determines which one to return + * @tp_metres_from_start: For the returned Trackpoint, returns the distance along the track + * + * TODO: Consider changing the boolean get_next_point into an enum with these options PREVIOUS, NEXT, NEAREST + * + * Returns: The #VikTrackpoint fitting the criteria or NULL + */ +VikTrackpoint *vik_track_get_tp_by_dist ( VikTrack *trk, gdouble meters_from_start, gboolean get_next_point, gdouble *tp_metres_from_start ) +{ + gdouble current_dist = 0.0; + gdouble current_inc = 0.0; + if ( tp_metres_from_start ) + *tp_metres_from_start = 0.0; + + if ( trk->trackpoints ) { + GList *iter = g_list_next ( g_list_first ( trk->trackpoints ) ); + while (iter) { + current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), + &(VIK_TRACKPOINT(iter->prev->data)->coord) ); + current_dist += current_inc; + if ( current_dist >= meters_from_start ) + break; + iter = g_list_next ( iter ); + } + // passed the end of the track + if ( !iter ) + return NULL; + + if ( tp_metres_from_start ) + *tp_metres_from_start = current_dist; + + // we've gone past the distance already, is the previous trackpoint wanted? + if ( !get_next_point ) { + if ( iter->prev ) { + if ( tp_metres_from_start ) + *tp_metres_from_start = current_dist-current_inc; + return VIK_TRACKPOINT(iter->prev->data); + } + } + return VIK_TRACKPOINT(iter->data); + } + + return NULL; +} + /* by Alex Foobarian */ VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start ) { @@ -1115,7 +1265,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; @@ -1246,6 +1396,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; @@ -1280,22 +1467,23 @@ 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); @@ -1319,6 +1507,8 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) /* basic properties: */ new_tr->visible = ((VikTrack *)data)->visible; new_tr->is_route = ((VikTrack *)data)->is_route; + new_tr->draw_name_mode = ((VikTrack *)data)->draw_name_mode; + new_tr->max_number_dist_labels = ((VikTrack *)data)->max_number_dist_labels; new_tr->has_color = ((VikTrack *)data)->has_color; new_tr->color = ((VikTrack *)data)->color; new_tr->bbox = ((VikTrack *)data)->bbox; @@ -1328,13 +1518,6 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) ntp = *(guint *)data; data += sizeof(ntp); - for (i=0; itrackpoints = g_list_append(new_tr->trackpoints, new_tp); - } - #define vtu_get(s) \ len = *(guint *)data; \ data += sizeof(len); \ @@ -1345,6 +1528,16 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) } \ data += len; + for (i=0; iname); + 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); @@ -1391,6 +1584,86 @@ void vik_track_calculate_bounds ( VikTrack *trk ) trk->bbox.west = topleft.lon; } +/** + * vik_track_anonymize_times: + * + * Shift all timestamps to be relatively offset from 1901-01-01 + */ +void vik_track_anonymize_times ( VikTrack *tr ) +{ + GTimeVal gtv; + g_time_val_from_iso8601 ( "1901-01-01T00:00:00Z", >v ); + + time_t anon_timestamp = gtv.tv_sec; + time_t offset = 0; + + GList *tp_iter; + tp_iter = tr->trackpoints; + while ( tp_iter ) { + VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data); + if ( tp->has_timestamp ) { + // Calculate an offset in time using the first available timestamp + if ( offset == 0 ) + offset = tp->timestamp - anon_timestamp; + + // Apply this offset to shift all timestamps towards 1901 & hence anonymising the time + // Note that the relative difference between timestamps is kept - thus calculating speeds will still work + tp->timestamp = tp->timestamp - offset; + } + tp_iter = tp_iter->next; + } +} + +/** + * 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: * @skip_existing: When TRUE, don't change the elevation if the trackpoint already has a value @@ -1568,11 +1841,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 */ @@ -1588,10 +1863,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; +}