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
43 VikTrack *vik_track_new()
45 VikTrack *tr = g_malloc0 ( sizeof ( VikTrack ) );
50 #define VIK_SETTINGS_TRACK_NAME_MODE "track_draw_name_mode"
51 #define VIK_SETTINGS_TRACK_NUM_DIST_LABELS "track_number_dist_labels"
54 * vik_track_set_defaults:
56 * Set some default values for a track.
57 * ATM This uses the 'settings' method to get values,
58 * so there is no GUI way to control these yet...
60 void vik_track_set_defaults(VikTrack *tr)
63 if ( a_settings_get_integer ( VIK_SETTINGS_TRACK_NAME_MODE, &tmp ) )
64 tr->draw_name_mode = tmp;
66 if ( a_settings_get_integer ( VIK_SETTINGS_TRACK_NUM_DIST_LABELS, &tmp ) )
67 tr->max_number_dist_labels = tmp;
70 void vik_track_set_comment_no_copy(VikTrack *tr, gchar *comment)
73 g_free ( tr->comment );
74 tr->comment = comment;
78 void vik_track_set_name(VikTrack *tr, const gchar *name)
83 tr->name = g_strdup(name);
86 void vik_track_set_comment(VikTrack *tr, const gchar *comment)
89 g_free ( tr->comment );
91 if ( comment && comment[0] != '\0' )
92 tr->comment = g_strdup(comment);
97 void vik_track_set_description(VikTrack *tr, const gchar *description)
99 if ( tr->description )
100 g_free ( tr->description );
102 if ( description && description[0] != '\0' )
103 tr->description = g_strdup(description);
105 tr->description = NULL;
108 void vik_track_ref(VikTrack *tr)
113 void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog)
115 /* Warning: does not check for existing dialog */
116 tr->property_dialog = dialog;
119 void vik_track_clear_property_dialog(VikTrack *tr)
121 tr->property_dialog = NULL;
124 void vik_track_free(VikTrack *tr)
126 if ( tr->ref_count-- > 1 )
132 g_free ( tr->comment );
133 if ( tr->description )
134 g_free ( tr->description );
135 g_list_foreach ( tr->trackpoints, (GFunc) vik_trackpoint_free, NULL );
136 g_list_free( tr->trackpoints );
137 if (tr->property_dialog)
138 if ( GTK_IS_WIDGET(tr->property_dialog) )
139 gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) );
145 * @tr: The Track to copy
146 * @copy_points: Whether to copy the track points or not
148 * Normally for copying the track it's best to copy all the trackpoints
149 * However for some operations such as splitting tracks the trackpoints will be managed separately, so no need to copy them.
151 * Returns: the copied VikTrack
153 VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points )
155 VikTrack *new_tr = vik_track_new();
156 new_tr->name = g_strdup(tr->name);
157 new_tr->visible = tr->visible;
158 new_tr->is_route = tr->is_route;
159 new_tr->draw_name_mode = tr->draw_name_mode;
160 new_tr->max_number_dist_labels = tr->max_number_dist_labels;
161 new_tr->has_color = tr->has_color;
162 new_tr->color = tr->color;
163 new_tr->bbox = tr->bbox;
164 new_tr->trackpoints = NULL;
167 GList *tp_iter = tr->trackpoints;
170 VikTrackpoint *new_tp = vik_trackpoint_copy ( (VikTrackpoint*)(tp_iter->data) );
171 new_tr->trackpoints = g_list_prepend ( new_tr->trackpoints, new_tp );
172 tp_iter = tp_iter->next;
174 if ( new_tr->trackpoints )
175 new_tr->trackpoints = g_list_reverse ( new_tr->trackpoints );
177 vik_track_set_name(new_tr,tr->name);
178 vik_track_set_comment(new_tr,tr->comment);
179 vik_track_set_description(new_tr,tr->description);
183 VikTrackpoint *vik_trackpoint_new()
185 VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint));
188 tp->altitude = VIK_DEFAULT_ALTITUDE;
189 tp->hdop = VIK_DEFAULT_DOP;
190 tp->vdop = VIK_DEFAULT_DOP;
191 tp->pdop = VIK_DEFAULT_DOP;
195 void vik_trackpoint_free(VikTrackpoint *tp)
201 void vik_trackpoint_set_name(VikTrackpoint *tp, const gchar *name)
206 // If the name is blank then completely remove it
207 if ( name && name[0] == '\0' )
210 tp->name = g_strdup(name);
215 VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
217 VikTrackpoint *new_tp = vik_trackpoint_new();
218 memcpy ( new_tp, tp, sizeof(VikTrackpoint) );
220 new_tp->name = g_strdup (tp->name);
225 * track_recalculate_bounds_last_tp:
226 * @trk: The track to consider the recalculation on
228 * A faster bounds check, since it only considers the last track point
230 static void track_recalculate_bounds_last_tp ( VikTrack *trk )
232 GList *tpl = g_list_last ( trk->trackpoints );
236 // See if this trackpoint increases the track bounds and update if so
237 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tpl->data)->coord), &ll );
238 if ( ll.lat > trk->bbox.north )
239 trk->bbox.north = ll.lat;
240 if ( ll.lon < trk->bbox.west )
241 trk->bbox.west = ll.lon;
242 if ( ll.lat < trk->bbox.south )
243 trk->bbox.south = ll.lat;
244 if ( ll.lon > trk->bbox.east )
245 trk->bbox.east = ll.lon;
250 * vik_track_add_trackpoint:
251 * @tr: The track to which the trackpoint will be added
252 * @tp: The trackpoint to add
253 * @recalculate: Whether to perform any associated properties recalculations
254 * Generally one should avoid recalculation via this method if adding lots of points
255 * (But ensure calculate_bounds() is called after adding all points!!)
257 * The trackpoint is added to the end of the existing trackpoint list
259 void vik_track_add_trackpoint ( VikTrack *tr, VikTrackpoint *tp, gboolean recalculate )
261 // When it's the first trackpoint need to ensure the bounding box is initialized correctly
262 gboolean adding_first_point = tr->trackpoints ? FALSE : TRUE;
263 tr->trackpoints = g_list_append ( tr->trackpoints, tp );
264 if ( adding_first_point )
265 vik_track_calculate_bounds ( tr );
266 else if ( recalculate )
267 track_recalculate_bounds_last_tp ( tr );
271 * vik_track_get_length_to_trackpoint:
274 gdouble vik_track_get_length_to_trackpoint (const VikTrack *tr, const VikTrackpoint *tp)
277 if ( tr->trackpoints )
279 // Is it the very first track point?
280 if ( VIK_TRACKPOINT(tr->trackpoints->data) == tp )
283 GList *iter = tr->trackpoints->next;
286 VikTrackpoint *tp1 = VIK_TRACKPOINT(iter->data);
287 if ( ! tp1->newsegment )
288 len += vik_coord_diff ( &(tp1->coord),
289 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
291 // Exit when we reach the desired point
301 gdouble vik_track_get_length(const VikTrack *tr)
304 if ( tr->trackpoints )
306 GList *iter = tr->trackpoints->next;
309 if ( ! VIK_TRACKPOINT(iter->data)->newsegment )
310 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
311 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
318 gdouble vik_track_get_length_including_gaps(const VikTrack *tr)
321 if ( tr->trackpoints )
323 GList *iter = tr->trackpoints->next;
326 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
327 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
334 gulong vik_track_get_tp_count(const VikTrack *tr)
336 return g_list_length(tr->trackpoints);
339 gulong vik_track_get_dup_point_count ( const VikTrack *tr )
342 GList *iter = tr->trackpoints;
345 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
346 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
354 * Deletes adjacent points that have the same position
355 * Returns the number of points that were deleted
357 gulong vik_track_remove_dup_points ( VikTrack *tr )
360 GList *iter = tr->trackpoints;
363 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
364 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
367 // Maintain track segments
368 if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
369 VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
371 vik_trackpoint_free ( iter->next->data );
372 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
378 // NB isn't really be necessary as removing duplicate points shouldn't alter the bounds!
379 vik_track_calculate_bounds ( tr );
385 * Get a count of trackpoints with the same defined timestamp
386 * Note is using timestamps with a resolution with 1 second
388 gulong vik_track_get_same_time_point_count ( const VikTrack *tr )
391 GList *iter = tr->trackpoints;
394 ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
395 VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
396 ( VIK_TRACKPOINT(iter->data)->timestamp ==
397 VIK_TRACKPOINT(iter->next->data)->timestamp) )
405 * Deletes adjacent points that have the same defined timestamp
406 * Returns the number of points that were deleted
408 gulong vik_track_remove_same_time_points ( VikTrack *tr )
411 GList *iter = tr->trackpoints;
414 ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
415 VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
416 ( VIK_TRACKPOINT(iter->data)->timestamp ==
417 VIK_TRACKPOINT(iter->next->data)->timestamp) ) {
421 // Maintain track segments
422 if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
423 VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
425 vik_trackpoint_free ( iter->next->data );
426 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
432 vik_track_calculate_bounds ( tr );
438 * Deletes all 'extra' trackpoint information
439 * such as time stamps, speed, course etc...
441 void vik_track_to_routepoints ( VikTrack *tr )
443 GList *iter = tr->trackpoints;
446 // c.f. with vik_trackpoint_new()
448 VIK_TRACKPOINT(iter->data)->has_timestamp = FALSE;
449 VIK_TRACKPOINT(iter->data)->timestamp = 0;
450 VIK_TRACKPOINT(iter->data)->speed = NAN;
451 VIK_TRACKPOINT(iter->data)->course = NAN;
452 VIK_TRACKPOINT(iter->data)->hdop = VIK_DEFAULT_DOP;
453 VIK_TRACKPOINT(iter->data)->vdop = VIK_DEFAULT_DOP;
454 VIK_TRACKPOINT(iter->data)->pdop = VIK_DEFAULT_DOP;
455 VIK_TRACKPOINT(iter->data)->nsats = 0;
456 VIK_TRACKPOINT(iter->data)->fix_mode = VIK_GPS_MODE_NOT_SEEN;
462 guint vik_track_get_segment_count(const VikTrack *tr)
465 GList *iter = tr->trackpoints;
468 while ( (iter = iter->next) )
470 if ( VIK_TRACKPOINT(iter->data)->newsegment )
476 VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
481 guint segs = vik_track_get_segment_count(t);
490 rv = g_malloc ( segs * sizeof(VikTrack *) );
491 tr = vik_track_copy ( t, TRUE );
493 iter = tr->trackpoints;
496 while ( (iter = iter->next) )
498 if ( VIK_TRACKPOINT(iter->data)->newsegment )
500 iter->prev->next = NULL;
502 rv[i] = vik_track_copy ( tr, FALSE );
503 rv[i]->trackpoints = iter;
505 vik_track_calculate_bounds ( rv[i] );
515 * Simply remove any subsequent segment markers in a track to form one continuous track
516 * Return the number of segments merged
518 guint vik_track_merge_segments(VikTrack *tr)
521 GList *iter = tr->trackpoints;
525 // Always skip the first point as this should be the first segment
528 while ( (iter = iter->next) )
530 if ( VIK_TRACKPOINT(iter->data)->newsegment ) {
531 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
538 void vik_track_reverse ( VikTrack *tr )
540 if ( ! tr->trackpoints )
543 tr->trackpoints = g_list_reverse(tr->trackpoints);
545 /* fix 'newsegment' */
546 GList *iter = g_list_last ( tr->trackpoints );
549 if ( ! iter->next ) /* last segment, was first, cancel newsegment. */
550 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
551 if ( ! iter->prev ) /* first segment by convention has newsegment flag. */
552 VIK_TRACKPOINT(iter->data)->newsegment = TRUE;
553 else if ( VIK_TRACKPOINT(iter->data)->newsegment && iter->next )
555 VIK_TRACKPOINT(iter->next->data)->newsegment = TRUE;
556 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
563 * vik_track_get_duration:
566 * Returns: The time in seconds that covers the whole track including gaps
567 * NB this may be negative particularly if the track has been reversed
569 time_t vik_track_get_duration(const VikTrack *trk)
572 if ( trk->trackpoints ) {
573 // Ensure times are available
574 if ( vik_track_get_tp_first(trk)->has_timestamp ) {
575 // Get trkpt only once - as using vik_track_get_tp_last() iterates whole track each time
576 VikTrackpoint *trkpt_last = vik_track_get_tp_last(trk);
577 if ( trkpt_last->has_timestamp ) {
578 time_t t1 = vik_track_get_tp_first(trk)->timestamp;
579 time_t t2 = trkpt_last->timestamp;
587 gdouble vik_track_get_average_speed(const VikTrack *tr)
591 if ( tr->trackpoints )
593 GList *iter = tr->trackpoints->next;
596 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
597 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
598 (! VIK_TRACKPOINT(iter->data)->newsegment) )
600 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
601 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
602 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
607 return (time == 0) ? 0 : ABS(len/time);
611 * Based on a simple average speed, but with a twist - to give a moving average.
612 * . GPSs often report a moving average in their statistics output
613 * . bicycle speedos often don't factor in time when stopped - hence reporting a moving average for speed
615 * Often GPS track will record every second but not when stationary
616 * This method doesn't use samples that differ over the specified time limit - effectively skipping that time chunk from the total time
618 * Suggest to use 60 seconds as the stop length (as the default used in the TrackWaypoint draw stops factor)
620 gdouble vik_track_get_average_speed_moving (const VikTrack *tr, int stop_length_seconds)
624 if ( tr->trackpoints )
626 GList *iter = tr->trackpoints->next;
629 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
630 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
631 (! VIK_TRACKPOINT(iter->data)->newsegment) )
633 if ( ( VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp ) < stop_length_seconds ) {
634 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
635 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
637 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
643 return (time == 0) ? 0 : ABS(len/time);
646 gdouble vik_track_get_max_speed(const VikTrack *tr)
648 gdouble maxspeed = 0.0, speed = 0.0;
649 if ( tr->trackpoints )
651 GList *iter = tr->trackpoints->next;
654 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
655 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
656 (! VIK_TRACKPOINT(iter->data)->newsegment) )
658 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
659 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
660 if ( speed > maxspeed )
669 void vik_track_convert ( VikTrack *tr, VikCoordMode dest_mode )
671 GList *iter = tr->trackpoints;
674 vik_coord_convert ( &(VIK_TRACKPOINT(iter->data)->coord), dest_mode );
679 /* I understood this when I wrote it ... maybe ... Basically it eats up the
680 * proper amounts of length on the track and averages elevation over that. */
681 gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
684 gdouble total_length, chunk_length, current_dist, current_area_under_curve, current_seg_length, dist_along_seg = 0.0;
685 gdouble altitude1, altitude2;
686 guint16 current_chunk;
687 gboolean ignore_it = FALSE;
689 GList *iter = tr->trackpoints;
691 if (!iter || !iter->next) /* zero- or one-point track */
694 { /* test if there's anything worth calculating */
695 gboolean okay = FALSE;
698 // Sometimes a GPS device (or indeed any random file) can have stupid numbers for elevations
699 // Since when is 9.9999e+24 a valid elevation!!
700 // 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)
701 // Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception)
702 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE &&
703 VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) {
712 iter = tr->trackpoints;
714 g_assert ( num_chunks < 16000 );
716 pts = g_malloc ( sizeof(gdouble) * num_chunks );
718 total_length = vik_track_get_length_including_gaps ( tr );
719 chunk_length = total_length / num_chunks;
721 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
722 if (chunk_length <= 0) {
728 current_area_under_curve = 0;
730 current_seg_length = 0;
732 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
733 &(VIK_TRACKPOINT(iter->next->data)->coord) );
734 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
735 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
738 while ( current_chunk < num_chunks ) {
740 /* go along current seg */
741 if ( current_seg_length && (current_seg_length - dist_along_seg) > chunk_length ) {
742 dist_along_seg += chunk_length;
746 * /x altitude = alt_at_pt_1 + alt_at_pt_2 / 2 = altitude1 + slope * dist_value_of_pt_inbetween_pt1_and_pt2
747 * /xx avg altitude = area under curve / chunk len
748 *pt1 *xxx avg altitude = altitude1 + (altitude2-altitude1)/(current_seg_length)*(dist_along_seg + (chunk_len/2))
754 // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero)
755 pts[current_chunk] = altitude1;
757 pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
761 /* finish current seg */
762 if ( current_seg_length ) {
763 gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg;
764 current_dist = current_seg_length - dist_along_seg;
765 current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5;
766 } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */
768 /* get intervening segs */
770 while ( iter && iter->next ) {
771 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
772 &(VIK_TRACKPOINT(iter->next->data)->coord) );
773 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
774 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
775 ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment;
777 if ( chunk_length - current_dist >= current_seg_length ) {
778 current_dist += current_seg_length;
779 current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5;
787 dist_along_seg = chunk_length - current_dist;
788 if ( ignore_it || ( iter && !iter->next ) ) {
789 pts[current_chunk] = current_area_under_curve / current_dist;
792 for (i = current_chunk + 1; i < num_chunks; i++)
793 pts[i] = pts[current_chunk];
798 current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length);
799 pts[current_chunk] = current_area_under_curve / chunk_length;
811 void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
815 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE )
817 GList *iter = tr->trackpoints->next;
820 diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude;
828 *up = *down = VIK_DEFAULT_ALTITUDE;
831 gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks )
835 gdouble total_length, chunk_length, current_gradient;
836 gdouble altitude1, altitude2;
837 guint16 current_chunk;
839 g_assert ( num_chunks < 16000 );
841 total_length = vik_track_get_length_including_gaps ( tr );
842 chunk_length = total_length / num_chunks;
844 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
845 if (chunk_length <= 0) {
849 altitudes = vik_track_make_elevation_map (tr, num_chunks);
850 if (altitudes == NULL) {
854 current_gradient = 0.0;
855 pts = g_malloc ( sizeof(gdouble) * num_chunks );
856 for (current_chunk = 0; current_chunk < (num_chunks - 1); current_chunk++) {
857 altitude1 = altitudes[current_chunk];
858 altitude2 = altitudes[current_chunk + 1];
859 current_gradient = 100.0 * (altitude2 - altitude1) / chunk_length;
861 pts[current_chunk] = current_gradient;
864 pts[current_chunk] = current_gradient;
866 g_free ( altitudes );
871 /* by Alex Foobarian */
872 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
875 gdouble duration, chunk_dur;
877 int i, pt_count, numpts, index;
880 if ( ! tr->trackpoints )
883 g_assert ( num_chunks < 16000 );
885 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
886 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
889 if ( !t1 || !t2 || !duration )
893 g_warning("negative duration: unsorted trackpoint timestamps?");
896 pt_count = vik_track_get_tp_count(tr);
898 v = g_malloc ( sizeof(gdouble) * num_chunks );
899 chunk_dur = duration / num_chunks;
901 s = g_malloc(sizeof(double) * pt_count);
902 t = g_malloc(sizeof(double) * pt_count);
904 iter = tr->trackpoints->next;
907 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
910 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
911 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
916 /* In the following computation, we iterate through periods of time of duration chunk_dur.
917 * The first period begins at the beginning of the track. The last period ends at the end of the track.
919 index = 0; /* index of the current trackpoint. */
920 for (i = 0; i < num_chunks; i++) {
921 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
922 * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints.
924 if (t[0] + i*chunk_dur >= t[index]) {
925 gdouble acc_t = 0, acc_s = 0;
926 while (t[0] + i*chunk_dur >= t[index]) {
927 acc_s += (s[index+1]-s[index]);
928 acc_t += (t[index+1]-t[index]);
946 * Make a distance/time map, heavily based on the vik_track_make_speed_map method
948 gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks )
951 gdouble duration, chunk_dur;
953 int i, pt_count, numpts, index;
956 if ( ! tr->trackpoints )
959 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
960 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
963 if ( !t1 || !t2 || !duration )
967 g_warning("negative duration: unsorted trackpoint timestamps?");
970 pt_count = vik_track_get_tp_count(tr);
972 v = g_malloc ( sizeof(gdouble) * num_chunks );
973 chunk_dur = duration / num_chunks;
975 s = g_malloc(sizeof(double) * pt_count);
976 t = g_malloc(sizeof(double) * pt_count);
978 iter = tr->trackpoints->next;
981 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
984 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
985 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
990 /* In the following computation, we iterate through periods of time of duration chunk_dur.
991 * The first period begins at the beginning of the track. The last period ends at the end of the track.
993 index = 0; /* index of the current trackpoint. */
994 for (i = 0; i < num_chunks; i++) {
995 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
996 * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints.
998 if (t[0] + i*chunk_dur >= t[index]) {
999 gdouble acc_s = 0; // No need for acc_t
1000 while (t[0] + i*chunk_dur >= t[index]) {
1001 acc_s += (s[index+1]-s[index]);
1004 // The only bit that's really different from the speed map - just keep an accululative record distance
1005 v[i] = i ? v[i-1]+acc_s : acc_s;
1020 * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance)
1021 * This results in a slightly blocky graph when it does not have many trackpoints: <60
1022 * NB Somehow the elevation/distance applies some kind of smoothing algorithm,
1023 * but I don't think any one understands it any more (I certainly don't ATM)
1025 gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks )
1028 gdouble duration, chunk_dur;
1029 GList *iter = tr->trackpoints;
1031 if (!iter || !iter->next) /* zero- or one-point track */
1034 /* test if there's anything worth calculating */
1035 gboolean okay = FALSE;
1037 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
1046 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1047 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1050 if ( !t1 || !t2 || !duration )
1054 g_warning("negative duration: unsorted trackpoint timestamps?");
1057 gint pt_count = vik_track_get_tp_count(tr);
1059 // Reset iterator back to the beginning
1060 iter = tr->trackpoints;
1062 gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values
1063 gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes
1064 gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times
1066 chunk_dur = duration / num_chunks;
1068 s[0] = VIK_TRACKPOINT(iter->data)->altitude;
1069 t[0] = VIK_TRACKPOINT(iter->data)->timestamp;
1070 iter = tr->trackpoints->next;
1073 s[numpts] = VIK_TRACKPOINT(iter->data)->altitude;
1074 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1079 /* In the following computation, we iterate through periods of time of duration chunk_dur.
1080 * The first period begins at the beginning of the track. The last period ends at the end of the track.
1082 gint index = 0; /* index of the current trackpoint. */
1084 for (i = 0; i < num_chunks; i++) {
1085 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
1086 * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints.
1088 if (t[0] + i*chunk_dur >= t[index]) {
1089 gdouble acc_s = s[index]; // initialise to first point
1090 while (t[0] + i*chunk_dur >= t[index]) {
1091 acc_s += (s[index+1]-s[index]);
1110 * Make a speed/distance map
1112 gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks )
1116 gint i, pt_count, numpts, index;
1118 gdouble duration, total_length, chunk_length;
1120 if ( ! tr->trackpoints )
1123 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1124 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1127 if ( !t1 || !t2 || !duration )
1131 g_warning("negative duration: unsorted trackpoint timestamps?");
1135 total_length = vik_track_get_length_including_gaps ( tr );
1136 chunk_length = total_length / num_chunks;
1137 pt_count = vik_track_get_tp_count(tr);
1139 if (chunk_length <= 0) {
1143 v = g_malloc ( sizeof(gdouble) * num_chunks );
1144 s = g_malloc ( sizeof(double) * pt_count );
1145 t = g_malloc ( sizeof(double) * pt_count );
1147 // No special handling of segments ATM...
1148 iter = tr->trackpoints->next;
1151 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1154 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
1155 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1160 // Iterate through a portion of the track to get an average speed for that part
1161 // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps'
1162 index = 0; /* index of the current trackpoint. */
1163 for (i = 0; i < num_chunks; i++) {
1164 // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk
1165 if (s[0] + i*chunk_length >= s[index]) {
1166 gdouble acc_t = 0, acc_s = 0;
1167 while (s[0] + i*chunk_length >= s[index]) {
1168 acc_s += (s[index+1]-s[index]);
1169 acc_t += (t[index+1]-t[index]);
1187 * vik_track_get_tp_by_dist:
1188 * @trk: The Track on which to find a Trackpoint
1189 * @meters_from_start: The distance along a track that the trackpoint returned is near
1190 * @get_next_point: Since there is a choice of trackpoints, this determines which one to return
1191 * @tp_metres_from_start: For the returned Trackpoint, returns the distance along the track
1193 * TODO: Consider changing the boolean get_next_point into an enum with these options PREVIOUS, NEXT, NEAREST
1195 * Returns: The #VikTrackpoint fitting the criteria or NULL
1197 VikTrackpoint *vik_track_get_tp_by_dist ( VikTrack *trk, gdouble meters_from_start, gboolean get_next_point, gdouble *tp_metres_from_start )
1199 gdouble current_dist = 0.0;
1200 gdouble current_inc = 0.0;
1201 if ( tp_metres_from_start )
1202 *tp_metres_from_start = 0.0;
1204 if ( trk->trackpoints ) {
1205 GList *iter = g_list_next ( g_list_first ( trk->trackpoints ) );
1207 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1208 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1209 current_dist += current_inc;
1210 if ( current_dist >= meters_from_start )
1212 iter = g_list_next ( iter );
1214 // passed the end of the track
1218 if ( tp_metres_from_start )
1219 *tp_metres_from_start = current_dist;
1221 // we've gone past the distance already, is the previous trackpoint wanted?
1222 if ( !get_next_point ) {
1224 if ( tp_metres_from_start )
1225 *tp_metres_from_start = current_dist-current_inc;
1226 return VIK_TRACKPOINT(iter->prev->data);
1229 return VIK_TRACKPOINT(iter->data);
1235 /* by Alex Foobarian */
1236 VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start )
1238 gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
1239 gdouble current_dist = 0.0;
1240 gdouble current_inc = 0.0;
1241 if ( tr->trackpoints )
1243 GList *iter = tr->trackpoints->next;
1244 GList *last_iter = NULL;
1245 gdouble last_dist = 0.0;
1248 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1249 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1250 last_dist = current_dist;
1251 current_dist += current_inc;
1252 if ( current_dist >= dist )
1257 if (!iter) { /* passing the end the track */
1259 if (meters_from_start)
1260 *meters_from_start = last_dist;
1261 return(VIK_TRACKPOINT(last_iter->data));
1266 /* we've gone past the dist already, was prev trackpoint closer? */
1267 /* should do a vik_coord_average_weighted() thingy. */
1268 if ( iter->prev && fabs(current_dist-current_inc-dist) < fabs(current_dist-dist) ) {
1269 if (meters_from_start)
1270 *meters_from_start = last_dist;
1274 if (meters_from_start)
1275 *meters_from_start = current_dist;
1277 return VIK_TRACKPOINT(iter->data);
1283 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
1285 time_t t_pos, t_start, t_end, t_total;
1286 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1287 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1288 t_total = t_end - t_start;
1290 t_pos = t_start + t_total * reltime;
1292 if ( !tr->trackpoints )
1295 GList *iter = tr->trackpoints;
1298 if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
1300 if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
1301 if (iter->prev == NULL) /* first trackpoint */
1303 time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev->data)->timestamp;
1304 time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
1305 if (t_before <= t_after)
1309 else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
1316 if (seconds_from_start)
1317 *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1318 return VIK_TRACKPOINT(iter->data);
1321 VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
1323 gdouble maxspeed = 0.0, speed = 0.0;
1325 if ( !tr->trackpoints )
1328 GList *iter = tr->trackpoints;
1329 VikTrackpoint *max_speed_tp = NULL;
1333 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
1334 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
1335 (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
1336 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
1337 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
1338 if ( speed > maxspeed ) {
1340 max_speed_tp = VIK_TRACKPOINT(iter->data);
1350 return max_speed_tp;
1353 VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
1355 gdouble maxalt = -5000.0;
1356 if ( !tr->trackpoints )
1359 GList *iter = tr->trackpoints;
1360 VikTrackpoint *max_alt_tp = NULL;
1363 if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
1364 maxalt = VIK_TRACKPOINT(iter->data)->altitude;
1365 max_alt_tp = VIK_TRACKPOINT(iter->data);
1376 VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
1378 gdouble minalt = 25000.0;
1379 if ( !tr->trackpoints )
1382 GList *iter = tr->trackpoints;
1383 VikTrackpoint *min_alt_tp = NULL;
1386 if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
1387 minalt = VIK_TRACKPOINT(iter->data)->altitude;
1388 min_alt_tp = VIK_TRACKPOINT(iter->data);
1399 VikTrackpoint *vik_track_get_tp_first( const VikTrack *tr )
1401 if ( !tr->trackpoints )
1404 return (VikTrackpoint*)g_list_first(tr->trackpoints)->data;
1407 VikTrackpoint *vik_track_get_tp_last ( const VikTrack *tr )
1409 if ( !tr->trackpoints )
1412 return (VikTrackpoint*)g_list_last(tr->trackpoints)->data;
1415 VikTrackpoint *vik_track_get_tp_prev ( const VikTrack *tr, VikTrackpoint *tp )
1417 if ( !tr->trackpoints )
1420 GList *iter = tr->trackpoints;
1421 VikTrackpoint *tp_prev = NULL;
1425 if ( VIK_TRACKPOINT(iter->data) == tp ) {
1426 tp_prev = VIK_TRACKPOINT(iter->prev->data);
1436 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
1440 if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1441 GList *iter = tr->trackpoints->next;
1445 tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
1446 if ( tmp_alt > *max_alt )
1448 if ( tmp_alt < *min_alt )
1457 void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
1460 GByteArray *b = g_byte_array_new();
1464 g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
1466 /* we'll fill out number of trackpoints later */
1468 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1470 // This allocates space for variant sized strings
1471 // and copies that amount of data from the track to byte array
1472 #define vtm_append(s) \
1473 len = (s) ? strlen(s)+1 : 0; \
1474 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
1475 if (s) g_byte_array_append(b, (guint8 *)s, len);
1477 tps = tr->trackpoints;
1480 g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
1481 vtm_append(VIK_TRACKPOINT(tps->data)->name);
1485 *(guint *)(b->data + intp) = ntp;
1487 vtm_append(tr->name);
1488 vtm_append(tr->comment);
1489 vtm_append(tr->description);
1493 g_byte_array_free(b, FALSE);
1497 * Take a byte array and convert it into a Track
1499 VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
1502 VikTrack *new_tr = vik_track_new();
1503 VikTrackpoint *new_tp;
1507 /* basic properties: */
1508 new_tr->visible = ((VikTrack *)data)->visible;
1509 new_tr->is_route = ((VikTrack *)data)->is_route;
1510 new_tr->draw_name_mode = ((VikTrack *)data)->draw_name_mode;
1511 new_tr->max_number_dist_labels = ((VikTrack *)data)->max_number_dist_labels;
1512 new_tr->has_color = ((VikTrack *)data)->has_color;
1513 new_tr->color = ((VikTrack *)data)->color;
1514 new_tr->bbox = ((VikTrack *)data)->bbox;
1516 data += sizeof(*new_tr);
1518 ntp = *(guint *)data;
1519 data += sizeof(ntp);
1521 #define vtu_get(s) \
1522 len = *(guint *)data; \
1523 data += sizeof(len); \
1525 (s) = g_strdup((gchar *)data); \
1531 for (i=0; i<ntp; i++) {
1532 new_tp = vik_trackpoint_new();
1533 memcpy(new_tp, data, sizeof(*new_tp));
1534 data += sizeof(*new_tp);
1535 vtu_get(new_tp->name);
1536 new_tr->trackpoints = g_list_prepend(new_tr->trackpoints, new_tp);
1538 if ( new_tr->trackpoints )
1539 new_tr->trackpoints = g_list_reverse(new_tr->trackpoints);
1541 vtu_get(new_tr->name);
1542 vtu_get(new_tr->comment);
1543 vtu_get(new_tr->description);
1549 * (Re)Calculate the bounds of the given track,
1550 * updating the track's bounds data.
1551 * This should be called whenever a track's trackpoints are changed
1553 void vik_track_calculate_bounds ( VikTrack *trk )
1556 tp_iter = trk->trackpoints;
1558 struct LatLon topleft, bottomright, ll;
1560 // Set bounds to first point
1562 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &topleft );
1563 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &bottomright );
1567 // See if this trackpoint increases the track bounds.
1569 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &ll );
1571 if ( ll.lat > topleft.lat) topleft.lat = ll.lat;
1572 if ( ll.lon < topleft.lon) topleft.lon = ll.lon;
1573 if ( ll.lat < bottomright.lat) bottomright.lat = ll.lat;
1574 if ( ll.lon > bottomright.lon) bottomright.lon = ll.lon;
1576 tp_iter = tp_iter->next;
1579 g_debug ( "Bounds of track: '%s' is: %f,%f to: %f,%f", trk->name, topleft.lat, topleft.lon, bottomright.lat, bottomright.lon );
1581 trk->bbox.north = topleft.lat;
1582 trk->bbox.east = bottomright.lon;
1583 trk->bbox.south = bottomright.lat;
1584 trk->bbox.west = topleft.lon;
1588 * vik_track_anonymize_times:
1590 * Shift all timestamps to be relatively offset from 1901-01-01
1592 void vik_track_anonymize_times ( VikTrack *tr )
1595 g_time_val_from_iso8601 ( "1901-01-01T00:00:00Z", >v );
1597 time_t anon_timestamp = gtv.tv_sec;
1601 tp_iter = tr->trackpoints;
1603 VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);
1604 if ( tp->has_timestamp ) {
1605 // Calculate an offset in time using the first available timestamp
1607 offset = tp->timestamp - anon_timestamp;
1609 // Apply this offset to shift all timestamps towards 1901 & hence anonymising the time
1610 // Note that the relative difference between timestamps is kept - thus calculating speeds will still work
1611 tp->timestamp = tp->timestamp - offset;
1613 tp_iter = tp_iter->next;
1618 * vik_track_interpolate_times:
1620 * Interpolate the timestamps between first and last trackpoint,
1621 * so that the track is driven at equal speed, regardless of the
1622 * distance between individual trackpoints.
1624 * NB This will overwrite any existing trackpoint timestamps
1626 void vik_track_interpolate_times ( VikTrack *tr )
1628 gdouble tr_dist, cur_dist;
1629 time_t tsdiff, tsfirst;
1632 iter = tr->trackpoints;
1634 VikTrackpoint *tp = VIK_TRACKPOINT(iter->data);
1635 if ( tp->has_timestamp ) {
1636 tsfirst = tp->timestamp;
1638 // Find the end of the track and the last timestamp
1639 while ( iter->next ) {
1642 tp = VIK_TRACKPOINT(iter->data);
1643 if ( tp->has_timestamp ) {
1644 tsdiff = tp->timestamp - tsfirst;
1646 tr_dist = vik_track_get_length_including_gaps ( tr );
1649 if ( tr_dist > 0 ) {
1650 iter = tr->trackpoints;
1651 // Apply the calculated timestamp to all trackpoints except the first and last ones
1652 while ( iter->next && iter->next->next ) {
1654 tp = VIK_TRACKPOINT(iter->data);
1655 cur_dist += vik_coord_diff ( &(tp->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1657 tp->timestamp = (cur_dist / tr_dist) * tsdiff + tsfirst;
1658 tp->has_timestamp = TRUE;
1660 // Some points may now have the same time so remove them.
1661 vik_track_remove_same_time_points ( tr );
1668 * vik_track_apply_dem_data:
1669 * @skip_existing: When TRUE, don't change the elevation if the trackpoint already has a value
1671 * Set elevation data for a track using any available DEM information
1673 gulong vik_track_apply_dem_data ( VikTrack *tr, gboolean skip_existing )
1678 tp_iter = tr->trackpoints;
1680 // Don't apply if the point already has a value and the overwrite is off
1681 if ( !(skip_existing && VIK_TRACKPOINT(tp_iter->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1682 /* TODO: of the 4 possible choices we have for choosing an elevation
1683 * (trackpoint in between samples), choose the one with the least elevation change
1685 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST );
1687 if ( elev != VIK_DEM_INVALID_ELEVATION ) {
1688 VIK_TRACKPOINT(tp_iter->data)->altitude = elev;
1692 tp_iter = tp_iter->next;
1698 * vik_track_apply_dem_data_last_trackpoint:
1699 * Apply DEM data (if available) - to only the last trackpoint
1701 void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr )
1704 if ( tr->trackpoints ) {
1705 /* As in vik_track_apply_dem_data above - use 'best' interpolation method */
1706 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->coord), VIK_DEM_INTERPOL_BEST );
1707 if ( elev != VIK_DEM_INVALID_ELEVATION )
1708 VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->altitude = elev;
1716 * Apply elevation smoothing over range of trackpoints between the list start and end points
1718 static void smoothie ( GList *tp1, GList *tp2, gdouble elev1, gdouble elev2, guint points )
1720 // If was really clever could try and weigh interpolation according to the distance between trackpoints somehow
1721 // Instead a simple average interpolation for the number of points given.
1722 gdouble change = (elev2 - elev1)/(points+1);
1724 GList *tp_iter = tp1;
1725 while ( tp_iter != tp2 && tp_iter ) {
1726 VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);
1728 tp->altitude = elev1 + (change*count);
1731 tp_iter = tp_iter->next;
1736 * vik_track_smooth_missing_elevation_data:
1737 * @flat: Specify how the missing elevations will be set.
1738 * When TRUE it uses a simple flat method, using the last known elevation
1739 * When FALSE is uses an interpolation method to the next known elevation
1741 * For each point with a missing elevation, set it to use the last known available elevation value.
1742 * Primarily of use for smallish DEM holes where it is missing elevation data.
1743 * Eg see Austria: around N47.3 & E13.8
1745 * Returns: The number of points that were adjusted
1747 gulong vik_track_smooth_missing_elevation_data ( VikTrack *tr, gboolean flat )
1752 gdouble elev = VIK_DEFAULT_ALTITUDE;
1754 VikTrackpoint *tp_missing = NULL;
1755 GList *iter_first = NULL;
1758 tp_iter = tr->trackpoints;
1760 VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);
1762 if ( VIK_DEFAULT_ALTITUDE == tp->altitude ) {
1764 // Simply assign to last known value
1765 if ( elev != VIK_DEFAULT_ALTITUDE ) {
1766 tp->altitude = elev;
1771 if ( !tp_missing ) {
1772 // Remember the first trackpoint (and the list pointer to it) of a section of no altitudes
1774 iter_first = tp_iter;
1778 // More missing altitudes
1784 // Altitude available (maybe again!)
1785 // If this marks the end of a section of altitude-less points
1786 // then apply smoothing for that section of points
1787 if ( points > 0 && elev != VIK_DEFAULT_ALTITUDE )
1789 smoothie ( iter_first, tp_iter, elev, tp->altitude, points );
1797 // Store for reuse as the last known good value
1798 elev = tp->altitude;
1801 tp_iter = tp_iter->next;
1808 * vik_track_steal_and_append_trackpoints:
1810 * appends t2 to t1, leaving t2 with no trackpoints
1812 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
1814 if ( t1->trackpoints ) {
1815 t1->trackpoints = g_list_concat ( t1->trackpoints, t2->trackpoints );
1817 t1->trackpoints = t2->trackpoints;
1818 t2->trackpoints = NULL;
1820 // Trackpoints updated - so update the bounds
1821 vik_track_calculate_bounds ( t1 );
1825 * vik_track_cut_back_to_double_point:
1827 * starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
1828 * If there is no double point, deletes all the trackpoints.
1830 * Returns: the new end of the track (or the start if there are no double points)
1832 VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr )
1834 GList *iter = tr->trackpoints;
1839 while ( iter->next )
1843 while ( iter->prev ) {
1844 VikCoord *cur_coord = &((VikTrackpoint*)iter->data)->coord;
1845 VikCoord *prev_coord = &((VikTrackpoint*)iter->prev->data)->coord;
1846 if ( vik_coord_equals(cur_coord, prev_coord) ) {
1847 GList *prev = iter->prev;
1849 rv = g_malloc(sizeof(VikCoord));
1852 /* truncate trackpoint list */
1853 iter->prev = NULL; /* pretend it's the end */
1854 g_list_foreach ( iter, (GFunc) g_free, NULL );
1855 g_list_free( iter );
1864 /* no double point found! */
1865 rv = g_malloc(sizeof(VikCoord));
1866 *rv = ((VikTrackpoint*) tr->trackpoints->data)->coord;
1867 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
1868 g_list_free( tr->trackpoints );
1869 tr->trackpoints = NULL;
1874 * Function to compare two tracks by their first timestamp
1876 int vik_track_compare_timestamp (const void *x, const void *y)
1878 VikTrack *a = (VikTrack *)x;
1879 VikTrack *b = (VikTrack *)y;
1881 VikTrackpoint *tpa = NULL;
1882 VikTrackpoint *tpb = NULL;
1884 if ( a->trackpoints )
1885 tpa = VIK_TRACKPOINT(g_list_first(a->trackpoints)->data);
1887 if ( b->trackpoints )
1888 tpb = VIK_TRACKPOINT(g_list_first(b->trackpoints)->data);
1891 if ( tpa->timestamp < tpb->timestamp )
1893 if ( tpa->timestamp > tpb->timestamp )