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 pts[current_chunk] = VIK_DEFAULT_ALTITUDE;
417 pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
421 /* finish current seg */
422 if ( current_seg_length ) {
423 gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg;
424 current_dist = current_seg_length - dist_along_seg;
425 current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5;
426 } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */
428 /* get intervening segs */
430 while ( iter && iter->next ) {
431 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
432 &(VIK_TRACKPOINT(iter->next->data)->coord) );
433 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
434 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
435 ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment;
437 if ( chunk_length - current_dist >= current_seg_length ) {
438 current_dist += current_seg_length;
439 current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5;
447 dist_along_seg = chunk_length - current_dist;
448 if ( ignore_it || !iter->next ) {
449 pts[current_chunk] = current_area_under_curve / current_dist;
452 for (i = current_chunk + 1; i < num_chunks; i++)
453 pts[i] = pts[current_chunk];
458 current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length);
459 pts[current_chunk] = current_area_under_curve / chunk_length;
471 void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
475 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE )
477 GList *iter = tr->trackpoints->next;
480 diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude;
488 *up = *down = VIK_DEFAULT_ALTITUDE;
492 /* by Alex Foobarian */
493 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
496 gdouble duration, chunk_dur;
498 int i, pt_count, numpts, index;
501 if ( ! tr->trackpoints )
504 g_assert ( num_chunks < 16000 );
506 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
507 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
510 if ( !t1 || !t2 || !duration )
514 g_warning("negative duration: unsorted trackpoint timestamps?");
517 pt_count = vik_track_get_tp_count(tr);
519 v = g_malloc ( sizeof(gdouble) * num_chunks );
520 chunk_dur = duration / num_chunks;
522 s = g_malloc(sizeof(double) * pt_count);
523 t = g_malloc(sizeof(double) * pt_count);
525 iter = tr->trackpoints->next;
528 t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
531 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
532 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
537 /* In the following computation, we iterate through periods of time of duration chunk_dur.
538 * The first period begins at the beginning of the track. The last period ends at the end of the track.
540 index = 0; /* index of the current trackpoint. */
541 for (i = 0; i < num_chunks; i++) {
542 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
543 * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints.
545 if (t[0] + i*chunk_dur >= t[index]) {
546 gdouble acc_t = 0, acc_s = 0;
547 while (t[0] + i*chunk_dur >= t[index]) {
548 acc_s += (s[index+1]-s[index]);
549 acc_t += (t[index+1]-t[index]);
567 * Make a distance/time map, heavily based on the vik_track_make_speed_map method
569 gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks )
572 gdouble duration, chunk_dur;
574 int i, pt_count, numpts, index;
577 if ( ! tr->trackpoints )
580 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
581 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
584 if ( !t1 || !t2 || !duration )
588 g_warning("negative duration: unsorted trackpoint timestamps?");
591 pt_count = vik_track_get_tp_count(tr);
593 v = g_malloc ( sizeof(gdouble) * num_chunks );
594 chunk_dur = duration / num_chunks;
596 s = g_malloc(sizeof(double) * pt_count);
597 t = g_malloc(sizeof(double) * pt_count);
599 iter = tr->trackpoints->next;
602 t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
605 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
606 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
611 /* In the following computation, we iterate through periods of time of duration chunk_dur.
612 * The first period begins at the beginning of the track. The last period ends at the end of the track.
614 index = 0; /* index of the current trackpoint. */
615 for (i = 0; i < num_chunks; i++) {
616 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
617 * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints.
619 if (t[0] + i*chunk_dur >= t[index]) {
620 gdouble acc_s = 0; // No need for acc_t
621 while (t[0] + i*chunk_dur >= t[index]) {
622 acc_s += (s[index+1]-s[index]);
625 // The only bit that's really different from the speed map - just keep an accululative record distance
626 v[i] = i ? v[i-1]+acc_s : acc_s;
641 * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance)
642 * This results in a slightly blocky graph when it does not have many trackpoints: <60
643 * NB Somehow the elevation/distance applies some kind of smoothing algorithm,
644 * but I don't think any one understands it any more (I certainly don't ATM)
646 gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks )
649 gdouble duration, chunk_dur;
650 GList *iter = tr->trackpoints;
652 if (!iter || !iter->next) /* zero- or one-point track */
655 /* test if there's anything worth calculating */
656 gboolean okay = FALSE;
658 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
667 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
668 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
671 if ( !t1 || !t2 || !duration )
675 g_warning("negative duration: unsorted trackpoint timestamps?");
678 gint pt_count = vik_track_get_tp_count(tr);
680 // Reset iterator back to the beginning
681 iter = tr->trackpoints;
683 gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values
684 gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes
685 gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times
687 chunk_dur = duration / num_chunks;
689 s[0] = VIK_TRACKPOINT(iter->data)->altitude;
690 t[0] = VIK_TRACKPOINT(iter->data)->timestamp;
691 iter = tr->trackpoints->next;
694 s[numpts] = VIK_TRACKPOINT(iter->data)->altitude;
695 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
700 /* In the following computation, we iterate through periods of time of duration chunk_dur.
701 * The first period begins at the beginning of the track. The last period ends at the end of the track.
703 gint index = 0; /* index of the current trackpoint. */
705 for (i = 0; i < num_chunks; i++) {
706 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
707 * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints.
709 if (t[0] + i*chunk_dur >= t[index]) {
710 gdouble acc_s = s[index]; // initialise to first point
711 while (t[0] + i*chunk_dur >= t[index]) {
712 acc_s += (s[index+1]-s[index]);
731 * Make a speed/distance map
733 gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks )
737 gint i, pt_count, numpts, index;
739 gdouble duration, total_length, chunk_length;
741 if ( ! tr->trackpoints )
744 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
745 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
748 if ( !t1 || !t2 || !duration )
752 g_warning("negative duration: unsorted trackpoint timestamps?");
756 total_length = vik_track_get_length_including_gaps ( tr );
757 chunk_length = total_length / num_chunks;
758 pt_count = vik_track_get_tp_count(tr);
760 if (chunk_length <= 0) {
764 v = g_malloc ( sizeof(gdouble) * num_chunks );
765 s = g_malloc ( sizeof(double) * pt_count );
766 t = g_malloc ( sizeof(double) * pt_count );
768 // No special handling of segments ATM...
769 iter = tr->trackpoints->next;
772 t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
775 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
776 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
781 // Iterate through a portion of the track to get an average speed for that part
782 // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps'
783 index = 0; /* index of the current trackpoint. */
784 for (i = 0; i < num_chunks; i++) {
785 // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk
786 if (s[0] + i*chunk_length >= s[index]) {
787 gdouble acc_t = 0, acc_s = 0;
788 while (s[0] + i*chunk_length >= s[index]) {
789 acc_s += (s[index+1]-s[index]);
790 acc_t += (t[index+1]-t[index]);
807 /* by Alex Foobarian */
808 VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start )
810 gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
811 gdouble current_dist = 0.0;
812 gdouble current_inc = 0.0;
813 if ( tr->trackpoints )
815 GList *iter = tr->trackpoints->next;
816 GList *last_iter = NULL;
817 gdouble last_dist = 0.0;
820 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
821 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
822 last_dist = current_dist;
823 current_dist += current_inc;
824 if ( current_dist >= dist )
829 if (!iter) { /* passing the end the track */
831 if (meters_from_start)
832 *meters_from_start = last_dist;
833 return(VIK_TRACKPOINT(last_iter->data));
838 /* we've gone past the dist already, was prev trackpoint closer? */
839 /* should do a vik_coord_average_weighted() thingy. */
840 if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) {
841 if (meters_from_start)
842 *meters_from_start = last_dist;
846 if (meters_from_start)
847 *meters_from_start = current_dist;
849 return VIK_TRACKPOINT(iter->data);
855 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
857 time_t t_pos, t_start, t_end, t_total;
858 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
859 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
860 t_total = t_end - t_start;
862 t_pos = t_start + t_total * reltime;
864 if ( !tr->trackpoints )
867 GList *iter = tr->trackpoints;
870 if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
872 if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
873 if (iter->prev == NULL) /* first trackpoint */
875 time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp;
876 time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
877 if (t_before <= t_after)
881 else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
888 if (seconds_from_start)
889 *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
890 return VIK_TRACKPOINT(iter->data);
893 VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
895 gdouble maxspeed = 0.0, speed = 0.0;
897 if ( !tr->trackpoints )
900 GList *iter = tr->trackpoints;
901 VikTrackpoint *max_speed_tp = NULL;
905 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
906 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
907 (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
908 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
909 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
910 if ( speed > maxspeed ) {
912 max_speed_tp = VIK_TRACKPOINT(iter->data);
925 VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
927 gdouble maxalt = -5000.0;
928 if ( !tr->trackpoints )
931 GList *iter = tr->trackpoints;
932 VikTrackpoint *max_alt_tp = NULL;
935 if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
936 maxalt = VIK_TRACKPOINT(iter->data)->altitude;
937 max_alt_tp = VIK_TRACKPOINT(iter->data);
948 VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
950 gdouble minalt = 25000.0;
951 if ( !tr->trackpoints )
954 GList *iter = tr->trackpoints;
955 VikTrackpoint *min_alt_tp = NULL;
958 if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
959 minalt = VIK_TRACKPOINT(iter->data)->altitude;
960 min_alt_tp = VIK_TRACKPOINT(iter->data);
971 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
975 if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
976 GList *iter = tr->trackpoints->next;
980 tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
981 if ( tmp_alt > *max_alt )
983 if ( tmp_alt < *min_alt )
992 void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
995 GByteArray *b = g_byte_array_new();
999 g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
1001 /* we'll fill out number of trackpoints later */
1003 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1005 tps = tr->trackpoints;
1008 g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
1012 *(guint *)(b->data + intp) = ntp;
1014 len = (tr->comment) ? strlen(tr->comment)+1 : 0;
1015 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1016 if (tr->comment) g_byte_array_append(b, (guint8 *)tr->comment, len);
1020 g_byte_array_free(b, FALSE);
1023 VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
1026 VikTrack *new_tr = vik_track_new();
1027 VikTrackpoint *new_tp;
1031 /* only the visibility is needed */
1032 new_tr->visible = ((VikTrack *)data)->visible;
1033 data += sizeof(*new_tr);
1035 ntp = *(guint *)data;
1036 data += sizeof(ntp);
1038 for (i=0; i<ntp; i++) {
1039 new_tp = vik_trackpoint_new();
1040 memcpy(new_tp, data, sizeof(*new_tp));
1041 data += sizeof(*new_tp);
1042 new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
1045 len = *(guint *)data;
1046 data += sizeof(len);
1048 new_tr->comment = g_strdup((gchar *)data);
1053 void vik_track_apply_dem_data ( VikTrack *tr )
1057 tp_iter = tr->trackpoints;
1059 /* TODO: of the 4 possible choices we have for choosing an elevation
1060 * (trackpoint in between samples), choose the one with the least elevation change
1062 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST );
1063 if ( elev != VIK_DEM_INVALID_ELEVATION )
1064 VIK_TRACKPOINT(tp_iter->data)->altitude = elev;
1065 tp_iter = tp_iter->next;
1069 /* appends t2 to t1, leaving t2 with no trackpoints */
1070 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
1072 if ( t1->trackpoints ) {
1073 GList *tpiter = t1->trackpoints;
1074 while ( tpiter->next )
1075 tpiter = tpiter->next;
1076 tpiter->next = t2->trackpoints;
1077 t2->trackpoints->prev = tpiter;
1079 t1->trackpoints = t2->trackpoints;
1080 t2->trackpoints = NULL;
1083 /* starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
1084 * this is indicative of magic scissors continued use. If there is no double point,
1085 * deletes all the trackpoints. Returns the new end of the track (or the start if
1086 * 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;