]> git.street.me.uk Git - andy/viking.git/blob - src/viktrack.c
Merge branch 'TrackMergeSplitImprovements'
[andy/viking.git] / src / viktrack.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <glib.h>
26 #include <time.h>
27 #include <stdlib.h>
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #endif
31 #ifdef HAVE_MATH_H
32 #include <math.h>
33 #endif
34
35 #include "coords.h"
36 #include "vikcoord.h"
37 #include "viktrack.h"
38 #include "globals.h"
39 #include "dems.h"
40
41 VikTrack *vik_track_new()
42 {
43   VikTrack *tr = g_malloc0 ( sizeof ( VikTrack ) );
44   tr->ref_count = 1;
45   return tr;
46 }
47
48 void vik_track_set_comment_no_copy(VikTrack *tr, gchar *comment)
49 {
50   if ( tr->comment )
51     g_free ( tr->comment );
52   tr->comment = comment;
53 }
54
55
56 void vik_track_set_name(VikTrack *tr, const gchar *name)
57 {
58   if ( tr->name )
59     g_free ( tr->name );
60
61   if ( name && name[0] != '\0' )
62     tr->name = g_strdup(name);
63   else
64     tr->name = NULL;
65 }
66
67 void vik_track_set_comment(VikTrack *tr, const gchar *comment)
68 {
69   if ( tr->comment )
70     g_free ( tr->comment );
71
72   if ( comment && comment[0] != '\0' )
73     tr->comment = g_strdup(comment);
74   else
75     tr->comment = NULL;
76 }
77
78 void vik_track_ref(VikTrack *tr)
79 {
80   tr->ref_count++;
81 }
82
83 void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog)
84 {
85   /* Warning: does not check for existing dialog */
86   tr->property_dialog = dialog;
87 }
88
89 void vik_track_clear_property_dialog(VikTrack *tr)
90 {
91   tr->property_dialog = NULL;
92 }
93
94 void vik_track_free(VikTrack *tr)
95 {
96   if ( tr->ref_count-- > 1 )
97     return;
98
99   if ( tr->name )
100     g_free ( tr->name );
101   if ( tr->comment )
102     g_free ( tr->comment );
103   g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
104   g_list_free( tr->trackpoints );
105   if (tr->property_dialog)
106     if ( GTK_IS_WIDGET(tr->property_dialog) )
107       gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) );
108   g_free ( tr );
109 }
110
111 VikTrack *vik_track_copy ( const VikTrack *tr )
112 {
113   VikTrack *new_tr = vik_track_new();
114   VikTrackpoint *new_tp;
115   GList *tp_iter = tr->trackpoints;
116   new_tr->visible = tr->visible;
117   new_tr->trackpoints = NULL;
118   while ( tp_iter )
119   {
120     new_tp = g_malloc ( sizeof ( VikTrackpoint ) );
121     *new_tp = *((VikTrackpoint *)(tp_iter->data));
122     new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp );
123     tp_iter = tp_iter->next;
124   }
125   vik_track_set_name(new_tr,tr->name);
126   vik_track_set_comment(new_tr,tr->comment);
127   return new_tr;
128 }
129
130 VikTrackpoint *vik_trackpoint_new()
131 {
132   VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint));
133   tp->speed = NAN;
134   tp->course = NAN;
135   tp->altitude = VIK_DEFAULT_ALTITUDE;
136   tp->hdop = VIK_DEFAULT_DOP;
137   tp->vdop = VIK_DEFAULT_DOP;
138   tp->pdop = VIK_DEFAULT_DOP;
139   return tp;
140 }
141
142 void vik_trackpoint_free(VikTrackpoint *tp)
143 {
144   g_free(tp);
145 }
146
147 VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
148 {
149   VikTrackpoint *rv = vik_trackpoint_new();
150   *rv = *tp;
151   return rv;
152 }
153
154 gdouble vik_track_get_length(const VikTrack *tr)
155 {
156   gdouble len = 0.0;
157   if ( tr->trackpoints )
158   {
159     GList *iter = tr->trackpoints->next;
160     while (iter)
161     {
162       if ( ! VIK_TRACKPOINT(iter->data)->newsegment )
163         len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
164                                 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
165       iter = iter->next;
166     }
167   }
168   return len;
169 }
170
171 gdouble vik_track_get_length_including_gaps(const VikTrack *tr)
172 {
173   gdouble len = 0.0;
174   if ( tr->trackpoints )
175   {
176     GList *iter = tr->trackpoints->next;
177     while (iter)
178     {
179       len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
180                               &(VIK_TRACKPOINT(iter->prev->data)->coord) );
181       iter = iter->next;
182     }
183   }
184   return len;
185 }
186
187 gulong vik_track_get_tp_count(const VikTrack *tr)
188 {
189   gulong num = 0;
190   GList *iter = tr->trackpoints;
191   while ( iter )
192   {
193     num++;
194     iter = iter->next;
195   }
196   return num;
197 }
198
199 gulong vik_track_get_dup_point_count ( const VikTrack *tr )
200 {
201   gulong num = 0;
202   GList *iter = tr->trackpoints;
203   while ( iter )
204   {
205     if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
206                        &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
207       num++;
208     iter = iter->next;
209   }
210   return num;
211 }
212
213 /*
214  * Deletes adjacent points that have the same position
215  * Returns the number of points that were deleted
216  */
217 gulong vik_track_remove_dup_points ( VikTrack *tr )
218 {
219   gulong num = 0;
220   GList *iter = tr->trackpoints;
221   while ( iter )
222   {
223     if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord),
224                        &(VIK_TRACKPOINT(iter->next->data)->coord) ) )
225     {
226       num++;
227       // Maintain track segments
228       if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
229         VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
230
231       vik_trackpoint_free ( iter->next->data );
232       tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
233     }
234     else
235       iter = iter->next;
236   }
237   return num;
238 }
239
240 /*
241  * Get a count of trackpoints with the same defined timestamp
242  * Note is using timestamps with a resolution with 1 second
243  */
244 gulong vik_track_get_same_time_point_count ( const VikTrack *tr )
245 {
246   gulong num = 0;
247   GList *iter = tr->trackpoints;
248   while ( iter ) {
249     if ( iter->next &&
250          ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
251            VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
252          ( VIK_TRACKPOINT(iter->data)->timestamp ==
253            VIK_TRACKPOINT(iter->next->data)->timestamp) )
254       num++;
255     iter = iter->next;
256   }
257   return num;
258 }
259
260 /*
261  * Deletes adjacent points that have the same defined timestamp
262  * Returns the number of points that were deleted
263  */
264 gulong vik_track_remove_same_time_points ( VikTrack *tr )
265 {
266   gulong num = 0;
267   GList *iter = tr->trackpoints;
268   while ( iter ) {
269     if ( iter->next &&
270          ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
271            VIK_TRACKPOINT(iter->next->data)->has_timestamp ) &&
272          ( VIK_TRACKPOINT(iter->data)->timestamp ==
273            VIK_TRACKPOINT(iter->next->data)->timestamp) ) {
274
275       num++;
276       
277       // Maintain track segments
278       if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next )
279         VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE;
280
281       vik_trackpoint_free ( iter->next->data );
282       tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next );
283     }
284     else
285       iter = iter->next;
286   }
287   return num;
288 }
289
290 guint vik_track_get_segment_count(const VikTrack *tr)
291 {
292   guint num = 1;
293   GList *iter = tr->trackpoints;
294   if ( !iter )
295     return 0;
296   while ( (iter = iter->next) )
297   {
298     if ( VIK_TRACKPOINT(iter->data)->newsegment )
299       num++;
300   }
301   return num;
302 }
303
304 VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
305 {
306   VikTrack **rv;
307   VikTrack *tr;
308   guint i;
309   guint segs = vik_track_get_segment_count(t);
310   GList *iter;
311
312   if ( segs < 2 )
313   {
314     *ret_len = 0;
315     return NULL;
316   }
317
318   rv = g_malloc ( segs * sizeof(VikTrack *) );
319   tr = vik_track_copy ( t );
320   rv[0] = tr;
321   iter = tr->trackpoints;
322
323   i = 1;
324   while ( (iter = iter->next) )
325   {
326     if ( VIK_TRACKPOINT(iter->data)->newsegment )
327     {
328       iter->prev->next = NULL;
329       iter->prev = NULL;
330       rv[i] = vik_track_new();
331       // TODO: consider new naming strategy here
332       if ( tr->name )
333         vik_track_set_name ( rv[i], tr->name );
334       if ( tr->comment )
335         vik_track_set_comment ( rv[i], tr->comment );
336       rv[i]->visible = tr->visible;
337       rv[i]->trackpoints = iter;
338       i++;
339     }
340   }
341   *ret_len = segs;
342   return rv;
343 }
344
345 /*
346  * Simply remove any subsequent segment markers in a track to form one continuous track
347  * Return the number of segments merged
348  */
349 guint vik_track_merge_segments(VikTrack *tr)
350 {
351   guint num = 0;
352   GList *iter = tr->trackpoints;
353   if ( !iter )
354     return num;
355
356   // Always skip the first point as this should be the first segment
357   iter = iter->next;
358
359   while ( (iter = iter->next) )
360   {
361     if ( VIK_TRACKPOINT(iter->data)->newsegment ) {
362       VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
363       num++;
364     }
365   }
366   return num;
367 }
368
369 void vik_track_reverse ( VikTrack *tr )
370 {
371   GList *iter;
372   tr->trackpoints = g_list_reverse(tr->trackpoints);
373
374   /* fix 'newsegment' */
375   iter = g_list_last ( tr->trackpoints );
376   while ( iter )
377   {
378     if ( ! iter->next ) /* last segment, was first, cancel newsegment. */
379       VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
380     if ( ! iter->prev ) /* first segment by convention has newsegment flag. */
381       VIK_TRACKPOINT(iter->data)->newsegment = TRUE;
382     else if ( VIK_TRACKPOINT(iter->data)->newsegment && iter->next )
383     {
384       VIK_TRACKPOINT(iter->next->data)->newsegment = TRUE;
385       VIK_TRACKPOINT(iter->data)->newsegment = FALSE;
386     }
387     iter = iter->prev;
388   }
389 }
390
391 gdouble vik_track_get_average_speed(const VikTrack *tr)
392 {
393   gdouble len = 0.0;
394   guint32 time = 0;
395   if ( tr->trackpoints )
396   {
397     GList *iter = tr->trackpoints->next;
398     while (iter)
399     {
400       if ( VIK_TRACKPOINT(iter->data)->has_timestamp && 
401           VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
402           (! VIK_TRACKPOINT(iter->data)->newsegment) )
403       {
404         len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
405                                 &(VIK_TRACKPOINT(iter->prev->data)->coord) );
406         time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
407       }
408       iter = iter->next;
409     }
410   }
411   return (time == 0) ? 0 : ABS(len/time);
412 }
413
414 /**
415  * Based on a simple average speed, but with a twist - to give a moving average.
416  *  . GPSs often report a moving average in their statistics output
417  *  . bicycle speedos often don't factor in time when stopped - hence reporting a moving average for speed
418  *
419  * Often GPS track will record every second but not when stationary
420  * This method doesn't use samples that differ over the specified time limit - effectively skipping that time chunk from the total time
421  *
422  * Suggest to use 60 seconds as the stop length (as the default used in the TrackWaypoint draw stops factor)
423  */
424 gdouble vik_track_get_average_speed_moving (const VikTrack *tr, int stop_length_seconds)
425 {
426   gdouble len = 0.0;
427   guint32 time = 0;
428   if ( tr->trackpoints )
429   {
430     GList *iter = tr->trackpoints->next;
431     while (iter)
432     {
433       if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
434           VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
435           (! VIK_TRACKPOINT(iter->data)->newsegment) )
436       {
437         if ( ( VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp ) < stop_length_seconds ) {
438           len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
439                                   &(VIK_TRACKPOINT(iter->prev->data)->coord) );
440         
441           time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
442         }
443       }
444       iter = iter->next;
445     }
446   }
447   return (time == 0) ? 0 : ABS(len/time);
448 }
449
450 gdouble vik_track_get_max_speed(const VikTrack *tr)
451 {
452   gdouble maxspeed = 0.0, speed = 0.0;
453   if ( tr->trackpoints )
454   {
455     GList *iter = tr->trackpoints->next;
456     while (iter)
457     {
458       if ( VIK_TRACKPOINT(iter->data)->has_timestamp && 
459           VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
460           (! VIK_TRACKPOINT(iter->data)->newsegment) )
461       {
462         speed =  vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
463                  / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
464         if ( speed > maxspeed )
465           maxspeed = speed;
466       }
467       iter = iter->next;
468     }
469   }
470   return maxspeed;
471 }
472
473 void vik_track_convert ( VikTrack *tr, VikCoordMode dest_mode )
474 {
475   GList *iter = tr->trackpoints;
476   while (iter)
477   {
478     vik_coord_convert ( &(VIK_TRACKPOINT(iter->data)->coord), dest_mode );
479     iter = iter->next;
480   }
481 }
482
483 /* I understood this when I wrote it ... maybe ... Basically it eats up the
484  * proper amounts of length on the track and averages elevation over that. */
485 gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks )
486 {
487   gdouble *pts;
488   gdouble total_length, chunk_length, current_dist, current_area_under_curve, current_seg_length, dist_along_seg = 0.0;
489   gdouble altitude1, altitude2;
490   guint16 current_chunk;
491   gboolean ignore_it = FALSE;
492
493   GList *iter = tr->trackpoints;
494
495   if (!iter || !iter->next) /* zero- or one-point track */
496           return NULL;
497
498   { /* test if there's anything worth calculating */
499     gboolean okay = FALSE;
500     while ( iter )
501     {
502       // Sometimes a GPS device (or indeed any random file) can have stupid numbers for elevations
503       // Since when is 9.9999e+24 a valid elevation!!
504       // 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)
505       // Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception)
506       if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE &&
507            VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) {
508         okay = TRUE; break;
509       }
510       iter = iter->next;
511     }
512     if ( ! okay )
513       return NULL;
514   }
515
516   iter = tr->trackpoints;
517
518   g_assert ( num_chunks < 16000 );
519
520   pts = g_malloc ( sizeof(gdouble) * num_chunks );
521
522   total_length = vik_track_get_length_including_gaps ( tr );
523   chunk_length = total_length / num_chunks;
524
525   /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
526   if (chunk_length <= 0) {
527     g_free(pts);
528     return NULL;
529   }
530
531   current_dist = 0.0;
532   current_area_under_curve = 0;
533   current_chunk = 0;
534   current_seg_length = 0;
535
536   current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
537       &(VIK_TRACKPOINT(iter->next->data)->coord) );
538   altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
539   altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
540   dist_along_seg = 0;
541
542   while ( current_chunk < num_chunks ) {
543
544     /* go along current seg */
545     if ( current_seg_length && (current_seg_length - dist_along_seg) > chunk_length ) {
546       dist_along_seg += chunk_length;
547
548       /*        /
549        *   pt2 *
550        *      /x       altitude = alt_at_pt_1 + alt_at_pt_2 / 2 = altitude1 + slope * dist_value_of_pt_inbetween_pt1_and_pt2
551        *     /xx   avg altitude = area under curve / chunk len
552        *pt1 *xxx   avg altitude = altitude1 + (altitude2-altitude1)/(current_seg_length)*(dist_along_seg + (chunk_len/2))
553        *   / xxx
554        *  /  xxx
555        **/
556
557       if ( ignore_it )
558         // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero)
559         pts[current_chunk] = altitude1;
560       else
561         pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length);
562
563       current_chunk++;
564     } else {
565       /* finish current seg */
566       if ( current_seg_length ) {
567         gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg;
568         current_dist = current_seg_length - dist_along_seg;
569         current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5;
570       } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */
571
572       /* get intervening segs */
573       iter = iter->next;
574       while ( iter && iter->next ) {
575         current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
576             &(VIK_TRACKPOINT(iter->next->data)->coord) );
577         altitude1 = VIK_TRACKPOINT(iter->data)->altitude;
578         altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude;
579         ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment;
580
581         if ( chunk_length - current_dist >= current_seg_length ) {
582           current_dist += current_seg_length;
583           current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5;
584           iter = iter->next;
585         } else {
586           break;
587         }
588       }
589
590       /* final seg */
591       dist_along_seg = chunk_length - current_dist;
592       if ( ignore_it || ( iter && !iter->next ) ) {
593         pts[current_chunk] = current_area_under_curve / current_dist;
594         if (!iter->next) {
595           int i;
596           for (i = current_chunk + 1; i < num_chunks; i++)
597             pts[i] = pts[current_chunk];
598           break;
599         }
600       } 
601       else {
602         current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length);
603         pts[current_chunk] = current_area_under_curve / chunk_length;
604       }
605
606       current_dist = 0;
607       current_chunk++;
608     }
609   }
610
611   return pts;
612 }
613
614
615 void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down)
616 {
617   gdouble diff;
618   *up = *down = 0;
619   if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE )
620   {
621     GList *iter = tr->trackpoints->next;
622     while (iter)
623     {
624       diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude;
625       if ( diff > 0 )
626         *up += diff;
627       else
628         *down -= diff;
629       iter = iter->next;
630     }
631   } else
632     *up = *down = VIK_DEFAULT_ALTITUDE;
633 }
634
635 gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks )
636 {
637   gdouble *pts;
638   gdouble *altitudes;
639   gdouble total_length, chunk_length, current_gradient;
640   gdouble altitude1, altitude2;
641   guint16 current_chunk;
642
643   g_assert ( num_chunks < 16000 );
644
645   total_length = vik_track_get_length_including_gaps ( tr );
646   chunk_length = total_length / num_chunks;
647
648   /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */
649   if (chunk_length <= 0) {
650     return NULL;
651   }
652
653   altitudes = vik_track_make_elevation_map (tr, num_chunks);
654   if (altitudes == NULL) {
655     return NULL;
656   }
657
658   current_gradient = 0.0;
659   pts = g_malloc ( sizeof(gdouble) * num_chunks );
660   for (current_chunk = 0; current_chunk < (num_chunks - 1); current_chunk++) {
661     altitude1 = altitudes[current_chunk];
662     altitude2 = altitudes[current_chunk + 1];
663     current_gradient = 100.0 * (altitude2 - altitude1) / chunk_length;
664
665     pts[current_chunk] = current_gradient;
666   }
667
668   pts[current_chunk] = current_gradient;
669
670   return pts;
671 }
672
673 /* by Alex Foobarian */
674 gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
675 {
676   gdouble *v, *s, *t;
677   gdouble duration, chunk_dur;
678   time_t t1, t2;
679   int i, pt_count, numpts, index;
680   GList *iter;
681
682   if ( ! tr->trackpoints )
683     return NULL;
684
685   g_assert ( num_chunks < 16000 );
686
687   t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
688   t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
689   duration = t2 - t1;
690
691   if ( !t1 || !t2 || !duration )
692     return NULL;
693
694   if (duration < 0) {
695     g_warning("negative duration: unsorted trackpoint timestamps?");
696     return NULL;
697   }
698   pt_count = vik_track_get_tp_count(tr);
699
700   v = g_malloc ( sizeof(gdouble) * num_chunks );
701   chunk_dur = duration / num_chunks;
702
703   s = g_malloc(sizeof(double) * pt_count);
704   t = g_malloc(sizeof(double) * pt_count);
705
706   iter = tr->trackpoints->next;
707   numpts = 0;
708   s[0] = 0;
709   t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
710   numpts++;
711   while (iter) {
712     s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
713     t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
714     numpts++;
715     iter = iter->next;
716   }
717
718   /* In the following computation, we iterate through periods of time of duration chunk_dur.
719    * The first period begins at the beginning of the track.  The last period ends at the end of the track.
720    */
721   index = 0; /* index of the current trackpoint. */
722   for (i = 0; i < num_chunks; i++) {
723     /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
724      * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints.
725      */
726     if (t[0] + i*chunk_dur >= t[index]) {
727       gdouble acc_t = 0, acc_s = 0;
728       while (t[0] + i*chunk_dur >= t[index]) {
729         acc_s += (s[index+1]-s[index]);
730         acc_t += (t[index+1]-t[index]);
731         index++;
732       }
733       v[i] = acc_s/acc_t;
734     } 
735     else if (i) {
736       v[i] = v[i-1];
737     }
738     else {
739       v[i] = 0;
740     }
741   }
742   g_free(s);
743   g_free(t);
744   return v;
745 }
746
747 /**
748  * Make a distance/time map, heavily based on the vik_track_make_speed_map method
749  */
750 gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks )
751 {
752   gdouble *v, *s, *t;
753   gdouble duration, chunk_dur;
754   time_t t1, t2;
755   int i, pt_count, numpts, index;
756   GList *iter;
757
758   if ( ! tr->trackpoints )
759     return NULL;
760
761   t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
762   t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
763   duration = t2 - t1;
764
765   if ( !t1 || !t2 || !duration )
766     return NULL;
767
768   if (duration < 0) {
769     g_warning("negative duration: unsorted trackpoint timestamps?");
770     return NULL;
771   }
772   pt_count = vik_track_get_tp_count(tr);
773
774   v = g_malloc ( sizeof(gdouble) * num_chunks );
775   chunk_dur = duration / num_chunks;
776
777   s = g_malloc(sizeof(double) * pt_count);
778   t = g_malloc(sizeof(double) * pt_count);
779
780   iter = tr->trackpoints->next;
781   numpts = 0;
782   s[0] = 0;
783   t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
784   numpts++;
785   while (iter) {
786     s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
787     t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
788     numpts++;
789     iter = iter->next;
790   }
791
792   /* In the following computation, we iterate through periods of time of duration chunk_dur.
793    * The first period begins at the beginning of the track.  The last period ends at the end of the track.
794    */
795   index = 0; /* index of the current trackpoint. */
796   for (i = 0; i < num_chunks; i++) {
797     /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
798      * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints.
799      */
800     if (t[0] + i*chunk_dur >= t[index]) {
801       gdouble acc_s = 0; // No need for acc_t
802       while (t[0] + i*chunk_dur >= t[index]) {
803         acc_s += (s[index+1]-s[index]);
804         index++;
805       }
806       // The only bit that's really different from the speed map - just keep an accululative record distance
807       v[i] = i ? v[i-1]+acc_s : acc_s;
808     }
809     else if (i) {
810       v[i] = v[i-1];
811     }
812     else {
813       v[i] = 0;
814     }
815   }
816   g_free(s);
817   g_free(t);
818   return v;
819 }
820
821 /**
822  * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance)
823  * This results in a slightly blocky graph when it does not have many trackpoints: <60
824  * NB Somehow the elevation/distance applies some kind of smoothing algorithm,
825  *   but I don't think any one understands it any more (I certainly don't ATM)
826  */
827 gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks )
828 {
829   time_t t1, t2;
830   gdouble duration, chunk_dur;
831   GList *iter = tr->trackpoints;
832
833   if (!iter || !iter->next) /* zero- or one-point track */
834     return NULL;
835
836   /* test if there's anything worth calculating */
837   gboolean okay = FALSE;
838   while ( iter ) {
839     if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) {
840       okay = TRUE;
841       break;
842     }
843     iter = iter->next;
844   }
845   if ( ! okay )
846     return NULL;
847
848   t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
849   t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
850   duration = t2 - t1;
851
852   if ( !t1 || !t2 || !duration )
853     return NULL;
854
855   if (duration < 0) {
856     g_warning("negative duration: unsorted trackpoint timestamps?");
857     return NULL;
858   }
859   gint pt_count = vik_track_get_tp_count(tr);
860
861   // Reset iterator back to the beginning
862   iter = tr->trackpoints;
863
864   gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values
865   gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes
866   gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times
867
868   chunk_dur = duration / num_chunks;
869
870   s[0] = VIK_TRACKPOINT(iter->data)->altitude;
871   t[0] = VIK_TRACKPOINT(iter->data)->timestamp;
872   iter = tr->trackpoints->next;
873   gint numpts = 1;
874   while (iter) {
875     s[numpts] = VIK_TRACKPOINT(iter->data)->altitude;
876     t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
877     numpts++;
878     iter = iter->next;
879   }
880
881  /* In the following computation, we iterate through periods of time of duration chunk_dur.
882    * The first period begins at the beginning of the track.  The last period ends at the end of the track.
883    */
884   gint index = 0; /* index of the current trackpoint. */
885   gint i;
886   for (i = 0; i < num_chunks; i++) {
887     /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur.
888      * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints.
889      */
890     if (t[0] + i*chunk_dur >= t[index]) {
891       gdouble acc_s = s[index]; // initialise to first point
892       while (t[0] + i*chunk_dur >= t[index]) {
893         acc_s += (s[index+1]-s[index]);
894         index++;
895       }
896       pts[i] = acc_s;
897     }
898     else if (i) {
899       pts[i] = pts[i-1];
900     }
901     else {
902       pts[i] = 0;
903     }
904   }
905   g_free(s);
906   g_free(t);
907
908   return pts;
909 }
910
911 /**
912  * Make a speed/distance map
913  */
914 gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks )
915 {
916   gdouble *v, *s, *t;
917   time_t t1, t2;
918   gint i, pt_count, numpts, index;
919   GList *iter;
920   gdouble duration, total_length, chunk_length;
921
922   if ( ! tr->trackpoints )
923     return NULL;
924
925   t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
926   t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
927   duration = t2 - t1;
928
929   if ( !t1 || !t2 || !duration )
930     return NULL;
931
932   if (duration < 0) {
933     g_warning("negative duration: unsorted trackpoint timestamps?");
934     return NULL;
935   }
936
937   total_length = vik_track_get_length_including_gaps ( tr );
938   chunk_length = total_length / num_chunks;
939   pt_count = vik_track_get_tp_count(tr);
940
941   if (chunk_length <= 0) {
942     return NULL;
943   }
944
945   v = g_malloc ( sizeof(gdouble) * num_chunks );
946   s = g_malloc ( sizeof(double) * pt_count );
947   t = g_malloc ( sizeof(double) * pt_count );
948
949   // No special handling of segments ATM...
950   iter = tr->trackpoints->next;
951   numpts = 0;
952   s[0] = 0;
953   t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
954   numpts++;
955   while (iter) {
956     s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
957     t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
958     numpts++;
959     iter = iter->next;
960   }
961
962   // Iterate through a portion of the track to get an average speed for that part
963   // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps'
964   index = 0; /* index of the current trackpoint. */
965   for (i = 0; i < num_chunks; i++) {
966     // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk
967     if (s[0] + i*chunk_length >= s[index]) {
968       gdouble acc_t = 0, acc_s = 0;
969       while (s[0] + i*chunk_length >= s[index]) {
970         acc_s += (s[index+1]-s[index]);
971         acc_t += (t[index+1]-t[index]);
972         index++;
973       }
974       v[i] = acc_s/acc_t;
975     }
976     else if (i) {
977       v[i] = v[i-1];
978     }
979     else {
980       v[i] = 0;
981     }
982   }
983   g_free(s);
984   g_free(t);
985   return v;
986 }
987
988 /* by Alex Foobarian */
989 VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start )
990 {
991   gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
992   gdouble current_dist = 0.0;
993   gdouble current_inc = 0.0;
994   if ( tr->trackpoints )
995   {
996     GList *iter = tr->trackpoints->next;
997     GList *last_iter = NULL;
998     gdouble last_dist = 0.0;
999     while (iter)
1000     {
1001       current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
1002                                      &(VIK_TRACKPOINT(iter->prev->data)->coord) );
1003       last_dist = current_dist;
1004       current_dist += current_inc;
1005       if ( current_dist >= dist )
1006         break;
1007       last_iter = iter;
1008       iter = iter->next;
1009     }
1010     if (!iter) { /* passing the end the track */
1011       if (last_iter) {
1012         if (meters_from_start)
1013           *meters_from_start = last_dist;
1014         return(VIK_TRACKPOINT(last_iter->data));
1015       }
1016       else
1017         return NULL;
1018     }
1019     /* we've gone past the dist already, was prev trackpoint closer? */
1020     /* should do a vik_coord_average_weighted() thingy. */
1021     if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) {
1022       if (meters_from_start)
1023         *meters_from_start = last_dist;
1024       iter = iter->prev;
1025     }
1026     else
1027       if (meters_from_start)
1028         *meters_from_start = current_dist;
1029
1030     return VIK_TRACKPOINT(iter->data);
1031
1032   }
1033   return NULL;
1034 }
1035
1036 VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
1037 {
1038   time_t t_pos, t_start, t_end, t_total;
1039   t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1040   t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
1041   t_total = t_end - t_start;
1042
1043   t_pos = t_start + t_total * reltime;
1044
1045   if ( !tr->trackpoints )
1046     return NULL;
1047
1048   GList *iter = tr->trackpoints;
1049
1050   while (iter) {
1051     if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
1052       break;
1053     if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
1054       if (iter->prev == NULL)  /* first trackpoint */
1055         break;
1056       time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp;
1057       time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
1058       if (t_before <= t_after)
1059         iter = iter->prev;
1060       break;
1061     }
1062     else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
1063       break;
1064     iter = iter->next;
1065   }
1066
1067   if (!iter)
1068     return NULL;
1069   if (seconds_from_start)
1070     *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
1071   return VIK_TRACKPOINT(iter->data);
1072 }
1073
1074 VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
1075 {
1076   gdouble maxspeed = 0.0, speed = 0.0;
1077
1078   if ( !tr->trackpoints )
1079     return NULL;
1080
1081   GList *iter = tr->trackpoints;
1082   VikTrackpoint *max_speed_tp = NULL;
1083
1084   while (iter) {
1085     if (iter->prev) {
1086       if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
1087            VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
1088            (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
1089         speed =  vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
1090           / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
1091         if ( speed > maxspeed ) {
1092           maxspeed = speed;
1093           max_speed_tp = VIK_TRACKPOINT(iter->data);
1094         }
1095       }
1096     }
1097     iter = iter->next;
1098   }
1099   
1100   if (!max_speed_tp)
1101     return NULL;
1102
1103   return max_speed_tp;
1104 }
1105
1106 VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
1107 {
1108   gdouble maxalt = -5000.0;
1109   if ( !tr->trackpoints )
1110     return NULL;
1111
1112   GList *iter = tr->trackpoints;
1113   VikTrackpoint *max_alt_tp = NULL;
1114
1115   while (iter) {
1116     if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
1117       maxalt = VIK_TRACKPOINT(iter->data)->altitude;
1118       max_alt_tp = VIK_TRACKPOINT(iter->data);
1119     }
1120     iter = iter->next;
1121   }
1122
1123   if (!max_alt_tp)
1124     return NULL;
1125
1126   return max_alt_tp;
1127 }
1128
1129 VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
1130 {
1131   gdouble minalt = 25000.0;
1132   if ( !tr->trackpoints )
1133     return NULL;
1134
1135   GList *iter = tr->trackpoints;
1136   VikTrackpoint *min_alt_tp = NULL;
1137
1138   while (iter) {
1139     if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
1140       minalt = VIK_TRACKPOINT(iter->data)->altitude;
1141       min_alt_tp = VIK_TRACKPOINT(iter->data);
1142     }
1143     iter = iter->next;
1144   }
1145
1146   if (!min_alt_tp)
1147     return NULL;
1148
1149   return min_alt_tp;
1150 }
1151
1152 gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
1153 {
1154   *min_alt = 25000;
1155   *max_alt = -5000;
1156   if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
1157     GList *iter = tr->trackpoints->next;
1158     gdouble tmp_alt;
1159     while (iter)
1160     {
1161       tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
1162       if ( tmp_alt > *max_alt )
1163         *max_alt = tmp_alt;
1164       if ( tmp_alt < *min_alt )
1165         *min_alt = tmp_alt;
1166       iter = iter->next;
1167     }
1168     return TRUE;
1169   }
1170   return FALSE;
1171 }
1172
1173 void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
1174 {
1175   GList *tps;
1176   GByteArray *b = g_byte_array_new();
1177   guint len;
1178   guint intp, ntp;
1179
1180   g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
1181
1182   /* we'll fill out number of trackpoints later */
1183   intp = b->len;
1184   g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1185
1186   tps = tr->trackpoints;
1187   ntp = 0;
1188   while (tps) {
1189     g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
1190     tps = tps->next;
1191     ntp++;
1192   }
1193   *(guint *)(b->data + intp) = ntp;
1194
1195   len = (tr->name) ? strlen(tr->name)+1 : 0;
1196   g_byte_array_append(b, (guint8 *)&len, sizeof(len));
1197   if (tr->name) g_byte_array_append(b, (guint8 *)tr->name, len);
1198
1199   len = (tr->comment) ? strlen(tr->comment)+1 : 0; 
1200   g_byte_array_append(b, (guint8 *)&len, sizeof(len)); 
1201   if (tr->comment) g_byte_array_append(b, (guint8 *)tr->comment, len);
1202
1203   *data = b->data;
1204   *datalen = b->len;
1205   g_byte_array_free(b, FALSE);
1206 }
1207
1208 VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
1209 {
1210   guint len;
1211   VikTrack *new_tr = vik_track_new();
1212   VikTrackpoint *new_tp;
1213   guint ntp;
1214   gint i;
1215
1216   /* only the visibility is needed */
1217   new_tr->visible = ((VikTrack *)data)->visible;
1218   data += sizeof(*new_tr);
1219
1220   ntp = *(guint *)data;
1221   data += sizeof(ntp);
1222
1223   for (i=0; i<ntp; i++) {
1224     new_tp = vik_trackpoint_new();
1225     memcpy(new_tp, data, sizeof(*new_tp));
1226     data += sizeof(*new_tp);
1227     new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
1228   }
1229
1230   len = *(guint *)data;
1231   data += sizeof(len);
1232   if (len) {
1233     new_tr->name = g_strdup((gchar *)data);
1234   }
1235   data += len;
1236
1237   len = *(guint *)data;
1238   data += sizeof(len);
1239   if (len) {
1240     new_tr->comment = g_strdup((gchar *)data);
1241   }
1242   return new_tr;
1243 }
1244
1245 void vik_track_apply_dem_data ( VikTrack *tr )
1246 {
1247   GList *tp_iter;
1248   gint16 elev;
1249   tp_iter = tr->trackpoints;
1250   while ( tp_iter ) {
1251     /* TODO: of the 4 possible choices we have for choosing an elevation
1252      * (trackpoint in between samples), choose the one with the least elevation change
1253      * as the last */
1254     elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST );
1255     if ( elev != VIK_DEM_INVALID_ELEVATION )
1256       VIK_TRACKPOINT(tp_iter->data)->altitude = elev;
1257     tp_iter = tp_iter->next;
1258   }
1259 }
1260
1261 /*
1262  * Apply DEM data (if available) - to only the last trackpoint
1263  */
1264 void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr )
1265 {
1266   gint16 elev;
1267   if ( tr->trackpoints ) {
1268     /* As in vik_track_apply_dem_data above - use 'best' interpolation method */
1269     elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->coord), VIK_DEM_INTERPOL_BEST );
1270     if ( elev != VIK_DEM_INVALID_ELEVATION )
1271       VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->altitude = elev;
1272   }
1273 }
1274
1275 /* appends t2 to t1, leaving t2 with no trackpoints */
1276 void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
1277 {
1278   if ( t1->trackpoints ) {
1279     GList *tpiter = t1->trackpoints;
1280     while ( tpiter->next )
1281       tpiter = tpiter->next;
1282     tpiter->next = t2->trackpoints;
1283     t2->trackpoints->prev = tpiter;
1284   } else
1285     t1->trackpoints = t2->trackpoints;
1286   t2->trackpoints = NULL;
1287 }
1288
1289 /* starting at the end, looks backwards for the last "double point", a duplicate trackpoint.
1290  * If there is no double point, deletes all the trackpoints.
1291  * Returns the new end of the track (or the start if there are no double points)
1292  */
1293 VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr )
1294 {
1295   GList *iter = tr->trackpoints;
1296   VikCoord *rv;
1297
1298   if ( !iter )
1299     return NULL;
1300   while ( iter->next )
1301     iter = iter->next;
1302
1303
1304   while ( iter->prev ) {
1305     if ( vik_coord_equals((VikCoord *)iter->data, (VikCoord *)iter->prev->data) ) {
1306       GList *prev = iter->prev;
1307
1308       rv = g_malloc(sizeof(VikCoord));
1309       *rv = *((VikCoord *) iter->data);
1310
1311       /* truncate trackpoint list */
1312       iter->prev = NULL; /* pretend it's the end */
1313       g_list_foreach ( iter, (GFunc) g_free, NULL );
1314       g_list_free( iter );
1315
1316       prev->next = NULL;
1317
1318       return rv;
1319     }
1320     iter = iter->prev;
1321   }
1322
1323   /* no double point found! */
1324   rv = g_malloc(sizeof(VikCoord));
1325   *rv = *((VikCoord *) tr->trackpoints->data);
1326   g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL );
1327   g_list_free( tr->trackpoints );
1328   tr->trackpoints = NULL;
1329   return rv;
1330 }
1331