]> git.street.me.uk Git - andy/viking.git/blobdiff - src/gpx.c
Put vikutils.h into viking.h
[andy/viking.git] / src / gpx.c
index c982aa845178b4a24a100f3f4fa36ea93b5c7e06..f11d781397256151aad6522c393ec55e7cfc8e1d 100644 (file)
--- a/src/gpx.c
+++ b/src/gpx.c
@@ -1,7 +1,11 @@
 /*
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
- * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
+ * Copyright (C) 2008, Hein Ragas <viking@ragas.nl>
+ * Copyright (C) 2009, Tal B <tal.bav@gmail.com>
+ * Copyright (c) 2012-2014, Rob Norris <rw_norris@hotmail.com>
  *
  * Some of the code adapted from GPSBabel 1.2.7
  * http://gpsbabel.sf.net/
  * 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 <expat.h>
+#ifdef HAVE_STRING_H
 #include <string.h>
+#endif
 #include <glib.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#ifdef HAVE_MATH_H
 #include <math.h>
-
-#define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ"
+#endif
+#include <time.h>
 
 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_src,
+        tt_wpt_type,
         tt_wpt_name,
         tt_wpt_ele,
-       tt_wpt_sym,
+        tt_wpt_sym,
+        tt_wpt_time,
+        tt_wpt_url,
         tt_wpt_link,            /* New in GPX 1.1 */
 
         tt_trk,
+        tt_trk_cmt,
         tt_trk_desc,
+        tt_trk_src,
+        tt_trk_type,
         tt_trk_name,
 
+        tt_rte,
+
         tt_trk_trkseg,
         tt_trk_trkseg_trkpt,
         tt_trk_trkseg_trkpt_ele,
         tt_trk_trkseg_trkpt_time,
+        tt_trk_trkseg_trkpt_name,
+        /* extended */
+        tt_trk_trkseg_trkpt_course,
+        tt_trk_trkseg_trkpt_speed,
+        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,
@@ -65,14 +102,33 @@ typedef struct tag_mapping {
         const char *tag_name;           /* xpath-ish tag name */
 } tag_mapping;
 
+typedef struct {
+       GpxWritingOptions *options;
+       FILE *file;
+} 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" },
@@ -80,21 +136,47 @@ 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" },
+        { tt_wpt_src, "/gpx/wpt/src" },
+        { tt_wpt_type, "/gpx/wpt/type" },
         { tt_wpt_sym, "/gpx/wpt/sym" },
         { tt_wpt_sym, "/loc/waypoint/type" },
+        { tt_wpt_url, "/gpx/wpt/url" },
         { tt_wpt_link, "/gpx/wpt/link" },                    /* GPX 1.1 */
 
         { tt_trk, "/gpx/trk" },
-        { tt_trk, "/gpx/rte" },
         { tt_trk_name, "/gpx/trk/name" },
+        { tt_trk_cmt, "/gpx/trk/cmt" },
         { tt_trk_desc, "/gpx/trk/desc" },
