]> git.street.me.uk Git - andy/viking.git/blobdiff - src/gpspoint.c
Mapnik Layer tool.
[andy/viking.git] / src / gpspoint.c
index 06c0f0157d04e6daa363bcedaac4d962a633054f..bee2d337a26fbaf408ff2a9e0546957154402aab 100644 (file)
@@ -2,7 +2,7 @@
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
- * Copyright (C) 2012, Rob Norris <rw_norris@hotmail.com>
+ * Copyright (C) 2012-2013, Rob Norris <rw_norris@hotmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -46,7 +46,6 @@ static void a_gpspoint_write_track ( const gpointer id, const VikTrack *t, FILE
 static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info );
 static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f );
 
-
 /* outline for file gpspoint.c
 
 reading file:
@@ -78,6 +77,8 @@ static gchar *line_name;
 static gchar *line_comment;
 static gchar *line_description;
 static gchar *line_color;
+static gint line_name_label = 0;
+static gint line_dist_label = 0;
 static gchar *line_image;
 static gchar *line_symbol;
 static gboolean line_newsegment = FALSE;
@@ -155,7 +156,7 @@ static gchar *deslashndup ( const gchar *str, guint16 len )
  * No obvious way to test for a 'gpspoint' file,
  *  thus set a flag if any actual tag found during processing of the file
  */
-gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
+gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f, const gchar *dirpath ) {
   VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw );
   gchar *tag_start, *tag_end;
   g_assert ( f != NULL && trw != NULL );
@@ -220,6 +221,8 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
       VikWaypoint *wp = vik_waypoint_new();
       wp->visible = line_visible;
       wp->altitude = line_altitude;
+      wp->has_timestamp = line_has_timestamp;
+      wp->timestamp = line_timestamp;
 
       vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon );
 
@@ -228,33 +231,34 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
       line_name = NULL;
 
       if ( line_comment )
-      {
         vik_waypoint_set_comment ( wp, line_comment );
-        line_comment = NULL;
-      }
 
       if ( line_description )
-      {
         vik_waypoint_set_description ( wp, line_description );
-        line_description = NULL;
-      }
 
-      if ( line_image )
-      {
-        vik_waypoint_set_image ( wp, line_image );
-        line_image = NULL;
+      if ( line_image ) {
+        // Ensure the filename is absolute
+        if ( g_path_is_absolute ( line_image ) )
+          vik_waypoint_set_image ( wp, line_image );
+        else {
+          // Otherwise create the absolute filename from the directory of the .vik file & and the relative filename
+          gchar *full = g_strconcat(dirpath, G_DIR_SEPARATOR_S, line_image, NULL);
+          gchar *absolute = file_realpath_dup ( full ); // resolved into the canonical name
+          vik_waypoint_set_image ( wp, absolute );
+          g_free ( absolute );
+          g_free ( full );
+        }
       }
 
       if ( line_symbol )
-      {
         vik_waypoint_set_symbol ( wp, line_symbol );
-       line_symbol = NULL;
-      }
     }
     else if ((line_type == GPSPOINT_TYPE_TRACK || line_type == GPSPOINT_TYPE_ROUTE) && line_name)
     {
       have_read_something = TRUE;
       VikTrack *pl = vik_track_new();
+      // NB don't set defaults here as all properties are stored in the GPS_POINT format
+      //vik_track_set_defaults ( pl );
 
       /* Thanks to Peter Jones for this Fix */
       if (!line_name) line_name = g_strdup("UNK");
@@ -263,16 +267,10 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
       pl->is_route = (line_type == GPSPOINT_TYPE_ROUTE);
 
       if ( line_comment )
-      {
         vik_track_set_comment ( pl, line_comment );
-        line_comment = NULL;
-      }
 
       if ( line_description )
-      {
         vik_track_set_description ( pl, line_description );
-        line_description = NULL;
-      }
 
       if ( line_color )
       {
@@ -280,6 +278,9 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
         pl->has_color = TRUE;
       }
 
+      pl->draw_name_mode = line_name_label;
+      pl->max_number_dist_labels = line_dist_label;
+
       pl->trackpoints = NULL;
       vik_trw_layer_filein_add_track ( trw, line_name, pl );
       g_free ( line_name );
@@ -296,6 +297,7 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
       tp->has_timestamp = line_has_timestamp;
       tp->timestamp = line_timestamp;
       tp->altitude = line_altitude;
+      vik_trackpoint_set_name ( tp, line_name );
       if (line_extended) {
         tp->speed = line_speed;
         tp->course = line_course;
@@ -336,6 +338,8 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
     line_course = NAN;
     line_sat = 0;
     line_fix = 0;
+    line_name_label = 0;
+    line_dist_label = 0;
   }
 
   return have_read_something;
@@ -416,7 +420,7 @@ static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, con
   {
     if (line_name == NULL)
     {
-      line_name = g_strndup ( value, value_len );
+      line_name = deslashndup ( value, value_len );
     }
   }
   else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL)
