X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/0b72c4358346b89e23ff20333808e7293e25111e..4f14a010d0a7d09721c32f0d35ffbeae05ea994c:/src/gpx.c diff --git a/src/gpx.c b/src/gpx.c index 66fa231b..adcfe411 100644 --- a/src/gpx.c +++ b/src/gpx.c @@ -1,7 +1,10 @@ /* * viking -- GPS Data and Topo Analyzer, Explorer, and Manager * - * Copyright (C) 2003-2005, Evan Battaglia + * Copyright (C) 2003-2007, Evan Battaglia + * Copyright (C) 2007, Quy Tonthat + * Copyright (C) 2008, Hein Ragas + * Copyright (C) 2009, Tal B * * Some of the code adapted from GPSBabel 1.2.7 * http://gpsbabel.sf.net/ @@ -22,18 +25,23 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ - +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #define _XOPEN_SOURCE /* glibc2 needs this */ #include "gpx.h" #include "viking.h" #include +#ifdef HAVE_STRING_H #include +#endif #include +#ifdef HAVE_MATH_H #include - -#define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ" +#endif +#include typedef enum { tt_unknown = 0, @@ -61,6 +69,10 @@ typedef enum { tt_trk_trkseg_trkpt_fix, tt_trk_trkseg_trkpt_sat, + tt_trk_trkseg_trkpt_hdop, + tt_trk_trkseg_trkpt_vdop, + tt_trk_trkseg_trkpt_pdop, + tt_waypoint, tt_waypoint_coord, tt_waypoint_name, @@ -112,6 +124,9 @@ tag_mapping tag_path_map[] = { { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" }, { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" }, + { tt_trk_trkseg_trkpt_hdop, "/gpx/trk/trkseg/trkpt/hdop" }, + { tt_trk_trkseg_trkpt_vdop, "/gpx/trk/trkseg/trkpt/vdop" }, + { tt_trk_trkseg_trkpt_pdop, "/gpx/trk/trkseg/trkpt/pdop" }, {0} }; @@ -161,8 +176,8 @@ static const char *get_attr ( const char **attr, const char *key ) static gboolean set_c_ll ( const char **attr ) { if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) { - c_ll.lat = g_strtod(c_slat, NULL); - c_ll.lon = g_strtod(c_slon, NULL); + c_ll.lat = g_ascii_strtod(c_slat, NULL); + c_ll.lon = g_ascii_strtod(c_slon, NULL); return TRUE; } return FALSE; @@ -181,7 +196,6 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) case tt_wpt: if ( set_c_ll( attr ) ) { c_wp = vik_waypoint_new (); - c_wp->altitude = VIK_DEFAULT_ALTITUDE; if ( ! get_attr ( attr, "hidden" ) ) c_wp->visible = TRUE; @@ -202,7 +216,6 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) case tt_trk_trkseg_trkpt: if ( set_c_ll( attr ) ) { c_tp = vik_trackpoint_new (); - c_tp->altitude = VIK_DEFAULT_ALTITUDE; vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); if ( f_tr_newseg ) { c_tp->newsegment = TRUE; @@ -225,7 +238,6 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) case tt_waypoint: c_wp = vik_waypoint_new (); - c_wp->altitude = VIK_DEFAULT_ALTITUDE; c_wp->visible = TRUE; break; @@ -249,7 +261,8 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) static void gpx_end(VikTrwLayer *vtl, const char *el) { - static struct tm tm; + static GTimeVal tp_time; + g_string_truncate ( xpath, xpath->len - strlen(el) - 1 ); switch ( current_tag ) { @@ -266,7 +279,7 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) case tt_trk: if ( ! c_tr_name ) - c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_waypoints++); + c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_tracks++); vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr ); g_free ( c_tr_name ); c_tr = NULL; @@ -288,12 +301,12 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) break; case tt_wpt_ele: - c_wp->altitude = g_strtod ( c_cdata->str, NULL ); + c_wp->altitude = g_ascii_strtod ( c_cdata->str, NULL ); g_string_erase ( c_cdata, 0, -1 ); break; case tt_trk_trkseg_trkpt_ele: - c_tp->altitude = g_strtod ( c_cdata->str, NULL ); + c_tp->altitude = g_ascii_strtod ( c_cdata->str, NULL ); g_string_erase ( c_cdata, 0, -1 ); break; @@ -322,28 +335,24 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) break; case tt_trk_trkseg_trkpt_time: - - if ( strptime(c_cdata->str, GPX_TIME_FORMAT, &tm) != c_cdata->str ) { /* it read at least one char */ - c_tp->timestamp = mktime(&tm); + if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) { + c_tp->timestamp = tp_time.tv_sec; c_tp->has_timestamp = TRUE; } g_string_erase ( c_cdata, 0, -1 ); break; case tt_trk_trkseg_trkpt_course: - c_tp->extended = TRUE; - c_tp->course = g_strtod ( c_cdata->str, NULL ); + c_tp->course = g_ascii_strtod ( c_cdata->str, NULL ); g_string_erase ( c_cdata, 0, -1 ); break; case tt_trk_trkseg_trkpt_speed: - c_tp->extended = TRUE; - c_tp->speed = g_strtod ( c_cdata->str, NULL ); + c_tp->speed = g_ascii_strtod ( c_cdata->str, NULL ); g_string_erase ( c_cdata, 0, -1 ); break; case tt_trk_trkseg_trkpt_fix: - c_tp->extended = TRUE; if (!strcmp("2d", c_cdata->str)) c_tp->fix_mode = VIK_GPS_MODE_2D; else if (!strcmp("3d", c_cdata->str)) @@ -354,11 +363,25 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) break; case tt_trk_trkseg_trkpt_sat: - c_tp->extended = TRUE; c_tp->nsats = atoi ( c_cdata->str ); g_string_erase ( c_cdata, 0, -1 ); break; + case tt_trk_trkseg_trkpt_hdop: + c_tp->hdop = g_strtod ( c_cdata->str, NULL ); + g_string_erase ( c_cdata, 0, -1 ); + break; + + case tt_trk_trkseg_trkpt_vdop: + c_tp->vdop = g_strtod ( c_cdata->str, NULL ); + g_string_erase ( c_cdata, 0, -1 ); + break; + + case tt_trk_trkseg_trkpt_pdop: + c_tp->pdop = g_strtod ( c_cdata->str, NULL ); + g_string_erase ( c_cdata, 0, -1 ); + break; + default: break; } @@ -381,6 +404,9 @@ static void gpx_cdata(void *dta, const XML_Char *s, int len) case tt_trk_trkseg_trkpt_speed: case tt_trk_trkseg_trkpt_fix: case tt_trk_trkseg_trkpt_sat: + case tt_trk_trkseg_trkpt_hdop: + case tt_trk_trkseg_trkpt_vdop: + case tt_trk_trkseg_trkpt_pdop: case tt_waypoint_name: /* .loc name is really description. */ g_string_append_len ( c_cdata, s, len ); break; @@ -416,6 +442,7 @@ void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) { XML_Parse(parser, buf, len, done); } + XML_ParserFree (parser); g_string_free ( xpath, TRUE ); g_string_free ( c_cdata, TRUE ); } @@ -639,8 +666,8 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context { FILE *f = context->file; static struct LatLon ll; - gchar *s_lat,*s_lon, *s_alt; - static gchar time_buf[30]; + gchar *s_lat,*s_lon, *s_alt, *s_dop; + gchar *time_iso8601; vik_coord_to_latlon ( &(tp->coord), &ll ); if ( tp->newsegment ) @@ -665,38 +692,68 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context fprintf ( f, " %s\n", s_alt ); g_free ( s_alt ); s_alt = NULL; - time_buf[0] = '\0'; + time_iso8601 = NULL; if ( tp->has_timestamp ) { - time_buf [ strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&(tp->timestamp)) ) ] = '\0'; + GTimeVal timestamp; + timestamp.tv_sec = tp->timestamp; + timestamp.tv_usec = 0; + + time_iso8601 = g_time_val_to_iso8601 ( ×tamp ); } else if ( context->options != NULL && context->options->force_time ) { - time_t rawtime; - time ( &rawtime ); + GTimeVal current; + g_get_current_time ( ¤t ); - time_buf [strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&rawtime)) ] ='\0'; + time_iso8601 = g_time_val_to_iso8601 ( ¤t ); } - if ( time_buf[0] != '\0' ) - fprintf ( f, " \n", time_buf ); + if ( time_iso8601 != NULL ) + fprintf ( f, " \n", time_iso8601 ); + g_free(time_iso8601); + time_iso8601 = NULL; - if (tp->extended && (tp->fix_mode >= VIK_GPS_MODE_2D)) { - if (!isnan(tp->course)) { - gchar *s_course = a_coords_dtostr(tp->course); - fprintf ( f, " %s\n", s_course ); - g_free(s_course); - } - if (!isnan(tp->speed)) { - gchar *s_speed = a_coords_dtostr(tp->speed); - fprintf ( f, " %s\n", s_speed ); - g_free(s_speed); - } - if (tp->fix_mode == VIK_GPS_MODE_2D) - fprintf ( f, " 2d\n"); - if (tp->fix_mode == VIK_GPS_MODE_3D) - fprintf ( f, " 3d\n"); - if (tp->nsats > 0) - fprintf ( f, " %d\n", tp->nsats ); + if (!isnan(tp->course)) { + gchar *s_course = a_coords_dtostr(tp->course); + fprintf ( f, " %s\n", s_course ); + g_free(s_course); + } + if (!isnan(tp->speed)) { + gchar *s_speed = a_coords_dtostr(tp->speed); + fprintf ( f, " %s\n", s_speed ); + g_free(s_speed); + } + if (tp->fix_mode == VIK_GPS_MODE_2D) + fprintf ( f, " 2d\n"); + if (tp->fix_mode == VIK_GPS_MODE_3D) + fprintf ( f, " 3d\n"); + if (tp->nsats > 0) + fprintf ( f, " %d\n", tp->nsats ); + + s_dop = NULL; + if ( tp->hdop != VIK_DEFAULT_DOP ) + { + s_dop = a_coords_dtostr ( tp->hdop ); + } + if (s_dop != NULL) + fprintf ( f, " %s\n", s_dop ); + g_free ( s_dop ); s_dop = NULL; + + if ( tp->vdop != VIK_DEFAULT_DOP ) + { + s_dop = a_coords_dtostr ( tp->vdop ); + } + if (s_dop != NULL) + fprintf ( f, " %s\n", s_dop ); + g_free ( s_dop ); s_dop = NULL; + + if ( tp->pdop != VIK_DEFAULT_DOP ) + { + s_dop = a_coords_dtostr ( tp->pdop ); } + if (s_dop != NULL) + fprintf ( f, " %s\n", s_dop ); + g_free ( s_dop ); s_dop = NULL; + fprintf ( f, " \n" ); } @@ -758,6 +815,18 @@ typedef struct { guint n_wps; } gpx_gather_waypoints_passalong_t; +/* Type to hold name of track and timestamp of first trackpoint */ +typedef struct { + time_t first_timestamp; + const gchar *name; +} gpx_track_and_timestamp; + +typedef struct { + gpx_track_and_timestamp *trks; + guint i; + guint n_trks; +} gpx_gather_tracks_passalong_t; + static void gpx_collect_waypoint ( const gchar *name, VikWaypoint *wp, gpx_gather_waypoints_passalong_t *passalong ) { if ( passalong->i < passalong->n_wps ) { @@ -767,6 +836,25 @@ static void gpx_collect_waypoint ( const gchar *name, VikWaypoint *wp, gpx_gathe } } +/* Function to collect a track and the first timestamp in the list */ +static void gpx_collect_track (const gchar *name, VikTrack *track, gpx_gather_tracks_passalong_t *passalong) +{ + if (passalong->i < passalong->n_trks) + { + passalong->trks[passalong->i].name = name; + if (track && track->trackpoints && track->trackpoints->data) + { + VikTrackpoint *first_point = (VikTrackpoint *)track->trackpoints->data; + passalong->trks[passalong->i].first_timestamp = first_point->timestamp; + } + else + { + passalong->trks[passalong->i].first_timestamp = 0; + } + passalong->i++; + } +} + static int gpx_waypoint_and_name_compar(const void *x, const void *y) { gpx_waypoint_and_name *a = (gpx_waypoint_and_name *)x; @@ -774,6 +862,22 @@ static int gpx_waypoint_and_name_compar(const void *x, const void *y) return strcmp(a->name,b->name); } +/* Function to compare two tracks by their first timestamp */ +static int gpx_track_and_timestamp_compar(const void *x, const void *y) +{ + gpx_track_and_timestamp *a = (gpx_track_and_timestamp *)x; + gpx_track_and_timestamp *b = (gpx_track_and_timestamp *)y; + if (a->first_timestamp < b->first_timestamp) + { + return -1; + } + if (a->first_timestamp > b->first_timestamp) + { + return 1; + } + return 0; +} + void a_gpx_write_file( VikTrwLayer *vtl, FILE *f ) { a_gpx_write_file_options(NULL, vtl, f); @@ -798,8 +902,18 @@ void a_gpx_write_file_options ( GpxWritingOptions *options, VikTrwLayer *vtl, FI gpx_write_waypoint ( passalong.wps[i].name, passalong.wps[i].wp, &context); g_free ( passalong.wps ); - g_hash_table_foreach ( vik_trw_layer_get_tracks ( vtl ), (GHFunc) gpx_write_track, &context ); - + gpx_gather_tracks_passalong_t passalong_tracks; + passalong_tracks.n_trks = g_hash_table_size ( vik_trw_layer_get_tracks (vtl) ); + passalong_tracks.i = 0; + passalong_tracks.trks = g_new(gpx_track_and_timestamp,passalong_tracks.n_trks); + g_hash_table_foreach (vik_trw_layer_get_tracks(vtl), (GHFunc) gpx_collect_track, &passalong_tracks); + /* Sort by timestamp */ + qsort(passalong_tracks.trks, passalong_tracks.n_trks, sizeof(gpx_track_and_timestamp), gpx_track_and_timestamp_compar); + for (i=0;i