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