@@ -434,6 +438,14 @@ static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, con
     if (line_color == NULL)
       line_color = deslashndup ( value, value_len );
   }
+  else if (key_len == 14 && strncasecmp( key, "draw_name_mode", key_len ) == 0 && value != NULL)
+  {
+    line_name_label = atoi(value);
+  }
+  else if (key_len == 18 && strncasecmp( key, "number_dist_labels", key_len ) == 0 && value != NULL)
+  {
+    line_dist_label = atoi(value);
+  }
   else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL)
   {
     if (line_image == NULL)
@@ -502,7 +514,9 @@ static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp
   vik_coord_to_latlon ( &(wp->coord), &ll );
   s_lat = a_coords_dtostr(ll.lat);
   s_lon = a_coords_dtostr(ll.lon);
-  fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, wp->name );
+  gchar *tmp_name = slashdup(wp->name);
+  fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, tmp_name );
+  g_free ( tmp_name );
   g_free ( s_lat ); 
   g_free ( s_lon );
 
@@ -511,6 +525,8 @@ static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp
     fprintf ( f, " altitude=\"%s\"", s_alt );
     g_free(s_alt);
   }
+  if ( wp->has_timestamp )
+    fprintf ( f, " unixtime=\"%ld\"", wp->timestamp );
   if ( wp->comment )
   {
     gchar *tmp_comment = slashdup(wp->comment);
@@ -525,13 +541,33 @@ static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp
   }
   if ( wp->image )
   {
-    gchar *tmp_image = slashdup(wp->image);
-    fprintf ( f, " image=\"%s\"", tmp_image );
+    gchar *tmp_image = NULL;
+    gchar *cwd = NULL;
+    if ( a_vik_get_file_ref_format() == VIK_FILE_REF_FORMAT_RELATIVE ) {
+      cwd = g_get_current_dir();
+      if ( cwd )
+        tmp_image = g_strdup ( file_GetRelativeFilename ( cwd, wp->image ) );
+    }
+
+    // if cwd not available - use image filename as is
+    // this should be an absolute path as set in thumbnails
+    if ( !cwd )
+      tmp_image = slashdup(wp->image);
+
+    if ( tmp_image )
+      fprintf ( f, " image=\"%s\"", tmp_image );
+
+    g_free ( cwd );
     g_free ( tmp_image );
   }
   if ( wp->symbol )
   {
-    fprintf ( f, " symbol=\"%s\"", wp->symbol );
+    // Due to changes in garminsymbols - the symbol name is now in Title Case
+    // However to keep newly generated .vik files better compatible with older Viking versions
+    //   The symbol names will always be lowercase
+    gchar *tmp_symbol = g_utf8_strdown(wp->symbol, -1);
+    fprintf ( f, " symbol=\"%s\"", tmp_symbol );
+    g_free ( tmp_symbol );
   }
   if ( ! wp->visible )
     fprintf ( f, " visible=\"n\"" );
@@ -554,6 +590,12 @@ static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type
   g_free ( s_lat ); 
   g_free ( s_lon );
 
+  if ( tp->name ) {
+    gchar *name = slashdup(tp->name);
+    fprintf ( f, " name=\"%s\"", name );
+    g_free(name);
+  }
+
   if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
     gchar *s_alt = a_coords_dtostr(tp->altitude);
     fprintf ( f, " altitude=\"%s\"", s_alt );
@@ -593,7 +635,9 @@ static void a_gpspoint_write_track ( const gpointer id, const VikTrack *trk, FIL
   if ( !(trk->name) )
     return;
 
-  fprintf ( f, "type=\"%s\" name=\"%s\"", trk->is_route ? "route" : "track", trk->name);
+  gchar *tmp_name = slashdup(trk->name);
+  fprintf ( f, "type=\"%s\" name=\"%s\"", trk->is_route ? "route" : "track", tmp_name );
+  g_free ( tmp_name );
 
   if ( trk->comment ) {
     gchar *tmp = slashdup(trk->comment);
@@ -611,6 +655,12 @@ static void a_gpspoint_write_track ( const gpointer id, const VikTrack *trk, FIL
     fprintf ( f, " color=#%.2x%.2x%.2x", (int)(trk->color.red/256),(int)(trk->color.green/256),(int)(trk->color.blue/256));
   }
 
+  if ( trk->draw_name_mode > 0 )
+    fprintf ( f, " draw_name_mode=\"%d\"", trk->draw_name_mode );
+
+  if ( trk->max_number_dist_labels > 0 )
+    fprintf ( f, " number_dist_labels=\"%d\"", trk->max_number_dist_labels );
+
   if ( ! trk->visible ) {
     fprintf ( f, " visible=\"n\"" );
   }