2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (c) 2012, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 VikTrack *vik_track_new()
44 VikTrack *tr = g_malloc0 ( sizeof ( VikTrack ) );
49 void vik_track_set_comment_no_copy(VikTrack *tr, gchar *comment)
52 g_free ( tr->comment );
53 tr->comment = comment;
57 void vik_track_set_name(VikTrack *tr, const gchar *name)
62 if ( name && name[0] != '\0' )
63 tr->name = g_strdup(name);
68 void vik_track_set_comment(VikTrack *tr, const gchar *comment)
71 g_free ( tr->comment );
73 if ( comment && comment[0] != '\0' )
74 tr->comment = g_strdup(comment);
79 void vik_track_set_description(VikTrack *tr, const gchar *description)
81 if ( tr->description )
82 g_free ( tr->description );
84 if ( description && description[0] != '\0' )
85 tr->description = g_strdup(description);
87 tr->description = NULL;
90 void vik_track_ref(VikTrack *tr)
95 void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog)
97 /* Warning: does not check for existing dialog */
98 tr->property_dialog = dialog;
101 void vik_track_clear_property_dialog(VikTrack *tr)
103 tr->property_dialog = NULL;
106 void vik_track_free(VikTrack *tr)
108 if ( tr->ref_count-- > 1 )
114 g_free ( tr->comment );
115 if ( tr->description )
116 g_free ( tr->description );
117 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
118 g_list_free( tr->trackpoints );
119 if (tr->property_dialog)
120 if ( GTK_IS_WIDGET(tr->property_dialog) )
121 gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) );
127 * @tr: The Track to copy
128 * @copy_points: Whether to copy the track points or not
130 * Normally for copying the track it's best to copy all the trackpoints
131 * However for some operations such as splitting tracks the trackpoints will be managed separately, so no need to copy them.
133 * Returns: the copied VikTrack
135 VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points )
137 VikTrack *new_tr = vik_track_new();
138 VikTrackpoint *new_tp;
139 GList *tp_iter = tr->trackpoints;
140 new_tr->visible = tr->visible;
141 new_tr->is_route = tr->is_route;
142 new_tr->has_color = tr->has_color;
143 new_tr->color = tr->color;
144 new_tr->trackpoints = NULL;
149 new_tp = g_malloc ( sizeof ( VikTrackpoint ) );
150 *new_tp = *((VikTrackpoint *)(tp_iter->data));
151 new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp );
152 tp_iter = tp_iter->next;
155 vik_track_set_name(new_tr,tr->name);
156 vik_track_set_comment(new_tr,tr->comment);
157 vik_track_set_description(new_tr,tr->description);
161 VikTrackpoint *vik_trackpoint_new()
163 VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint));
166 tp->altitude = VIK_DEFAULT_ALTITUDE;
167 tp->hdop = VIK_DEFAULT_DOP;
168 tp->vdop = VIK_DEFAULT_DOP;
169 tp->pdop = VIK_DEFAULT_DOP;
173 void vik_trackpoint_free(VikTrackpoint *tp)
178 VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
180 VikTrackpoint *rv = vik_trackpoint_new();
185 gdouble vik_track_get_length(const VikTrack *tr)
188 if ( tr->trackpoints )
190 GList *iter = tr->trackpoints->next;
193 if ( ! VIK_TRACKPOINT(iter->data)->newsegment )
194 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
195 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
202 gdouble vik_track_get_length_including_gaps(const VikTrack *tr)
205 if ( tr->trackpoints )
207 GList *iter = tr->trackpoints->next;
210 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
211 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
218 gulong vik_track_get_tp_count(const VikTrack *tr)
221 GList *iter = tr->trackpoints;
230 gulong vik_track_get_dup_point_count ( const VikTrack *tr )
233 GList *iter = tr->trackpoints;
236 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
237 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
245 * Deletes adjacent points that have the same position
246 * Returns the number of points that were deleted
248 gulong vik_track_remove_dup_points ( VikTrack *tr )
251 GList *iter = tr->trackpoints;
254 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
255 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
258 // Maintain track segments
259 if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
260 VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
262 vik_trackpoint_free ( iter->next->data );
263 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
272 * Get a count of trackpoints with the same defined timestamp
273 * Note is using timestamps with a resolution with 1 second
275 gulong vik_track_get_same_time_point_count ( const VikTrack *tr )
278 GList *iter = tr->trackpoints;
281 ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
282 VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
283 ( VIK_TRACKPOINT(iter->data)->timestamp ==
284 VIK_TRACKPOINT(iter->next->data)->timestamp) )
292 * Deletes adjacent points that have the same defined timestamp
293 * Returns the number of points that were deleted
295 gulong vik_track_remove_same_time_points ( VikTrack *tr )
298 GList *iter = tr->trackpoints;
301 ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
302 VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
303 ( VIK_TRACKPOINT(iter->data)->timestamp ==
304 VIK_TRACKPOINT(iter->next->data)->timestamp) ) {
308 // Maintain track segments
309 if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
310 VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
312 vik_trackpoint_free ( iter->next->data );
313 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
322 * Deletes all 'extra' trackpoint information
323 * such as time stamps, speed, course etc...
325 void vik_track_to_routepoints ( VikTrack *tr )
327 GList *iter = tr->trackpoints;
330 // c.f. with vik_trackpoint_new()
332 VIK_TRACKPOINT(iter->data)->has_timestamp = FALSE;
333 VIK_TRACKPOINT(iter->data)->timestamp = 0;
334 VIK_TRACKPOINT(iter->data)->speed = NAN;
335 VIK_TRACKPOINT(iter->data)->course = NAN;
336 VIK_TRACKPOINT(iter->data)->hdop = VIK_DEFAULT_DOP;
337 VIK_TRACKPOINT(iter->data)->vdop = VIK_DEFAULT_DOP;
338 VIK_TRACKPOINT(iter->data)->pdop = VIK_DEFAULT_DOP;
339 VIK_TRACKPOINT(iter->data)->nsats = 0;
340 VIK_TRACKPOINT(iter->data)->fix_mode = VIK_GPS_MODE_NOT_SEEN;
346 guint vik_track_get_segment_count(const VikTrack *tr)
349 GList *iter = tr->trackpoints;
352 while ( (iter = iter->next) )
354 if ( VIK_TRACKPOINT(iter->data)->newsegment )
360 VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
365 guint segs = vik_track_get_segment_count(t);
374 rv = g_malloc ( segs * sizeof(VikTrack *) );
375 tr = vik_track_copy ( t, TRUE );
377 iter = tr->trackpoints;
380 while ( (iter = iter->next) )
382 if ( VIK_TRACKPOINT(iter->data)->newsegment )
384 iter->prev->next = NULL;
386 rv[i] = vik_track_copy ( tr, FALSE );
387 rv[i]->trackpoints = iter;
396 * Simply remove any subsequent segment markers in a track to form one continuous track
397 * Return the number of segments merged
399 guint vik_track_merge_segments(VikTrack *tr)
402 GList *iter = tr->trackpoints;
406 // Always skip the first point as this should be the first segment
409 while ( (iter = iter->next) )
411 if ( VIK_TRACKPOINT(iter->data)->newsegment ) {
412 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
419 void vik_track_reverse ( VikTrack *tr )
422 tr->trackpoints = g_list_reverse(tr->trackpoints);
424 /* fix 'newsegment' */
425 iter = g_list_last ( tr->trackpoints );
428 if ( ! iter->next ) /* last segment, was first, cancel newsegment. */
429 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
430 if ( ! iter->prev ) /* first segment by convention has newsegment flag. */
431 VIK_TRACKPOINT(iter->data)->newsegment = TRUE;
432 else if ( VIK_TRACKPOINT(iter->data)->newsegment && iter->next )
434 VIK_TRACKPOINT(iter->next->data)->newsegment = TRUE;
435 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
441 gdouble vik_track_get_average_speed(const VikTrack *tr)
445 if ( tr->trackpoints )
447 GList *iter = tr->trackpoints->next;
450 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
451 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
452 (! VIK_TRACKPOINT(iter->data)->newsegment) )
454 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
455 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
456 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
461 return (time == 0) ? 0 : ABS(len/time);
465 * Based on a simple average speed, but with a twist - to give a moving average.
466 * . GPSs often report a moving average in their statistics output
467 * . bicycle speedos often don't factor in time when stopped - hence reporting a moving average for speed
469 * Often GPS track will record every second but not when stationary
470 * This method doesn't use samples that differ over the specified time limit - effectively skipping that time chunk from the total time
472 * Suggest to use 60 seconds as the stop length (as the default used in the TrackWaypoint draw stops factor)
474 gdouble vik_track_get_average_speed_moving (const VikTrack *tr, int stop_length_seconds)
478 if ( tr->trackpoints )
480 GList *iter = tr->trackpoints->next;
483 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
484 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
485 (! VIK_TRACKPOINT(iter->data)->newsegment) )
487 if ( ( VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp ) < stop_length_seconds ) {
488 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
489 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
491 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
497 return (time == 0) ? 0 : ABS(len/time);
500 gdouble vik_track_get_max_speed(const VikTrack *tr)
502 gdouble maxspeed = 0.0, speed = 0.0;
503 if ( tr->trackpoints )
505 GList *iter = tr->trackpoints->next;
508 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
509 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
510 (! VIK_TRACKPOINT(iter->data)->newsegment) )
512 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
513 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
514 if ( speed > maxspeed )
523 void vik_track_convert ( VikTrack *tr, VikCoordMode dest_mode )
525 GList *iter = tr->trackpoints;
528 vik_coord_convert ( &(VIK_TRACKPOINT(iter->data)->coord), dest_mode );
533 /* I understood this when I wrote it ... maybe ... Basically it eats up the
534 * proper amounts of length on the track and averages elevation over that. */
535 gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
538 gdouble total_length, chunk_length, current_dist, current_area_under_curve, current_seg_length, dist_along_seg = 0.0;
539 gdouble altitude1, altitude2;
540 guint16 current_chunk;
541 gboolean ignore_it = FALSE;
543 GList *iter = tr->trackpoints;
545 if (!iter || !iter->next) /* zero- or one-point track */
548 { /* test if there's anything worth calculating */
549 gboolean okay = FALSE;
552 // Sometimes a GPS device (or indeed any random file) can have stupid numbers for elevations
553 // Since when is 9.9999e+24 a valid elevation!!
554 // This can happen when a track (with no elevations) is uploaded to a GPS device and then redownloaded (e.g. using a Garmin Legend EtrexHCx)
555 // Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception)
556 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE &&
557 VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) {
566 iter = tr->trackpoints;
568 g_assert ( num_chunks < 16000 );
570 pts = g_malloc ( sizeof(gdouble) * num_chunks );
572 total_length = vik_track_get_length_including_gaps ( tr );
573 chunk_length = total_length / num_chunks;
575 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
576 if (chunk_length <= 0) {
582 current_area_under_curve = 0;
584 current_seg_length = 0;
586 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
587 &(VIK_TRACKPOINT(iter->next->data)->coord) );
588 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
589 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
592 while ( current_chunk < num_chunks ) {
594 /* go along current seg */
595 if ( current_seg_length && (current_seg_length - dist_along_seg) > chunk_length ) {
596 dist_along_seg += chunk_length;
600 * /x altitude = alt_at_pt_1 + alt_at_pt_2 / 2 = altitude1 + slope * dist_value_of_pt_inbetween_pt1_and_pt2
601 * /xx avg altitude = area under curve / chunk len
602 *pt1 *xxx avg altitude = altitude1 + (altitude2-altitude1)/(current_seg_length)*(dist_along_seg + (chunk_len/2))
608 // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero)
609 pts[current_chunk] = altitude1;
611 pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
615 /* finish current seg */
616 if ( current_seg_length ) {
617 gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg;
618 current_dist = current_seg_length - dist_along_seg;
619 current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5;
620 } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */
622 /* get intervening segs */
624 while ( iter && iter->next ) {
625 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
626 &(VIK_TRACKPOINT(iter->next->data)->coord) );
627 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
628 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
629 ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment;
631 if ( chunk_length - current_dist >= current_seg_length ) {
632 current_dist += current_seg_length;
633 current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5;
641 dist_along_seg = chunk_length - current_dist;
642 if ( ignore_it || ( iter && !iter->next ) ) {
643 pts[current_chunk] = current_area_under_curve / current_dist;
646 for (i = current_chunk + 1; i < num_chunks; i++)
647 pts[i] = pts[current_chunk];
652 current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length);
653 pts[current_chunk] = current_area_under_curve / chunk_length;
665 void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
669 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE )
671 GList *iter = tr->trackpoints->next;
674 diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude;
682 *up = *down = VIK_DEFAULT_ALTITUDE;
685 gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks )
689 gdouble total_length, chunk_length, current_gradient;
690 gdouble altitude1, altitude2;
691 guint16 current_chunk;
693 g_assert ( num_chunks < 16000 );
695 total_length = vik_track_get_length_including_gaps ( tr );
696 chunk_length = total_length / num_chunks;
698 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
699 if (chunk_length <= 0) {
703 altitudes = vik_track_make_elevation_map (tr, num_chunks);
704 if (altitudes == NULL) {
708 current_gradient = 0.0;
709 pts = g_malloc ( sizeof(gdouble) * num_chunks );
710 for (current_chunk = 0; current_chunk < (num_chunks - 1); current_chunk++) {
711 altitude1 = altitudes[current_chunk];
712 altitude2 = altitudes[current_chunk + 1];
713 current_gradient = 100.0 * (altitude2 - altitude1) / chunk_length;
715 pts[current_chunk] = current_gradient;
718 pts[current_chunk] = current_gradient;
723 /* by Alex Foobarian */
724 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
727 gdouble duration, chunk_dur;
729 int i, pt_count, numpts, index;
732 if ( ! tr->trackpoints )
735 g_assert ( num_chunks < 16000 );
737 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
738 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
741 if ( !t1 || !t2 || !duration )
745 g_warning("negative duration: unsorted trackpoint timestamps?");
748 pt_count = vik_track_get_tp_count(tr);
750 v = g_malloc ( sizeof(gdouble) * num_chunks );
751 chunk_dur = duration / num_chunks;
753 s = g_malloc(sizeof(double) * pt_count);
754 t = g_malloc(sizeof(double) * pt_count);
756 iter = tr->trackpoints->next;
759 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
762 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
763 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
768 /* In the following computation, we iterate through periods of time of duration chunk_dur.
769 * The first period begins at the beginning of the track. The last period ends at the end of the track.
771 index = 0; /* index of the current trackpoint. */
772 for (i = 0; i < num_chunks; i++) {
773 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
774 * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints.
776 if (t[0] + i*chunk_dur >= t[index]) {
777 gdouble acc_t = 0, acc_s = 0;
778 while (t[0] + i*chunk_dur >= t[index]) {
779 acc_s += (s[index+1]-s[index]);
780 acc_t += (t[index+1]-t[index]);
798 * Make a distance/time map, heavily based on the vik_track_make_speed_map method
800 gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks )
803 gdouble duration, chunk_dur;
805 int i, pt_count, numpts, index;
808 if ( ! tr->trackpoints )
811 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
812 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
815 if ( !t1 || !t2 || !duration )
819 g_warning("negative duration: unsorted trackpoint timestamps?");
822 pt_count = vik_track_get_tp_count(tr);
824 v = g_malloc ( sizeof(gdouble) * num_chunks );
825 chunk_dur = duration / num_chunks;
827 s = g_malloc(sizeof(double) * pt_count);
828 t = g_malloc(sizeof(double) * pt_count);
830 iter = tr->trackpoints->next;
833 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
836 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
837 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
842 /* In the following computation, we iterate through periods of time of duration chunk_dur.
843 * The first period begins at the beginning of the track. The last period ends at the end of the track.
845 index = 0; /* index of the current trackpoint. */
846 for (i = 0; i < num_chunks; i++) {
847 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
848 * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints.
850 if (t[0] + i*chunk_dur >= t[index]) {
851 gdouble acc_s = 0; // No need for acc_t
852 while (t[0] + i*chunk_dur >= t[index]) {
853 acc_s += (s[index+1]-s[index]);
856 // The only bit that's really different from the speed map - just keep an accululative record distance
857 v[i] = i ? v[i-1]+acc_s : acc_s;
872 * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance)
873 * This results in a slightly blocky graph when it does not have many trackpoints: <60
874 * NB Somehow the elevation/distance applies some kind of smoothing algorithm,
875 * but I don't think any one understands it any more (I certainly don't ATM)
877 gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks )
880 gdouble duration, chunk_dur;
881 GList *iter = tr->trackpoints;
883 if (!iter || !iter->next) /* zero- or one-point track */
886 /* test if there's anything worth calculating */
887 gboolean okay = FALSE;
889 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
898 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
899 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
902 if ( !t1 || !t2 || !duration )
906 g_warning("negative duration: unsorted trackpoint timestamps?");
909 gint pt_count = vik_track_get_tp_count(tr);
911 // Reset iterator back to the beginning
912 iter = tr->trackpoints;
914 gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values
915 gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes
916 gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times
918 chunk_dur = duration / num_chunks;
920 s[0] = VIK_TRACKPOINT(iter->data)->altitude;
921 t[0] = VIK_TRACKPOINT(iter->data)->timestamp;
922 iter = tr->trackpoints->next;
925 s[numpts] = VIK_TRACKPOINT(iter->data)->altitude;
926 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
931 /* In the following computation, we iterate through periods of time of duration chunk_dur.
932 * The first period begins at the beginning of the track. The last period ends at the end of the track.
934 gint index = 0; /* index of the current trackpoint. */
936 for (i = 0; i < num_chunks; i++) {
937 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
938 * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints.
940 if (t[0] + i*chunk_dur >= t[index]) {
941 gdouble acc_s = s[index]; // initialise to first point
942 while (t[0] + i*chunk_dur >= t[index]) {
943 acc_s += (s[index+1]-s[index]);
962 * Make a speed/distance map
964 gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks )
968 gint i, pt_count, numpts, index;
970 gdouble duration, total_length, chunk_length;
972 if ( ! tr->trackpoints )
975 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
976 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
979 if ( !t1 || !t2 || !duration )
983 g_warning("negative duration: unsorted trackpoint timestamps?");
987 total_length = vik_track_get_length_including_gaps ( tr );
988 chunk_length = total_length / num_chunks;
989 pt_count = vik_track_get_tp_count(tr);
991 if (chunk_length <= 0) {
995 v = g_malloc ( sizeof(gdouble) * num_chunks );
996 s = g_malloc ( sizeof(double) * pt_count );
997 t = g_malloc ( sizeof(double) * pt_count );
999 // No special handling of segments ATM...
1000 iter = tr->trackpoints->next;
1003 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1006 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
1007 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1012 // Iterate through a portion of the track to get an average speed for that part
1013 // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps'
1014 index = 0; /* index of the current trackpoint. */
1015 for (i = 0; i < num_chunks; i++) {
1016 // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk
1017 if (s[0] + i*chunk_length >= s[index]) {
1018 gdouble acc_t = 0, acc_s = 0;
1019 while (s[0] + i*chunk_length >= s[index]) {
1020 acc_s += (s[index+1]-s[index]);
1021 acc_t += (t[index+1]-t[index]);
1038 /* by Alex Foobarian */
1039 VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start )
1041 gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
1042 gdouble current_dist = 0.0;
1043 gdouble current_inc = 0.0;
1044 if ( tr->trackpoints )
1046 GList *iter = tr->trackpoints->next;
1047 GList *last_iter = NULL;
1048 gdouble last_dist = 0.0;
1051 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1052 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1053 last_dist = current_dist;
1054 current_dist += current_inc;
1055 if ( current_dist >= dist )
1060 if (!iter) { /* passing the end the track */
1062 if (meters_from_start)
1063 *meters_from_start = last_dist;
1064 return(VIK_TRACKPOINT(last_iter->data));
1069 /* we've gone past the dist already, was prev trackpoint closer? */
1070 /* should do a vik_coord_average_weighted() thingy. */
1071 if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) {
1072 if (meters_from_start)
1073 *meters_from_start = last_dist;
1077 if (meters_from_start)
1078 *meters_from_start = current_dist;
1080 return VIK_TRACKPOINT(iter->data);
1086 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
1088 time_t t_pos, t_start, t_end, t_total;
1089 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1090 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1091 t_total = t_end - t_start;
1093 t_pos = t_start + t_total * reltime;
1095 if ( !tr->trackpoints )
1098 GList *iter = tr->trackpoints;
1101 if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
1103 if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
1104 if (iter->prev == NULL) /* first trackpoint */
1106 time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp;
1107 time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
1108 if (t_before <= t_after)
1112 else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
1119 if (seconds_from_start)
1120 *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1121 return VIK_TRACKPOINT(iter->data);
1124 VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
1126 gdouble maxspeed = 0.0, speed = 0.0;
1128 if ( !tr->trackpoints )
1131 GList *iter = tr->trackpoints;
1132 VikTrackpoint *max_speed_tp = NULL;
1136 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
1137 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
1138 (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
1139 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
1140 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
1141 if ( speed > maxspeed ) {
1143 max_speed_tp = VIK_TRACKPOINT(iter->data);
1153 return max_speed_tp;
1156 VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
1158 gdouble maxalt = -5000.0;
1159 if ( !tr->trackpoints )
1162 GList *iter = tr->trackpoints;
1163 VikTrackpoint *max_alt_tp = NULL;
1166 if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
1167 maxalt = VIK_TRACKPOINT(iter->data)->altitude;
1168 max_alt_tp = VIK_TRACKPOINT(iter->data);
1179 VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
1181 gdouble minalt = 25000.0;
1182 if ( !tr->trackpoints )
1185 GList *iter = tr->trackpoints;
1186 VikTrackpoint *min_alt_tp = NULL;
1189 if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
1190 minalt = VIK_TRACKPOINT(iter->data)->altitude;
1191 min_alt_tp = VIK_TRACKPOINT(iter->data);
1202 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
1206 if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1207 GList *iter = tr->trackpoints->next;
1211 tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
1212 if ( tmp_alt > *max_alt )
1214 if ( tmp_alt < *min_alt )
1223 void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
1226 GByteArray *b = g_byte_array_new();
1230 g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
1232 /* we'll fill out number of trackpoints later */
1234 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1236 tps = tr->trackpoints;
1239 g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
1243 *(guint *)(b->data + intp) = ntp;
1245 // This allocates space for variant sized strings
1246 // and copies that amount of data from the track to byte array
1247 #define vtm_append(s) \
1248 len = (s) ? strlen(s)+1 : 0; \
1249 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
1250 if (s) g_byte_array_append(b, (guint8 *)s, len);
1252 vtm_append(tr->name);
1253 vtm_append(tr->comment);
1254 vtm_append(tr->description);
1258 g_byte_array_free(b, FALSE);
1262 * Take a byte array and convert it into a Track
1264 VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
1267 VikTrack *new_tr = vik_track_new();
1268 VikTrackpoint *new_tp;
1272 /* basic properties: */
1273 new_tr->visible = ((VikTrack *)data)->visible;
1274 new_tr->is_route = ((VikTrack *)data)->is_route;
1275 new_tr->has_color = ((VikTrack *)data)->has_color;
1276 new_tr->color = ((VikTrack *)data)->color;
1278 data += sizeof(*new_tr);
1280 ntp = *(guint *)data;
1281 data += sizeof(ntp);
1283 for (i=0; i<ntp; i++) {
1284 new_tp = vik_trackpoint_new();
1285 memcpy(new_tp, data, sizeof(*new_tp));
1286 data += sizeof(*new_tp);
1287 new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
1290 #define vtu_get(s) \
1291 len = *(guint *)data; \
1292 data += sizeof(len); \
1294 (s) = g_strdup((gchar *)data); \
1300 vtu_get(new_tr->name);
1301 vtu_get(new_tr->comment);
1302 vtu_get(new_tr->description);
1307 void vik_track_apply_dem_data ( VikTrack *tr )
1311 tp_iter = tr->trackpoints;
1313 /* TODO: of the 4 possible choices we have for choosing an elevation
1314 * (trackpoint in between samples), choose the one with the least elevation change
1316 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST );
1317 if ( elev != VIK_DEM_INVALID_ELEVATION )
1318 VIK_TRACKPOINT(tp_iter->data)->altitude = elev;
1319 tp_iter = tp_iter->next;
1324 * Apply DEM data (if available) - to only the last trackpoint
1326 void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr )
1329 if ( tr->trackpoints ) {
1330 /* As in vik_track_apply_dem_data above - use 'best' interpolation method */
1331 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->coord), VIK_DEM_INTERPOL_BEST );
1332 if ( elev != VIK_DEM_INVALID_ELEVATION )
1333 VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->altitude = elev;
1337 /* appends t2 to t1, leaving t2 with no trackpoints */
1338 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
1340 if ( t1->trackpoints ) {
1341 GList *tpiter = t1->trackpoints;
1342 while ( tpiter->next )
1343 tpiter = tpiter->next;
1344 tpiter->next = t2->trackpoints;
1345 t2->trackpoints->prev = tpiter;
1347 t1->trackpoints = t2->trackpoints;
1348 t2->trackpoints = NULL;
1351 /* starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
1352 * If there is no double point, deletes all the trackpoints.
1353 * Returns the new end of the track (or the start if there are no double points)
1355 VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr )
1357 GList *iter = tr->trackpoints;
1362 while ( iter->next )
1366 while ( iter->prev ) {
1367 if ( vik_coord_equals((VikCoord *)iter->data, (VikCoord *)iter->prev->data) ) {
1368 GList *prev = iter->prev;
1370 rv = g_malloc(sizeof(VikCoord));
1371 *rv = *((VikCoord *) iter->data);
1373 /* truncate trackpoint list */
1374 iter->prev = NULL; /* pretend it's the end */
1375 g_list_foreach ( iter, (GFunc) g_free, NULL );
1376 g_list_free( iter );
1385 /* no double point found! */
1386 rv = g_malloc(sizeof(VikCoord));
1387 *rv = *((VikCoord *) tr->trackpoints->data);
1388 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
1389 g_list_free( tr->trackpoints );
1390 tr->trackpoints = NULL;