X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/24d5c7e28cd8a8f90bc51562dcec0234a455a528..4a050412da32aa6f237c358cfa0c5a3775f21e9a:/src/viktrack.c diff --git a/src/viktrack.c b/src/viktrack.c index b550ef78..6eb685ad 100644 --- a/src/viktrack.c +++ b/src/viktrack.c @@ -18,20 +18,29 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include #include -#include +#include +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_MATH_H +#include +#endif + #include "coords.h" #include "vikcoord.h" #include "viktrack.h" #include "globals.h" +#include "dems.h" VikTrack *vik_track_new() { - VikTrack *tr = g_malloc ( sizeof ( VikTrack ) ); - tr->trackpoints = NULL; - tr->comment = NULL; + VikTrack *tr = g_malloc0 ( sizeof ( VikTrack ) ); tr->ref_count = 1; return tr; } @@ -60,6 +69,17 @@ void vik_track_ref(VikTrack *tr) tr->ref_count++; } +void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog) +{ + /* Warning: does not check for existing dialog */ + tr->property_dialog = dialog; +} + +void vik_track_clear_property_dialog(VikTrack *tr) +{ + tr->property_dialog = NULL; +} + void vik_track_free(VikTrack *tr) { if ( tr->ref_count-- > 1 ) @@ -69,6 +89,9 @@ void vik_track_free(VikTrack *tr) g_free ( tr->comment ); g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL ); g_list_free( tr->trackpoints ); + if (tr->property_dialog) + if ( GTK_IS_WIDGET(tr->property_dialog) ) + gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) ); g_free ( tr ); } @@ -92,7 +115,14 @@ VikTrack *vik_track_copy ( const VikTrack *tr ) VikTrackpoint *vik_trackpoint_new() { - return g_malloc0(sizeof(VikTrackpoint)); + VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint)); + tp->speed = NAN; + tp->course = NAN; + tp->altitude = VIK_DEFAULT_ALTITUDE; + tp->hdop = VIK_DEFAULT_DOP; + tp->vdop = VIK_DEFAULT_DOP; + tp->pdop = VIK_DEFAULT_DOP; + return tp; } void vik_trackpoint_free(VikTrackpoint *tp) @@ -324,6 +354,9 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) GList *iter = tr->trackpoints; + if (!iter || !iter->next) /* zero- or one-point track */ + return NULL; + { /* test if there's anything worth calculating */ gboolean okay = FALSE; while ( iter ) @@ -337,7 +370,7 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) return NULL; } - + iter = tr->trackpoints; g_assert ( num_chunks < 16000 ); @@ -346,6 +379,12 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) 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) { + g_free(pts); + return NULL; + } + current_dist = 0.0; current_area_under_curve = 0; current_chunk = 0; @@ -373,7 +412,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); @@ -408,6 +448,12 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) dist_along_seg = chunk_length - current_dist; if ( ignore_it || !iter->next ) { pts[current_chunk] = current_area_under_curve / current_dist; + if (!iter->next) { + int i; + for (i = current_chunk + 1; i < num_chunks; i++) + pts[i] = pts[current_chunk]; + break; + } } else { current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length); @@ -427,7 +473,7 @@ void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble { gdouble diff; *up = *down = 0; - if ( tr->trackpoints ) + if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE ) { GList *iter = tr->trackpoints->next; while (iter) @@ -439,17 +485,19 @@ void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down -= diff; iter = iter->next; } - } + } else + *up = *down = VIK_DEFAULT_ALTITUDE; } + +/* by Alex Foobarian */ gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks ) { - gdouble *pts; - gdouble duration, chunk_dur, t; + gdouble *v, *s, *t; + gdouble duration, chunk_dur; time_t t1, t2; - int i; - - GList *iter = tr->trackpoints; + int i, pt_count, numpts, index; + GList *iter; if ( ! tr->trackpoints ) return NULL; @@ -464,56 +512,629 @@ gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks ) return NULL; if (duration < 0) { - fprintf(stderr, "negative duration: unsorted trackpoint timestamps?\n"); + g_warning("negative duration: unsorted trackpoint timestamps?"); return NULL; } - - pts = g_malloc ( sizeof(gdouble) * num_chunks ); + pt_count = vik_track_get_tp_count(tr); + v = g_malloc ( sizeof(gdouble) * num_chunks ); chunk_dur = duration / num_chunks; - t = t1; - for (i = 0; i < num_chunks; i++, t+=chunk_dur) { - while (iter && VIK_TRACKPOINT(iter->data)->timestamp <= t) { - iter = iter->next; + + s = g_malloc(sizeof(double) * pt_count); + t = g_malloc(sizeof(double) * pt_count); + + iter = tr->trackpoints->next; + numpts = 0; + s[0] = 0; + t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp; + numpts++; + while (iter) { + s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) ); + t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp; + numpts++; + iter = iter->next; + } + + /* In the following computation, we iterate through periods of time of duration chunk_dur. + * The first period begins at the beginning of the track. The last period ends at the end of the track. + */ + index = 0; /* index of the current trackpoint. */ + for (i = 0; i < num_chunks; i++) { + /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur. + * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints. + */ + if (t[0] + i*chunk_dur >= t[index]) { + gdouble acc_t = 0, acc_s = 0; + while (t[0] + i*chunk_dur >= t[index]) { + acc_s += (s[index+1]-s[index]); + acc_t += (t[index+1]-t[index]); + index++; + } + v[i] = acc_s/acc_t; + } + else if (i) { + v[i] = v[i-1]; + } + else { + v[i] = 0; + } + } + g_free(s); + g_free(t); + return v; +} + +/** + * Make a distance/time map, heavily based on the vik_track_make_speed_map method + */ +gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks ) +{ + gdouble *v, *s, *t; + gdouble duration, chunk_dur; + time_t t1, t2; + int i, pt_count, numpts, index; + GList *iter; + + if ( ! tr->trackpoints ) + return NULL; + + t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; + t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; + duration = t2 - t1; + + if ( !t1 || !t2 || !duration ) + return NULL; + + if (duration < 0) { + g_warning("negative duration: unsorted trackpoint timestamps?"); + return NULL; + } + pt_count = vik_track_get_tp_count(tr); + + v = g_malloc ( sizeof(gdouble) * num_chunks ); + chunk_dur = duration / num_chunks; + + s = g_malloc(sizeof(double) * pt_count); + t = g_malloc(sizeof(double) * pt_count); + + iter = tr->trackpoints->next; + numpts = 0; + s[0] = 0; + t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp; + numpts++; + while (iter) { + s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) ); + t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp; + numpts++; + iter = iter->next; + } + + /* In the following computation, we iterate through periods of time of duration chunk_dur. + * The first period begins at the beginning of the track. The last period ends at the end of the track. + */ + index = 0; /* index of the current trackpoint. */ + for (i = 0; i < num_chunks; i++) { + /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur. + * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints. + */ + if (t[0] + i*chunk_dur >= t[index]) { + gdouble acc_s = 0; // No need for acc_t + while (t[0] + i*chunk_dur >= t[index]) { + acc_s += (s[index+1]-s[index]); + index++; + } + // The only bit that's really different from the speed map - just keep an accululative record distance + v[i] = i ? v[i-1]+acc_s : acc_s; + } + else if (i) { + v[i] = v[i-1]; + } + else { + v[i] = 0; + } + } + g_free(s); + g_free(t); + return v; +} + +/** + * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance) + * This results in a slightly blocky graph when it does not have many trackpoints: <60 + * NB Somehow the elevation/distance applies some kind of smoothing algorithm, + * but I don't think any one understands it any more (I certainly don't ATM) + */ +gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks ) +{ + time_t t1, t2; + gdouble duration, chunk_dur; + GList *iter = tr->trackpoints; + + if (!iter || !iter->next) /* zero- or one-point track */ + return NULL; + + /* test if there's anything worth calculating */ + gboolean okay = FALSE; + while ( iter ) { + if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) { + okay = TRUE; + break; + } + iter = iter->next; + } + if ( ! okay ) + return NULL; + + t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; + t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; + duration = t2 - t1; + + if ( !t1 || !t2 || !duration ) + return NULL; + + if (duration < 0) { + g_warning("negative duration: unsorted trackpoint timestamps?"); + return NULL; + } + gint pt_count = vik_track_get_tp_count(tr); + + // Reset iterator back to the beginning + iter = tr->trackpoints; + + gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values + gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes + gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times + + chunk_dur = duration / num_chunks; + + s[0] = VIK_TRACKPOINT(iter->data)->altitude; + t[0] = VIK_TRACKPOINT(iter->data)->timestamp; + iter = tr->trackpoints->next; + gint numpts = 1; + while (iter) { + s[numpts] = VIK_TRACKPOINT(iter->data)->altitude; + t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp; + numpts++; + iter = iter->next; + } + + /* In the following computation, we iterate through periods of time of duration chunk_dur. + * The first period begins at the beginning of the track. The last period ends at the end of the track. + */ + gint index = 0; /* index of the current trackpoint. */ + gint i; + for (i = 0; i < num_chunks; i++) { + /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur. + * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints. + */ + if (t[0] + i*chunk_dur >= t[index]) { + gdouble acc_s = s[index]; // initialise to first point + while (t[0] + i*chunk_dur >= t[index]) { + acc_s += (s[index+1]-s[index]); + index++; + } + pts[i] = acc_s; + } + else if (i) { + pts[i] = pts[i-1]; + } + else { + pts[i] = 0; } - pts[i] = vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), - &(VIK_TRACKPOINT(iter->data)->coord) ) / - (gdouble)(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp); } + g_free(s); + g_free(t); return pts; } -VikCoord *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist ) +/** + * Make a speed/distance map + */ +gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks ) +{ + gdouble *v, *s, *t; + time_t t1, t2; + gint i, pt_count, numpts, index; + GList *iter; + gdouble duration, total_length, chunk_length; + + if ( ! tr->trackpoints ) + return NULL; + + t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; + t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; + duration = t2 - t1; + + if ( !t1 || !t2 || !duration ) + return NULL; + + if (duration < 0) { + g_warning("negative duration: unsorted trackpoint timestamps?"); + return NULL; + } + + total_length = vik_track_get_length_including_gaps ( tr ); + chunk_length = total_length / num_chunks; + pt_count = vik_track_get_tp_count(tr); + + if (chunk_length <= 0) { + return NULL; + } + + v = g_malloc ( sizeof(gdouble) * num_chunks ); + s = g_malloc ( sizeof(double) * pt_count ); + t = g_malloc ( sizeof(double) * pt_count ); + + // No special handling of segments ATM... + iter = tr->trackpoints->next; + numpts = 0; + s[0] = 0; + t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp; + numpts++; + while (iter) { + s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) ); + t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp; + numpts++; + iter = iter->next; + } + + // Iterate through a portion of the track to get an average speed for that part + // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps' + index = 0; /* index of the current trackpoint. */ + for (i = 0; i < num_chunks; i++) { + // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk + if (s[0] + i*chunk_length >= s[index]) { + gdouble acc_t = 0, acc_s = 0; + while (s[0] + i*chunk_length >= s[index]) { + acc_s += (s[index+1]-s[index]); + acc_t += (t[index+1]-t[index]); + index++; + } + v[i] = acc_s/acc_t; + } + else if (i) { + v[i] = v[i-1]; + } + else { + v[i] = 0; + } + } + g_free(s); + g_free(t); + return v; +} + +/* by Alex Foobarian */ +VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start ) { gdouble dist = vik_track_get_length_including_gaps(tr) * reldist; gdouble current_dist = 0.0; gdouble current_inc = 0.0; - VikCoord *rv; if ( tr->trackpoints ) { GList *iter = tr->trackpoints->next; + GList *last_iter = NULL; + gdouble last_dist = 0.0; while (iter) { current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) ); + last_dist = current_dist; current_dist += current_inc; if ( current_dist >= dist ) break; + last_iter = iter; iter = iter->next; } + if (!iter) { /* passing the end the track */ + if (last_iter) { + if (meters_from_start) + *meters_from_start = last_dist; + return(VIK_TRACKPOINT(last_iter->data)); + } + else + return NULL; + } /* 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 && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) { + if (meters_from_start) + *meters_from_start = last_dist; iter = iter->prev; + } + else + if (meters_from_start) + *meters_from_start = current_dist; + + return VIK_TRACKPOINT(iter->data); + } + return NULL; +} +VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start ) +{ + 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; + t_total = t_end - t_start; - rv = g_malloc(sizeof(VikCoord)); - *rv = VIK_TRACKPOINT(iter->data)->coord; + t_pos = t_start + t_total * reltime; - return rv; + if ( !tr->trackpoints ) + return NULL; + GList *iter = tr->trackpoints; + + while (iter) { + if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos) + break; + if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) { + if (iter->prev == NULL) /* first trackpoint */ + break; + time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp; + time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos; + if (t_before <= t_after) + iter = iter->prev; + break; + } + else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */ + break; + iter = iter->next; } - return NULL; + + if (!iter) + return NULL; + if (seconds_from_start) + *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; + return VIK_TRACKPOINT(iter->data); +} + +VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr ) +{ + gdouble maxspeed = 0.0, speed = 0.0; + + if ( !tr->trackpoints ) + return NULL; + + GList *iter = tr->trackpoints; + VikTrackpoint *max_speed_tp = NULL; + + while (iter) { + if (iter->prev) { + if ( VIK_TRACKPOINT(iter->data)->has_timestamp && + VIK_TRACKPOINT(iter->prev->data)->has_timestamp && + (! VIK_TRACKPOINT(iter->data)->newsegment) ) { + speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) ) + / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp); + if ( speed > maxspeed ) { + maxspeed = speed; + max_speed_tp = VIK_TRACKPOINT(iter->data); + } + } + } + iter = iter->next; + } + + if (!max_speed_tp) + return NULL; + + return max_speed_tp; } + +VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr ) +{ + gdouble maxalt = -5000.0; + if ( !tr->trackpoints ) + return NULL; + + GList *iter = tr->trackpoints; + VikTrackpoint *max_alt_tp = NULL; + + while (iter) { + if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) { + maxalt = VIK_TRACKPOINT(iter->data)->altitude; + max_alt_tp = VIK_TRACKPOINT(iter->data); + } + iter = iter->next; + } + + if (!max_alt_tp) + return NULL; + + return max_alt_tp; +} + +VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr ) +{ + gdouble minalt = 25000.0; + if ( !tr->trackpoints ) + return NULL; + + GList *iter = tr->trackpoints; + VikTrackpoint *min_alt_tp = NULL; + + while (iter) { + if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) { + minalt = VIK_TRACKPOINT(iter->data)->altitude; + min_alt_tp = VIK_TRACKPOINT(iter->data); + } + iter = iter->next; + } + + if (!min_alt_tp) + return NULL; + + return min_alt_tp; +} + +gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt ) +{ + *min_alt = 25000; + *max_alt = -5000; + if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) { + GList *iter = tr->trackpoints->next; + gdouble tmp_alt; + while (iter) + { + tmp_alt = VIK_TRACKPOINT(iter->data)->altitude; + if ( tmp_alt > *max_alt ) + *max_alt = tmp_alt; + if ( tmp_alt < *min_alt ) + *min_alt = tmp_alt; + iter = iter->next; + } + return TRUE; + } + return FALSE; +} + +void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen) +{ + GList *tps; + GByteArray *b = g_byte_array_new(); + guint len; + guint intp, ntp; + + g_byte_array_append(b, (guint8 *)tr, sizeof(*tr)); + + /* we'll fill out number of trackpoints later */ + intp = b->len; + g_byte_array_append(b, (guint8 *)&len, sizeof(len)); + + tps = tr->trackpoints; + ntp = 0; + while (tps) { + g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint)); + tps = tps->next; + ntp++; + } + *(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); + + *data = b->data; + *datalen = b->len; + g_byte_array_free(b, FALSE); +} + +VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) +{ + guint len; + VikTrack *new_tr = vik_track_new(); + VikTrackpoint *new_tp; + guint ntp; + gint i; + + /* only the visibility is needed */ + new_tr->visible = ((VikTrack *)data)->visible; + data += sizeof(*new_tr); + + ntp = *(guint *)data; + data += sizeof(ntp); + + for (i=0; itrackpoints = g_list_append(new_tr->trackpoints, new_tp); + } + + len = *(guint *)data; + data += sizeof(len); + if (len) { + new_tr->comment = g_strdup((gchar *)data); + } + return new_tr; +} + +void vik_track_apply_dem_data ( VikTrack *tr ) +{ + GList *tp_iter; + gint16 elev; + tp_iter = tr->trackpoints; + while ( tp_iter ) { + /* TODO: of the 4 possible choices we have for choosing an elevation + * (trackpoint in between samples), choose the one with the least elevation change + * as the last */ + elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST ); + if ( elev != VIK_DEM_INVALID_ELEVATION ) + VIK_TRACKPOINT(tp_iter->data)->altitude = elev; + tp_iter = tp_iter->next; + } +} + +/* + * 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 ) +{ + if ( t1->trackpoints ) { + GList *tpiter = t1->trackpoints; + while ( tpiter->next ) + tpiter = tpiter->next; + tpiter->next = t2->trackpoints; + t2->trackpoints->prev = tpiter; + } else + t1->trackpoints = t2->trackpoints; + t2->trackpoints = NULL; +} + +/* starting at the end, looks backwards for the last "double point", a duplicate trackpoint. + * 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 ) +{ + GList *iter = tr->trackpoints; + VikCoord *rv; + + if ( !iter ) + return NULL; + while ( iter->next ) + iter = iter->next; + + + while ( iter->prev ) { + if ( vik_coord_equals((VikCoord *)iter->data, (VikCoord *)iter->prev->data) ) { + GList *prev = iter->prev; + + rv = g_malloc(sizeof(VikCoord)); + *rv = *((VikCoord *) iter->data); + + /* truncate trackpoint list */ + iter->prev = NULL; /* pretend it's the end */ + g_list_foreach ( iter, (GFunc) g_free, NULL ); + g_list_free( iter ); + + prev->next = NULL; + + return rv; + } + iter = iter->prev; + } + + /* no double point found! */ + rv = g_malloc(sizeof(VikCoord)); + *rv = *((VikCoord *) tr->trackpoints->data); + g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL ); + g_list_free( tr->trackpoints ); + tr->trackpoints = NULL; + return rv; +} +