X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/ce1c0489dcc4a2aeeab587b05ac872395107e4fd..3c29a5665c45b2f7c999341e6e0e583715114381:/src/viktrack.c diff --git a/src/viktrack.c b/src/viktrack.c index ae3110c5..f0c3c6c8 100644 --- a/src/viktrack.c +++ b/src/viktrack.c @@ -2,6 +2,7 @@ * viking -- GPS Data and Topo Analyzer, Explorer, and Manager * * Copyright (C) 2003-2005, Evan Battaglia + * Copyright (c) 2012, Rob Norris * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -75,6 +76,17 @@ void vik_track_set_comment(VikTrack *tr, const gchar *comment) tr->comment = NULL; } +void vik_track_set_description(VikTrack *tr, const gchar *description) +{ + if ( tr->description ) + g_free ( tr->description ); + + if ( description && description[0] != '\0' ) + tr->description = g_strdup(description); + else + tr->description = NULL; +} + void vik_track_ref(VikTrack *tr) { tr->ref_count++; @@ -100,6 +112,8 @@ void vik_track_free(VikTrack *tr) g_free ( tr->name ); if ( tr->comment ) g_free ( tr->comment ); + if ( tr->description ) + g_free ( tr->description ); g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL ); g_list_free( tr->trackpoints ); if (tr->property_dialog) @@ -108,22 +122,39 @@ void vik_track_free(VikTrack *tr) g_free ( tr ); } -VikTrack *vik_track_copy ( const VikTrack *tr ) +/** + * vik_track_copy: + * @tr: The Track to copy + * @copy_points: Whether to copy the track points or not + * + * Normally for copying the track it's best to copy all the trackpoints + * However for some operations such as splitting tracks the trackpoints will be managed separately, so no need to copy them. + * + * Returns: the copied VikTrack + */ +VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points ) { VikTrack *new_tr = vik_track_new(); VikTrackpoint *new_tp; GList *tp_iter = tr->trackpoints; new_tr->visible = tr->visible; + new_tr->is_route = tr->is_route; + new_tr->has_color = tr->has_color; + new_tr->color = tr->color; new_tr->trackpoints = NULL; - while ( tp_iter ) + if ( copy_points ) { - new_tp = g_malloc ( sizeof ( VikTrackpoint ) ); - *new_tp = *((VikTrackpoint *)(tp_iter->data)); - new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp ); - tp_iter = tp_iter->next; + while ( tp_iter ) + { + new_tp = g_malloc ( sizeof ( VikTrackpoint ) ); + *new_tp = *((VikTrackpoint *)(tp_iter->data)); + new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp ); + tp_iter = tp_iter->next; + } } vik_track_set_name(new_tr,tr->name); vik_track_set_comment(new_tr,tr->comment); + vik_track_set_description(new_tr,tr->description); return new_tr; } @@ -287,6 +318,31 @@ gulong vik_track_remove_same_time_points ( VikTrack *tr ) return num; } +/* + * Deletes all 'extra' trackpoint information + * such as time stamps, speed, course etc... + */ +void vik_track_to_routepoints ( VikTrack *tr ) +{ + GList *iter = tr->trackpoints; + while ( iter ) { + + // c.f. with vik_trackpoint_new() + + VIK_TRACKPOINT(iter->data)->has_timestamp = FALSE; + VIK_TRACKPOINT(iter->data)->timestamp = 0; + VIK_TRACKPOINT(iter->data)->speed = NAN; + VIK_TRACKPOINT(iter->data)->course = NAN; + VIK_TRACKPOINT(iter->data)->hdop = VIK_DEFAULT_DOP; + VIK_TRACKPOINT(iter->data)->vdop = VIK_DEFAULT_DOP; + VIK_TRACKPOINT(iter->data)->pdop = VIK_DEFAULT_DOP; + VIK_TRACKPOINT(iter->data)->nsats = 0; + VIK_TRACKPOINT(iter->data)->fix_mode = VIK_GPS_MODE_NOT_SEEN; + + iter = iter->next; + } +} + guint vik_track_get_segment_count(const VikTrack *tr) { guint num = 1; @@ -316,7 +372,7 @@ VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len) } rv = g_malloc ( segs * sizeof(VikTrack *) ); - tr = vik_track_copy ( t ); + tr = vik_track_copy ( t, TRUE ); rv[0] = tr; iter = tr->trackpoints; @@ -327,13 +383,7 @@ VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len) { iter->prev->next = NULL; iter->prev = NULL; - rv[i] = vik_track_new(); - // TODO: consider new naming strategy here - if ( tr->name ) - vik_track_set_name ( rv[i], tr->name ); - if ( tr->comment ) - vik_track_set_comment ( rv[i], tr->comment ); - rv[i]->visible = tr->visible; + rv[i] = vik_track_copy ( tr, FALSE ); rv[i]->trackpoints = iter; i++; } @@ -499,7 +549,12 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) gboolean okay = FALSE; while ( iter ) { - if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) { + // Sometimes a GPS device (or indeed any random file) can have stupid numbers for elevations + // Since when is 9.9999e+24 a valid elevation!! + // 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) + // Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception) + if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE && + VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) { okay = TRUE; break; } iter = iter->next; @@ -701,7 +756,7 @@ gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks ) iter = tr->trackpoints->next; numpts = 0; s[0] = 0; - t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp; + t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; numpts++; while (iter) { s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) ); @@ -775,7 +830,7 @@ gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks ) iter = tr->trackpoints->next; numpts = 0; s[0] = 0; - t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp; + t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; numpts++; while (iter) { s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) ); @@ -945,7 +1000,7 @@ gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks iter = tr->trackpoints->next; numpts = 0; s[0] = 0; - t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp; + t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; numpts++; while (iter) { s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) ); @@ -1187,19 +1242,25 @@ void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen) } *(guint *)(b->data + intp) = ntp; - len = (tr->name) ? strlen(tr->name)+1 : 0; - g_byte_array_append(b, (guint8 *)&len, sizeof(len)); - if (tr->name) g_byte_array_append(b, (guint8 *)tr->name, len); + // This allocates space for variant sized strings + // and copies that amount of data from the track to byte array +#define vtm_append(s) \ + len = (s) ? strlen(s)+1 : 0; \ + g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \ + if (s) g_byte_array_append(b, (guint8 *)s, len); - len = (tr->comment) ? strlen(tr->comment)+1 : 0; - g_byte_array_append(b, (guint8 *)&len, sizeof(len)); - if (tr->comment) g_byte_array_append(b, (guint8 *)tr->comment, len); + vtm_append(tr->name); + vtm_append(tr->comment); + vtm_append(tr->description); *data = b->data; *datalen = b->len; g_byte_array_free(b, FALSE); } +/* + * Take a byte array and convert it into a Track + */ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) { guint len; @@ -1208,8 +1269,12 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) guint ntp; gint i; - /* only the visibility is needed */ + /* basic properties: */ new_tr->visible = ((VikTrack *)data)->visible; + new_tr->is_route = ((VikTrack *)data)->is_route; + new_tr->has_color = ((VikTrack *)data)->has_color; + new_tr->color = ((VikTrack *)data)->color; + data += sizeof(*new_tr); ntp = *(guint *)data; @@ -1222,18 +1287,20 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp); } - len = *(guint *)data; - data += sizeof(len); - if (len) { - new_tr->name = g_strdup((gchar *)data); - } +#define vtu_get(s) \ + len = *(guint *)data; \ + data += sizeof(len); \ + if (len) { \ + (s) = g_strdup((gchar *)data); \ + } else { \ + (s) = NULL; \ + } \ data += len; - len = *(guint *)data; - data += sizeof(len); - if (len) { - new_tr->comment = g_strdup((gchar *)data); - } + vtu_get(new_tr->name); + vtu_get(new_tr->comment); + vtu_get(new_tr->description); + return new_tr; } @@ -1253,7 +1320,9 @@ void vik_track_apply_dem_data ( VikTrack *tr ) } } -/* +/** + * vik_track_apply_dem_data_last_trackpoint: + * * Apply DEM data (if available) - to only the last trackpoint */ void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr ) @@ -1267,7 +1336,11 @@ void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr ) } } -/* appends t2 to t1, leaving t2 with no trackpoints */ +/** + * vik_track_steal_and_append_trackpoints: + * + * appends t2 to t1, leaving t2 with no trackpoints + */ void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 ) { if ( t1->trackpoints ) { @@ -1281,9 +1354,13 @@ void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 ) t2->trackpoints = NULL; } -/* starting at the end, looks backwards for the last "double point", a duplicate trackpoint. +/** + * vik_track_cut_back_to_double_point: + * + * starting at the end, looks backwards for the last "double point", a duplicate trackpoint. * If there is no double point, deletes all the trackpoints. - * Returns the new end of the track (or the start if there are no double points) + * + * Returns: the new end of the track (or the start if there are no double points) */ VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr ) {