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 * Deletes all 'extra' trackpoint information
466 * such as time stamps, speed, course etc...
468 void vik_track_to_routepoints ( VikTrack *tr )
470 GList *iter = tr->trackpoints;
473 // c.f. with vik_trackpoint_new()
475 VIK_TRACKPOINT(iter->data)->has_timestamp = FALSE;
476 VIK_TRACKPOINT(iter->data)->timestamp = 0;
477 VIK_TRACKPOINT(iter->data)->speed = NAN;
478 VIK_TRACKPOINT(iter->data)->course = NAN;
479 VIK_TRACKPOINT(iter->data)->hdop = VIK_DEFAULT_DOP;
480 VIK_TRACKPOINT(iter->data)->vdop = VIK_DEFAULT_DOP;
481 VIK_TRACKPOINT(iter->data)->pdop = VIK_DEFAULT_DOP;
482 VIK_TRACKPOINT(iter->data)->nsats = 0;
483 VIK_TRACKPOINT(iter->data)->fix_mode = VIK_GPS_MODE_NOT_SEEN;
489 guint vik_track_get_segment_count(const VikTrack *tr)
492 GList *iter = tr->trackpoints;
495 while ( (iter = iter->next) )
497 if ( VIK_TRACKPOINT(iter->data)->newsegment )
503 VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
508 guint segs = vik_track_get_segment_count(t);
517 rv = g_malloc ( segs * sizeof(VikTrack *) );
518 tr = vik_track_copy ( t, TRUE );
520 iter = tr->trackpoints;
523 while ( (iter = iter->next) )
525 if ( VIK_TRACKPOINT(iter->data)->newsegment )
527 iter->prev->next = NULL;
529 rv[i] = vik_track_copy ( tr, FALSE );
530 rv[i]->trackpoints = iter;
532 vik_track_calculate_bounds ( rv[i] );
542 * Simply remove any subsequent segment markers in a track to form one continuous track
543 * Return the number of segments merged
545 guint vik_track_merge_segments(VikTrack *tr)
548 GList *iter = tr->trackpoints;
552 // Always skip the first point as this should be the first segment
555 while ( (iter = iter->next) )
557 if ( VIK_TRACKPOINT(iter->data)->newsegment ) {
558 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
565 void vik_track_reverse ( VikTrack *tr )
567 if ( ! tr->trackpoints )
570 tr->trackpoints = g_list_reverse(tr->trackpoints);
572 /* fix 'newsegment' */
573 GList *iter = g_list_last ( tr->trackpoints );
576 if ( ! iter->next ) /* last segment, was first, cancel newsegment. */
577 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
578 if ( ! iter->prev ) /* first segment by convention has newsegment flag. */
579 VIK_TRACKPOINT(iter->data)->newsegment = TRUE;
580 else if ( VIK_TRACKPOINT(iter->data)->newsegment && iter->next )
582 VIK_TRACKPOINT(iter->next->data)->newsegment = TRUE;
583 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
590 * vik_track_get_duration:
592 * @segment_gaps: Whether the duration should include gaps between segments
594 * Returns: The time in seconds
595 * NB this may be negative particularly if the track has been reversed
597 time_t vik_track_get_duration(const VikTrack *trk, gboolean segment_gaps)
600 if ( trk->trackpoints ) {
601 // Ensure times are available
602 if ( vik_track_get_tp_first(trk)->has_timestamp ) {
603 // Get trkpt only once - as using vik_track_get_tp_last() iterates whole track each time
606 VikTrackpoint *trkpt_last = vik_track_get_tp_last(trk);
607 if ( trkpt_last->has_timestamp ) {
608 time_t t1 = vik_track_get_tp_first(trk)->timestamp;
609 time_t t2 = trkpt_last->timestamp;
614 // Total within segments
615 GList *iter = trk->trackpoints->next;
617 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
618 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
619 (!VIK_TRACKPOINT(iter->data)->newsegment) ) {
620 duration += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
630 gdouble vik_track_get_average_speed(const VikTrack *tr)
634 if ( tr->trackpoints )
636 GList *iter = tr->trackpoints->next;
639 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
640 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
641 (! VIK_TRACKPOINT(iter->data)->newsegment) )
643 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
644 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
645 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
650 return (time == 0) ? 0 : ABS(len/time);
654 * Based on a simple average speed, but with a twist - to give a moving average.
655 * . GPSs often report a moving average in their statistics output
656 * . bicycle speedos often don't factor in time when stopped - hence reporting a moving average for speed
658 * Often GPS track will record every second but not when stationary
659 * This method doesn't use samples that differ over the specified time limit - effectively skipping that time chunk from the total time
661 * Suggest to use 60 seconds as the stop length (as the default used in the TrackWaypoint draw stops factor)
663 gdouble vik_track_get_average_speed_moving (const VikTrack *tr, int stop_length_seconds)
667 if ( tr->trackpoints )
669 GList *iter = tr->trackpoints->next;
672 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
673 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
674 (! VIK_TRACKPOINT(iter->data)->newsegment) )
676 if ( ( VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp ) < stop_length_seconds ) {
677 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
678 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
680 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
686 return (time == 0) ? 0 : ABS(len/time);
689 gdouble vik_track_get_max_speed(const VikTrack *tr)
691 gdouble maxspeed = 0.0, speed = 0.0;
692 if ( tr->trackpoints )
694 GList *iter = tr->trackpoints->next;
697 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
698 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
699 (! VIK_TRACKPOINT(iter->data)->newsegment) )
701 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
702 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
703 if ( speed > maxspeed )
712 void vik_track_convert ( VikTrack *tr, VikCoordMode dest_mode )
714 GList *iter = tr->trackpoints;
717 vik_coord_convert ( &(VIK_TRACKPOINT(iter->data)->coord), dest_mode );
722 /* I understood this when I wrote it ... maybe ... Basically it eats up the
723 * proper amounts of length on the track and averages elevation over that. */
724 gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
727 gdouble total_length, chunk_length, current_dist, current_area_under_curve, current_seg_length, dist_along_seg = 0.0;
728 gdouble altitude1, altitude2;
729 guint16 current_chunk;
730 gboolean ignore_it = FALSE;
732 GList *iter = tr->trackpoints;
734 if (!iter || !iter->next) /* zero- or one-point track */
737 { /* test if there's anything worth calculating */
738 gboolean okay = FALSE;
741 // Sometimes a GPS device (or indeed any random file) can have stupid numbers for elevations
742 // Since when is 9.9999e+24 a valid elevation!!
743 // 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)
744 // Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception)
745 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE &&
746 VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) {
755 iter = tr->trackpoints;
757 g_assert ( num_chunks < 16000 );
759 pts = g_malloc ( sizeof(gdouble) * num_chunks );
761 total_length = vik_track_get_length_including_gaps ( tr );
762 chunk_length = total_length / num_chunks;
764 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
765 if (chunk_length <= 0) {
771 current_area_under_curve = 0;
773 current_seg_length = 0;
775 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
776 &(VIK_TRACKPOINT(iter->next->data)->coord) );
777 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
778 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
781 while ( current_chunk < num_chunks ) {
783 /* go along current seg */
784 if ( current_seg_length && (current_seg_length - dist_along_seg) > chunk_length ) {
785 dist_along_seg += chunk_length;
789 * /x altitude = alt_at_pt_1 + alt_at_pt_2 / 2 = altitude1 + slope * dist_value_of_pt_inbetween_pt1_and_pt2
790 * /xx avg altitude = area under curve / chunk len
791 *pt1 *xxx avg altitude = altitude1 + (altitude2-altitude1)/(current_seg_length)*(dist_along_seg + (chunk_len/2))
797 // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero)
798 pts[current_chunk] = altitude1;
800 pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
804 /* finish current seg */
805 if ( current_seg_length ) {
806 gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg;
807 current_dist = current_seg_length - dist_along_seg;
808 current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5;
809 } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */
811 /* get intervening segs */
813 while ( iter && iter->next ) {
814 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
815 &(VIK_TRACKPOINT(iter->next->data)->coord) );
816 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
817 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
818 ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment;
820 if ( chunk_length - current_dist >= current_seg_length ) {
821 current_dist += current_seg_length;
822 current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5;
830 dist_along_seg = chunk_length - current_dist;
831 if ( ignore_it || ( iter && !iter->next ) ) {
832 pts[current_chunk] = current_area_under_curve / current_dist;
835 for (i = current_chunk + 1; i < num_chunks; i++)
836 pts[i] = pts[current_chunk];
841 current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length);
842 pts[current_chunk] = current_area_under_curve / chunk_length;
854 void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
858 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE )
860 GList *iter = tr->trackpoints->next;
863 diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude;
871 *up = *down = VIK_DEFAULT_ALTITUDE;
874 gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks )
878 gdouble total_length, chunk_length, current_gradient;
879 gdouble altitude1, altitude2;
880 guint16 current_chunk;
882 g_assert ( num_chunks < 16000 );
884 total_length = vik_track_get_length_including_gaps ( tr );
885 chunk_length = total_length / num_chunks;
887 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
888 if (chunk_length <= 0) {
892 altitudes = vik_track_make_elevation_map (tr, num_chunks);
893 if (altitudes == NULL) {
897 current_gradient = 0.0;
898 pts = g_malloc ( sizeof(gdouble) * num_chunks );
899 for (current_chunk = 0; current_chunk < (num_chunks - 1); current_chunk++) {
900 altitude1 = altitudes[current_chunk];
901 altitude2 = altitudes[current_chunk + 1];
902 current_gradient = 100.0 * (altitude2 - altitude1) / chunk_length;
904 pts[current_chunk] = current_gradient;
907 pts[current_chunk] = current_gradient;
909 g_free ( altitudes );
914 /* by Alex Foobarian */
915 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
918 gdouble duration, chunk_dur;
920 int i, pt_count, numpts, index;
923 if ( ! tr->trackpoints )
926 g_assert ( num_chunks < 16000 );
928 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
929 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
932 if ( !t1 || !t2 || !duration )
936 g_warning("negative duration: unsorted trackpoint timestamps?");
939 pt_count = vik_track_get_tp_count(tr);
941 v = g_malloc ( sizeof(gdouble) * num_chunks );
942 chunk_dur = duration / num_chunks;
944 s = g_malloc(sizeof(double) * pt_count);
945 t = g_malloc(sizeof(double) * pt_count);
947 iter = tr->trackpoints->next;
950 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
953 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
954 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
959 /* In the following computation, we iterate through periods of time of duration chunk_dur.
960 * The first period begins at the beginning of the track. The last period ends at the end of the track.
962 index = 0; /* index of the current trackpoint. */
963 for (i = 0; i < num_chunks; i++) {
964 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
965 * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints.
967 if (t[0] + i*chunk_dur >= t[index]) {
968 gdouble acc_t = 0, acc_s = 0;
969 while (t[0] + i*chunk_dur >= t[index]) {
970 acc_s += (s[index+1]-s[index]);
971 acc_t += (t[index+1]-t[index]);
989 * Make a distance/time map, heavily based on the vik_track_make_speed_map method
991 gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks )
994 gdouble duration, chunk_dur;
996 int i, pt_count, numpts, index;
999 if ( ! tr->trackpoints )
1002 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1003 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1006 if ( !t1 || !t2 || !duration )
1010 g_warning("negative duration: unsorted trackpoint timestamps?");
1013 pt_count = vik_track_get_tp_count(tr);
1015 v = g_malloc ( sizeof(gdouble) * num_chunks );
1016 chunk_dur = duration / num_chunks;
1018 s = g_malloc(sizeof(double) * pt_count);
1019 t = g_malloc(sizeof(double) * pt_count);
1021 iter = tr->trackpoints->next;
1024 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1027 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
1028 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1033 /* In the following computation, we iterate through periods of time of duration chunk_dur.
1034 * The first period begins at the beginning of the track. The last period ends at the end of the track.
1036 index = 0; /* index of the current trackpoint. */
1037 for (i = 0; i < num_chunks; i++) {
1038 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
1039 * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints.
1041 if (t[0] + i*chunk_dur >= t[index]) {
1042 gdouble acc_s = 0; // No need for acc_t
1043 while (t[0] + i*chunk_dur >= t[index]) {
1044 acc_s += (s[index+1]-s[index]);
1047 // The only bit that's really different from the speed map - just keep an accululative record distance
1048 v[i] = i ? v[i-1]+acc_s : acc_s;
1063 * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance)
1064 * This results in a slightly blocky graph when it does not have many trackpoints: <60
1065 * NB Somehow the elevation/distance applies some kind of smoothing algorithm,
1066 * but I don't think any one understands it any more (I certainly don't ATM)
1068 gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks )
1071 gdouble duration, chunk_dur;
1072 GList *iter = tr->trackpoints;
1074 if (!iter || !iter->next) /* zero- or one-point track */
1077 /* test if there's anything worth calculating */
1078 gboolean okay = FALSE;
1080 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
1089 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1090 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1093 if ( !t1 || !t2 || !duration )
1097 g_warning("negative duration: unsorted trackpoint timestamps?");
1100 gint pt_count = vik_track_get_tp_count(tr);
1102 // Reset iterator back to the beginning
1103 iter = tr->trackpoints;
1105 gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values
1106 gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes
1107 gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times
1109 chunk_dur = duration / num_chunks;
1111 s[0] = VIK_TRACKPOINT(iter->data)->altitude;
1112 t[0] = VIK_TRACKPOINT(iter->data)->timestamp;
1113 iter = tr->trackpoints->next;
1116 s[numpts] = VIK_TRACKPOINT(iter->data)->altitude;
1117 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1122 /* In the following computation, we iterate through periods of time of duration chunk_dur.
1123 * The first period begins at the beginning of the track. The last period ends at the end of the track.
1125 gint index = 0; /* index of the current trackpoint. */
1127 for (i = 0; i < num_chunks; i++) {
1128 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
1129 * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints.
1131 if (t[0] + i*chunk_dur >= t[index]) {
1132 gdouble acc_s = s[index]; // initialise to first point
1133 while (t[0] + i*chunk_dur >= t[index]) {
1134 acc_s += (s[index+1]-s[index]);
1153 * Make a speed/distance map
1155 gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks )
1159 gint i, pt_count, numpts, index;
1161 gdouble duration, total_length, chunk_length;
1163 if ( ! tr->trackpoints )
1166 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1167 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1170 if ( !t1 || !t2 || !duration )
1174 g_warning("negative duration: unsorted trackpoint timestamps?");
1178 total_length = vik_track_get_length_including_gaps ( tr );
1179 chunk_length = total_length / num_chunks;
1180 pt_count = vik_track_get_tp_count(tr);
1182 if (chunk_length <= 0) {
1186 v = g_malloc ( sizeof(gdouble) * num_chunks );
1187 s = g_malloc ( sizeof(double) * pt_count );
1188 t = g_malloc ( sizeof(double) * pt_count );
1190 // No special handling of segments ATM...
1191 iter = tr->trackpoints->next;
1194 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1197 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
1198 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1203 // Iterate through a portion of the track to get an average speed for that part
1204 // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps'
1205 index = 0; /* index of the current trackpoint. */
1206 for (i = 0; i < num_chunks; i++) {
1207 // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk
1208 if (s[0] + i*chunk_length >= s[index]) {
1209 gdouble acc_t = 0, acc_s = 0;
1210 while (s[0] + i*chunk_length >= s[index]) {
1211 acc_s += (s[index+1]-s[index]);
1212 acc_t += (t[index+1]-t[index]);
1230 * vik_track_get_tp_by_dist:
1231 * @trk: The Track on which to find a Trackpoint
1232 * @meters_from_start: The distance along a track that the trackpoint returned is near
1233 * @get_next_point: Since there is a choice of trackpoints, this determines which one to return
1234 * @tp_metres_from_start: For the returned Trackpoint, returns the distance along the track
1236 * TODO: Consider changing the boolean get_next_point into an enum with these options PREVIOUS, NEXT, NEAREST
1238 * Returns: The #VikTrackpoint fitting the criteria or NULL
1240 VikTrackpoint *vik_track_get_tp_by_dist ( VikTrack *trk, gdouble meters_from_start, gboolean get_next_point, gdouble *tp_metres_from_start )
1242 gdouble current_dist = 0.0;
1243 gdouble current_inc = 0.0;
1244 if ( tp_metres_from_start )
1245 *tp_metres_from_start = 0.0;
1247 if ( trk->trackpoints ) {
1248 GList *iter = g_list_next ( g_list_first ( trk->trackpoints ) );
1250 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1251 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1252 current_dist += current_inc;
1253 if ( current_dist >= meters_from_start )
1255 iter = g_list_next ( iter );
1257 // passed the end of the track
1261 if ( tp_metres_from_start )
1262 *tp_metres_from_start = current_dist;
1264 // we've gone past the distance already, is the previous trackpoint wanted?
1265 if ( !get_next_point ) {
1267 if ( tp_metres_from_start )
1268 *tp_metres_from_start = current_dist-current_inc;
1269 return VIK_TRACKPOINT(iter->prev->data);
1272 return VIK_TRACKPOINT(iter->data);
1278 /* by Alex Foobarian */
1279 VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start )
1281 gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
1282 gdouble current_dist = 0.0;
1283 gdouble current_inc = 0.0;
1284 if ( tr->trackpoints )
1286 GList *iter = tr->trackpoints->next;
1287 GList *last_iter = NULL;
1288 gdouble last_dist = 0.0;
1291 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1292 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1293 last_dist = current_dist;
1294 current_dist += current_inc;
1295 if ( current_dist >= dist )
1300 if (!iter) { /* passing the end the track */
1302 if (meters_from_start)
1303 *meters_from_start = last_dist;
1304 return(VIK_TRACKPOINT(last_iter->data));
1309 /* we've gone past the dist already, was prev trackpoint closer? */
1310 /* should do a vik_coord_average_weighted() thingy. */
1311 if ( iter->prev && fabs(current_dist-current_inc-dist) < fabs(current_dist-dist) ) {
1312 if (meters_from_start)
1313 *meters_from_start = last_dist;
1317 if (meters_from_start)
1318 *meters_from_start = current_dist;
1320 return VIK_TRACKPOINT(iter->data);
1326 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
1328 if ( !tr->trackpoints )
1331 time_t t_pos, t_start, t_end, t_total;
1332 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1333 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1334 t_total = t_end - t_start;
1336 t_pos = t_start + t_total * reltime;
1338 GList *iter = tr->trackpoints;
1341 if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
1343 if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
1344 if (iter->prev == NULL) /* first trackpoint */
1346 time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev->data)->timestamp;
1347 time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
1348 if (t_before <= t_after)
1352 else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
1359 if (seconds_from_start)
1360 *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1361 return VIK_TRACKPOINT(iter->data);
1364 VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
1366 gdouble maxspeed = 0.0, speed = 0.0;
1368 if ( !tr->trackpoints )
1371 GList *iter = tr->trackpoints;
1372 VikTrackpoint *max_speed_tp = NULL;
1376 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
1377 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
1378 (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
1379 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
1380 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
1381 if ( speed > maxspeed ) {
1383 max_speed_tp = VIK_TRACKPOINT(iter->data);
1393 return max_speed_tp;
1396 VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
1398 gdouble maxalt = -5000.0;
1399 if ( !tr->trackpoints )
1402 GList *iter = tr->trackpoints;
1403 VikTrackpoint *max_alt_tp = NULL;
1406 if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
1407 maxalt = VIK_TRACKPOINT(iter->data)->altitude;
1408 max_alt_tp = VIK_TRACKPOINT(iter->data);
1419 VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
1421 gdouble minalt = 25000.0;
1422 if ( !tr->trackpoints )
1425 GList *iter = tr->trackpoints;
1426 VikTrackpoint *min_alt_tp = NULL;
1429 if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
1430 minalt = VIK_TRACKPOINT(iter->data)->altitude;
1431 min_alt_tp = VIK_TRACKPOINT(iter->data);
1442 VikTrackpoint *vik_track_get_tp_first( const VikTrack *tr )
1444 if ( !tr->trackpoints )
1447 return (VikTrackpoint*)g_list_first(tr->trackpoints)->data;
1450 VikTrackpoint *vik_track_get_tp_last ( const VikTrack *tr )
1452 if ( !tr->trackpoints )
1455 return (VikTrackpoint*)g_list_last(tr->trackpoints)->data;
1458 VikTrackpoint *vik_track_get_tp_prev ( const VikTrack *tr, VikTrackpoint *tp )
1460 if ( !tr->trackpoints )
1463 GList *iter = tr->trackpoints;
1464 VikTrackpoint *tp_prev = NULL;
1468 if ( VIK_TRACKPOINT(iter->data) == tp ) {
1469 tp_prev = VIK_TRACKPOINT(iter->prev->data);
1479 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
1483 if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1484 GList *iter = tr->trackpoints->next;
1488 tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
1489 if ( tmp_alt > *max_alt )
1491 if ( tmp_alt < *min_alt )
1500 void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
1503 GByteArray *b = g_byte_array_new();
1507 g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
1509 /* we'll fill out number of trackpoints later */
1511 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1513 // This allocates space for variant sized strings
1514 // and copies that amount of data from the track to byte array
1515 #define vtm_append(s) \
1516 len = (s) ? strlen(s)+1 : 0; \
1517 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
1518 if (s) g_byte_array_append(b, (guint8 *)s, len);
1520 tps = tr->trackpoints;
1523 g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
1524 vtm_append(VIK_TRACKPOINT(tps->data)->name);
1528 *(guint *)(b->data + intp) = ntp;
1530 vtm_append(tr->name);
1531 vtm_append(tr->comment);
1532 vtm_append(tr->description);
1533 vtm_append(tr->source);
1537 g_byte_array_free(b, FALSE);
1541 * Take a byte array and convert it into a Track
1543 VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
1546 VikTrack *new_tr = vik_track_new();
1547 VikTrackpoint *new_tp;
1551 /* basic properties: */
1552 new_tr->visible = ((VikTrack *)data)->visible;
1553 new_tr->is_route = ((VikTrack *)data)->is_route;
1554 new_tr->draw_name_mode = ((VikTrack *)data)->draw_name_mode;
1555 new_tr->max_number_dist_labels = ((VikTrack *)data)->max_number_dist_labels;
1556 new_tr->has_color = ((VikTrack *)data)->has_color;
1557 new_tr->color = ((VikTrack *)data)->color;
1558 new_tr->bbox = ((VikTrack *)data)->bbox;
1560 data += sizeof(*new_tr);
1562 ntp = *(guint *)data;
1563 data += sizeof(ntp);
1565 #define vtu_get(s) \
1566 len = *(guint *)data; \
1567 data += sizeof(len); \
1569 (s) = g_strdup((gchar *)data); \
1575 for (i=0; i<ntp; i++) {
1576 new_tp = vik_trackpoint_new();
1577 memcpy(new_tp, data, sizeof(*new_tp));
1578 data += sizeof(*new_tp);
1579 vtu_get(new_tp->name);
1580 new_tr->trackpoints = g_list_prepend(new_tr->trackpoints, new_tp);
1582 if ( new_tr->trackpoints )
1583 new_tr->trackpoints = g_list_reverse(new_tr->trackpoints);
1585 vtu_get(new_tr->name);
1586 vtu_get(new_tr->comment);
1587 vtu_get(new_tr->description);
1588 vtu_get(new_tr->source);
1594 * (Re)Calculate the bounds of the given track,
1595 * updating the track's bounds data.
1596 * This should be called whenever a track's trackpoints are changed
1598 void vik_track_calculate_bounds ( VikTrack *trk )
1601 tp_iter = trk->trackpoints;
1603 struct LatLon topleft, bottomright, ll;
1605 // Set bounds to first point
1607 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &topleft );
1608 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &bottomright );
1612 // See if this trackpoint increases the track bounds.
1614 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &ll );
1616 if ( ll.lat > topleft.lat) topleft.lat = ll.lat;
1617 if ( ll.lon < topleft.lon) topleft.lon = ll.lon;
1618 if ( ll.lat < bottomright.lat) bottomright.lat = ll.lat;
1619 if ( ll.lon > bottomright.lon) bottomright.lon = ll.lon;
1621 tp_iter = tp_iter->next;
1624 g_debug ( "Bounds of track: '%s' is: %f,%f to: %f,%f", trk->name, topleft.lat, topleft.lon, bottomright.lat, bottomright.lon );
1626 trk->bbox.north = topleft.lat;
1627 trk->bbox.east = bottomright.lon;
1628 trk->bbox.south = bottomright.lat;
1629 trk->bbox.west = topleft.lon;
1633 * vik_track_anonymize_times:
1635 * Shift all timestamps to be relatively offset from 1901-01-01
1637 void vik_track_anonymize_times ( VikTrack *tr )
1640 // Check result just to please Coverity - even though it shouldn't fail as it's a hard coded value here!
1641 if ( !g_time_val_from_iso8601 ( "1901-01-01T00:00:00Z", >v ) ) {
1642 g_critical ( "Calendar time value failure" );
1646 time_t anon_timestamp = gtv.tv_sec;
1650 tp_iter = tr->trackpoints;
1652 VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);
1653 if ( tp->has_timestamp ) {
1654 // Calculate an offset in time using the first available timestamp
1656 offset = tp->timestamp - anon_timestamp;
1658 // Apply this offset to shift all timestamps towards 1901 & hence anonymising the time
1659 // Note that the relative difference between timestamps is kept - thus calculating speeds will still work
1660 tp->timestamp = tp->timestamp - offset;
1662 tp_iter = tp_iter->next;
1667 * vik_track_interpolate_times:
1669 * Interpolate the timestamps between first and last trackpoint,
1670 * so that the track is driven at equal speed, regardless of the
1671 * distance between individual trackpoints.
1673 * NB This will overwrite any existing trackpoint timestamps
1675 void vik_track_interpolate_times ( VikTrack *tr )
1677 gdouble tr_dist, cur_dist;
1678 time_t tsdiff, tsfirst;
1681 iter = tr->trackpoints;
1683 VikTrackpoint *tp = VIK_TRACKPOINT(iter->data);
1684 if ( tp->has_timestamp ) {
1685 tsfirst = tp->timestamp;
1687 // Find the end of the track and the last timestamp
1688 while ( iter->next ) {
1691 tp = VIK_TRACKPOINT(iter->data);
1692 if ( tp->has_timestamp ) {
1693 tsdiff = tp->timestamp - tsfirst;
1695 tr_dist = vik_track_get_length_including_gaps ( tr );
1698 if ( tr_dist > 0 ) {
1699 iter = tr->trackpoints;
1700 // Apply the calculated timestamp to all trackpoints except the first and last ones
1701 while ( iter->next && iter->next->next ) {
1703 tp = VIK_TRACKPOINT(iter->data);
1704 cur_dist += vik_coord_diff ( &(tp->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1706 tp->timestamp = (cur_dist / tr_dist) * tsdiff + tsfirst;
1707 tp->has_timestamp = TRUE;
1709 // Some points may now have the same time so remove them.
1710 vik_track_remove_same_time_points ( tr );
1717 * vik_track_apply_dem_data:
1718 * @skip_existing: When TRUE, don't change the elevation if the trackpoint already has a value
1720 * Set elevation data for a track using any available DEM information
1722 gulong vik_track_apply_dem_data ( VikTrack *tr, gboolean skip_existing )
1727 tp_iter = tr->trackpoints;
1729 // Don't apply if the point already has a value and the overwrite is off
1730 if ( !(skip_existing && VIK_TRACKPOINT(tp_iter->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1731 /* TODO: of the 4 possible choices we have for choosing an elevation
1732 * (trackpoint in between samples), choose the one with the least elevation change
1734 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST );
1736 if ( elev != VIK_DEM_INVALID_ELEVATION ) {
1737 VIK_TRACKPOINT(tp_iter->data)->altitude = elev;
1741 tp_iter = tp_iter->next;
1747 * vik_track_apply_dem_data_last_trackpoint:
1748 * Apply DEM data (if available) - to only the last trackpoint
1750 void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr )
1753 if ( tr->trackpoints ) {
1754 /* As in vik_track_apply_dem_data above - use 'best' interpolation method */
1755 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->coord), VIK_DEM_INTERPOL_BEST );
1756 if ( elev != VIK_DEM_INVALID_ELEVATION )
1757 VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->altitude = elev;
1765 * Apply elevation smoothing over range of trackpoints between the list start and end points
1767 static void smoothie ( GList *tp1, GList *tp2, gdouble elev1, gdouble elev2, guint points )
1769 // If was really clever could try and weigh interpolation according to the distance between trackpoints somehow
1770 // Instead a simple average interpolation for the number of points given.
1771 gdouble change = (elev2 - elev1)/(points+1);
1773 GList *tp_iter = tp1;
1774 while ( tp_iter != tp2 && tp_iter ) {
1775 VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);
1777 tp->altitude = elev1 + (change*count);
1780 tp_iter = tp_iter->next;
1785 * vik_track_smooth_missing_elevation_data:
1786 * @flat: Specify how the missing elevations will be set.
1787 * When TRUE it uses a simple flat method, using the last known elevation
1788 * When FALSE is uses an interpolation method to the next known elevation
1790 * For each point with a missing elevation, set it to use the last known available elevation value.
1791 * Primarily of use for smallish DEM holes where it is missing elevation data.
1792 * Eg see Austria: around N47.3 & E13.8
1794 * Returns: The number of points that were adjusted
1796 gulong vik_track_smooth_missing_elevation_data ( VikTrack *tr, gboolean flat )
1801 gdouble elev = VIK_DEFAULT_ALTITUDE;
1803 VikTrackpoint *tp_missing = NULL;
1804 GList *iter_first = NULL;
1807 tp_iter = tr->trackpoints;
1809 VikTrackpoint *tp = VIK_TRACKPOINT(tp_iter->data);
1811 if ( VIK_DEFAULT_ALTITUDE == tp->altitude ) {
1813 // Simply assign to last known value
1814 if ( elev != VIK_DEFAULT_ALTITUDE ) {
1815 tp->altitude = elev;
1820 if ( !tp_missing ) {
1821 // Remember the first trackpoint (and the list pointer to it) of a section of no altitudes
1823 iter_first = tp_iter;
1827 // More missing altitudes
1833 // Altitude available (maybe again!)
1834 // If this marks the end of a section of altitude-less points
1835 // then apply smoothing for that section of points
1836 if ( points > 0 && elev != VIK_DEFAULT_ALTITUDE )
1838 smoothie ( iter_first, tp_iter, elev, tp->altitude, points );
1846 // Store for reuse as the last known good value
1847 elev = tp->altitude;
1850 tp_iter = tp_iter->next;
1857 * vik_track_steal_and_append_trackpoints:
1859 * appends t2 to t1, leaving t2 with no trackpoints
1861 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
1863 if ( t1->trackpoints ) {
1864 t1->trackpoints = g_list_concat ( t1->trackpoints, t2->trackpoints );
1866 t1->trackpoints = t2->trackpoints;
1867 t2->trackpoints = NULL;
1869 // Trackpoints updated - so update the bounds
1870 vik_track_calculate_bounds ( t1 );
1874 * vik_track_cut_back_to_double_point:
1876 * starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
1877 * If there is no double point, deletes all the trackpoints.
1879 * Returns: the new end of the track (or the start if there are no double points)
1881 VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr )
1883 GList *iter = tr->trackpoints;
1888 while ( iter->next )
1892 while ( iter->prev ) {
1893 VikCoord *cur_coord = &((VikTrackpoint*)iter->data)->coord;
1894 VikCoord *prev_coord = &((VikTrackpoint*)iter->prev->data)->coord;
1895 if ( vik_coord_equals(cur_coord, prev_coord) ) {
1896 GList *prev = iter->prev;
1898 rv = g_malloc(sizeof(VikCoord));
1901 /* truncate trackpoint list */
1902 iter->prev = NULL; /* pretend it's the end */
1903 g_list_foreach ( iter, (GFunc) g_free, NULL );
1904 g_list_free( iter );
1913 /* no double point found! */
1914 rv = g_malloc(sizeof(VikCoord));
1915 *rv = ((VikTrackpoint*) tr->trackpoints->data)->coord;
1916 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
1917 g_list_free( tr->trackpoints );
1918 tr->trackpoints = NULL;
1923 * Function to compare two tracks by their first timestamp
1925 int vik_track_compare_timestamp (const void *x, const void *y)
1927 VikTrack *a = (VikTrack *)x;
1928 VikTrack *b = (VikTrack *)y;
1930 VikTrackpoint *tpa = NULL;
1931 VikTrackpoint *tpb = NULL;
1933 if ( a->trackpoints )
1934 tpa = VIK_TRACKPOINT(g_list_first(a->trackpoints)->data);
1936 if ( b->trackpoints )
1937 tpb = VIK_TRACKPOINT(g_list_first(b->trackpoints)->data);
1940 if ( tpa->timestamp < tpb->timestamp )
1942 if ( tpa->timestamp > tpb->timestamp )