+        { tt_trk_src, "/gpx/trk/src" },
+        { tt_trk_type, "/gpx/trk/type" },
         { tt_trk_trkseg, "/gpx/trk/trkseg" },
         { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
-        { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
         { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
         { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
+        { 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" },
+
+        { 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" },
+
+        { tt_rte, "/gpx/rte" },
+        // NB Route reuses track point feature tags
+        { tt_trk_name, "/gpx/rte/name" },
+        { tt_trk_cmt, "/gpx/rte/cmt" },
+        { tt_trk_desc, "/gpx/rte/desc" },
+        { tt_trk_src, "/gpx/rte/src" },
+        { 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}
 };
@@ -118,6 +200,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;
@@ -130,7 +213,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 )
 {
@@ -145,8 +228,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;
@@ -162,21 +245,29 @@ 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 ();
-         c_wp->altitude = VIK_DEFAULT_ALTITUDE;
-         if ( get_attr ( attr, "hidden" ) )
-           c_wp->visible = TRUE;
+         c_wp->visible = TRUE;
+         if ( get_attr ( attr, "hidden" ) )
+           c_wp->visible = FALSE;
 
          vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
        }
        break;
 
      case tt_trk:
+     case tt_rte:
        c_tr = vik_track_new ();
-       if ( ! get_attr ( attr, "hidden" ) )
-         c_tr->visible = TRUE;
+       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" ) )
+         c_tr->visible = FALSE;
        break;
 
      case tt_trk_trkseg:
@@ -186,7 +277,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;
@@ -196,20 +286,33 @@ 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_src:
+     case tt_wpt_type:
      case tt_wpt_name:
      case tt_wpt_ele:
+     case tt_wpt_time:
+     case tt_wpt_url:
      case tt_wpt_link:
+     case tt_trk_cmt:
      case tt_trk_desc:
+     case tt_trk_src:
+     case tt_trk_type:
      case tt_trk_name:
        g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */
        break;
 
      case tt_waypoint:
        c_wp = vik_waypoint_new ();
-       c_wp->altitude = VIK_DEFAULT_ALTITUDE;
        c_wp->visible = TRUE;
        break;
 
@@ -233,15 +336,55 @@ 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;
+  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->author );
+       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;
@@ -250,7 +393,11 @@ 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%03d", unnamed_tracks++);
+       // Delibrate fall through
+     case tt_rte:
+       if ( ! c_tr_name )
+         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;
@@ -272,48 +419,137 @@ 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;
 
      case tt_waypoint_name: /* .loc name is really description. */
      case tt_wpt_desc:
+       vik_waypoint_set_description ( c_wp, c_cdata->str );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
+     case tt_wpt_cmt:
        vik_waypoint_set_comment ( c_wp, c_cdata->str );
        g_string_erase ( c_cdata, 0, -1 );
        break;
 
+     case tt_wpt_src:
+       vik_waypoint_set_source ( c_wp, c_cdata->str );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
+     case tt_wpt_type:
+       vik_waypoint_set_type ( c_wp, c_cdata->str );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
+     case tt_wpt_url:
+       vik_waypoint_set_url ( c_wp, c_cdata->str );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
      case tt_wpt_link:
        vik_waypoint_set_image ( c_wp, c_cdata->str );
        g_string_erase ( c_cdata, 0, -1 );
        break;
 
      case tt_wpt_sym: {
-       gchar *tmp_lower = g_utf8_strdown(c_cdata->str, -1); /* for things like <type>Geocache</type> */
-       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;
        }
 
      case tt_trk_desc:
+       vik_track_set_description ( c_tr, c_cdata->str );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
+     case tt_trk_src:
+       vik_track_set_source ( c_tr, c_cdata->str );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
+     case tt_trk_type:
+       vik_track_set_type ( c_tr, c_cdata->str );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
+     case tt_trk_cmt:
        vik_track_set_comment ( c_tr, c_cdata->str );
        g_string_erase ( c_cdata, 0, -1 );
        break;
 
-     case tt_trk_trkseg_trkpt_time:
+     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;
 
-       if ( strptime(c_cdata->str, GPX_TIME_FORMAT, &tm) != c_cdata->str ) { /* it read at least one char */
-         c_tp->timestamp = mktime(&tm);
+     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;
          c_tp->has_timestamp = TRUE;
        }
        g_string_erase ( c_cdata, 0, -1 );
        break;
 
+     case tt_trk_trkseg_trkpt_course:
+       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->speed = g_ascii_strtod ( c_cdata->str, NULL );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
+     case tt_trk_trkseg_trkpt_fix:
+       if (!strcmp("2d", c_cdata->str))
+         c_tp->fix_mode = VIK_GPS_MODE_2D;
+       else if (!strcmp("3d", c_cdata->str))
+         c_tp->fix_mode = VIK_GPS_MODE_3D;
+       else if (!strcmp("dgps", c_cdata->str))
+         c_tp->fix_mode = VIK_GPS_MODE_DGPS;
+       else if (!strcmp("pps", c_cdata->str))
+         c_tp->fix_mode = VIK_GPS_MODE_PPS;
+       else
+         c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN;
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
+     case tt_trk_trkseg_trkpt_sat:
+       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;
   }
 
@@ -323,15 +559,36 @@ 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:
     case tt_trk_trkseg_trkpt_ele:
+    case tt_wpt_cmt:
     case tt_wpt_desc:
+    case tt_wpt_src:
+    case tt_wpt_type:
     case tt_wpt_sym:
+    case tt_wpt_url:
     case tt_wpt_link:
+    case tt_trk_cmt:
     case tt_trk_desc:
+    case tt_trk_src:
+    case tt_trk_type:
     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:
+    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;
@@ -343,9 +600,10 @@ static void gpx_cdata(void *dta, const XML_Char *s, int len)
 // make like a "stack" of tag names
 // like gpspoint's separated like /gpx/wpt/whatever
 
-void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
+gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
   XML_Parser parser = XML_ParserCreate(NULL);
   int done=0, len;
+  enum XML_Status status = XML_STATUS_ERROR;
 
   XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
   XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
@@ -358,17 +616,21 @@ void 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);
     done = feof(f) || !len;
-    XML_Parse(parser, buf, len, done);
+    status = XML_Parse(parser, buf, len, done);
   }
  
