]> git.street.me.uk Git - andy/viking.git/blobdiff - src/gpx.c
Bug fix: View Mode displayed wrong setting on startup.
[andy/viking.git] / src / gpx.c
index 1aad2b0bdf377e4223194bf1d35d1a63064531b6..e82e89809fd404017ae8b2f4ca29eee52eae6058 100644 (file)
--- a/src/gpx.c
+++ b/src/gpx.c
@@ -29,6 +29,8 @@
 #include "viking.h"
 #include <expat.h>
 #include <string.h>
 #include "viking.h"
 #include <expat.h>
 #include <string.h>
+#include <glib.h>
+#include <math.h>
 
 #define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ"
 
 
 #define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ"
 
@@ -41,6 +43,7 @@ typedef enum {
         tt_wpt_desc,
         tt_wpt_name,
         tt_wpt_ele,
         tt_wpt_desc,
         tt_wpt_name,
         tt_wpt_ele,
+       tt_wpt_sym,
         tt_wpt_link,            /* New in GPX 1.1 */
 
         tt_trk,
         tt_wpt_link,            /* New in GPX 1.1 */
 
         tt_trk,
@@ -79,6 +82,8 @@ tag_mapping tag_path_map[] = {
         { tt_wpt_ele, "/gpx/wpt/ele" },
         { tt_wpt_name, "/gpx/wpt/name" },
         { tt_wpt_desc, "/gpx/wpt/desc" },
         { tt_wpt_ele, "/gpx/wpt/ele" },
         { tt_wpt_name, "/gpx/wpt/name" },
         { tt_wpt_desc, "/gpx/wpt/desc" },
+        { tt_wpt_sym, "/gpx/wpt/sym" },
+        { tt_wpt_sym, "/loc/waypoint/type" },
         { tt_wpt_link, "/gpx/wpt/link" },                    /* GPX 1.1 */
 
         { tt_trk, "/gpx/trk" },
         { tt_wpt_link, "/gpx/wpt/link" },                    /* GPX 1.1 */
 
         { tt_trk, "/gpx/trk" },
@@ -237,7 +242,8 @@ static void gpx_end(VikTrwLayer *vtl, const char *el)
      case tt_wpt:
        if ( ! c_wp_name )
          c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++);
      case tt_wpt:
        if ( ! c_wp_name )
          c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++);
-       g_hash_table_insert ( vik_trw_layer_get_waypoints ( vtl ), c_wp_name, c_wp );
+       vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp );
+       g_free ( c_wp_name );
        c_wp = NULL;
        c_wp_name = NULL;
        break;
        c_wp = NULL;
        c_wp_name = NULL;
        break;
@@ -245,7 +251,8 @@ 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++);
      case tt_trk:
        if ( ! c_tr_name )
          c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_waypoints++);
-       g_hash_table_insert ( vik_trw_layer_get_tracks ( vtl ), c_tr_name, c_tr );
+       vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr );
+       g_free ( c_tr_name );
        c_tr = NULL;
        c_tr_name = NULL;
        break;
        c_tr = NULL;
        c_tr_name = NULL;
        break;
@@ -285,12 +292,21 @@ static void gpx_end(VikTrwLayer *vtl, const char *el)
        g_string_erase ( c_cdata, 0, -1 );
        break;
 
        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 );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+       }
