X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/9c237137b2ffa3b22215fb21243910bb89c18eec..23d6216c871ca648a093bd992f25fd9ce18f971d:/src/gpx.c
diff --git a/src/gpx.c b/src/gpx.c
index 7892b200..f42b8cc2 100644
--- a/src/gpx.c
+++ b/src/gpx.c
@@ -48,13 +48,19 @@ typedef enum {
tt_unknown = 0,
tt_gpx,
+ tt_gpx_name,
+ tt_gpx_desc,
+ tt_gpx_author,
+ tt_gpx_time,
+ tt_gpx_keywords,
tt_wpt,
tt_wpt_cmt,
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,
@@ -68,7 +74,8 @@ typedef enum {
tt_trk_trkseg_trkpt,
tt_trk_trkseg_trkpt_ele,
tt_trk_trkseg_trkpt_time,
- /* extended */
+ tt_trk_trkseg_trkpt_name,
+ /* extended */
tt_trk_trkseg_trkpt_course,
tt_trk_trkseg_trkpt_speed,
tt_trk_trkseg_trkpt_fix,
@@ -94,13 +101,27 @@ typedef struct {
} GpxWritingContext;
/*
- * xpath(ish) mappings between full tag paths and internal identifers.
+ * xpath(ish) mappings between full tag paths and internal identifiers.
* These appear in the order they appear in the GPX specification.
- * If it's not a tag we explictly handle, it doesn't go here.
+ * If it's not a tag we explicitly handle, it doesn't go here.
*/
tag_mapping tag_path_map[] = {
+ { tt_gpx, "/gpx" },
+ { tt_gpx_name, "/gpx/name" },
+ { tt_gpx_desc, "/gpx/desc" },
+ { tt_gpx_time, "/gpx/time" },
+ { tt_gpx_author, "/gpx/author" },
+ { tt_gpx_keywords, "/gpx/keywords" },
+
+ // GPX 1.1 variant - basic properties moved into metadata namespace
+ { tt_gpx_name, "/gpx/metadata/name" },
+ { tt_gpx_desc, "/gpx/metadata/desc" },
+ { tt_gpx_time, "/gpx/metadata/time" },
+ { tt_gpx_author, "/gpx/metadata/author" },
+ { tt_gpx_keywords, "/gpx/metadata/keywords" },
+
{ tt_wpt, "/gpx/wpt" },
{ tt_waypoint, "/loc/waypoint" },
@@ -108,6 +129,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" },
@@ -123,8 +145,9 @@ tag_mapping tag_path_map[] = {
{ tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
{ tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
{ tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
- /* extended */
- { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" },
+ { tt_trk_trkseg_trkpt_name, "/gpx/trk/trkseg/trkpt/name" },
+ /* extended */
+ { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" },
{ tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" },
{ tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" },
{ tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" },
@@ -139,6 +162,7 @@ tag_mapping tag_path_map[] = {
{ tt_trk_cmt, "/gpx/rte/cmt" },
{ tt_trk_desc, "/gpx/rte/desc" },
{ tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
+ { tt_trk_trkseg_trkpt_name, "/gpx/rte/rtept/name" },
{ tt_trk_trkseg_trkpt_ele, "/gpx/rte/rtept/ele" },
{0}
@@ -163,6 +187,7 @@ GString *c_cdata = NULL;
VikTrackpoint *c_tp = NULL;
VikWaypoint *c_wp = NULL;
VikTrack *c_tr = NULL;
+VikTRWMetadata *c_md = NULL;
gchar *c_wp_name = NULL;
gchar *c_tr_name = NULL;
@@ -175,7 +200,7 @@ struct LatLon c_ll;
gboolean f_tr_newseg;
guint unnamed_waypoints = 0;
guint unnamed_tracks = 0;
-
+guint unnamed_routes = 0;
static const char *get_attr ( const char **attr, const char *key )
{
@@ -207,6 +232,10 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
switch ( current_tag ) {
+ case tt_gpx:
+ c_md = vik_trw_metadata_new();
+ break;
+
case tt_wpt:
if ( set_c_ll( attr ) ) {
c_wp = vik_waypoint_new ();
@@ -221,6 +250,7 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
case tt_trk:
case tt_rte:
c_tr = vik_track_new ();
+ vik_track_set_defaults ( c_tr );
c_tr->is_route = (current_tag == tt_rte) ? TRUE : FALSE;
c_tr->visible = TRUE;
if ( get_attr ( attr, "hidden" ) )
@@ -243,12 +273,19 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
}
break;
+ case tt_gpx_name:
+ case tt_gpx_author:
+ case tt_gpx_desc:
+ case tt_gpx_keywords:
+ case tt_gpx_time:
+ case tt_trk_trkseg_trkpt_name:
case tt_trk_trkseg_trkpt_ele:
case tt_trk_trkseg_trkpt_time:
case tt_wpt_cmt:
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,15 +319,54 @@ 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 );
switch ( current_tag ) {
+ case tt_gpx:
+ vik_trw_layer_set_metadata ( vtl, c_md );
+ c_md = NULL;
+ break;
+
+ case tt_gpx_name:
+ vik_layer_rename ( VIK_LAYER(vtl), c_cdata->str );
+ g_string_erase ( c_cdata, 0, -1 );
+ break;
+
+ case tt_gpx_author:
+ if ( c_md->author )
+ g_free ( c_md->description );
+ c_md->author = g_strdup ( c_cdata->str );
+ g_string_erase ( c_cdata, 0, -1 );
+ break;
+
+ case tt_gpx_desc:
+ if ( c_md->description )
+ g_free ( c_md->description );
+ c_md->description = g_strdup ( c_cdata->str );
+ g_string_erase ( c_cdata, 0, -1 );
+ break;
+
+ case tt_gpx_keywords:
+ if ( c_md->keywords )
+ g_free ( c_md->keywords );
+ c_md->keywords = g_strdup ( c_cdata->str );
+ g_string_erase ( c_cdata, 0, -1 );
+ break;
+
+ case tt_gpx_time:
+ if ( c_md->timestamp )
+ g_free ( c_md->timestamp );
+ c_md->timestamp = g_strdup ( c_cdata->str );
+ g_string_erase ( c_cdata, 0, -1 );
+ break;
+
case tt_waypoint:
case tt_wpt:
if ( ! c_wp_name )
- c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++);
+ c_wp_name = g_strdup_printf("VIKING_WP%04d", unnamed_waypoints++);
vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp );
g_free ( c_wp_name );
c_wp = NULL;
@@ -298,9 +374,12 @@ static void gpx_end(VikTrwLayer *vtl, const char *el)
break;
case tt_trk:
+ if ( ! c_tr_name )
+ c_tr_name = g_strdup_printf("VIKING_TR%03d", unnamed_tracks++);
+ // Delibrate fall through
case tt_rte:
if ( ! c_tr_name )
- c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_tracks++);
+ c_tr_name = g_strdup_printf("VIKING_RT%03d", unnamed_routes++);
vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr );
g_free ( c_tr_name );
c_tr = NULL;
@@ -348,9 +427,7 @@ static void gpx_end(VikTrwLayer *vtl, const char *el)
break;
case tt_wpt_sym: {
- gchar *tmp_lower = g_utf8_strdown(c_cdata->str, -1); /* for things like Geocache */
- vik_waypoint_set_symbol ( c_wp, tmp_lower );
- g_free ( tmp_lower );
+ vik_waypoint_set_symbol ( c_wp, c_cdata->str );
g_string_erase ( c_cdata, 0, -1 );
break;
}
@@ -365,6 +442,19 @@ 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_name:
+ vik_trackpoint_set_name ( c_tp, c_cdata->str );
+ 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;
@@ -422,6 +512,11 @@ static void gpx_end(VikTrwLayer *vtl, const char *el)
static void gpx_cdata(void *dta, const XML_Char *s, int len)
{
switch ( current_tag ) {
+ case tt_gpx_name:
+ case tt_gpx_author:
+ case tt_gpx_desc:
+ case tt_gpx_keywords:
+ case tt_gpx_time:
case tt_wpt_name:
case tt_trk_name:
case tt_wpt_ele:
@@ -433,6 +528,8 @@ 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_name:
case tt_trk_trkseg_trkpt_course:
case tt_trk_trkseg_trkpt_speed:
case tt_trk_trkseg_trkpt_fix:
@@ -467,8 +564,9 @@ gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
xpath = g_string_new ( "" );
c_cdata = g_string_new ( "" );
- unnamed_waypoints = 0;
- unnamed_tracks = 0;
+ unnamed_waypoints = 1;
+ unnamed_tracks = 1;
+ unnamed_routes = 1;
while (!done) {
len = fread(buf, 1, sizeof(buf)-7, f);
@@ -687,6 +785,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);
@@ -708,7 +818,14 @@ static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
if ( wp->symbol )
{
tmp = entitize(wp->symbol);
- fprintf ( f, " %s\n", tmp);
+ if ( a_vik_gpx_export_wpt_sym_name ( ) ) {
+ // Lowercase the symbol name
+ gchar *tmp2 = g_utf8_strdown ( tmp, -1 );
+ fprintf ( f, " %s\n", tmp2 );
+ g_free ( tmp2 );
+ }
+ else
+ fprintf ( f, " %s\n", tmp);
g_free ( tmp );
}
@@ -733,6 +850,12 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context
g_free ( s_lat ); s_lat = NULL;
g_free ( s_lon ); s_lon = NULL;
+ if (tp->name) {
+ gchar *s_name = entitize(tp->name);
+ fprintf ( f, " %s\n", s_name );
+ g_free(s_name);
+ }
+
s_alt = NULL;
if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
{
@@ -896,43 +1019,44 @@ static int gpx_track_compare_name(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_compare_timestamp (const void *x, const void *y)
-{
- VikTrack *a = (VikTrack *)x;
- VikTrack *b = (VikTrack *)y;
-
- VikTrackpoint *tpa = NULL;
- VikTrackpoint *tpb = NULL;
-
- if ( a->trackpoints )
- tpa = VIK_TRACKPOINT(g_list_first(a->trackpoints)->data);
-
- if ( b->trackpoints )
- tpb = VIK_TRACKPOINT(g_list_first(b->trackpoints)->data);
-
- if ( tpa && tpb ) {
- if ( tpa->timestamp < tpb->timestamp )
- return -1;
- if ( tpa->timestamp > tpb->timestamp )
- return 1;
- }
-
- if ( tpa && !tpb )
- return 1;
-
- if ( !tpa && tpb )
- return -1;
-
- return 0;
-}
-
void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
{
GpxWritingContext context = { options, f };
gpx_write_header ( f );
+ gchar *tmp;
+ const gchar *name = vik_layer_get_name(VIK_LAYER(vtl));
+ if ( name ) {
+ tmp = entitize ( name );
+ fprintf ( f, " %s\n", tmp );
+ g_free ( tmp );
+ }
+
+ VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
+ if ( md ) {
+ if ( md->author ) {
+ tmp = entitize ( md->author );
+ fprintf ( f, " %s\n", tmp );
+ g_free ( tmp );
+ }
+ if ( md->description ) {
+ tmp = entitize ( md->description );
+ fprintf ( f, " %s\n", tmp );
+ g_free ( tmp );
+ }
+ if ( md->timestamp ) {
+ tmp = entitize ( md->timestamp );
+ fprintf ( f, " \n", tmp );
+ g_free ( tmp );
+ }
+ if ( md->keywords ) {
+ tmp = entitize ( md->keywords );
+ fprintf ( f, " %s\n", tmp );
+ g_free ( tmp );
+ }
+ }
+
// gather waypoints in a list, then sort
// g_hash_table_get_values: glib 2.14+
GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
@@ -945,11 +1069,21 @@ void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
g_list_free ( gl );
- gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
+ //gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
+ // Forming the list manually seems to produce one that is more likely to be nearer to the creation order
+ gl = NULL;
+ gpointer key, value;
+ GHashTableIter ght_iter;
+ g_hash_table_iter_init ( &ght_iter, vik_trw_layer_get_tracks ( vtl ) );
+ while ( g_hash_table_iter_next (&ght_iter, &key, &value) ) {
+ gl = g_list_prepend ( gl ,value );
+ }
+ gl = g_list_reverse ( gl );
+
// Sort method determined by preference
if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
- gl = g_list_sort ( gl, gpx_track_compare_timestamp );
- else
+ gl = g_list_sort ( gl, vik_track_compare_timestamp );
+ else if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_ALPHA )
gl = g_list_sort ( gl, gpx_track_compare_name );
// Routes sorted by name