+  XML_ParserFree (parser);
   g_string_free ( xpath, TRUE );
   g_string_free ( c_cdata, TRUE );
+
+  return status != XML_STATUS_ERROR;
 }
 
 /**** entitize from GPSBabel ****/
@@ -540,35 +802,82 @@ entitize(const char * str)
 
 /* export GPX */
 
-static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f ) 
+static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
 {
-  static struct LatLon ll;
-  gchar *s_lat,*s_lon;
+  // Don't write invisible waypoints when specified
+  if (context->options && !context->options->hidden && !wp->visible)
+    return;
+
+  FILE *f = context->file;
+  struct LatLon ll;
+  gchar s_lat[COORDS_STR_BUFFER_SIZE];
+  gchar s_lon[COORDS_STR_BUFFER_SIZE];
   gchar *tmp;
   vik_coord_to_latlon ( &(wp->coord), &ll );
-  s_lat = a_coords_dtostr( ll.lat );
-  s_lon = a_coords_dtostr( ll.lon );
+  a_coords_dtostr_buffer ( ll.lat, s_lat );
+  a_coords_dtostr_buffer ( ll.lon, s_lon );
+  // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
+  //  luckily most other GPX processing software ignores things they don't understand
   fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
                s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );
-  g_free ( s_lat );
-  g_free ( s_lon );
 
-  tmp = entitize ( name );
+  // Sanity clause
+  if ( wp->name )
+    tmp = entitize ( wp->name );
+  else
+    tmp = g_strdup ("waypoint");
+
   fprintf ( f, "  <name>%s</name>\n", tmp );
   g_free ( tmp);
 
   if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
   {
-    tmp = a_coords_dtostr ( wp->altitude );
-    fprintf ( f, "  <ele>%s</ele>\n", tmp );
-    g_free ( tmp );
+    gchar s_alt[COORDS_STR_BUFFER_SIZE];
+    a_coords_dtostr_buffer ( wp->altitude, s_alt );
+    fprintf ( f, "  <ele>%s</ele>\n", s_alt );
   }
+
+  if ( wp->has_timestamp ) {
+    GTimeVal timestamp;
+    timestamp.tv_sec = wp->timestamp;
+    timestamp.tv_usec = 0;
+
+    gchar *time_iso8601 = g_time_val_to_iso8601 ( &timestamp );
+    if ( time_iso8601 != NULL )
+      fprintf ( f, "  <time>%s</time>\n", time_iso8601 );
+    g_free ( time_iso8601 );
+  }
+
   if ( wp->comment )
   {
     tmp = entitize(wp->comment);
+    fprintf ( f, "  <cmt>%s</cmt>\n", tmp );
+    g_free ( tmp );
+  }
+  if ( wp->description )
+  {
+    tmp = entitize(wp->description);
     fprintf ( f, "  <desc>%s</desc>\n", tmp );
     g_free ( tmp );
   }
+  if ( wp->source )
+  {
+    tmp = entitize(wp->source);
+    fprintf ( f, "  <src>%s</src>\n", tmp );
+    g_free ( tmp );
+  }
+  if ( wp->type )
+  {
+    tmp = entitize(wp->type);
+    fprintf ( f, "  <type>%s</type>\n", tmp );
+    g_free ( tmp );
+  }
+  if ( wp->url )
+  {
+    tmp = entitize(wp->url);
+    fprintf ( f, "  <url>%s</url>\n", tmp );
+    g_free ( tmp );
+  }
   if ( wp->image )
   {
     tmp = entitize(wp->image);
@@ -578,81 +887,375 @@ static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f )
   if ( wp->symbol ) 
   {
     tmp = entitize(wp->symbol);
-    fprintf ( f, "  <sym>%s</sym>\n", tmp);
+    if ( a_vik_gpx_export_wpt_sym_name ( ) ) {
+       // Lowercase the symbol name
+       gchar *tmp2 = g_utf8_strdown ( tmp, -1 );
+       fprintf ( f, "  <sym>%s</sym>\n",  tmp2 );
+       g_free ( tmp2 );
+    }
+    else
+      fprintf ( f, "  <sym>%s</sym>\n", tmp);
     g_free ( tmp );
   }
 
   fprintf ( f, "</wpt>\n" );
 }
 
