2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (c) 2012, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 VikTrack *vik_track_new()
44 VikTrack *tr = g_malloc0 ( sizeof ( VikTrack ) );
49 void vik_track_set_comment_no_copy(VikTrack *tr, gchar *comment)
52 g_free ( tr->comment );
53 tr->comment = comment;
57 void vik_track_set_name(VikTrack *tr, const gchar *name)
62 if ( name && name[0] != '\0' )
63 tr->name = g_strdup(name);
68 void vik_track_set_comment(VikTrack *tr, const gchar *comment)
71 g_free ( tr->comment );
73 if ( comment && comment[0] != '\0' )
74 tr->comment = g_strdup(comment);
79 void vik_track_set_description(VikTrack *tr, const gchar *description)
81 if ( tr->description )
82 g_free ( tr->description );
84 if ( description && description[0] != '\0' )
85 tr->description = g_strdup(description);
87 tr->description = NULL;
90 void vik_track_ref(VikTrack *tr)
95 void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog)
97 /* Warning: does not check for existing dialog */
98 tr->property_dialog = dialog;
101 void vik_track_clear_property_dialog(VikTrack *tr)
103 tr->property_dialog = NULL;
106 void vik_track_free(VikTrack *tr)
108 if ( tr->ref_count-- > 1 )
114 g_free ( tr->comment );
115 if ( tr->description )
116 g_free ( tr->description );
117 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
118 g_list_free( tr->trackpoints );
119 if (tr->property_dialog)
120 if ( GTK_IS_WIDGET(tr->property_dialog) )
121 gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) );
127 * @tr: The Track to copy
128 * @copy_points: Whether to copy the track points or not
130 * Normally for copying the track it's best to copy all the trackpoints
131 * However for some operations such as splitting tracks the trackpoints will be managed separately, so no need to copy them.
133 * Returns: the copied VikTrack
135 VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points )
137 VikTrack *new_tr = vik_track_new();
138 VikTrackpoint *new_tp;
139 GList *tp_iter = tr->trackpoints;
140 new_tr->visible = tr->visible;
141 new_tr->is_route = tr->is_route;
142 new_tr->has_color = tr->has_color;
143 new_tr->color = tr->color;
144 new_tr->bbox = tr->bbox;
145 new_tr->trackpoints = NULL;
150 new_tp = g_malloc ( sizeof ( VikTrackpoint ) );
151 *new_tp = *((VikTrackpoint *)(tp_iter->data));
152 new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp );
153 tp_iter = tp_iter->next;
156 vik_track_set_name(new_tr,tr->name);
157 vik_track_set_comment(new_tr,tr->comment);
158 vik_track_set_description(new_tr,tr->description);
162 VikTrackpoint *vik_trackpoint_new()
164 VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint));
167 tp->altitude = VIK_DEFAULT_ALTITUDE;
168 tp->hdop = VIK_DEFAULT_DOP;
169 tp->vdop = VIK_DEFAULT_DOP;
170 tp->pdop = VIK_DEFAULT_DOP;
174 void vik_trackpoint_free(VikTrackpoint *tp)
179 VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
181 VikTrackpoint *rv = vik_trackpoint_new();
186 gdouble vik_track_get_length(const VikTrack *tr)
189 if ( tr->trackpoints )
191 GList *iter = tr->trackpoints->next;
194 if ( ! VIK_TRACKPOINT(iter->data)->newsegment )
195 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
196 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
203 gdouble vik_track_get_length_including_gaps(const VikTrack *tr)
206 if ( tr->trackpoints )
208 GList *iter = tr->trackpoints->next;
211 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
212 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
219 gulong vik_track_get_tp_count(const VikTrack *tr)
222 GList *iter = tr->trackpoints;
231 gulong vik_track_get_dup_point_count ( const VikTrack *tr )
234 GList *iter = tr->trackpoints;
237 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
238 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
246 * Deletes adjacent points that have the same position
247 * Returns the number of points that were deleted
249 gulong vik_track_remove_dup_points ( VikTrack *tr )
252 GList *iter = tr->trackpoints;
255 if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
256 &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
259 // Maintain track segments
260 if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
261 VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
263 vik_trackpoint_free ( iter->next->data );
264 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
270 // NB isn't really be necessary as removing duplicate points shouldn't alter the bounds!
271 vik_track_calculate_bounds ( tr );
277 * Get a count of trackpoints with the same defined timestamp
278 * Note is using timestamps with a resolution with 1 second
280 gulong vik_track_get_same_time_point_count ( const VikTrack *tr )
283 GList *iter = tr->trackpoints;
286 ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
287 VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
288 ( VIK_TRACKPOINT(iter->data)->timestamp ==
289 VIK_TRACKPOINT(iter->next->data)->timestamp) )
297 * Deletes adjacent points that have the same defined timestamp
298 * Returns the number of points that were deleted
300 gulong vik_track_remove_same_time_points ( VikTrack *tr )
303 GList *iter = tr->trackpoints;
306 ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
307 VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
308 ( VIK_TRACKPOINT(iter->data)->timestamp ==
309 VIK_TRACKPOINT(iter->next->data)->timestamp) ) {
313 // Maintain track segments
314 if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
315 VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
317 vik_trackpoint_free ( iter->next->data );
318 tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
324 vik_track_calculate_bounds ( tr );
330 * Deletes all 'extra' trackpoint information
331 * such as time stamps, speed, course etc...
333 void vik_track_to_routepoints ( VikTrack *tr )
335 GList *iter = tr->trackpoints;
338 // c.f. with vik_trackpoint_new()
340 VIK_TRACKPOINT(iter->data)->has_timestamp = FALSE;
341 VIK_TRACKPOINT(iter->data)->timestamp = 0;
342 VIK_TRACKPOINT(iter->data)->speed = NAN;
343 VIK_TRACKPOINT(iter->data)->course = NAN;
344 VIK_TRACKPOINT(iter->data)->hdop = VIK_DEFAULT_DOP;
345 VIK_TRACKPOINT(iter->data)->vdop = VIK_DEFAULT_DOP;
346 VIK_TRACKPOINT(iter->data)->pdop = VIK_DEFAULT_DOP;
347 VIK_TRACKPOINT(iter->data)->nsats = 0;
348 VIK_TRACKPOINT(iter->data)->fix_mode = VIK_GPS_MODE_NOT_SEEN;
354 guint vik_track_get_segment_count(const VikTrack *tr)
357 GList *iter = tr->trackpoints;
360 while ( (iter = iter->next) )
362 if ( VIK_TRACKPOINT(iter->data)->newsegment )
368 VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
373 guint segs = vik_track_get_segment_count(t);
382 rv = g_malloc ( segs * sizeof(VikTrack *) );
383 tr = vik_track_copy ( t, TRUE );
385 iter = tr->trackpoints;
388 while ( (iter = iter->next) )
390 if ( VIK_TRACKPOINT(iter->data)->newsegment )
392 iter->prev->next = NULL;
394 rv[i] = vik_track_copy ( tr, FALSE );
395 rv[i]->trackpoints = iter;
397 vik_track_calculate_bounds ( rv[i] );
407 * Simply remove any subsequent segment markers in a track to form one continuous track
408 * Return the number of segments merged
410 guint vik_track_merge_segments(VikTrack *tr)
413 GList *iter = tr->trackpoints;
417 // Always skip the first point as this should be the first segment
420 while ( (iter = iter->next) )
422 if ( VIK_TRACKPOINT(iter->data)->newsegment ) {
423 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
430 void vik_track_reverse ( VikTrack *tr )
433 tr->trackpoints = g_list_reverse(tr->trackpoints);
435 /* fix 'newsegment' */
436 iter = g_list_last ( tr->trackpoints );
439 if ( ! iter->next ) /* last segment, was first, cancel newsegment. */
440 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
441 if ( ! iter->prev ) /* first segment by convention has newsegment flag. */
442 VIK_TRACKPOINT(iter->data)->newsegment = TRUE;
443 else if ( VIK_TRACKPOINT(iter->data)->newsegment && iter->next )
445 VIK_TRACKPOINT(iter->next->data)->newsegment = TRUE;
446 VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
452 gdouble vik_track_get_average_speed(const VikTrack *tr)
456 if ( tr->trackpoints )
458 GList *iter = tr->trackpoints->next;
461 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
462 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
463 (! VIK_TRACKPOINT(iter->data)->newsegment) )
465 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
466 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
467 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
472 return (time == 0) ? 0 : ABS(len/time);
476 * Based on a simple average speed, but with a twist - to give a moving average.
477 * . GPSs often report a moving average in their statistics output
478 * . bicycle speedos often don't factor in time when stopped - hence reporting a moving average for speed
480 * Often GPS track will record every second but not when stationary
481 * This method doesn't use samples that differ over the specified time limit - effectively skipping that time chunk from the total time
483 * Suggest to use 60 seconds as the stop length (as the default used in the TrackWaypoint draw stops factor)
485 gdouble vik_track_get_average_speed_moving (const VikTrack *tr, int stop_length_seconds)
489 if ( tr->trackpoints )
491 GList *iter = tr->trackpoints->next;
494 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
495 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
496 (! VIK_TRACKPOINT(iter->data)->newsegment) )
498 if ( ( VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp ) < stop_length_seconds ) {
499 len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
500 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
502 time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
508 return (time == 0) ? 0 : ABS(len/time);
511 gdouble vik_track_get_max_speed(const VikTrack *tr)
513 gdouble maxspeed = 0.0, speed = 0.0;
514 if ( tr->trackpoints )
516 GList *iter = tr->trackpoints->next;
519 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
520 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
521 (! VIK_TRACKPOINT(iter->data)->newsegment) )
523 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
524 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
525 if ( speed > maxspeed )
534 void vik_track_convert ( VikTrack *tr, VikCoordMode dest_mode )
536 GList *iter = tr->trackpoints;
539 vik_coord_convert ( &(VIK_TRACKPOINT(iter->data)->coord), dest_mode );
544 /* I understood this when I wrote it ... maybe ... Basically it eats up the
545 * proper amounts of length on the track and averages elevation over that. */
546 gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
549 gdouble total_length, chunk_length, current_dist, current_area_under_curve, current_seg_length, dist_along_seg = 0.0;
550 gdouble altitude1, altitude2;
551 guint16 current_chunk;
552 gboolean ignore_it = FALSE;
554 GList *iter = tr->trackpoints;
556 if (!iter || !iter->next) /* zero- or one-point track */
559 { /* test if there's anything worth calculating */
560 gboolean okay = FALSE;
563 // Sometimes a GPS device (or indeed any random file) can have stupid numbers for elevations
564 // Since when is 9.9999e+24 a valid elevation!!
565 // 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)
566 // Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception)
567 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE &&
568 VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) {
577 iter = tr->trackpoints;
579 g_assert ( num_chunks < 16000 );
581 pts = g_malloc ( sizeof(gdouble) * num_chunks );
583 total_length = vik_track_get_length_including_gaps ( tr );
584 chunk_length = total_length / num_chunks;
586 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
587 if (chunk_length <= 0) {
593 current_area_under_curve = 0;
595 current_seg_length = 0;
597 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
598 &(VIK_TRACKPOINT(iter->next->data)->coord) );
599 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
600 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
603 while ( current_chunk < num_chunks ) {
605 /* go along current seg */
606 if ( current_seg_length && (current_seg_length - dist_along_seg) > chunk_length ) {
607 dist_along_seg += chunk_length;
611 * /x altitude = alt_at_pt_1 + alt_at_pt_2 / 2 = altitude1 + slope * dist_value_of_pt_inbetween_pt1_and_pt2
612 * /xx avg altitude = area under curve / chunk len
613 *pt1 *xxx avg altitude = altitude1 + (altitude2-altitude1)/(current_seg_length)*(dist_along_seg + (chunk_len/2))
619 // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero)
620 pts[current_chunk] = altitude1;
622 pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
626 /* finish current seg */
627 if ( current_seg_length ) {
628 gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg;
629 current_dist = current_seg_length - dist_along_seg;
630 current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5;
631 } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */
633 /* get intervening segs */
635 while ( iter && iter->next ) {
636 current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
637 &(VIK_TRACKPOINT(iter->next->data)->coord) );
638 altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
639 altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
640 ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment;
642 if ( chunk_length - current_dist >= current_seg_length ) {
643 current_dist += current_seg_length;
644 current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5;
652 dist_along_seg = chunk_length - current_dist;
653 if ( ignore_it || ( iter && !iter->next ) ) {
654 pts[current_chunk] = current_area_under_curve / current_dist;
657 for (i = current_chunk + 1; i < num_chunks; i++)
658 pts[i] = pts[current_chunk];
663 current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length);
664 pts[current_chunk] = current_area_under_curve / chunk_length;
676 void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
680 if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE )
682 GList *iter = tr->trackpoints->next;
685 diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude;
693 *up = *down = VIK_DEFAULT_ALTITUDE;
696 gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks )
700 gdouble total_length, chunk_length, current_gradient;
701 gdouble altitude1, altitude2;
702 guint16 current_chunk;
704 g_assert ( num_chunks < 16000 );
706 total_length = vik_track_get_length_including_gaps ( tr );
707 chunk_length = total_length / num_chunks;
709 /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
710 if (chunk_length <= 0) {
714 altitudes = vik_track_make_elevation_map (tr, num_chunks);
715 if (altitudes == NULL) {
719 current_gradient = 0.0;
720 pts = g_malloc ( sizeof(gdouble) * num_chunks );
721 for (current_chunk = 0; current_chunk < (num_chunks - 1); current_chunk++) {
722 altitude1 = altitudes[current_chunk];
723 altitude2 = altitudes[current_chunk + 1];
724 current_gradient = 100.0 * (altitude2 - altitude1) / chunk_length;
726 pts[current_chunk] = current_gradient;
729 pts[current_chunk] = current_gradient;
734 /* by Alex Foobarian */
735 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
738 gdouble duration, chunk_dur;
740 int i, pt_count, numpts, index;
743 if ( ! tr->trackpoints )
746 g_assert ( num_chunks < 16000 );
748 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
749 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
752 if ( !t1 || !t2 || !duration )
756 g_warning("negative duration: unsorted trackpoint timestamps?");
759 pt_count = vik_track_get_tp_count(tr);
761 v = g_malloc ( sizeof(gdouble) * num_chunks );
762 chunk_dur = duration / num_chunks;
764 s = g_malloc(sizeof(double) * pt_count);
765 t = g_malloc(sizeof(double) * pt_count);
767 iter = tr->trackpoints->next;
770 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
773 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
774 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
779 /* In the following computation, we iterate through periods of time of duration chunk_dur.
780 * The first period begins at the beginning of the track. The last period ends at the end of the track.
782 index = 0; /* index of the current trackpoint. */
783 for (i = 0; i < num_chunks; i++) {
784 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
785 * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints.
787 if (t[0] + i*chunk_dur >= t[index]) {
788 gdouble acc_t = 0, acc_s = 0;
789 while (t[0] + i*chunk_dur >= t[index]) {
790 acc_s += (s[index+1]-s[index]);
791 acc_t += (t[index+1]-t[index]);
809 * Make a distance/time map, heavily based on the vik_track_make_speed_map method
811 gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks )
814 gdouble duration, chunk_dur;
816 int i, pt_count, numpts, index;
819 if ( ! tr->trackpoints )
822 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
823 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
826 if ( !t1 || !t2 || !duration )
830 g_warning("negative duration: unsorted trackpoint timestamps?");
833 pt_count = vik_track_get_tp_count(tr);
835 v = g_malloc ( sizeof(gdouble) * num_chunks );
836 chunk_dur = duration / num_chunks;
838 s = g_malloc(sizeof(double) * pt_count);
839 t = g_malloc(sizeof(double) * pt_count);
841 iter = tr->trackpoints->next;
844 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
847 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
848 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
853 /* In the following computation, we iterate through periods of time of duration chunk_dur.
854 * The first period begins at the beginning of the track. The last period ends at the end of the track.
856 index = 0; /* index of the current trackpoint. */
857 for (i = 0; i < num_chunks; i++) {
858 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
859 * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints.
861 if (t[0] + i*chunk_dur >= t[index]) {
862 gdouble acc_s = 0; // No need for acc_t
863 while (t[0] + i*chunk_dur >= t[index]) {
864 acc_s += (s[index+1]-s[index]);
867 // The only bit that's really different from the speed map - just keep an accululative record distance
868 v[i] = i ? v[i-1]+acc_s : acc_s;
883 * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance)
884 * This results in a slightly blocky graph when it does not have many trackpoints: <60
885 * NB Somehow the elevation/distance applies some kind of smoothing algorithm,
886 * but I don't think any one understands it any more (I certainly don't ATM)
888 gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks )
891 gdouble duration, chunk_dur;
892 GList *iter = tr->trackpoints;
894 if (!iter || !iter->next) /* zero- or one-point track */
897 /* test if there's anything worth calculating */
898 gboolean okay = FALSE;
900 if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
909 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
910 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
913 if ( !t1 || !t2 || !duration )
917 g_warning("negative duration: unsorted trackpoint timestamps?");
920 gint pt_count = vik_track_get_tp_count(tr);
922 // Reset iterator back to the beginning
923 iter = tr->trackpoints;
925 gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values
926 gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes
927 gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times
929 chunk_dur = duration / num_chunks;
931 s[0] = VIK_TRACKPOINT(iter->data)->altitude;
932 t[0] = VIK_TRACKPOINT(iter->data)->timestamp;
933 iter = tr->trackpoints->next;
936 s[numpts] = VIK_TRACKPOINT(iter->data)->altitude;
937 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
942 /* In the following computation, we iterate through periods of time of duration chunk_dur.
943 * The first period begins at the beginning of the track. The last period ends at the end of the track.
945 gint index = 0; /* index of the current trackpoint. */
947 for (i = 0; i < num_chunks; i++) {
948 /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
949 * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints.
951 if (t[0] + i*chunk_dur >= t[index]) {
952 gdouble acc_s = s[index]; // initialise to first point
953 while (t[0] + i*chunk_dur >= t[index]) {
954 acc_s += (s[index+1]-s[index]);
973 * Make a speed/distance map
975 gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks )
979 gint i, pt_count, numpts, index;
981 gdouble duration, total_length, chunk_length;
983 if ( ! tr->trackpoints )
986 t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
987 t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
990 if ( !t1 || !t2 || !duration )
994 g_warning("negative duration: unsorted trackpoint timestamps?");
998 total_length = vik_track_get_length_including_gaps ( tr );
999 chunk_length = total_length / num_chunks;
1000 pt_count = vik_track_get_tp_count(tr);
1002 if (chunk_length <= 0) {
1006 v = g_malloc ( sizeof(gdouble) * num_chunks );
1007 s = g_malloc ( sizeof(double) * pt_count );
1008 t = g_malloc ( sizeof(double) * pt_count );
1010 // No special handling of segments ATM...
1011 iter = tr->trackpoints->next;
1014 t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1017 s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
1018 t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
1023 // Iterate through a portion of the track to get an average speed for that part
1024 // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps'
1025 index = 0; /* index of the current trackpoint. */
1026 for (i = 0; i < num_chunks; i++) {
1027 // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk
1028 if (s[0] + i*chunk_length >= s[index]) {
1029 gdouble acc_t = 0, acc_s = 0;
1030 while (s[0] + i*chunk_length >= s[index]) {
1031 acc_s += (s[index+1]-s[index]);
1032 acc_t += (t[index+1]-t[index]);
1049 /* by Alex Foobarian */
1050 VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start )
1052 gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
1053 gdouble current_dist = 0.0;
1054 gdouble current_inc = 0.0;
1055 if ( tr->trackpoints )
1057 GList *iter = tr->trackpoints->next;
1058 GList *last_iter = NULL;
1059 gdouble last_dist = 0.0;
1062 current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1063 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1064 last_dist = current_dist;
1065 current_dist += current_inc;
1066 if ( current_dist >= dist )
1071 if (!iter) { /* passing the end the track */
1073 if (meters_from_start)
1074 *meters_from_start = last_dist;
1075 return(VIK_TRACKPOINT(last_iter->data));
1080 /* we've gone past the dist already, was prev trackpoint closer? */
1081 /* should do a vik_coord_average_weighted() thingy. */
1082 if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) {
1083 if (meters_from_start)
1084 *meters_from_start = last_dist;
1088 if (meters_from_start)
1089 *meters_from_start = current_dist;
1091 return VIK_TRACKPOINT(iter->data);
1097 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
1099 time_t t_pos, t_start, t_end, t_total;
1100 t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1101 t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1102 t_total = t_end - t_start;
1104 t_pos = t_start + t_total * reltime;
1106 if ( !tr->trackpoints )
1109 GList *iter = tr->trackpoints;
1112 if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
1114 if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
1115 if (iter->prev == NULL) /* first trackpoint */
1117 time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp;
1118 time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
1119 if (t_before <= t_after)
1123 else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
1130 if (seconds_from_start)
1131 *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1132 return VIK_TRACKPOINT(iter->data);
1135 VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
1137 gdouble maxspeed = 0.0, speed = 0.0;
1139 if ( !tr->trackpoints )
1142 GList *iter = tr->trackpoints;
1143 VikTrackpoint *max_speed_tp = NULL;
1147 if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
1148 VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
1149 (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
1150 speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
1151 / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
1152 if ( speed > maxspeed ) {
1154 max_speed_tp = VIK_TRACKPOINT(iter->data);
1164 return max_speed_tp;
1167 VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
1169 gdouble maxalt = -5000.0;
1170 if ( !tr->trackpoints )
1173 GList *iter = tr->trackpoints;
1174 VikTrackpoint *max_alt_tp = NULL;
1177 if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
1178 maxalt = VIK_TRACKPOINT(iter->data)->altitude;
1179 max_alt_tp = VIK_TRACKPOINT(iter->data);
1190 VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
1192 gdouble minalt = 25000.0;
1193 if ( !tr->trackpoints )
1196 GList *iter = tr->trackpoints;
1197 VikTrackpoint *min_alt_tp = NULL;
1200 if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
1201 minalt = VIK_TRACKPOINT(iter->data)->altitude;
1202 min_alt_tp = VIK_TRACKPOINT(iter->data);
1213 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
1217 if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1218 GList *iter = tr->trackpoints->next;
1222 tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
1223 if ( tmp_alt > *max_alt )
1225 if ( tmp_alt < *min_alt )
1234 void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
1237 GByteArray *b = g_byte_array_new();
1241 g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
1243 /* we'll fill out number of trackpoints later */
1245 g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1247 tps = tr->trackpoints;
1250 g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
1254 *(guint *)(b->data + intp) = ntp;
1256 // This allocates space for variant sized strings
1257 // and copies that amount of data from the track to byte array
1258 #define vtm_append(s) \
1259 len = (s) ? strlen(s)+1 : 0; \
1260 g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \
1261 if (s) g_byte_array_append(b, (guint8 *)s, len);
1263 vtm_append(tr->name);
1264 vtm_append(tr->comment);
1265 vtm_append(tr->description);
1269 g_byte_array_free(b, FALSE);
1273 * Take a byte array and convert it into a Track
1275 VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
1278 VikTrack *new_tr = vik_track_new();
1279 VikTrackpoint *new_tp;
1283 /* basic properties: */
1284 new_tr->visible = ((VikTrack *)data)->visible;
1285 new_tr->is_route = ((VikTrack *)data)->is_route;
1286 new_tr->has_color = ((VikTrack *)data)->has_color;
1287 new_tr->color = ((VikTrack *)data)->color;
1288 new_tr->bbox = ((VikTrack *)data)->bbox;
1290 data += sizeof(*new_tr);
1292 ntp = *(guint *)data;
1293 data += sizeof(ntp);
1295 for (i=0; i<ntp; i++) {
1296 new_tp = vik_trackpoint_new();
1297 memcpy(new_tp, data, sizeof(*new_tp));
1298 data += sizeof(*new_tp);
1299 new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
1302 #define vtu_get(s) \
1303 len = *(guint *)data; \
1304 data += sizeof(len); \
1306 (s) = g_strdup((gchar *)data); \
1312 vtu_get(new_tr->name);
1313 vtu_get(new_tr->comment);
1314 vtu_get(new_tr->description);
1320 * (Re)Calculate the bounds of the given track,
1321 * updating the track's bounds data.
1322 * This should be called whenever a track's trackpoints are changed
1324 void vik_track_calculate_bounds ( VikTrack *trk )
1327 tp_iter = trk->trackpoints;
1329 struct LatLon topleft, bottomright, ll;
1331 // Set bounds to first point
1333 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &topleft );
1334 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &bottomright );
1338 // See if this trackpoint increases the track bounds.
1340 vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &ll );
1342 if ( ll.lat > topleft.lat) topleft.lat = ll.lat;
1343 if ( ll.lon < topleft.lon) topleft.lon = ll.lon;
1344 if ( ll.lat < bottomright.lat) bottomright.lat = ll.lat;
1345 if ( ll.lon > bottomright.lon) bottomright.lon = ll.lon;
1347 tp_iter = tp_iter->next;
1350 g_debug ( g_strdup_printf("Bounds of track: '%s' is: %f,%f to: %f,%f", trk->name, topleft.lat, topleft.lon, bottomright.lat, bottomright.lon ) );
1352 trk->bbox.north = topleft.lat;
1353 trk->bbox.east = bottomright.lon;
1354 trk->bbox.south = bottomright.lat;
1355 trk->bbox.west = topleft.lon;
1361 void vik_track_apply_dem_data ( VikTrack *tr )
1365 tp_iter = tr->trackpoints;
1367 /* TODO: of the 4 possible choices we have for choosing an elevation
1368 * (trackpoint in between samples), choose the one with the least elevation change
1370 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST );
1371 if ( elev != VIK_DEM_INVALID_ELEVATION )
1372 VIK_TRACKPOINT(tp_iter->data)->altitude = elev;
1373 tp_iter = tp_iter->next;
1378 * vik_track_apply_dem_data_last_trackpoint:
1379 * Apply DEM data (if available) - to only the last trackpoint
1381 void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr )
1384 if ( tr->trackpoints ) {
1385 /* As in vik_track_apply_dem_data above - use 'best' interpolation method */
1386 elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->coord), VIK_DEM_INTERPOL_BEST );
1387 if ( elev != VIK_DEM_INVALID_ELEVATION )
1388 VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->altitude = elev;
1393 * vik_track_steal_and_append_trackpoints:
1395 * appends t2 to t1, leaving t2 with no trackpoints
1397 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
1399 if ( t1->trackpoints ) {
1400 t1->trackpoints = g_list_concat ( t1->trackpoints, t2->trackpoints );
1402 t1->trackpoints = t2->trackpoints;
1403 t2->trackpoints = NULL;
1405 // Trackpoints updated - so update the bounds
1406 vik_track_calculate_bounds ( t1 );
1410 * vik_track_cut_back_to_double_point:
1412 * starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
1413 * If there is no double point, deletes all the trackpoints.
1415 * Returns: the new end of the track (or the start if there are no double points)
1417 VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr )
1419 GList *iter = tr->trackpoints;
1424 while ( iter->next )
1428 while ( iter->prev ) {
1429 if ( vik_coord_equals((VikCoord *)iter->data, (VikCoord *)iter->prev->data) ) {
1430 GList *prev = iter->prev;
1432 rv = g_malloc(sizeof(VikCoord));
1433 *rv = *((VikCoord *) iter->data);
1435 /* truncate trackpoint list */
1436 iter->prev = NULL; /* pretend it's the end */
1437 g_list_foreach ( iter, (GFunc) g_free, NULL );
1438 g_list_free( iter );
1447 /* no double point found! */
1448 rv = g_malloc(sizeof(VikCoord));
1449 *rv = *((VikCoord *) tr->trackpoints->data);
1450 g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
1451 g_list_free( tr->trackpoints );
1452 tr->trackpoints = NULL;