From f2ef466d2ba3418edf03bc792d36b20f79272bf6 Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Tue, 30 Apr 2013 20:47:17 +0100 Subject: [PATCH] Add time property to Waypoints since it's in the GPX1.0 standard. Waypoints with timestamps are written by my Garmin Etrex 20, so I want to be able to use them! --- src/gpx.c | 27 ++++++++++++++++++++++++++- src/gpx.h | 1 + src/vikwaypoint.c | 2 ++ src/vikwaypoint.h | 2 ++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/gpx.c b/src/gpx.c index f7667538..97c723f0 100644 --- a/src/gpx.c +++ b/src/gpx.c @@ -54,7 +54,8 @@ typedef enum { tt_wpt_desc, tt_wpt_name, tt_wpt_ele, - tt_wpt_sym, + tt_wpt_sym, + tt_wpt_time, tt_wpt_link, /* New in GPX 1.1 */ tt_trk, @@ -108,6 +109,7 @@ tag_mapping tag_path_map[] = { { tt_waypoint_name, "/loc/waypoint/name" }, { tt_wpt_ele, "/gpx/wpt/ele" }, + { tt_wpt_time, "/gpx/wpt/time" }, { tt_wpt_name, "/gpx/wpt/name" }, { tt_wpt_cmt, "/gpx/wpt/cmt" }, { tt_wpt_desc, "/gpx/wpt/desc" }, @@ -249,6 +251,7 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) case tt_wpt_desc: case tt_wpt_name: case tt_wpt_ele: + case tt_wpt_time: case tt_wpt_link: case tt_trk_cmt: case tt_trk_desc: @@ -282,6 +285,7 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) static void gpx_end(VikTrwLayer *vtl, const char *el) { static GTimeVal tp_time; + static GTimeVal wp_time; g_string_truncate ( xpath, xpath->len - strlen(el) - 1 ); @@ -366,6 +370,14 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) g_string_erase ( c_cdata, 0, -1 ); break; + case tt_wpt_time: + if ( g_time_val_from_iso8601(c_cdata->str, &wp_time) ) { + c_wp->timestamp = wp_time.tv_sec; + c_wp->has_timestamp = TRUE; + } + g_string_erase ( c_cdata, 0, -1 ); + break; + case tt_trk_trkseg_trkpt_time: if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) { c_tp->timestamp = tp_time.tv_sec; @@ -434,6 +446,7 @@ static void gpx_cdata(void *dta, const XML_Char *s, int len) case tt_trk_cmt: case tt_trk_desc: case tt_trk_trkseg_trkpt_time: + case tt_wpt_time: case tt_trk_trkseg_trkpt_course: case tt_trk_trkseg_trkpt_speed: case tt_trk_trkseg_trkpt_fix: @@ -689,6 +702,18 @@ static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context ) fprintf ( f, " %s\n", tmp ); g_free ( tmp ); } + + if ( wp->has_timestamp ) { + GTimeVal timestamp; + timestamp.tv_sec = wp->timestamp; + timestamp.tv_usec = 0; + + gchar *time_iso8601 = g_time_val_to_iso8601 ( ×tamp ); + if ( time_iso8601 != NULL ) + fprintf ( f, " \n", time_iso8601 ); + g_free ( time_iso8601 ); + } + if ( wp->comment ) { tmp = entitize(wp->comment); diff --git a/src/gpx.h b/src/gpx.h index d8f7d4e2..7b1c925e 100644 --- a/src/gpx.h +++ b/src/gpx.h @@ -30,6 +30,7 @@ G_BEGIN_DECLS * Options adapting GPX writing. */ typedef struct { + // NB force options only apply to trackpoints gboolean force_ele; /// Force ele field gboolean force_time; /// Force time field gboolean hidden; /// Write invisible tracks/waypoints (default is yes) diff --git a/src/vikwaypoint.c b/src/vikwaypoint.c index 48cd2f2f..0a42c12a 100644 --- a/src/vikwaypoint.c +++ b/src/vikwaypoint.c @@ -127,6 +127,8 @@ VikWaypoint *vik_waypoint_copy(const VikWaypoint *wp) new_wp->coord = wp->coord; new_wp->visible = wp->visible; new_wp->altitude = wp->altitude; + new_wp->has_timestamp = wp->has_timestamp; + new_wp->timestamp = wp->timestamp; vik_waypoint_set_name(new_wp,wp->name); vik_waypoint_set_comment(new_wp,wp->comment); vik_waypoint_set_description(new_wp,wp->description); diff --git a/src/vikwaypoint.h b/src/vikwaypoint.h index 8f8a53af..5fd8da99 100644 --- a/src/vikwaypoint.h +++ b/src/vikwaypoint.h @@ -36,6 +36,8 @@ typedef struct _VikWaypoint VikWaypoint; struct _VikWaypoint { VikCoord coord; gboolean visible; + gboolean has_timestamp; + time_t timestamp; gdouble altitude; gchar *name; gchar *comment; -- 2.39.5