-static void gpx_write_trackpoint ( VikTrackpoint *tp, FILE *f )
+static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context )
 {
-  static struct LatLon ll;
-  gchar *s_lat,*s_lon, *s_alt;
-  static gchar time_buf[30];
+  FILE *f = context->file;
+  struct LatLon ll;
+  gchar s_lat[COORDS_STR_BUFFER_SIZE];
+  gchar s_lon[COORDS_STR_BUFFER_SIZE];
+  gchar s_alt[COORDS_STR_BUFFER_SIZE];
+  gchar s_dop[COORDS_STR_BUFFER_SIZE];
+  gchar *time_iso8601;
   vik_coord_to_latlon ( &(tp->coord), &ll );
 
-  if ( tp->newsegment )
+  // No such thing as a rteseg! So make sure we don't put them in
+  if ( context->options && !context->options->is_route && tp->newsegment )
     fprintf ( f, "  </trkseg>\n  <trkseg>\n" );
 
-  s_lat = a_coords_dtostr( ll.lat );
-  s_lon = a_coords_dtostr( ll.lon );
-  fprintf ( f, "  <trkpt lat=\"%s\" lon=\"%s\">\n", s_lat, s_lon );
-  g_free ( s_lat );
-  g_free ( s_lon );
+  a_coords_dtostr_buffer ( ll.lat, s_lat );
+  a_coords_dtostr_buffer ( ll.lon, s_lon );
+  fprintf ( f, "  <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon );
+
+  if (tp->name) {
+    gchar *s_name = entitize(tp->name);
+    fprintf ( f, "    <name>%s</name>\n", s_name );
+    g_free(s_name);
+  }
 
   if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
   {
-    s_alt = a_coords_dtostr ( tp->altitude );
+    a_coords_dtostr_buffer ( tp->altitude, s_alt );
     fprintf ( f, "    <ele>%s</ele>\n", s_alt );
-    g_free ( s_alt );
   }
+  else if ( context->options != NULL && context->options->force_ele )
+  {
+    fprintf ( f, "    <ele>0</ele>\n" );
+  }
+  
+  time_iso8601 = NULL;
   if ( tp->has_timestamp ) {
-    time_buf [ strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&(tp->timestamp)) ) ] = '\0';
-    fprintf ( f, "    <time>%s</time>\n", time_buf );
+    GTimeVal timestamp;
+    timestamp.tv_sec = tp->timestamp;
+    timestamp.tv_usec = 0;
+  
+    time_iso8601 = g_time_val_to_iso8601 ( &timestamp );
+  }
+  else if ( context->options != NULL && context->options->force_time )
+  {
+    GTimeVal current;
+    g_get_current_time ( &current );
+  
+    time_iso8601 = g_time_val_to_iso8601 ( &current );
+  }
+  if ( time_iso8601 != NULL )
+    fprintf ( f, "    <time>%s</time>\n", time_iso8601 );
+  g_free(time_iso8601);
+  time_iso8601 = NULL;
+  
+  if (!isnan(tp->course)) {
+    gchar s_course[COORDS_STR_BUFFER_SIZE];
+    a_coords_dtostr_buffer ( tp->course, s_course );
+    fprintf ( f, "    <course>%s</course>\n", s_course );
   }
-  fprintf ( f, "  </trkpt>\n" );
+  if (!isnan(tp->speed)) {
+    gchar s_speed[COORDS_STR_BUFFER_SIZE];
+    a_coords_dtostr_buffer ( tp->speed, s_speed );
+    fprintf ( f, "    <speed>%s</speed>\n", s_speed );
+  }
+  if (tp->fix_mode == VIK_GPS_MODE_2D)
+    fprintf ( f, "    <fix>2d</fix>\n");
+  if (tp->fix_mode == VIK_GPS_MODE_3D)
+    fprintf ( f, "    <fix>3d</fix>\n");
+  if (tp->fix_mode == VIK_GPS_MODE_DGPS)
+    fprintf ( f, "    <fix>dgps</fix>\n");
+  if (tp->fix_mode == VIK_GPS_MODE_PPS)
+    fprintf ( f, "    <fix>pps</fix>\n");
+  if (tp->nsats > 0)
+    fprintf ( f, "    <sat>%d</sat>\n", tp->nsats );
+
+  if ( tp->hdop != VIK_DEFAULT_DOP ) {
+    a_coords_dtostr_buffer ( tp->hdop, s_dop );
+    fprintf ( f, "    <hdop>%s</hdop>\n", s_dop );
+  }
+
+  if ( tp->vdop != VIK_DEFAULT_DOP ) {
+    a_coords_dtostr_buffer ( tp->vdop, s_dop );
+    fprintf ( f, "    <vdop>%s</vdop>\n", s_dop );
+  }
+
+  if ( tp->pdop != VIK_DEFAULT_DOP ) {
+    a_coords_dtostr_buffer ( tp->pdop, s_dop );
+    fprintf ( f, "    <pdop>%s</pdop>\n", s_dop );
+  }
+
+  fprintf ( f, "  </%spt>\n", (context->options && context->options->is_route) ? "rte" : "trk" );
 }
 
 
