]> git.street.me.uk Git - andy/viking.git/blobdiff - src/gpx.c
Enable option to not write invisible tracks or waypoints in GPX file and don't write...
[andy/viking.git] / src / gpx.c
index fd5e024f160de433c2e7984209267f46499863ed..63dd68f99dd14f284c7976a1febc7364356bab7b 100644 (file)
--- 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 <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>
  *
  * Some of the code adapted from GPSBabel 1.2.7
  * http://gpsbabel.sf.net/
@@ -276,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;
@@ -415,9 +418,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 */
@@ -436,12 +440,14 @@ void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
   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 ****/
@@ -613,8 +619,12 @@ entitize(const char * str)
 
 /* export GPX */
 
-static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, GpxWritingContext *context ) 
+static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
 {
+  // Don't write invisible waypoints when specified
+  if (context->options && !context->options->hidden && !wp->visible)
+    return;
+
   FILE *f = context->file;
   static struct LatLon ll;
   gchar *s_lat,*s_lon;
@@ -622,12 +632,19 @@ static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, GpxWritingC
   vik_coord_to_latlon ( &(wp->coord), &ll );
   s_lat = a_coords_dtostr( ll.lat );
   s_lon = a_coords_dtostr( ll.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);
 
@@ -756,13 +773,24 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context
 }
 
 
-static void gpx_write_track ( const gchar *name, VikTrack *t, GpxWritingContext *context )
+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 );
+  // 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, "<trk%s>\n  <name>%s</name>\n", t->visible ? "" : " hidden=\"hidden\"", tmp );
   g_free ( tmp );
 
@@ -772,7 +800,6 @@ static void gpx_write_track ( const gchar *name, VikTrack *t, GpxWritingContext
     fprintf ( f, "  <desc>%s</desc>\n", tmp );
     g_free ( tmp );
   }
-
   fprintf ( f, "  <trkseg>\n" );
 
   if ( t->trackpoints && t->trackpoints->data ) {
@@ -799,23 +826,10 @@ static void gpx_write_footer( FILE *f )
   fprintf(f, "</gpx>\n");
 }
 
-
-
-typedef struct {
-  VikWaypoint *wp;
-  const gchar *name;
-} gpx_waypoint_and_name;
-
-typedef struct {
-  gpx_waypoint_and_name *wps;
-  guint i;
-  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;
+  gpointer id;
 } gpx_track_and_timestamp;
 
 typedef struct {
@@ -824,21 +838,12 @@ typedef struct {
   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 ) {
-    passalong->wps[passalong->i].name = name;
-    passalong->wps[passalong->i].wp = wp;
-    passalong->i++;
-  }
-}
-
 /* 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)
+static void gpx_collect_track (const gpointer id, VikTrack *track, gpx_gather_tracks_passalong_t *passalong)
 {
   if (passalong->i < passalong->n_trks)
   {
-    passalong->trks[passalong->i].name = name;
+    passalong->trks[passalong->i].id = id;
     if (track && track->trackpoints && track->trackpoints->data)
     {
       VikTrackpoint *first_point = (VikTrackpoint *)track->trackpoints->data;
@@ -852,15 +857,15 @@ static void gpx_collect_track (const gchar *name, VikTrack *track, gpx_gather_tr
   }
 }
 
-static int gpx_waypoint_and_name_compar(const void *x, const void *y)
+static int gpx_waypoint_compare(const void *x, const void *y)
 {
-  gpx_waypoint_and_name *a = (gpx_waypoint_and_name *)x;
-  gpx_waypoint_and_name *b = (gpx_waypoint_and_name *)y;
+  VikWaypoint *a = (VikWaypoint *)x;
+  VikWaypoint *b = (VikWaypoint *)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)
+static int gpx_track_and_timestamp_compare(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;
@@ -875,54 +880,47 @@ static int gpx_track_and_timestamp_compar(const void *x, const void *y)
   return 0;
 }
 
-void a_gpx_write_file( VikTrwLayer *vtl, FILE *f )
-{
-       a_gpx_write_file_options(NULL, vtl, f);
-}
-
-void a_gpx_write_file_options ( GpxWritingOptions *options, VikTrwLayer *vtl, FILE *f )
+void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
 {
   GpxWritingContext context = { options, f };
-  int i;
 
   gpx_write_header ( f );
 
+  // 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 ) );
+  gl = g_list_sort ( gl, gpx_waypoint_compare );
 
-  gpx_gather_waypoints_passalong_t passalong;
-  passalong.n_wps = g_hash_table_size ( vik_trw_layer_get_waypoints ( vtl ) );
-  passalong.i = 0;
-  passalong.wps = g_new(gpx_waypoint_and_name,passalong.n_wps);
-  g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_collect_waypoint, &passalong );
-  /* gather waypoints in a list, then sort */
-  qsort(passalong.wps, passalong.n_wps, sizeof(gpx_waypoint_and_name), gpx_waypoint_and_name_compar);
-  for ( i = 0; i < passalong.n_wps; i++ )
-    gpx_write_waypoint ( passalong.wps[i].name, passalong.wps[i].wp, &context);
-  g_free ( passalong.wps );
+  GList *iter;
+  for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
+    gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
+  }
 
+  g_list_free ( gl );
+            
   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<passalong_tracks.n_trks; i++)
-  {
-    gpx_write_track(passalong_tracks.trks[i].name, (VikTrack *)g_hash_table_lookup(vik_trw_layer_get_tracks(vtl), passalong_tracks.trks[i].name), &context);
+  gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
+  gl = g_list_sort ( gl, gpx_track_and_timestamp_compare );
+
+  for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
+    gpx_write_track ( (VikTrack*)iter->data, &context );
   }
+
+  g_list_free ( gl );
+
   g_free ( passalong_tracks.trks );
   gpx_write_footer ( f );
 }
 
-void a_gpx_write_track_file ( const gchar *name, VikTrack *t, FILE *f )
-{
-  a_gpx_write_track_file_options ( NULL, name, t, f );
-}
-
-void a_gpx_write_track_file_options ( GpxWritingOptions *options, const gchar *name, VikTrack *t, FILE *f )
+void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
 {
   GpxWritingContext context = {options, f};
   gpx_write_header ( f );
-  gpx_write_track ( name, t, &context );
+  gpx_write_track ( trk, &context );
   gpx_write_footer ( f );
 }