+
      case tt_trk_desc:
        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_trk_desc:
        vik_track_set_comment ( c_tr, c_cdata->str );
        g_string_erase ( c_cdata, 0, -1 );
        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);
          c_tp->has_timestamp = TRUE;
        if ( strptime(c_cdata->str, GPX_TIME_FORMAT, &tm) != c_cdata->str ) { /* it read at least one char */
          c_tp->timestamp = mktime(&tm);
          c_tp->has_timestamp = TRUE;
@@ -312,6 +328,7 @@ static void gpx_cdata(void *dta, const XML_Char *s, int len)
     case tt_wpt_ele:
     case tt_trk_trkseg_trkpt_ele:
     case tt_wpt_desc:
     case tt_wpt_ele:
     case tt_trk_trkseg_trkpt_ele:
     case tt_wpt_desc:
+    case tt_wpt_sym:
     case tt_wpt_link:
     case tt_trk_desc:
     case tt_trk_trkseg_trkpt_time:
     case tt_wpt_link:
     case tt_trk_desc:
     case tt_trk_trkseg_trkpt_time:
@@ -522,20 +539,30 @@ entitize(const char * str)
 /**** end GPSBabel code ****/
 
 /* export GPX */
 /**** end GPSBabel code ****/
 
 /* export GPX */
+
 static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f ) 
 {
   static struct LatLon ll;
 static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f ) 
 {
   static struct LatLon ll;
+  gchar *s_lat,*s_lon;
   gchar *tmp;
   vik_coord_to_latlon ( &(wp->coord), &ll );
   gchar *tmp;
   vik_coord_to_latlon ( &(wp->coord), &ll );
-  fprintf ( f, "<wpt lat=\"%f\" lon=\"%f\"%s>\n",
-               ll.lat, ll.lon, wp->visible ? "" : " hidden=\"hidden\"" );
+  s_lat = a_coords_dtostr( ll.lat );
+  s_lon = a_coords_dtostr( ll.lon );
+  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 );
   fprintf ( f, "  <name>%s</name>\n", tmp );
   g_free ( tmp);
 
   if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
 
   tmp = entitize ( name );
   fprintf ( f, "  <name>%s</name>\n", tmp );
   g_free ( tmp);
 
   if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
-    fprintf ( f, "  <ele>%f</ele>\n", wp->altitude );
+  {
+    tmp = a_coords_dtostr ( wp->altitude );
+    fprintf ( f, "  <ele>%s</ele>\n", tmp );
+    g_free ( tmp );
+  }
   if ( wp->comment )
   {
     tmp = entitize(wp->comment);
   if ( wp->comment )
   {
     tmp = entitize(wp->comment);
@@ -548,22 +575,38 @@ static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f )
     fprintf ( f, "  <link>%s</link>\n", tmp );
     g_free ( tmp );
   }
     fprintf ( f, "  <link>%s</link>\n", tmp );
     g_free ( tmp );
   }
+  if ( wp->symbol ) 
+  {
+    tmp = entitize(wp->symbol);
+    fprintf ( f, "  <sym>%s</sym>\n", tmp);
+    g_free ( tmp );
+  }
+
   fprintf ( f, "</wpt>\n" );
 }
 
 static void gpx_write_trackpoint ( VikTrackpoint *tp, FILE *f )
 {
   static struct LatLon ll;
   fprintf ( f, "</wpt>\n" );
 }
 
 static void gpx_write_trackpoint ( VikTrackpoint *tp, FILE *f )
 {
   static struct LatLon ll;
+  gchar *s_lat,*s_lon, *s_alt;
   static gchar time_buf[30];
   vik_coord_to_latlon ( &(tp->coord), &ll );
 
   if ( tp->newsegment )
     fprintf ( f, "  </trkseg>\n  <trkseg>\n" );
 
   static gchar time_buf[30];
   vik_coord_to_latlon ( &(tp->coord), &ll );
 
   if ( tp->newsegment )
     fprintf ( f, "  </trkseg>\n  <trkseg>\n" );
 
-  fprintf ( f, "  <trkpt lat=\"%f\" lon=\"%f\">\n", ll.lat, ll.lon );
+  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 );
 
   if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
 
   if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
-    fprintf ( f, "    <ele>%f</ele>\n", tp->altitude );
+  {
+    s_alt = a_coords_dtostr ( tp->altitude );
+    fprintf ( f, "    <ele>%s</ele>\n", s_alt );
+    g_free ( s_alt );
+  }
   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 );
   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 );