-static void gpx_write_track ( const gchar *name, VikTrack *t, FILE *f )
+static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
 {
+  // Don't write invisible tracks when specified
+  if (context->options && !context->options->hidden && !t->visible)
+    return;
+
+  FILE *f = context->file;
   gchar *tmp;
   gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */
 
-  tmp = entitize ( name );
-  fprintf ( f, "<trk%s>\n  <name>%s</name>\n", t->visible ? "" : " hidden=\"hidden\"", tmp );
+  // Sanity clause
+  if ( t->name )
+    tmp = entitize ( t->name );
+  else
+    tmp = g_strdup ("track");
+
+  // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
+  //  luckily most other GPX processing software ignores things they don't understand
+  fprintf ( f, "<%s%s>\n  <name>%s</name>\n",
+           t->is_route ? "rte" : "trk",
+           t->visible ? "" : " hidden=\"hidden\"",
+           tmp );
   g_free ( tmp );
 
   if ( t->comment )
   {
     tmp = entitize ( t->comment );
+    fprintf ( f, "  <cmt>%s</cmt>\n", tmp );
+    g_free ( tmp );
+  }
+
+  if ( t->description )
+  {
+    tmp = entitize ( t->description );
     fprintf ( f, "  <desc>%s</desc>\n", tmp );
     g_free ( tmp );
   }
 
-  fprintf ( f, "  <trkseg>\n" );
+  if ( t->source )
+  {
+    tmp = entitize ( t->source );
+    fprintf ( f, "  <src>%s</src>\n", tmp );
+    g_free ( tmp );
+  }
+
+  if ( t->type )
+  {
+    tmp = entitize ( t->type );
+    fprintf ( f, "  <type>%s</type>\n", tmp );
+    g_free ( tmp );
+  }
+
+  /* No such thing as a rteseg! */
+  if ( !t->is_route )
+    fprintf ( f, "  <trkseg>\n" );
 
   if ( t->trackpoints && t->trackpoints->data ) {
     first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment;
     VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */
-    g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, f );
+    g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context );
     VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
   }
 
-  fprintf ( f, "</trkseg>\n</trk>\n" );
+  /* NB apparently no such thing as a rteseg! */
+  if (!t->is_route)
+    fprintf ( f, "  </trkseg>\n");
+
+  fprintf ( f, "</%s>\n", t->is_route ? "rte" : "trk" );
 }
 
