2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 VikTrack *vik_track_new()
43 VikTrack *tr = g_malloc0 ( sizeof ( VikTrack ) );
48 void vik_track_set_comment_no_copy(VikTrack *tr, gchar *comment)
51 g_free ( tr->comment );
52 tr->comment = comment;
56 void vik_track_set_comment(VikTrack *tr, const gchar *comment)
59 g_free ( tr->comment );
61 if ( comment && comment[0] != '\0' )
62 tr->comment = g_strdup(comment);
67 void vik_track_ref(VikTrack *tr)
72 void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog)
74 /* Warning: does not check for existing dialog */
75 tr->property_dialog = dialog;
78 void vik_track_clear_property_dialog(VikTrack *tr)
80 tr->property_dialog = NULL;
83 void vik_track_free(VikTrack *tr)
85 if ( tr->ref_count-- > 1 )
89 g_free ( tr->comment );
90 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
91 g_list_free( tr->trackpoints );
92 if (tr->property_dialog)
93 if ( GTK_IS_WIDGET(tr->property_dialog) )
94 gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) );
98 VikTrack *vik_track_copy ( const VikTrack *tr )
100 VikTrack *new_tr = vik_track_new();
101 VikTrackpoint *new_tp;
102 GList *tp_iter = tr->trackpoints;
103 new_tr->visible = tr->visible;
104 new_tr->trackpoints = NULL;
107 new_tp = g_malloc ( sizeof ( VikTrackpoint ) );
108 *new_tp = *((VikTrackpoint *)(tp_iter->data));
109 new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp );
110 tp_iter = tp_iter->next;
112 vik_track_set_comment(new_tr,tr->comment);
116 VikTrackpoint *vik_trackpoint_new()
118 VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint));
121 tp->altitude = VIK_DEFAULT_ALTITUDE;
122 tp->hdop = VIK_DEFAULT_DOP;
123 tp->vdop = VIK_DEFAULT_DOP;
124 tp->pdop = VIK_DEFAULT_DOP;
128 void vik_trackpoint_free(VikTrackpoint *tp)
133 VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
135 VikTrackpoint *rv = vik_trackpoint_new();
140 gdouble vik_track_get_length(const VikTrack *tr)
143 if ( tr->trackpoints )
145 GList *iter = tr->trackpoints->next;
148 if ( ! VIK_TRACKPOINT(iter->data)->newsegment )
149 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
150 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
157 gdouble vik_track_get_length_including_gaps(const VikTrack *tr)
160 if ( tr->trackpoints )
162 GList *iter = tr->trackpoints->next;
165 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
166 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
173 gulong vik_track_get_tp_count(const VikTrack *tr)
176 GList *iter = tr->trackpoints;
185 gulong vik_track_get_dup_point_count ( const VikTrack *tr )
188 GList *iter = tr->trackpoints;
191 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
192 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
199 void vik_track_remove_dup_points ( VikTrack *tr )
201 GList *iter = tr->trackpoints;
204 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
205 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
207 g_free ( iter->next->data );
208 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
215 guint vik_track_get_segment_count(const VikTrack *tr)
218 GList *iter = tr->trackpoints;
221 while ( (iter = iter->next) )
223 if ( VIK_TRACKPOINT(iter->data)->newsegment )
229 VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
234 guint segs = vik_track_get_segment_count(t);
243 rv = g_malloc ( segs * sizeof(VikTrack *) );
244 tr = vik_track_copy ( t );
246 iter = tr->trackpoints;
249 while ( (iter = iter->next) )
251 if ( VIK_TRACKPOINT(iter->data)->newsegment )
253 iter->prev->next = NULL;
255 rv[i] = vik_track_new();
257 vik_track_set_comment ( rv[i], tr->comment );
258 rv[i]->visible = tr->visible;
259 rv[i]->trackpoints = iter;
267 void vik_track_reverse ( VikTrack *tr )
270 tr->trackpoints = g_list_reverse(tr->trackpoints);
272 /* fix 'newsegment' */
273 iter = g_list_last ( tr->trackpoints );
276 if ( ! iter->next ) /* last segment, was first, cancel newsegment. */
277 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
278 if ( ! iter->prev ) /* first segment by convention has newsegment flag. */
279 VIK_TRACKPOINT(iter->data)->newsegment = TRUE;
280 else if ( VIK_TRACKPOINT(iter->data)->newsegment && iter->next )
282 VIK_TRACKPOINT(iter->next->data)->newsegment = TRUE;
283 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
289 gdouble vik_track_get_average_speed(const VikTrack *tr)
293 if ( tr->trackpoints )
295 GList *iter = tr->trackpoints->next;
298 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
299 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
300 (! VIK_TRACKPOINT(iter->data)->newsegment) )
302 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
303 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
304 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
309 return (time == 0) ? 0 : ABS(len/time);
312 gdouble vik_track_get_max_speed(const VikTrack *tr)
314 gdouble maxspeed = 0.0, speed = 0.0;
315 if ( tr->trackpoints )
317 GList *iter = tr->trackpoints->next;
320 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
321 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
322 (! VIK_TRACKPOINT(iter->data)->newsegment) )
324 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
325 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
326 if ( speed > maxspeed )
335 void vik_track_convert ( VikTrack *tr, VikCoordMode dest_mode )
337 GList *iter = tr->trackpoints;
340 vik_coord_convert ( &(VIK_TRACKPOINT(iter->data)->coord), dest_mode );
345 /* I understood this when I wrote it ... maybe ... Basically it eats up the
346 * proper amounts of length on the track and averages elevation over that. */
347 gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
350 gdouble total_length, chunk_length, current_dist, current_area_under_curve, current_seg_length, dist_along_seg = 0.0;
351 gdouble altitude1, altitude2;
352 guint16 current_chunk;
353 gboolean ignore_it = FALSE;
355 GList *iter = tr->trackpoints;
357 if (!iter || !iter->next) /* zero- or one-point track */
360 { /* test if there's anything worth calculating */
361 gboolean okay = FALSE;
364 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
373 iter = tr->trackpoints;
375 g_assert ( num_chunks < 16000 );
377 pts = g_malloc ( sizeof(gdouble) * num_chunks );
379 total_length = vik_track_get_length_including_gaps ( tr );
380 chunk_length = total_length / num_chunks;
382 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
383 if (chunk_length <= 0) {
389 current_area_under_curve = 0;
391 current_seg_length = 0;
393 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
394 &(VIK_TRACKPOINT(iter->next->data)->coord) );
395 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
396 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
399 while ( current_chunk < num_chunks ) {
401 /* go along current seg */
402 if ( current_seg_length && (current_seg_length - dist_along_seg) > chunk_length ) {
403 dist_along_seg += chunk_length;
407 * /x altitude = alt_at_pt_1 + alt_at_pt_2 / 2 = altitude1 + slope * dist_value_of_pt_inbetween_pt1_and_pt2
408 * /xx avg altitude = area under curve / chunk len
409 *pt1 *xxx avg altitude = altitude1 + (altitude2-altitude1)/(current_seg_length)*(dist_along_seg + (chunk_len/2))
415 // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero)
416 pts[current_chunk] = altitude1;
418 pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
422 /* finish current seg */
423 if ( current_seg_length ) {
424 gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg;
425 current_dist = current_seg_length - dist_along_seg;
426 current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5;
427 } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */
429 /* get intervening segs */
431 while ( iter && iter->next ) {
432 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
433 &(VIK_TRACKPOINT(iter->next->data)->coord) );
434 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
435 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
436 ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment;
438 if ( chunk_length - current_dist >= current_seg_length ) {
439 current_dist += current_seg_length;
440 current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5;
448 dist_along_seg = chunk_length - current_dist;
449 if ( ignore_it || !iter->next ) {
450 pts[current_chunk] = current_area_under_curve / current_dist;
453 for (i = current_chunk + 1; i < num_chunks; i++)
454 pts[i] = pts[current_chunk];
459 current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length);
460 pts[current_chunk] = current_area_under_curve / chunk_length;
472 void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
476 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE )
478 GList *iter = tr->trackpoints->next;
481 diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude;
489 *up = *down = VIK_DEFAULT_ALTITUDE;
493 /* by Alex Foobarian */
494 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
497 gdouble duration, chunk_dur;
499 int i, pt_count, numpts, index;
502 if ( ! tr->trackpoints )
505 g_assert ( num_chunks < 16000 );
507 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
508 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
511 if ( !t1 || !t2 || !duration )
515 g_warning("negative duration: unsorted trackpoint timestamps?");
518 pt_count = vik_track_get_tp_count(tr);
520 v = g_malloc ( sizeof(gdouble) * num_chunks );
521 chunk_dur = duration / num_chunks;
523 s = g_malloc(sizeof(double) * pt_count);
524 t = g_malloc(sizeof(double) * pt_count);
526 iter = tr->trackpoints->next;
529 t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
532 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
533 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
538 /* In the following computation, we iterate through periods of time of duration chunk_dur.
539 * The first period begins at the beginning of the track. The last period ends at the end of the track.
541 index = 0; /* index of the current trackpoint. */
542 for (i = 0; i < num_chunks; i++) {
543 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
544 * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints.
546 if (t[0] + i*chunk_dur >= t[index]) {
547 gdouble acc_t = 0, acc_s = 0;
548 while (t[0] + i*chunk_dur >= t[index]) {
549 acc_s += (s[index+1]-s[index]);
550 acc_t += (t[index+1]-t[index]);
568 * Make a distance/time map, heavily based on the vik_track_make_speed_map method
570 gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks )
573 gdouble duration, chunk_dur;
575 int i, pt_count, numpts, index;
578 if ( ! tr->trackpoints )
581 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
582 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
585 if ( !t1 || !t2 || !duration )
589 g_warning("negative duration: unsorted trackpoint timestamps?");
592 pt_count = vik_track_get_tp_count(tr);
594 v = g_malloc ( sizeof(gdouble) * num_chunks );
595 chunk_dur = duration / num_chunks;
597 s = g_malloc(sizeof(double) * pt_count);
598 t = g_malloc(sizeof(double) * pt_count);
600 iter = tr->trackpoints->next;
603 t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
606 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
607 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
612 /* In the following computation, we iterate through periods of time of duration chunk_dur.
613 * The first period begins at the beginning of the track. The last period ends at the end of the track.
615 index = 0; /* index of the current trackpoint. */
616 for (i = 0; i < num_chunks; i++) {
617 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
618 * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints.
620 if (t[0] + i*chunk_dur >= t[index]) {
621 gdouble acc_s = 0; // No need for acc_t
622 while (t[0] + i*chunk_dur >= t[index]) {
623 acc_s += (s[index+1]-s[index]);
626 // The only bit that's really different from the speed map - just keep an accululative record distance
627 v[i] = i ? v[i-1]+acc_s : acc_s;
642 * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance)
643 * This results in a slightly blocky graph when it does not have many trackpoints: <60
644 * NB Somehow the elevation/distance applies some kind of smoothing algorithm,
645 * but I don't think any one understands it any more (I certainly don't ATM)
647 gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks )
650 gdouble duration, chunk_dur;
651 GList *iter = tr->trackpoints;
653 if (!iter || !iter->next) /* zero- or one-point track */
656 /* test if there's anything worth calculating */
657 gboolean okay = FALSE;
659 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
668 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
669 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
672 if ( !t1 || !t2 || !duration )
676 g_warning("negative duration: unsorted trackpoint timestamps?");
679 gint pt_count = vik_track_get_tp_count(tr);
681 // Reset iterator back to the beginning
682 iter = tr->trackpoints;
684 gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values
685 gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes
686 gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times
688 chunk_dur = duration / num_chunks;
690 s[0] = VIK_TRACKPOINT(iter->data)->altitude;
691 t[0] = VIK_TRACKPOINT(iter->data)->timestamp;
692 iter = tr->trackpoints->next;
695 s[numpts] = VIK_TRACKPOINT(iter->data)->altitude;
696 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
701 /* In the following computation, we iterate through periods of time of duration chunk_dur.
702 * The first period begins at the beginning of the track. The last period ends at the end of the track.
704 gint index = 0; /* index of the current trackpoint. */
706 for (i = 0; i < num_chunks; i++) {
707 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
708 * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints.
710 if (t[0] + i*chunk_dur >= t[index]) {
711 gdouble acc_s = s[index]; // initialise to first point
712 while (t[0] + i*chunk_dur >= t[index]) {
713 acc_s += (s[index+1]-s[index]);
732 * Make a speed/distance map
734 gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks )
738 gint i, pt_count, numpts, index;
740 gdouble duration, total_length, chunk_length;
742 if ( ! tr->trackpoints )
745 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
746 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
749 if ( !t1 || !t2 || !duration )
753 g_warning("negative duration: unsorted trackpoint timestamps?");
757 total_length = vik_track_get_length_including_gaps ( tr );
758 chunk_length = total_length / num_chunks;
759 pt_count = vik_track_get_tp_count(tr);
761 if (chunk_length <= 0) {
765 v = g_malloc ( sizeof(gdouble) * num_chunks );
766 s = g_malloc ( sizeof(double) * pt_count );
767 t = g_malloc ( sizeof(double) * pt_count );
769 // No special handling of segments ATM...
770 iter = tr->trackpoints->next;
773 t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
776 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
777 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
782 // Iterate through a portion of the track to get an average speed for that part
783 // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps'
784 index = 0; /* index of the current trackpoint. */
785 for (i = 0; i < num_chunks; i++) {
786 // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk
787 if (s[0] + i*chunk_length >= s[index]) {
788 gdouble acc_t = 0, acc_s = 0;
789 while (s[0] + i*chunk_length >= s[index]) {
790 acc_s += (s[index+1]-s[index]);
791 acc_t += (t[index+1]-t[index]);
808 /* by Alex Foobarian */
809 VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start )
811 gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
812 gdouble current_dist = 0.0;
813 gdouble current_inc = 0.0;
814 if ( tr->trackpoints )
816 GList *iter = tr->trackpoints->next;
817 GList *last_iter = NULL;
818 gdouble last_dist = 0.0;
821 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
822 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
823 last_dist = current_dist;
824 current_dist += current_inc;
825 if ( current_dist >= dist )
830 if (!iter) { /* passing the end the track */
832 if (meters_from_start)
833 *meters_from_start = last_dist;
834 return(VIK_TRACKPOINT(last_iter->data));
839 /* we've gone past the dist already, was prev trackpoint closer? */
840 /* should do a vik_coord_average_weighted() thingy. */
841 if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) {
842 if (meters_from_start)
843 *meters_from_start = last_dist;
847 if (meters_from_start)
848 *meters_from_start = current_dist;
850 return VIK_TRACKPOINT(iter->data);
856 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
858 time_t t_pos, t_start, t_end, t_total;
859 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
860 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
861 t_total = t_end - t_start;
863 t_pos = t_start + t_total * reltime;
865 if ( !tr->trackpoints )
868 GList *iter = tr->trackpoints;
871 if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
873 if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
874 if (iter->prev == NULL) /* first trackpoint */
876 time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp;
877 time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
878 if (t_before <= t_after)
882 else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
889 if (seconds_from_start)
890 *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
891 return VIK_TRACKPOINT(iter->data);
894 VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
896 gdouble maxspeed = 0.0, speed = 0.0;
898 if ( !tr->trackpoints )
901 GList *iter = tr->trackpoints;
902 VikTrackpoint *max_speed_tp = NULL;
906 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
907 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
908 (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
909 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
910 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
911 if ( speed > maxspeed ) {
913 max_speed_tp = VIK_TRACKPOINT(iter->data);
926 VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
928 gdouble maxalt = -5000.0;
929 if ( !tr->trackpoints )
932 GList *iter = tr->trackpoints;
933 VikTrackpoint *max_alt_tp = NULL;
936 if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
937 maxalt = VIK_TRACKPOINT(iter->data)->altitude;
938 max_alt_tp = VIK_TRACKPOINT(iter->data);
949 VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
951 gdouble minalt = 25000.0;
952 if ( !tr->trackpoints )
955 GList *iter = tr->trackpoints;
956 VikTrackpoint *min_alt_tp = NULL;
959 if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
960 minalt = VIK_TRACKPOINT(iter->data)->altitude;
961 min_alt_tp = VIK_TRACKPOINT(iter->data);
972 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
976 if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
977 GList *iter = tr->trackpoints->next;
981 tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
982 if ( tmp_alt > *max_alt )
984 if ( tmp_alt < *min_alt )
993 void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
996 GByteArray *b = g_byte_array_new();
1000 g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
1002 /* we'll fill out number of trackpoints later */
1004 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1006 tps = tr->trackpoints;
1009 g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
1013 *(guint *)(b->data + intp) = ntp;
1015 len = (tr->comment) ? strlen(tr->comment)+1 : 0;
1016 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1017 if (tr->comment) g_byte_array_append(b, (guint8 *)tr->comment, len);
1021 g_byte_array_free(b, FALSE);
1024 VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
1027 VikTrack *new_tr = vik_track_new();
1028 VikTrackpoint *new_tp;
1032 /* only the visibility is needed */
1033 new_tr->visible = ((VikTrack *)data)->visible;
1034 data += sizeof(*new_tr);
1036 ntp = *(guint *)data;
1037 data += sizeof(ntp);
1039 for (i=0; i<ntp; i++) {
1040 new_tp = vik_trackpoint_new();
1041 memcpy(new_tp, data, sizeof(*new_tp));
1042 data += sizeof(*new_tp);
1043 new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
1046 len = *(guint *)data;
1047 data += sizeof(len);
1049 new_tr->comment = g_strdup((gchar *)data);
1054 void vik_track_apply_dem_data ( VikTrack *tr )
1058 tp_iter = tr->trackpoints;
1060 /* TODO: of the 4 possible choices we have for choosing an elevation
1061 * (trackpoint in between samples), choose the one with the least elevation change
1063 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST );
1064 if ( elev != VIK_DEM_INVALID_ELEVATION )
1065 VIK_TRACKPOINT(tp_iter->data)->altitude = elev;
1066 tp_iter = tp_iter->next;
1070 /* appends t2 to t1, leaving t2 with no trackpoints */
1071 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
1073 if ( t1->trackpoints ) {
1074 GList *tpiter = t1->trackpoints;
1075 while ( tpiter->next )
1076 tpiter = tpiter->next;
1077 tpiter->next = t2->trackpoints;
1078 t2->trackpoints->prev = tpiter;
1080 t1->trackpoints = t2->trackpoints;
1081 t2->trackpoints = NULL;
1084 /* starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
1085 * If there is no double point, deletes all the trackpoints.
1086 * Returns the new end of the track (or the start if there are no double points)
1088 VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr )
1090 GList *iter = tr->trackpoints;
1095 while ( iter->next )
1099 while ( iter->prev ) {
1100 if ( vik_coord_equals((VikCoord *)iter->data, (VikCoord *)iter->prev->data) ) {
1101 GList *prev = iter->prev;
1103 rv = g_malloc(sizeof(VikCoord));
1104 *rv = *((VikCoord *) iter->data);
1106 /* truncate trackpoint list */
1107 iter->prev = NULL; /* pretend it's the end */
1108 g_list_foreach ( iter, (GFunc) g_free, NULL );
1109 g_list_free( iter );
1118 /* no double point found! */
1119 rv = g_malloc(sizeof(VikCoord));
1120 *rv = *((VikCoord *) tr->trackpoints->data);
1121 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
1122 g_list_free( tr->trackpoints );
1123 tr->trackpoints = NULL;