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_name(VikTrack *tr, const gchar *name)
61 if ( name && name[0] != '\0' )
62 tr->name = g_strdup(name);
67 void vik_track_set_comment(VikTrack *tr, const gchar *comment)
70 g_free ( tr->comment );
72 if ( comment && comment[0] != '\0' )
73 tr->comment = g_strdup(comment);
78 void vik_track_ref(VikTrack *tr)
83 void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog)
85 /* Warning: does not check for existing dialog */
86 tr->property_dialog = dialog;
89 void vik_track_clear_property_dialog(VikTrack *tr)
91 tr->property_dialog = NULL;
94 void vik_track_free(VikTrack *tr)
96 if ( tr->ref_count-- > 1 )
102 g_free ( tr->comment );
103 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
104 g_list_free( tr->trackpoints );
105 if (tr->property_dialog)
106 if ( GTK_IS_WIDGET(tr->property_dialog) )
107 gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) );
111 VikTrack *vik_track_copy ( const VikTrack *tr )
113 VikTrack *new_tr = vik_track_new();
114 VikTrackpoint *new_tp;
115 GList *tp_iter = tr->trackpoints;
116 new_tr->visible = tr->visible;
117 new_tr->trackpoints = NULL;
120 new_tp = g_malloc ( sizeof ( VikTrackpoint ) );
121 *new_tp = *((VikTrackpoint *)(tp_iter->data));
122 new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp );
123 tp_iter = tp_iter->next;
125 vik_track_set_name(new_tr,tr->name);
126 vik_track_set_comment(new_tr,tr->comment);
130 VikTrackpoint *vik_trackpoint_new()
132 VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint));
135 tp->altitude = VIK_DEFAULT_ALTITUDE;
136 tp->hdop = VIK_DEFAULT_DOP;
137 tp->vdop = VIK_DEFAULT_DOP;
138 tp->pdop = VIK_DEFAULT_DOP;
142 void vik_trackpoint_free(VikTrackpoint *tp)
147 VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
149 VikTrackpoint *rv = vik_trackpoint_new();
154 gdouble vik_track_get_length(const VikTrack *tr)
157 if ( tr->trackpoints )
159 GList *iter = tr->trackpoints->next;
162 if ( ! VIK_TRACKPOINT(iter->data)->newsegment )
163 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
164 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
171 gdouble vik_track_get_length_including_gaps(const VikTrack *tr)
174 if ( tr->trackpoints )
176 GList *iter = tr->trackpoints->next;
179 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
180 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
187 gulong vik_track_get_tp_count(const VikTrack *tr)
190 GList *iter = tr->trackpoints;
199 gulong vik_track_get_dup_point_count ( const VikTrack *tr )
202 GList *iter = tr->trackpoints;
205 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
206 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
213 void vik_track_remove_dup_points ( VikTrack *tr )
215 GList *iter = tr->trackpoints;
218 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
219 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
221 g_free ( iter->next->data );
222 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
229 guint vik_track_get_segment_count(const VikTrack *tr)
232 GList *iter = tr->trackpoints;
235 while ( (iter = iter->next) )
237 if ( VIK_TRACKPOINT(iter->data)->newsegment )
243 VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
248 guint segs = vik_track_get_segment_count(t);
257 rv = g_malloc ( segs * sizeof(VikTrack *) );
258 tr = vik_track_copy ( t );
260 iter = tr->trackpoints;
263 while ( (iter = iter->next) )
265 if ( VIK_TRACKPOINT(iter->data)->newsegment )
267 iter->prev->next = NULL;
269 rv[i] = vik_track_new();
270 // TODO: consider new naming strategy here
272 vik_track_set_name ( rv[i], tr->name );
274 vik_track_set_comment ( rv[i], tr->comment );
275 rv[i]->visible = tr->visible;
276 rv[i]->trackpoints = iter;
284 void vik_track_reverse ( VikTrack *tr )
287 tr->trackpoints = g_list_reverse(tr->trackpoints);
289 /* fix 'newsegment' */
290 iter = g_list_last ( tr->trackpoints );
293 if ( ! iter->next ) /* last segment, was first, cancel newsegment. */
294 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
295 if ( ! iter->prev ) /* first segment by convention has newsegment flag. */
296 VIK_TRACKPOINT(iter->data)->newsegment = TRUE;
297 else if ( VIK_TRACKPOINT(iter->data)->newsegment && iter->next )
299 VIK_TRACKPOINT(iter->next->data)->newsegment = TRUE;
300 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
306 gdouble vik_track_get_average_speed(const VikTrack *tr)
310 if ( tr->trackpoints )
312 GList *iter = tr->trackpoints->next;
315 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
316 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
317 (! VIK_TRACKPOINT(iter->data)->newsegment) )
319 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
320 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
321 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
326 return (time == 0) ? 0 : ABS(len/time);
330 * Based on a simple average speed, but with a twist - to give a moving average.
331 * . GPSs often report a moving average in their statistics output
332 * . bicycle speedos often don't factor in time when stopped - hence reporting a moving average for speed
334 * Often GPS track will record every second but not when stationary
335 * This method doesn't use samples that differ over the specified time limit - effectively skipping that time chunk from the total time
337 * Suggest to use 60 seconds as the stop length (as the default used in the TrackWaypoint draw stops factor)
339 gdouble vik_track_get_average_speed_moving (const VikTrack *tr, int stop_length_seconds)
343 if ( tr->trackpoints )
345 GList *iter = tr->trackpoints->next;
348 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
349 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
350 (! VIK_TRACKPOINT(iter->data)->newsegment) )
352 if ( ( VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp ) < stop_length_seconds ) {
353 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
354 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
356 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
362 return (time == 0) ? 0 : ABS(len/time);
365 gdouble vik_track_get_max_speed(const VikTrack *tr)
367 gdouble maxspeed = 0.0, speed = 0.0;
368 if ( tr->trackpoints )
370 GList *iter = tr->trackpoints->next;
373 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
374 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
375 (! VIK_TRACKPOINT(iter->data)->newsegment) )
377 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
378 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
379 if ( speed > maxspeed )
388 void vik_track_convert ( VikTrack *tr, VikCoordMode dest_mode )
390 GList *iter = tr->trackpoints;
393 vik_coord_convert ( &(VIK_TRACKPOINT(iter->data)->coord), dest_mode );
398 /* I understood this when I wrote it ... maybe ... Basically it eats up the
399 * proper amounts of length on the track and averages elevation over that. */
400 gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
403 gdouble total_length, chunk_length, current_dist, current_area_under_curve, current_seg_length, dist_along_seg = 0.0;
404 gdouble altitude1, altitude2;
405 guint16 current_chunk;
406 gboolean ignore_it = FALSE;
408 GList *iter = tr->trackpoints;
410 if (!iter || !iter->next) /* zero- or one-point track */
413 { /* test if there's anything worth calculating */
414 gboolean okay = FALSE;
417 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
426 iter = tr->trackpoints;
428 g_assert ( num_chunks < 16000 );
430 pts = g_malloc ( sizeof(gdouble) * num_chunks );
432 total_length = vik_track_get_length_including_gaps ( tr );
433 chunk_length = total_length / num_chunks;
435 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
436 if (chunk_length <= 0) {
442 current_area_under_curve = 0;
444 current_seg_length = 0;
446 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
447 &(VIK_TRACKPOINT(iter->next->data)->coord) );
448 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
449 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
452 while ( current_chunk < num_chunks ) {
454 /* go along current seg */
455 if ( current_seg_length && (current_seg_length - dist_along_seg) > chunk_length ) {
456 dist_along_seg += chunk_length;
460 * /x altitude = alt_at_pt_1 + alt_at_pt_2 / 2 = altitude1 + slope * dist_value_of_pt_inbetween_pt1_and_pt2
461 * /xx avg altitude = area under curve / chunk len
462 *pt1 *xxx avg altitude = altitude1 + (altitude2-altitude1)/(current_seg_length)*(dist_along_seg + (chunk_len/2))
468 // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero)
469 pts[current_chunk] = altitude1;
471 pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
475 /* finish current seg */
476 if ( current_seg_length ) {
477 gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg;
478 current_dist = current_seg_length - dist_along_seg;
479 current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5;
480 } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */
482 /* get intervening segs */
484 while ( iter && iter->next ) {
485 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
486 &(VIK_TRACKPOINT(iter->next->data)->coord) );
487 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
488 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
489 ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment;
491 if ( chunk_length - current_dist >= current_seg_length ) {
492 current_dist += current_seg_length;
493 current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5;
501 dist_along_seg = chunk_length - current_dist;
502 if ( ignore_it || ( iter && !iter->next ) ) {
503 pts[current_chunk] = current_area_under_curve / current_dist;
506 for (i = current_chunk + 1; i < num_chunks; i++)
507 pts[i] = pts[current_chunk];
512 current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length);
513 pts[current_chunk] = current_area_under_curve / chunk_length;
525 void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
529 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE )
531 GList *iter = tr->trackpoints->next;
534 diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude;
542 *up = *down = VIK_DEFAULT_ALTITUDE;
545 gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks )
549 gdouble total_length, chunk_length, current_gradient;
550 gdouble altitude1, altitude2;
551 guint16 current_chunk;
553 g_assert ( num_chunks < 16000 );
555 total_length = vik_track_get_length_including_gaps ( tr );
556 chunk_length = total_length / num_chunks;
558 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
559 if (chunk_length <= 0) {
563 altitudes = vik_track_make_elevation_map (tr, num_chunks);
564 if (altitudes == NULL) {
568 current_gradient = 0.0;
569 pts = g_malloc ( sizeof(gdouble) * num_chunks );
570 for (current_chunk = 0; current_chunk < (num_chunks - 1); current_chunk++) {
571 altitude1 = altitudes[current_chunk];
572 altitude2 = altitudes[current_chunk + 1];
573 current_gradient = 100.0 * (altitude2 - altitude1) / chunk_length;
575 pts[current_chunk] = current_gradient;
578 pts[current_chunk] = current_gradient;
583 /* by Alex Foobarian */
584 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
587 gdouble duration, chunk_dur;
589 int i, pt_count, numpts, index;
592 if ( ! tr->trackpoints )
595 g_assert ( num_chunks < 16000 );
597 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
598 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
601 if ( !t1 || !t2 || !duration )
605 g_warning("negative duration: unsorted trackpoint timestamps?");
608 pt_count = vik_track_get_tp_count(tr);
610 v = g_malloc ( sizeof(gdouble) * num_chunks );
611 chunk_dur = duration / num_chunks;
613 s = g_malloc(sizeof(double) * pt_count);
614 t = g_malloc(sizeof(double) * pt_count);
616 iter = tr->trackpoints->next;
619 t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
622 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
623 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
628 /* In the following computation, we iterate through periods of time of duration chunk_dur.
629 * The first period begins at the beginning of the track. The last period ends at the end of the track.
631 index = 0; /* index of the current trackpoint. */
632 for (i = 0; i < num_chunks; i++) {
633 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
634 * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints.
636 if (t[0] + i*chunk_dur >= t[index]) {
637 gdouble acc_t = 0, acc_s = 0;
638 while (t[0] + i*chunk_dur >= t[index]) {
639 acc_s += (s[index+1]-s[index]);
640 acc_t += (t[index+1]-t[index]);
658 * Make a distance/time map, heavily based on the vik_track_make_speed_map method
660 gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks )
663 gdouble duration, chunk_dur;
665 int i, pt_count, numpts, index;
668 if ( ! tr->trackpoints )
671 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
672 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
675 if ( !t1 || !t2 || !duration )
679 g_warning("negative duration: unsorted trackpoint timestamps?");
682 pt_count = vik_track_get_tp_count(tr);
684 v = g_malloc ( sizeof(gdouble) * num_chunks );
685 chunk_dur = duration / num_chunks;
687 s = g_malloc(sizeof(double) * pt_count);
688 t = g_malloc(sizeof(double) * pt_count);
690 iter = tr->trackpoints->next;
693 t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
696 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
697 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
702 /* In the following computation, we iterate through periods of time of duration chunk_dur.
703 * The first period begins at the beginning of the track. The last period ends at the end of the track.
705 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 distance between intermediate trackpoints.
710 if (t[0] + i*chunk_dur >= t[index]) {
711 gdouble acc_s = 0; // No need for acc_t
712 while (t[0] + i*chunk_dur >= t[index]) {
713 acc_s += (s[index+1]-s[index]);
716 // The only bit that's really different from the speed map - just keep an accululative record distance
717 v[i] = i ? v[i-1]+acc_s : acc_s;
732 * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance)
733 * This results in a slightly blocky graph when it does not have many trackpoints: <60
734 * NB Somehow the elevation/distance applies some kind of smoothing algorithm,
735 * but I don't think any one understands it any more (I certainly don't ATM)
737 gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks )
740 gdouble duration, chunk_dur;
741 GList *iter = tr->trackpoints;
743 if (!iter || !iter->next) /* zero- or one-point track */
746 /* test if there's anything worth calculating */
747 gboolean okay = FALSE;
749 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
758 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
759 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
762 if ( !t1 || !t2 || !duration )
766 g_warning("negative duration: unsorted trackpoint timestamps?");
769 gint pt_count = vik_track_get_tp_count(tr);
771 // Reset iterator back to the beginning
772 iter = tr->trackpoints;
774 gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values
775 gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes
776 gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times
778 chunk_dur = duration / num_chunks;
780 s[0] = VIK_TRACKPOINT(iter->data)->altitude;
781 t[0] = VIK_TRACKPOINT(iter->data)->timestamp;
782 iter = tr->trackpoints->next;
785 s[numpts] = VIK_TRACKPOINT(iter->data)->altitude;
786 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
791 /* In the following computation, we iterate through periods of time of duration chunk_dur.
792 * The first period begins at the beginning of the track. The last period ends at the end of the track.
794 gint index = 0; /* index of the current trackpoint. */
796 for (i = 0; i < num_chunks; i++) {
797 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
798 * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints.
800 if (t[0] + i*chunk_dur >= t[index]) {
801 gdouble acc_s = s[index]; // initialise to first point
802 while (t[0] + i*chunk_dur >= t[index]) {
803 acc_s += (s[index+1]-s[index]);
822 * Make a speed/distance map
824 gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks )
828 gint i, pt_count, numpts, index;
830 gdouble duration, total_length, chunk_length;
832 if ( ! tr->trackpoints )
835 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
836 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
839 if ( !t1 || !t2 || !duration )
843 g_warning("negative duration: unsorted trackpoint timestamps?");
847 total_length = vik_track_get_length_including_gaps ( tr );
848 chunk_length = total_length / num_chunks;
849 pt_count = vik_track_get_tp_count(tr);
851 if (chunk_length <= 0) {
855 v = g_malloc ( sizeof(gdouble) * num_chunks );
856 s = g_malloc ( sizeof(double) * pt_count );
857 t = g_malloc ( sizeof(double) * pt_count );
859 // No special handling of segments ATM...
860 iter = tr->trackpoints->next;
863 t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
866 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
867 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
872 // Iterate through a portion of the track to get an average speed for that part
873 // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps'
874 index = 0; /* index of the current trackpoint. */
875 for (i = 0; i < num_chunks; i++) {
876 // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk
877 if (s[0] + i*chunk_length >= s[index]) {
878 gdouble acc_t = 0, acc_s = 0;
879 while (s[0] + i*chunk_length >= s[index]) {
880 acc_s += (s[index+1]-s[index]);
881 acc_t += (t[index+1]-t[index]);
898 /* by Alex Foobarian */
899 VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start )
901 gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
902 gdouble current_dist = 0.0;
903 gdouble current_inc = 0.0;
904 if ( tr->trackpoints )
906 GList *iter = tr->trackpoints->next;
907 GList *last_iter = NULL;
908 gdouble last_dist = 0.0;
911 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
912 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
913 last_dist = current_dist;
914 current_dist += current_inc;
915 if ( current_dist >= dist )
920 if (!iter) { /* passing the end the track */
922 if (meters_from_start)
923 *meters_from_start = last_dist;
924 return(VIK_TRACKPOINT(last_iter->data));
929 /* we've gone past the dist already, was prev trackpoint closer? */
930 /* should do a vik_coord_average_weighted() thingy. */
931 if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) {
932 if (meters_from_start)
933 *meters_from_start = last_dist;
937 if (meters_from_start)
938 *meters_from_start = current_dist;
940 return VIK_TRACKPOINT(iter->data);
946 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
948 time_t t_pos, t_start, t_end, t_total;
949 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
950 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
951 t_total = t_end - t_start;
953 t_pos = t_start + t_total * reltime;
955 if ( !tr->trackpoints )
958 GList *iter = tr->trackpoints;
961 if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
963 if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
964 if (iter->prev == NULL) /* first trackpoint */
966 time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp;
967 time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
968 if (t_before <= t_after)
972 else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
979 if (seconds_from_start)
980 *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
981 return VIK_TRACKPOINT(iter->data);
984 VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
986 gdouble maxspeed = 0.0, speed = 0.0;
988 if ( !tr->trackpoints )
991 GList *iter = tr->trackpoints;
992 VikTrackpoint *max_speed_tp = NULL;
996 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
997 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
998 (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
999 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
1000 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
1001 if ( speed > maxspeed ) {
1003 max_speed_tp = VIK_TRACKPOINT(iter->data);
1013 return max_speed_tp;
1016 VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
1018 gdouble maxalt = -5000.0;
1019 if ( !tr->trackpoints )
1022 GList *iter = tr->trackpoints;
1023 VikTrackpoint *max_alt_tp = NULL;
1026 if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
1027 maxalt = VIK_TRACKPOINT(iter->data)->altitude;
1028 max_alt_tp = VIK_TRACKPOINT(iter->data);
1039 VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
1041 gdouble minalt = 25000.0;
1042 if ( !tr->trackpoints )
1045 GList *iter = tr->trackpoints;
1046 VikTrackpoint *min_alt_tp = NULL;
1049 if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
1050 minalt = VIK_TRACKPOINT(iter->data)->altitude;
1051 min_alt_tp = VIK_TRACKPOINT(iter->data);
1062 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
1066 if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1067 GList *iter = tr->trackpoints->next;
1071 tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
1072 if ( tmp_alt > *max_alt )
1074 if ( tmp_alt < *min_alt )
1083 void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
1086 GByteArray *b = g_byte_array_new();
1090 g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
1092 /* we'll fill out number of trackpoints later */
1094 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1096 tps = tr->trackpoints;
1099 g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
1103 *(guint *)(b->data + intp) = ntp;
1105 len = (tr->name) ? strlen(tr->name)+1 : 0;
1106 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1107 if (tr->name) g_byte_array_append(b, (guint8 *)tr->name, len);
1109 len = (tr->comment) ? strlen(tr->comment)+1 : 0;
1110 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1111 if (tr->comment) g_byte_array_append(b, (guint8 *)tr->comment, len);
1115 g_byte_array_free(b, FALSE);
1118 VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
1121 VikTrack *new_tr = vik_track_new();
1122 VikTrackpoint *new_tp;
1126 /* only the visibility is needed */
1127 new_tr->visible = ((VikTrack *)data)->visible;
1128 data += sizeof(*new_tr);
1130 ntp = *(guint *)data;
1131 data += sizeof(ntp);
1133 for (i=0; i<ntp; i++) {
1134 new_tp = vik_trackpoint_new();
1135 memcpy(new_tp, data, sizeof(*new_tp));
1136 data += sizeof(*new_tp);
1137 new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
1140 len = *(guint *)data;
1141 data += sizeof(len);
1143 new_tr->name = g_strdup((gchar *)data);
1147 len = *(guint *)data;
1148 data += sizeof(len);
1150 new_tr->comment = g_strdup((gchar *)data);
1155 void vik_track_apply_dem_data ( VikTrack *tr )
1159 tp_iter = tr->trackpoints;
1161 /* TODO: of the 4 possible choices we have for choosing an elevation
1162 * (trackpoint in between samples), choose the one with the least elevation change
1164 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST );
1165 if ( elev != VIK_DEM_INVALID_ELEVATION )
1166 VIK_TRACKPOINT(tp_iter->data)->altitude = elev;
1167 tp_iter = tp_iter->next;
1172 * Apply DEM data (if available) - to only the last trackpoint
1174 void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr )
1177 if ( tr->trackpoints ) {
1178 /* As in vik_track_apply_dem_data above - use 'best' interpolation method */
1179 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->coord), VIK_DEM_INTERPOL_BEST );
1180 if ( elev != VIK_DEM_INVALID_ELEVATION )
1181 VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->altitude = elev;
1185 /* appends t2 to t1, leaving t2 with no trackpoints */
1186 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
1188 if ( t1->trackpoints ) {
1189 GList *tpiter = t1->trackpoints;
1190 while ( tpiter->next )
1191 tpiter = tpiter->next;
1192 tpiter->next = t2->trackpoints;
1193 t2->trackpoints->prev = tpiter;
1195 t1->trackpoints = t2->trackpoints;
1196 t2->trackpoints = NULL;
1199 /* starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
1200 * If there is no double point, deletes all the trackpoints.
1201 * Returns the new end of the track (or the start if there are no double points)
1203 VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr )
1205 GList *iter = tr->trackpoints;
1210 while ( iter->next )
1214 while ( iter->prev ) {
1215 if ( vik_coord_equals((VikCoord *)iter->data, (VikCoord *)iter->prev->data) ) {
1216 GList *prev = iter->prev;
1218 rv = g_malloc(sizeof(VikCoord));
1219 *rv = *((VikCoord *) iter->data);
1221 /* truncate trackpoint list */
1222 iter->prev = NULL; /* pretend it's the end */
1223 g_list_foreach ( iter, (GFunc) g_free, NULL );
1224 g_list_free( iter );
1233 /* no double point found! */
1234 rv = g_malloc(sizeof(VikCoord));
1235 *rv = *((VikCoord *) tr->trackpoints->data);
1236 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
1237 g_list_free( tr->trackpoints );
1238 tr->trackpoints = NULL;