-void a_gpx_write_file( VikTrwLayer *vtl, FILE *f )
+static void gpx_write_header( FILE *f )
 {
   fprintf(f, "<?xml version=\"1.0\"?>\n"
           "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
           "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
           "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
           "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
-  g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_write_waypoint, f );
-  g_hash_table_foreach ( vik_trw_layer_get_tracks ( vtl ), (GHFunc) gpx_write_track, f );
+}
+
+static void gpx_write_footer( FILE *f )
+{
   fprintf(f, "</gpx>\n");
+}
+
+static int gpx_waypoint_compare(const void *x, const void *y)
+{
+  VikWaypoint *a = (VikWaypoint *)x;
+  VikWaypoint *b = (VikWaypoint *)y;
+  return strcmp(a->name,b->name);
+}
+
+static int gpx_track_compare_name(const void *x, const void *y)
+{
+  VikTrack *a = (VikTrack *)x;
+  VikTrack *b = (VikTrack *)y;
+  return strcmp(a->name,b->name);
+}
+
+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, "  <name>%s</name>\n", tmp );
+    g_free ( tmp );
+  }
+
+  VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
+  if ( md ) {
+    if ( md->author && strlen(md->author) > 0 ) {
+      tmp = entitize ( md->author );
+      fprintf ( f, "  <author>%s</author>\n", tmp );
+      g_free ( tmp );
+    }
+    if ( md->description && strlen(md->description) > 0) {
+      tmp = entitize ( md->description );
+      fprintf ( f, "  <desc>%s</desc>\n", tmp );
+      g_free ( tmp );
+    }
+    if ( md->timestamp ) {
+      tmp = entitize ( md->timestamp );
+      fprintf ( f, "  <time>%s</time>\n", tmp );
+      g_free ( tmp );
+    }
+    if ( md->keywords && strlen(md->keywords) > 0) {
+      tmp = entitize ( md->keywords );
+      fprintf ( f, "  <keywords>%s</keywords>\n", tmp );
+      g_free ( tmp );
+    }
+  }
+
+  if ( vik_trw_layer_get_waypoints_visibility(vtl) || (options && options->hidden) ) {
+    // gather waypoints in a list, then sort
+    GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
+    gl = g_list_sort ( gl, gpx_waypoint_compare );
+
+    for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
+      gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
+    }
+    g_list_free ( gl );
+  }
+
+  GList *gl = NULL;
+  if ( vik_trw_layer_get_tracks_visibility(vtl) || (options && options->hidden) ) {
+    //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
+    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, 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 );
+  }
+
+  GList *glrte = NULL;
+  // Routes sorted by name
+  if ( vik_trw_layer_get_routes_visibility(vtl) || (options && options->hidden) ) {
+    glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
+    glrte = g_list_sort ( glrte, gpx_track_compare_name );
+  }
+
+  // g_list_concat doesn't copy memory properly
+  // so process each list separately
+
+  GpxWritingContext context_tmp = context;
+  GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE, FALSE };
+  // Force trackpoints on tracks
+  if ( !context.options )
+    context_tmp.options = &opt_tmp;
+  context_tmp.options->is_route = FALSE;
+
+  // Loop around each list and write each one
+  for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
+    gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
+  }
 
+  // Routes (to get routepoints)
+  context_tmp.options->is_route = TRUE;
+  for (GList *iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
+    gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
+  }
+
+  g_list_free ( gl );
+  g_list_free ( glrte );
+
+  gpx_write_footer ( f );
+}
+
+void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
+{
+  GpxWritingContext context = {options, f};
+  gpx_write_header ( f );
+  gpx_write_track ( trk, &context );
+  gpx_write_footer ( f );
+}
+
+/**
+ * Common write of a temporary GPX file
+ */
+static gchar* write_tmp_file ( VikTrwLayer *vtl, VikTrack *trk, GpxWritingOptions *options )
+{
+       gchar *tmp_filename = NULL;
+       GError *error = NULL;
+       // Opening temporary file
+       int fd = g_file_open_tmp("viking_XXXXXX.gpx", &tmp_filename, &error);
+       if (fd < 0) {
+               g_warning ( _("failed to open temporary file: %s"), error->message );
+               g_clear_error ( &error );
+               return NULL;
+       }
+       g_debug ("%s: temporary file = %s", __FUNCTION__, tmp_filename);
+
+       FILE *ff = fdopen (fd, "w");
+
+       if ( trk )
+               a_gpx_write_track_file ( trk, ff, options );
+       else
+               a_gpx_write_file ( vtl, ff, options );
+
+       fclose (ff);
+
+       return tmp_filename;
+}
+
+/*
+ * a_gpx_write_tmp_file:
+ * @vtl:     The #VikTrwLayer to write
+ * @options: Possible ways of writing the file data (can be NULL)
+ *
+ * Returns: The name of newly created temporary GPX file
+ *          This file should be removed once used and the string freed.
+ *          If NULL then the process failed.
+ */
+gchar* a_gpx_write_tmp_file ( VikTrwLayer *vtl, GpxWritingOptions *options )
+{
+       return write_tmp_file ( vtl, NULL, options );
+}
+
+/*
+ * a_gpx_write_track_tmp_file:
+ * @trk:     The #VikTrack to write
+ * @options: Possible ways of writing the file data (can be NULL)
+ *
+ * Returns: The name of newly created temporary GPX file
+ *          This file should be removed once used and the string freed.
+ *          If NULL then the process failed.
+ */
+gchar* a_gpx_write_track_tmp_file ( VikTrack *trk, GpxWritingOptions *options )
+{
+       return write_tmp_file ( NULL, trk, options );
 }