+
+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; 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);
+ }
+
+ 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;
+}
+