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_set_source(VikTrack *tr, const gchar *source)
111 g_free ( tr->source );
113 if ( source && source[0] != '\0' )
114 tr->source = g_strdup(source);
119 void vik_track_set_type(VikTrack *tr, const gchar *type)
124 if ( type && type[0] != '\0' )
125 tr->type = g_strdup(type);
130 void vik_track_ref(VikTrack *tr)
135 void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog)
137 /* Warning: does not check for existing dialog */
138 tr->property_dialog = dialog;
141 void vik_track_clear_property_dialog(VikTrack *tr)
143 tr->property_dialog = NULL;
146 void vik_track_free(VikTrack *tr)
148 if ( tr->ref_count-- > 1 )
154 g_free ( tr->comment );
155 if ( tr->description )
156 g_free ( tr->description );
158 g_free ( tr->source );
161 g_list_foreach ( tr->trackpoints, (GFunc) vik_trackpoint_free, NULL );
162 g_list_free( tr->trackpoints );
163 if (tr->property_dialog)
164 if ( GTK_IS_WIDGET(tr->property_dialog) )
165 gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) );
171 * @tr: The Track to copy
172 * @copy_points: Whether to copy the track points or not
174 * Normally for copying the track it's best to copy all the trackpoints
175 * However for some operations such as splitting tracks the trackpoints will be managed separately, so no need to copy them.
177 * Returns: the copied VikTrack
179 VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points )
181 VikTrack *new_tr = vik_track_new();
182 new_tr->name = g_strdup(tr->name);
183 new_tr->visible = tr->visible;
184 new_tr->is_route = tr->is_route;
185 new_tr->draw_name_mode = tr->draw_name_mode;
186 new_tr->max_number_dist_labels = tr->max_number_dist_labels;
187 new_tr->has_color = tr->has_color;
188 new_tr->color = tr->color;
189 new_tr->bbox = tr->bbox;
190 new_tr->trackpoints = NULL;
193 GList *tp_iter = tr->trackpoints;
196 VikTrackpoint *new_tp = vik_trackpoint_copy ( (VikTrackpoint*)(tp_iter->data) );
197 new_tr->trackpoints = g_list_prepend ( new_tr->trackpoints, new_tp );
198 tp_iter = tp_iter->next;
200 if ( new_tr->trackpoints )
201 new_tr->trackpoints = g_list_reverse ( new_tr->trackpoints );
203 vik_track_set_name(new_tr,tr->name);
204 vik_track_set_comment(new_tr,tr->comment);
205 vik_track_set_description(new_tr,tr->description);
206 vik_track_set_source(new_tr,tr->source);
210 VikTrackpoint *vik_trackpoint_new()
212 VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint));
215 tp->altitude = VIK_DEFAULT_ALTITUDE;
216 tp->hdop = VIK_DEFAULT_DOP;
217 tp->vdop = VIK_DEFAULT_DOP;
218 tp->pdop = VIK_DEFAULT_DOP;
222 void vik_trackpoint_free(VikTrackpoint *tp)
228 void vik_trackpoint_set_name(VikTrackpoint *tp, const gchar *name)
233 // If the name is blank then completely remove it
234 if ( name && name[0] == '\0' )
237 tp->name = g_strdup(name);
242 VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
244 VikTrackpoint *new_tp = vik_trackpoint_new();
245 memcpy ( new_tp, tp, sizeof(VikTrackpoint) );
247 new_tp->name = g_strdup (tp->name);
252 * track_recalculate_bounds_last_tp:
253 * @trk: The track to consider the recalculation on
255 * A faster bounds check, since it only considers the last track point
257 static void track_recalculate_bounds_last_tp ( VikTrack *trk )
259 GList *tpl = g_list_last ( trk->trackpoints );
263 // See if this trackpoint increases the track bounds and update if so
264 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tpl->data)->coord), &ll );
265 if ( ll.lat > trk->bbox.north )
266 trk->bbox.north = ll.lat;
267 if ( ll.lon < trk->bbox.west )
268 trk->bbox.west = ll.lon;
269 if ( ll.lat < trk->bbox.south )
270 trk->bbox.south = ll.lat;
271 if ( ll.lon > trk->bbox.east )
272 trk->bbox.east = ll.lon;
277 * vik_track_add_trackpoint:
278 * @tr: The track to which the trackpoint will be added
279 * @tp: The trackpoint to add
280 * @recalculate: Whether to perform any associated properties recalculations
281 * Generally one should avoid recalculation via this method if adding lots of points
282 * (But ensure calculate_bounds() is called after adding all points!!)
284 * The trackpoint is added to the end of the existing trackpoint list
286 void vik_track_add_trackpoint ( VikTrack *tr, VikTrackpoint *tp, gboolean recalculate )
288 // When it's the first trackpoint need to ensure the bounding box is initialized correctly
289 gboolean adding_first_point = tr->trackpoints ? FALSE : TRUE;
290 tr->trackpoints = g_list_append ( tr->trackpoints, tp );
291 if ( adding_first_point )
292 vik_track_calculate_bounds ( tr );
293 else if ( recalculate )
294 track_recalculate_bounds_last_tp ( tr );
298 * vik_track_get_length_to_trackpoint:
301 gdouble vik_track_get_length_to_trackpoint (const VikTrack *tr, const VikTrackpoint *tp)
304 if ( tr->trackpoints )
306 // Is it the very first track point?
307 if ( VIK_TRACKPOINT(tr->trackpoints->data) == tp )
310 GList *iter = tr->trackpoints->next;
313 VikTrackpoint *tp1 = VIK_TRACKPOINT(iter->data);
314 if ( ! tp1->newsegment )
315 len += vik_coord_diff ( &(tp1->coord),
316 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
318 // Exit when we reach the desired point
328 gdouble vik_track_get_length(const VikTrack *tr)
331 if ( tr->trackpoints )
333 GList *iter = tr->trackpoints->next;
336 if ( ! VIK_TRACKPOINT(iter->data)->newsegment )
337 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
338 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
345 gdouble vik_track_get_length_including_gaps(const VikTrack *tr)
348 if ( tr->trackpoints )
350 GList *iter = tr->trackpoints->next;
353 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
354 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
361 gulong vik_track_get_tp_count(const VikTrack *tr)
363 return g_list_length(tr->trackpoints);
366 gulong vik_track_get_dup_point_count ( const VikTrack *tr )
369 GList *iter = tr->trackpoints;
372 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
373 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
381 * Deletes adjacent points that have the same position
382 * Returns the number of points that were deleted
384 gulong vik_track_remove_dup_points ( VikTrack *tr )
387 GList *iter = tr->trackpoints;
390 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
391 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
394 // Maintain track segments
395 if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
396 VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
398 vik_trackpoint_free ( iter->next->data );
399 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
405 // NB isn't really be necessary as removing duplicate points shouldn't alter the bounds!
406 vik_track_calculate_bounds ( tr );
412 * Get a count of trackpoints with the same defined timestamp
413 * Note is using timestamps with a resolution with 1 second
415 gulong vik_track_get_same_time_point_count ( const VikTrack *tr )
418 GList *iter = tr->trackpoints;
421 ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
422 VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
423 ( VIK_TRACKPOINT(iter->data)->timestamp ==
424 VIK_TRACKPOINT(iter->next->data)->timestamp) )
432 * Deletes adjacent points that have the same defined timestamp
433 * Returns the number of points that were deleted
435 gulong vik_track_remove_same_time_points ( VikTrack *tr )
438 GList *iter = tr->trackpoints;
441 ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
442 VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
443 ( VIK_TRACKPOINT(iter->data)->timestamp ==
444 VIK_TRACKPOINT(iter->next->data)->timestamp) ) {
448 // Maintain track segments
449 if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
450 VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
452 vik_trackpoint_free ( iter->next->data );
453 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
459 vik_track_calculate_bounds ( tr );
465 * vik_track_remove_dodgy_first_point:
467 * @speed: Maximum speed in m/s between points allowed
468 * @recalc_bounds: Whether track bounds should be recalculated
469 * (i.e. can be skipped if bounds will get calculated later on)
471 * Returns: Whether the first point was removed
473 * ATM Primarily to remove dodgy first point inserted back at previous
474 * location by some Garmin GPS Etrex units after being significantly moved
475 * e.g. you've driven somewhere else and start recording a new cycle/walk etc...
477 * NB This function is limited to just handling first point issues,
478 * rather than a more comprehensive attempt to remove any suspicious points
479 * through-out the track.
481 gboolean vik_track_remove_dodgy_first_point ( VikTrack *vt, guint speed, gboolean recalc_bounds )
483 gboolean deleted = FALSE;
485 if ( vt->trackpoints ) {
486 GList *iter = g_list_first ( vt->trackpoints );
487 VikTrackpoint *tp1 = VIK_TRACKPOINT(iter->data);
489 if ( tp1->has_timestamp ) {
491 VikTrackpoint *tp2 = VIK_TRACKPOINT(iter->next->data);
492 if ( tp2->has_timestamp ) {
493 gdouble dist_diff = vik_coord_diff ( &tp1->coord, &tp2->coord );
494 time_t time_diff = tp2->timestamp - tp1->timestamp;
496 gdouble spd = fabs(dist_diff / (gint)time_diff);
499 vik_trackpoint_free ( tp1 );
500 vt->trackpoints = g_list_delete_link ( vt->trackpoints, iter );
502 vik_track_calculate_bounds ( vt );
513 * Deletes all 'extra' trackpoint information
514 * such as time stamps, speed, course etc...
516 void vik_track_to_routepoints ( VikTrack *tr )
518 GList *iter = tr->trackpoints;
521 // c.f. with vik_trackpoint_new()
523 VIK_TRACKPOINT(iter->data)->has_timestamp = FALSE;
524 VIK_TRACKPOINT(iter->data)->timestamp = 0;
525 VIK_TRACKPOINT(iter->data)->speed = NAN;
526 VIK_TRACKPOINT(iter->data)->course = NAN;
527 VIK_TRACKPOINT(iter->data)->hdop = VIK_DEFAULT_DOP;
528 VIK_TRACKPOINT(iter->data)->vdop = VIK_DEFAULT_DOP;
529 VIK_TRACKPOINT(iter->data)->pdop = VIK_DEFAULT_DOP;
530 VIK_TRACKPOINT(iter->data)->nsats = 0;
531 VIK_TRACKPOINT(iter->data)->fix_mode = VIK_GPS_MODE_NOT_SEEN;
537 guint vik_track_get_segment_count(const VikTrack *tr)
540 GList *iter = tr->trackpoints;
543 while ( (iter = iter->next) )
545 if ( VIK_TRACKPOINT(iter->data)->newsegment )
551 VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
556 guint segs = vik_track_get_segment_count(t);
565 rv = g_malloc ( segs * sizeof(VikTrack *) );
566 tr = vik_track_copy ( t, TRUE );
568 iter = tr->trackpoints;
571 while ( (iter = iter->next) )
573 if ( VIK_TRACKPOINT(iter->data)->newsegment )
575 iter->prev->next = NULL;
577 rv[i] = vik_track_copy ( tr, FALSE );
578 rv[i]->trackpoints = iter;
580 vik_track_calculate_bounds ( rv[i] );
590 * Simply remove any subsequent segment markers in a track to form one continuous track
591 * Return the number of segments merged
593 guint vik_track_merge_segments(VikTrack *tr)
596 GList *iter = tr->trackpoints;
600 // Always skip the first point as this should be the first segment
603 while ( (iter = iter->next) )
605 if ( VIK_TRACKPOINT(iter->data)->newsegment ) {
606 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
613 void vik_track_reverse ( VikTrack *tr )
615 if ( ! tr->trackpoints )
618 tr->trackpoints = g_list_reverse(tr->trackpoints);
620 /* fix 'newsegment' */
621 GList *iter = g_list_last ( tr->trackpoints );
624 if ( ! iter->next ) /* last segment, was first, cancel newsegment. */
625 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
626 if ( ! iter->prev ) /* first segment by convention has newsegment flag. */
627 VIK_TRACKPOINT(iter->data)->newsegment = TRUE;
628 else if ( VIK_TRACKPOINT(iter->data)->newsegment && iter->next )
630 VIK_TRACKPOINT(iter->next->data)->newsegment = TRUE;
631 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
638 * vik_track_get_duration:
640 * @segment_gaps: Whether the duration should include gaps between segments
642 * Returns: The time in seconds
643 * NB this may be negative particularly if the track has been reversed
645 time_t vik_track_get_duration(const VikTrack *trk, gboolean segment_gaps)
648 if ( trk->trackpoints ) {
649 // Ensure times are available
650 if ( vik_track_get_tp_first(trk)->has_timestamp ) {
651 // Get trkpt only once - as using vik_track_get_tp_last() iterates whole track each time
654 VikTrackpoint *trkpt_last = vik_track_get_tp_last(trk);
655 if ( trkpt_last->has_timestamp ) {
656 time_t t1 = vik_track_get_tp_first(trk)->timestamp;
657 time_t t2 = trkpt_last->timestamp;
662 // Total within segments
663 GList *iter = trk->trackpoints->next;
665 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
666 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
667 (!VIK_TRACKPOINT(iter->data)->newsegment) ) {
668 duration += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
678 gdouble vik_track_get_average_speed(const VikTrack *tr)
682 if ( tr->trackpoints )
684 GList *iter = tr->trackpoints->next;
687 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
688 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
689 (! VIK_TRACKPOINT(iter->data)->newsegment) )
691 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
692 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
693 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
698 return (time == 0) ? 0 : ABS(len/time);
702 * Based on a simple average speed, but with a twist - to give a moving average.
703 * . GPSs often report a moving average in their statistics output
704 * . bicycle speedos often don't factor in time when stopped - hence reporting a moving average for speed
706 * Often GPS track will record every second but not when stationary
707 * This method doesn't use samples that differ over the specified time limit - effectively skipping that time chunk from the total time
709 * Suggest to use 60 seconds as the stop length (as the default used in the TrackWaypoint draw stops factor)
711 gdouble vik_track_get_average_speed_moving (const VikTrack *tr, int stop_length_seconds)
715 if ( tr->trackpoints )
717 GList *iter = tr->trackpoints->next;
720 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
721 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
722 (! VIK_TRACKPOINT(iter->data)->newsegment) )
724 if ( ( VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp ) < stop_length_seconds ) {
725 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
726 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
728 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
734 return (time == 0) ? 0 : ABS(len/time);
737 gdouble vik_track_get_max_speed(const VikTrack *tr)
739 gdouble maxspeed = 0.0, speed = 0.0;
740 if ( tr->trackpoints )
742 GList *iter = tr->trackpoints->next;
745 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
746 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
747 (! VIK_TRACKPOINT(iter->data)->newsegment) )
749 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
750 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
751 if ( speed > maxspeed )
760 void vik_track_convert ( VikTrack *tr, VikCoordMode dest_mode )
762 GList *iter = tr->trackpoints;
765 vik_coord_convert ( &(VIK_TRACKPOINT(iter->data)->coord), dest_mode );
770 /* I understood this when I wrote it ... maybe ... Basically it eats up the
771 * proper amounts of length on the track and averages elevation over that. */
772 gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
775 gdouble total_length, chunk_length, current_dist, current_area_under_curve, current_seg_length, dist_along_seg = 0.0;
776 gdouble altitude1, altitude2;
777 guint16 current_chunk;
778 gboolean ignore_it = FALSE;
780 GList *iter = tr->trackpoints;
782 if (!iter || !iter->next) /* zero- or one-point track */
785 { /* test if there's anything worth calculating */
786 gboolean okay = FALSE;
789 // Sometimes a GPS device (or indeed any random file) can have stupid numbers for elevations
790 // Since when is 9.9999e+24 a valid elevation!!
791 // 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)
792 // Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception)
793 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE &&
794 VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) {
803 iter = tr->trackpoints;
805 g_assert ( num_chunks < 16000 );
807 pts = g_malloc ( sizeof(gdouble) * num_chunks );
809 total_length = vik_track_get_length_including_gaps ( tr );
810 chunk_length = total_length / num_chunks;
812 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
813 if (chunk_length <= 0) {
819 current_area_under_curve = 0;
821 current_seg_length = 0;
823 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
824 &(VIK_TRACKPOINT(iter->next->data)->coord) );
825 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
826 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
829 while ( current_chunk < num_chunks ) {
831 /* go along current seg */
832 if ( current_seg_length && (current_seg_length - dist_along_seg) > chunk_length ) {
833 dist_along_seg += chunk_length;
837 * /x altitude = alt_at_pt_1 + alt_at_pt_2 / 2 = altitude1 + slope * dist_value_of_pt_inbetween_pt1_and_pt2
838 * /xx avg altitude = area under curve / chunk len
839 *pt1 *xxx avg altitude = altitude1 + (altitude2-altitude1)/(current_seg_length)*(dist_along_seg + (chunk_len/2))
845 // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero)
846 pts[current_chunk] = altitude1;
848 pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
852 /* finish current seg */
853 if ( current_seg_length ) {
854 gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg;
855 current_dist = current_seg_length - dist_along_seg;
856 current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5;
857 } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */
859 /* get intervening segs */
861 while ( iter && iter->next ) {
862 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
863 &(VIK_TRACKPOINT(iter->next->data)->coord) );
864 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
865 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
866 ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment;
868 if ( chunk_length - current_dist >= current_seg_length ) {
869 current_dist += current_seg_length;
870 current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5;
878 dist_along_seg = chunk_length - current_dist;
879 if ( ignore_it || ( iter && !iter->next ) ) {
880 pts[current_chunk] = current_area_under_curve / current_dist;
883 for (i = current_chunk + 1; i < num_chunks; i++)
884 pts[i] = pts[current_chunk];
889 current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length);
890 pts[current_chunk] = current_area_under_curve / chunk_length;
902 void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
906 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE )
908 GList *iter = tr->trackpoints->next;
911 diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude;
919 *up = *down = VIK_DEFAULT_ALTITUDE;
922 gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks )
926 gdouble total_length, chunk_length, current_gradient;
927 gdouble altitude1, altitude2;
928 guint16 current_chunk;
930 g_assert ( num_chunks < 16000 );
932 total_length = vik_track_get_length_including_gaps ( tr );
933 chunk_length = total_length / num_chunks;
935 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
936 if (chunk_length <= 0) {
940 altitudes = vik_track_make_elevation_map (tr, num_chunks);
941 if (altitudes == NULL) {
945 current_gradient = 0.0;
946 pts = g_malloc ( sizeof(gdouble) * num_chunks );
947 for (current_chunk = 0; current_chunk < (num_chunks - 1); current_chunk++) {
948 altitude1 = altitudes[current_chunk];
949 altitude2 = altitudes[current_chunk + 1];
950 current_gradient = 100.0 * (altitude2 - altitude1) / chunk_length;
952 pts[current_chunk] = current_gradient;
955 pts[current_chunk] = current_gradient;
957 g_free ( altitudes );
962 /* by Alex Foobarian */
963 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
966 gdouble duration, chunk_dur;
968 int i, pt_count, numpts, index;
971 if ( ! tr->trackpoints )
974 g_assert ( num_chunks < 16000 );
976 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
977 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
980 if ( !t1 || !t2 || !duration )
984 g_warning("negative duration: unsorted trackpoint timestamps?");
987 pt_count = vik_track_get_tp_count(tr);
989 v = g_malloc ( sizeof(gdouble) * num_chunks );
990 chunk_dur = duration / num_chunks;
992 s = g_malloc(sizeof(double) * pt_count);
993 t = g_malloc(sizeof(double) * pt_count);
995 iter = tr->trackpoints->next;
998 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1001 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
1002 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1007 /* In the following computation, we iterate through periods of time of duration chunk_dur.
1008 * The first period begins at the beginning of the track. The last period ends at the end of the track.
1010 index = 0; /* index of the current trackpoint. */
1011 for (i = 0; i < num_chunks; i++) {
1012 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
1013 * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints.
1015 if (t[0] + i*chunk_dur >= t[index]) {
1016 gdouble acc_t = 0, acc_s = 0;
1017 while (t[0] + i*chunk_dur >= t[index]) {
1018 acc_s += (s[index+1]-s[index]);
1019 acc_t += (t[index+1]-t[index]);
1037 * Make a distance/time map, heavily based on the vik_track_make_speed_map method
1039 gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks )
1042 gdouble duration, chunk_dur;
1044 int i, pt_count, numpts, index;
1047 if ( ! tr->trackpoints )
1050 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1051 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1054 if ( !t1 || !t2 || !duration )
1058 g_warning("negative duration: unsorted trackpoint timestamps?");
1061 pt_count = vik_track_get_tp_count(tr);
1063 v = g_malloc ( sizeof(gdouble) * num_chunks );
1064 chunk_dur = duration / num_chunks;
1066 s = g_malloc(sizeof(double) * pt_count);
1067 t = g_malloc(sizeof(double) * pt_count);
1069 iter = tr->trackpoints->next;
1072 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1075 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
1076 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1081 /* In the following computation, we iterate through periods of time of duration chunk_dur.
1082 * The first period begins at the beginning of the track. The last period ends at the end of the track.
1084 index = 0; /* index of the current trackpoint. */
1085 for (i = 0; i < num_chunks; i++) {
1086 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
1087 * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints.
1089 if (t[0] + i*chunk_dur >= t[index]) {
1090 gdouble acc_s = 0; // No need for acc_t
1091 while (t[0] + i*chunk_dur >= t[index]) {
1092 acc_s += (s[index+1]-s[index]);
1095 // The only bit that's really different from the speed map - just keep an accululative record distance
1096 v[i] = i ? v[i-1]+acc_s : acc_s;
1111 * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance)
1112 * This results in a slightly blocky graph when it does not have many trackpoints: <60
1113 * NB Somehow the elevation/distance applies some kind of smoothing algorithm,
1114 * but I don't think any one understands it any more (I certainly don't ATM)
1116 gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks )
1119 gdouble duration, chunk_dur;
1120 GList *iter = tr->trackpoints;
1122 if (!iter || !iter->next) /* zero- or one-point track */
1125 /* test if there's anything worth calculating */
1126 gboolean okay = FALSE;
1128 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
1137 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1138 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1141 if ( !t1 || !t2 || !duration )
1145 g_warning("negative duration: unsorted trackpoint timestamps?");
1148 gint pt_count = vik_track_get_tp_count(tr);
1150 // Reset iterator back to the beginning
1151 iter = tr->trackpoints;
1153 gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values
1154 gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes
1155 gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times
1157 chunk_dur = duration / num_chunks;
1159 s[0] = VIK_TRACKPOINT(iter->data)->altitude;
1160 t[0] = VIK_TRACKPOINT(iter->data)->timestamp;
1161 iter = tr->trackpoints->next;
1164 s[numpts] = VIK_TRACKPOINT(iter->data)->altitude;
1165 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1170 /* In the following computation, we iterate through periods of time of duration chunk_dur.
1171 * The first period begins at the beginning of the track. The last period ends at the end of the track.
1173 gint index = 0; /* index of the current trackpoint. */
1175 for (i = 0; i < num_chunks; i++) {
1176 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
1177 * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints.
1179 if (t[0] + i*chunk_dur >= t[index]) {
1180 gdouble acc_s = s[index]; // initialise to first point
1181 while (t[0] + i*chunk_dur >= t[index]) {
1182 acc_s += (s[index+1]-s[index]);
1201 * Make a speed/distance map
1203 gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks )
1207 gint i, pt_count, numpts, index;
1209 gdouble duration, total_length, chunk_length;
1211 if ( ! tr->trackpoints )
1214 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1215 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1218 if ( !t1 || !t2 || !duration )
1222 g_warning("negative duration: unsorted trackpoint timestamps?");
1226 total_length = vik_track_get_length_including_gaps ( tr );
1227 chunk_length = total_length / num_chunks;
1228 pt_count = vik_track_get_tp_count(tr);
1230 if (chunk_length <= 0) {
1234 v = g_malloc ( sizeof(gdouble) * num_chunks );
1235 s = g_malloc ( sizeof(double) * pt_count );
1236 t = g_malloc ( sizeof(double) * pt_count );
1238 // No special handling of segments ATM...
1239 iter = tr->trackpoints->next;
1242 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1245 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
1246 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1251 // Iterate through a portion of the track to get an average speed for that part
1252 // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps'
1253 index = 0; /* index of the current trackpoint. */
1254 for (i = 0; i < num_chunks; i++) {
1255 // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk
1256 if (s[0] + i*chunk_length >= s[index]) {
1257 gdouble acc_t = 0, acc_s = 0;
1258 while (s[0] + i*chunk_length >= s[index]) {
1259 acc_s += (s[index+1]-s[index]);
1260 acc_t += (t[index+1]-t[index]);
1278 * vik_track_get_tp_by_dist:
1279 * @trk: The Track on which to find a Trackpoint
1280 * @meters_from_start: The distance along a track that the trackpoint returned is near
1281 * @get_next_point: Since there is a choice of trackpoints, this determines which one to return
1282 * @tp_metres_from_start: For the returned Trackpoint, returns the distance along the track
1284 * TODO: Consider changing the boolean get_next_point into an enum with these options PREVIOUS, NEXT, NEAREST
1286 * Returns: The #VikTrackpoint fitting the criteria or NULL
1288 VikTrackpoint *vik_track_get_tp_by_dist ( VikTrack *trk, gdouble meters_from_start, gboolean get_next_point, gdouble *tp_metres_from_start )
1290 gdouble current_dist = 0.0;
1291 gdouble current_inc = 0.0;
1292 if ( tp_metres_from_start )
1293 *tp_metres_from_start = 0.0;
1295 if ( trk->trackpoints ) {
1296 GList *iter = g_list_next ( g_list_first ( trk->trackpoints ) );
1298 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1299 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1300 current_dist += current_inc;
1301 if ( current_dist >= meters_from_start )
1303 iter = g_list_next ( iter );
1305 // passed the end of the track
1309 if ( tp_metres_from_start )
1310 *tp_metres_from_start = current_dist;
1312 // we've gone past the distance already, is the previous trackpoint wanted?
1313 if ( !get_next_point ) {
1315 if ( tp_metres_from_start )
1316 *tp_metres_from_start = current_dist-current_inc;
1317 return VIK_TRACKPOINT(iter->prev->data);
1320 return VIK_TRACKPOINT(iter->data);
1326 /* by Alex Foobarian */
1327 VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start )
1329 gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
1330 gdouble current_dist = 0.0;
1331 gdouble current_inc = 0.0;
1332 if ( tr->trackpoints )
1334 GList *iter = tr->trackpoints->next;
1335 GList *last_iter = NULL;
1336 gdouble last_dist = 0.0;
1339 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1340 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1341 last_dist = current_dist;
1342 current_dist += current_inc;
1343 if ( current_dist >= dist )
1348 if (!iter) { /* passing the end the track */
1350 if (meters_from_start)
1351 *meters_from_start = last_dist;
1352 return(VIK_TRACKPOINT(last_iter->data));
1357 /* we've gone past the dist already, was prev trackpoint closer? */
1358 /* should do a vik_coord_average_weighted() thingy. */
1359 if ( iter->prev && fabs(current_dist-current_inc-dist) < fabs(current_dist-dist) ) {
1360 if (meters_from_start)
1361 *meters_from_start = last_dist;
1365 if (meters_from_start)
1366 *meters_from_start = current_dist;
1368 return VIK_TRACKPOINT(iter->data);
1374 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
1376 if ( !tr->trackpoints )
1379 time_t t_pos, t_start, t_end, t_total;
1380 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1381 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1382 t_total = t_end - t_start;
1384 t_pos = t_start + t_total * reltime;
1386 GList *iter = tr->trackpoints;
1389 if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
1391 if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
1392 if (iter->prev == NULL) /* first trackpoint */
1394 time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev->data)->timestamp;
1395 time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
1396 if (t_before <= t_after)
1400 else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
1407 if (seconds_from_start)
1408 *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1409 return VIK_TRACKPOINT(iter->data);
1412 VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
1414 gdouble maxspeed = 0.0, speed = 0.0;
1416 if ( !tr->trackpoints )
1419 GList *iter = tr->trackpoints;
1420 VikTrackpoint *max_speed_tp = NULL;
1424 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
1425 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
1426 (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
1427 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
1428 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
1429 if ( speed > maxspeed ) {
1431 max_speed_tp = VIK_TRACKPOINT(iter->data);
1441 return max_speed_tp;
1444 VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
1446 gdouble maxalt = -5000.0;
1447 if ( !tr->trackpoints )
1450 GList *iter = tr->trackpoints;
1451 VikTrackpoint *max_alt_tp = NULL;
1454 if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
1455 maxalt = VIK_TRACKPOINT(iter->data)->altitude;
1456 max_alt_tp = VIK_TRACKPOINT(iter->data);
1467 VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
1469 gdouble minalt = 25000.0;
1470 if ( !tr->trackpoints )
1473 GList *iter = tr->trackpoints;
1474 VikTrackpoint *min_alt_tp = NULL;
1477 if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
1478 minalt = VIK_TRACKPOINT(iter->data)->altitude;
1479 min_alt_tp = VIK_TRACKPOINT(iter->data);
1490 VikTrackpoint *vik_track_get_tp_first( const VikTrack *tr )
1492 if ( !tr->trackpoints )
1495 return (VikTrackpoint*)g_list_first(tr->trackpoints)->data;
1498 VikTrackpoint *vik_track_get_tp_last ( const VikTrack *tr )
1500 if ( !tr->trackpoints )
1503 return (VikTrackpoint*)g_list_last(tr->trackpoints)->data;
1506 VikTrackpoint *vik_track_get_tp_prev ( const VikTrack *tr, VikTrackpoint *tp )
1508 if ( !tr->trackpoints )
1511 GList *iter = tr->trackpoints;
1512 VikTrackpoint *tp_prev = NULL;
1516 if ( VIK_TRACKPOINT(iter->data) == tp ) {
1517 tp_prev = VIK_TRACKPOINT(iter->prev->data);
1527 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
1531 if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1532 GList *iter = tr->trackpoints->next;
1536 tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
1537 if ( tmp_alt > *max_alt )
1539 if ( tmp_alt < *min_alt )
1548 void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
1551 GByteArray *b = g_byte_array_new();
1555 g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
1557 /* we'll fill out number of trackpoints later */
1559 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1561 // This allocates space for variant sized strings
1562 // and copies that amount of data from the track to byte array
1563 #define vtm_append(s) \
1564 len = (s) ? strlen(s)+1 : 0; \
1565 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
1566 if (s) g_byte_array_append(b, (guint8 *)s, len);
1568 tps = tr->trackpoints;
1571 g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
1572 vtm_append(VIK_TRACKPOINT(tps->data)->name);
1576 *(guint *)(b->data + intp) = ntp;
1578 vtm_append(tr->name);
1579 vtm_append(tr->comment);
1580 vtm_append(tr->description);
1581 vtm_append(tr->source);
1585 g_byte_array_free(b, FALSE);
1589 * Take a byte array and convert it into a Track
1591 VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
1594 VikTrack *new_tr = vik_track_new();
1595 VikTrackpoint *new_tp;
1599 /* basic properties: */
1600 new_tr->visible = ((VikTrack *)data)->visible;
1601 new_tr->is_route = ((VikTrack *)data)->is_route;
1602 new_tr->draw_name_mode = ((VikTrack *)data)->draw_name_mode;
1603 new_tr->max_number_dist_labels = ((VikTrack *)data)->max_number_dist_labels;
1604 new_tr->has_color = ((VikTrack *)data)->has_color;
1605 new_tr->color = ((VikTrack *)data)->color;
1606 new_tr->bbox = ((VikTrack *)data)->bbox;
1608 data += sizeof(*new_tr);
1610 ntp = *(guint *)data;
1611 data += sizeof(ntp);
1613 #define vtu_get(s) \
1614 len = *(guint *)data; \
1615 data += sizeof(len); \
1617 (s) = g_strdup((gchar *)data); \
1623 for (i=0; i<ntp; i++) {
1624 new_tp = vik_trackpoint_new();
1625 memcpy(new_tp, data, sizeof(*new_tp));
1626 data += sizeof(*new_tp);
1627 vtu_get(new_tp->name);
1628 new_tr->trackpoints = g_list_prepend(new_tr->trackpoints, new_tp);
1630 if ( new_tr->trackpoints )
1631 new_tr->trackpoints = g_list_reverse(new_tr->trackpoints);
1633 vtu_get(new_tr->name);
1634 vtu_get(new_tr->comment);
1635 vtu_get(new_tr->description);
1636 vtu_get(new_tr->source);
1642 * (Re)Calculate the bounds of the given track,
1643 * updating the track's bounds data.
1644 * This should be called whenever a track's trackpoints are changed
1646 void vik_track_calculate_bounds ( VikTrack *trk )
1649 tp_iter = trk->trackpoints;
1651 struct LatLon topleft, bottomright, ll;
1653 // Set bounds to first point
1655 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &topleft );
1656 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &bottomright );
1660 // See if this trackpoint increases the track bounds.
1662 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &ll );
1664 if ( ll.lat > topleft.lat) topleft.lat = ll.lat;
1665 if ( ll.lon < topleft.lon) topleft.lon = ll.lon;
1666 if ( ll.lat < bottomright.lat) bottomright.lat = ll.lat;
1667 if ( ll.lon > bottomright.lon) bottomright.lon = ll.lon;
1669 tp_iter = tp_iter->next;
1672 g_debug ( "Bounds of track: '%s' is: %f,%f to: %f,%f", trk->name, topleft.lat, topleft.lon, bottomright.lat, bottomright.lon );
1674 trk->bbox.north = topleft.lat;
1675 trk->bbox.east = bottomright.lon;
1676 trk->bbox.south = bottomright.lat;
1677 trk->bbox.west = topleft.lon;
1681 * vik_track_anonymize_times:
1683 * Shift all timestamps to be relatively offset from 1901-01-01
1685 void vik_track_anonymize_times ( VikTrack *tr )
1688 // Check result just to please Coverity - even though it shouldn't fail as it's a hard coded value here!
1689 if ( !g_time_val_from_iso8601 ( "1901-01-01T00:00:00Z", >v ) ) {
1690 g_critical ( "Calendar time value failure" );
1694 time_t anon_timestamp = gtv.tv_sec;
1698 tp_iter = tr->trackpoints;
1700 VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);
1701 if ( tp->has_timestamp ) {
1702 // Calculate an offset in time using the first available timestamp
1704 offset = tp->timestamp - anon_timestamp;
1706 // Apply this offset to shift all timestamps towards 1901 & hence anonymising the time
1707 // Note that the relative difference between timestamps is kept - thus calculating speeds will still work
1708 tp->timestamp = tp->timestamp - offset;
1710 tp_iter = tp_iter->next;
1715 * vik_track_interpolate_times:
1717 * Interpolate the timestamps between first and last trackpoint,
1718 * so that the track is driven at equal speed, regardless of the
1719 * distance between individual trackpoints.
1721 * NB This will overwrite any existing trackpoint timestamps
1723 void vik_track_interpolate_times ( VikTrack *tr )
1725 gdouble tr_dist, cur_dist;
1726 time_t tsdiff, tsfirst;
1729 iter = tr->trackpoints;
1731 VikTrackpoint *tp = VIK_TRACKPOINT(iter->data);
1732 if ( tp->has_timestamp ) {
1733 tsfirst = tp->timestamp;
1735 // Find the end of the track and the last timestamp
1736 while ( iter->next ) {
1739 tp = VIK_TRACKPOINT(iter->data);
1740 if ( tp->has_timestamp ) {
1741 tsdiff = tp->timestamp - tsfirst;
1743 tr_dist = vik_track_get_length_including_gaps ( tr );
1746 if ( tr_dist > 0 ) {
1747 iter = tr->trackpoints;
1748 // Apply the calculated timestamp to all trackpoints except the first and last ones
1749 while ( iter->next && iter->next->next ) {
1751 tp = VIK_TRACKPOINT(iter->data);
1752 cur_dist += vik_coord_diff ( &(tp->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1754 tp->timestamp = (cur_dist / tr_dist) * tsdiff + tsfirst;
1755 tp->has_timestamp = TRUE;
1757 // Some points may now have the same time so remove them.
1758 vik_track_remove_same_time_points ( tr );
1765 * vik_track_apply_dem_data:
1766 * @skip_existing: When TRUE, don't change the elevation if the trackpoint already has a value
1768 * Set elevation data for a track using any available DEM information
1770 gulong vik_track_apply_dem_data ( VikTrack *tr, gboolean skip_existing )
1775 tp_iter = tr->trackpoints;
1777 // Don't apply if the point already has a value and the overwrite is off
1778 if ( !(skip_existing && VIK_TRACKPOINT(tp_iter->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1779 /* TODO: of the 4 possible choices we have for choosing an elevation
1780 * (trackpoint in between samples), choose the one with the least elevation change
1782 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST );
1784 if ( elev != VIK_DEM_INVALID_ELEVATION ) {
1785 VIK_TRACKPOINT(tp_iter->data)->altitude = elev;
1789 tp_iter = tp_iter->next;
1795 * vik_track_apply_dem_data_last_trackpoint:
1796 * Apply DEM data (if available) - to only the last trackpoint
1798 void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr )
1801 if ( tr->trackpoints ) {
1802 /* As in vik_track_apply_dem_data above - use 'best' interpolation method */
1803 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->coord), VIK_DEM_INTERPOL_BEST );
1804 if ( elev != VIK_DEM_INVALID_ELEVATION )
1805 VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->altitude = elev;
1813 * Apply elevation smoothing over range of trackpoints between the list start and end points
1815 static void smoothie ( GList *tp1, GList *tp2, gdouble elev1, gdouble elev2, guint points )
1817 // If was really clever could try and weigh interpolation according to the distance between trackpoints somehow
1818 // Instead a simple average interpolation for the number of points given.
1819 gdouble change = (elev2 - elev1)/(points+1);
1821 GList *tp_iter = tp1;
1822 while ( tp_iter != tp2 && tp_iter ) {
1823 VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);
1825 tp->altitude = elev1 + (change*count);
1828 tp_iter = tp_iter->next;
1833 * vik_track_smooth_missing_elevation_data:
1834 * @flat: Specify how the missing elevations will be set.
1835 * When TRUE it uses a simple flat method, using the last known elevation
1836 * When FALSE is uses an interpolation method to the next known elevation
1838 * For each point with a missing elevation, set it to use the last known available elevation value.
1839 * Primarily of use for smallish DEM holes where it is missing elevation data.
1840 * Eg see Austria: around N47.3 & E13.8
1842 * Returns: The number of points that were adjusted
1844 gulong vik_track_smooth_missing_elevation_data ( VikTrack *tr, gboolean flat )
1849 gdouble elev = VIK_DEFAULT_ALTITUDE;
1851 VikTrackpoint *tp_missing = NULL;
1852 GList *iter_first = NULL;
1855 tp_iter = tr->trackpoints;
1857 VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);
1859 if ( VIK_DEFAULT_ALTITUDE == tp->altitude ) {
1861 // Simply assign to last known value
1862 if ( elev != VIK_DEFAULT_ALTITUDE ) {
1863 tp->altitude = elev;
1868 if ( !tp_missing ) {
1869 // Remember the first trackpoint (and the list pointer to it) of a section of no altitudes
1871 iter_first = tp_iter;
1875 // More missing altitudes
1881 // Altitude available (maybe again!)
1882 // If this marks the end of a section of altitude-less points
1883 // then apply smoothing for that section of points
1884 if ( points > 0 && elev != VIK_DEFAULT_ALTITUDE )
1886 smoothie ( iter_first, tp_iter, elev, tp->altitude, points );
1894 // Store for reuse as the last known good value
1895 elev = tp->altitude;
1898 tp_iter = tp_iter->next;
1905 * vik_track_steal_and_append_trackpoints:
1907 * appends t2 to t1, leaving t2 with no trackpoints
1909 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
1911 if ( t1->trackpoints ) {
1912 t1->trackpoints = g_list_concat ( t1->trackpoints, t2->trackpoints );
1914 t1->trackpoints = t2->trackpoints;
1915 t2->trackpoints = NULL;
1917 // Trackpoints updated - so update the bounds
1918 vik_track_calculate_bounds ( t1 );
1922 * vik_track_cut_back_to_double_point:
1924 * starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
1925 * If there is no double point, deletes all the trackpoints.
1927 * Returns: the new end of the track (or the start if there are no double points)
1929 VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr )
1931 GList *iter = tr->trackpoints;
1936 while ( iter->next )
1940 while ( iter->prev ) {
1941 VikCoord *cur_coord = &((VikTrackpoint*)iter->data)->coord;
1942 VikCoord *prev_coord = &((VikTrackpoint*)iter->prev->data)->coord;
1943 if ( vik_coord_equals(cur_coord, prev_coord) ) {
1944 GList *prev = iter->prev;
1946 rv = g_malloc(sizeof(VikCoord));
1949 /* truncate trackpoint list */
1950 iter->prev = NULL; /* pretend it's the end */
1951 g_list_foreach ( iter, (GFunc) g_free, NULL );
1952 g_list_free( iter );
1961 /* no double point found! */
1962 rv = g_malloc(sizeof(VikCoord));
1963 *rv = ((VikTrackpoint*) tr->trackpoints->data)->coord;
1964 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
1965 g_list_free( tr->trackpoints );
1966 tr->trackpoints = NULL;
1971 * Function to compare two tracks by their first timestamp
1973 int vik_track_compare_timestamp (const void *x, const void *y)
1975 VikTrack *a = (VikTrack *)x;
1976 VikTrack *b = (VikTrack *)y;
1978 VikTrackpoint *tpa = NULL;
1979 VikTrackpoint *tpb = NULL;
1981 if ( a->trackpoints )
1982 tpa = VIK_TRACKPOINT(g_list_first(a->trackpoints)->data);
1984 if ( b->trackpoints )
1985 tpb = VIK_TRACKPOINT(g_list_first(b->trackpoints)->data);
1988 if ( tpa->timestamp < tpb->timestamp )
1990 if ( tpa->timestamp > tpb->timestamp )