]> git.street.me.uk Git - andy/viking.git/blame - src/gpspoint.c
Support GPX src field on Waypoints, Tracks and Routes.
[andy/viking.git] / src / gpspoint.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
1b14d0d2 5 * Copyright (C) 2012-2013, Rob Norris <rw_norris@hotmail.com>
50a14534
EB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
8c00358d
GB
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
50a14534 25
8c00358d 26#ifdef HAVE_MATH_H
a2817d3c 27#include <math.h>
8c00358d
GB
28#endif
29
50a14534
EB
30#include "viking.h"
31
32#include <ctype.h>
8c00358d 33#ifdef HAVE_STRING_H
50a14534 34#include <string.h>
8c00358d 35#endif
50a14534
EB
36
37#include <stdlib.h>
38/* strtod */
39
0d2b891f
RN
40typedef struct {
41 FILE *f;
42 gboolean is_route;
43} TP_write_info_type;
50a14534 44
ce4bd1cf 45static void a_gpspoint_write_track ( const gpointer id, const VikTrack *t, FILE *f );
0d2b891f 46static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info );
c9570f86 47static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f );
50a14534 48
50a14534
EB
49/* outline for file gpspoint.c
50
51reading file:
52
53take a line.
54get first tag, if not type, skip it.
55if type, record type. if waypoint list, etc move on. if track, make a new track, make it current track, add it, etc.
56if waypoint, read on and store to the waypoint.
57if trackpoint, make trackpoint, store to current track (error / skip if none)
58
59*/
60
61/* Thanks to etrex-cache's gpsbabel's gpspoint.c for starting me off! */
62
63static char line_buffer[2048];
64
65#define GPSPOINT_TYPE_NONE 0
66#define GPSPOINT_TYPE_WAYPOINT 1
67#define GPSPOINT_TYPE_TRACKPOINT 2
0d2b891f 68#define GPSPOINT_TYPE_ROUTEPOINT 3
50a14534 69#define GPSPOINT_TYPE_TRACK 4
0d2b891f 70#define GPSPOINT_TYPE_ROUTE 5
50a14534
EB
71
72static VikTrack *current_track; /* pointer to pointer to first GList */
73
74static gint line_type = GPSPOINT_TYPE_NONE;
75static struct LatLon line_latlon;
76static gchar *line_name;
77static gchar *line_comment;
6b2f262e 78static gchar *line_description;
be455472 79static gchar *line_source;
b1453c16 80static gchar *line_color;
387ff7ac
RN
81static gint line_name_label = 0;
82static gint line_dist_label = 0;
50a14534 83static gchar *line_image;
acaf7113 84static gchar *line_symbol;
50a14534
EB
85static gboolean line_newsegment = FALSE;
86static gboolean line_has_timestamp = FALSE;
87static time_t line_timestamp = 0;
88static gdouble line_altitude = VIK_DEFAULT_ALTITUDE;
89static gboolean line_visible = TRUE;
a2817d3c
QT
90
91static gboolean line_extended = FALSE;
92static gdouble line_speed = NAN;
93static gdouble line_course = NAN;
94static gint line_sat = 0;
95static gint line_fix = 0;
9d342fdd
RN
96static gdouble line_hdop = VIK_DEFAULT_DOP;
97static gdouble line_vdop = VIK_DEFAULT_DOP;
98static gdouble line_pdop = VIK_DEFAULT_DOP;
50a14534
EB
99/* other possible properties go here */
100
101
7dfdf988
RN
102static void gpspoint_process_tag ( const gchar *tag, guint len );
103static void gpspoint_process_key_and_value ( const gchar *key, guint key_len, const gchar *value, guint value_len );
50a14534
EB
104
105static gchar *slashdup(const gchar *str)
106{
7dfdf988
RN
107 size_t len = strlen(str);
108 size_t need_bs_count, i, j;
50a14534
EB
109 gchar *rv;
110 for ( i = 0, need_bs_count = 0; i < len; i++ )
111 if ( str[i] == '\\' || str[i] == '"' )
112 need_bs_count++;
113 rv = g_malloc ( (len+need_bs_count+1) * sizeof(gchar) );
114 for ( i = 0, j = 0; i < len; i++, j++ )
115 {
116 if ( str[i] == '\\' || str[i] == '"' )
117 rv[j++] = '\\';
118 rv[j] = str[i];
56dbc6f4
RN
119 // Basic normalization of strings - replace Linefeed and Carriage returns as blanks.
120 // although allowed in GPX Spec - Viking file format can't handle multi-line strings yet...
121 if ( str[i] == '\n' || str[i] == '\r' )
122 rv[j] = ' ';
50a14534
EB
123 }
124 rv[j] = '\0';
125 return rv;
126}
127
128static gchar *deslashndup ( const gchar *str, guint16 len )
129{
130 guint16 i,j, bs_count, new_len;
131 gboolean backslash = FALSE;
132 gchar *rv;
133
134 if ( len < 1 )
135 return NULL;
136
137 for ( i = 0, bs_count = 0; i < len; i++ )
138 if ( str[i] == '\\' )
139 {
140 bs_count++;
141 i++;
142 }
143
144 if ( str[i-1] == '\\' && (len == 1 || str[i-2] != '\\') )
145 bs_count--;
146
147 new_len = len - bs_count;
148 rv = g_malloc ( (new_len+1) * sizeof(gchar) );
149 for ( i = 0, j = 0; i < len && j < new_len; i++ )
150 if ( str[i] == '\\' && !backslash )
151 backslash = TRUE;
152 else
153 {
154 rv[j++] = str[i];
155 backslash = FALSE;
156 }
157
158 rv[new_len] = '\0';
159 return rv;
160}
161
cb5ec7a8
RN
162/*
163 * Returns whether file read was a success
164 * No obvious way to test for a 'gpspoint' file,
165 * thus set a flag if any actual tag found during processing of the file
166 */
1b14d0d2 167gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f, const gchar *dirpath ) {
50a14534
EB
168 VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw );
169 gchar *tag_start, *tag_end;
50a14534
EB
170 g_assert ( f != NULL && trw != NULL );
171 line_type = 0;
172 line_timestamp = 0;
173 line_newsegment = FALSE;
acaf7113
AF
174 line_image = NULL;
175 line_symbol = NULL;
50a14534 176 current_track = NULL;
cb5ec7a8
RN
177 gboolean have_read_something = FALSE;
178
50a14534
EB
179 while (fgets(line_buffer, 2048, f))
180 {
181 gboolean inside_quote = 0;
182 gboolean backslash = 0;
183
184 line_buffer[strlen(line_buffer)-1] = '\0'; /* chop off newline */
185
186 /* for gpspoint files wrapped inside */
119ada4c
RN
187 if ( strlen(line_buffer) >= 13 && strncmp ( line_buffer, "~EndLayerData", 13 ) == 0 ) {
188 // Even just a blank TRW is ok when in a .vik file
189 have_read_something = TRUE;
50a14534 190 break;
119ada4c 191 }
50a14534 192
cb5ec7a8 193 /* each line: nullify stuff, make thing if nes, free name if ness */
50a14534
EB
194 tag_start = line_buffer;
195 for (;;)
196 {
197 /* my addition: find first non-whitespace character. if the null, skip line. */
198 while (*tag_start != '\0' && isspace(*tag_start))
199 tag_start++;
5934bd21 200 if (*tag_start == '\0')
50a14534
EB
201 break;
202
203 if (*tag_start == '#')
204 break;
205
206 tag_end = tag_start;
207 if (*tag_end == '"')
208 inside_quote = !inside_quote;
209 while (*tag_end != '\0' && (!isspace(*tag_end) || inside_quote)) {
210 tag_end++;
211 if (*tag_end == '\\' && !backslash)
212 backslash = TRUE;
213 else if (backslash)
214 backslash = FALSE;
215 else if (*tag_end == '"')
216 inside_quote = !inside_quote;
217 }
218
7dfdf988
RN
219 // Won't have super massively long strings, so potential truncation in cast is acceptable.
220 guint len = (guint)(tag_end - tag_start);
ce7ec497 221 gpspoint_process_tag ( tag_start, len );
50a14534
EB
222
223 if (*tag_end == '\0' )
224 break;
225 else
226 tag_start = tag_end+1;
227 }
228 if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name)
229 {
cb5ec7a8 230 have_read_something = TRUE;
50a14534 231 VikWaypoint *wp = vik_waypoint_new();
50a14534
EB
232 wp->visible = line_visible;
233 wp->altitude = line_altitude;
a2d207d7
RN
234 wp->has_timestamp = line_has_timestamp;
235 wp->timestamp = line_timestamp;
50a14534
EB
236
237 vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon );
238
805d282e
EB
239 vik_trw_layer_filein_add_waypoint ( trw, line_name, wp );
240 g_free ( line_name );
241 line_name = NULL;
50a14534
EB
242
243 if ( line_comment )
50a14534 244 vik_waypoint_set_comment ( wp, line_comment );
50a14534 245
6b2f262e 246 if ( line_description )
6b2f262e 247 vik_waypoint_set_description ( wp, line_description );
6b2f262e 248
be455472
RN
249 if ( line_source )
250 vik_waypoint_set_source ( wp, line_source );
251
1b14d0d2
RN
252 if ( line_image ) {
253 // Ensure the filename is absolute
254 if ( g_path_is_absolute ( line_image ) )
255 vik_waypoint_set_image ( wp, line_image );
256 else {
257 // Otherwise create the absolute filename from the directory of the .vik file & and the relative filename
258 gchar *full = g_strconcat(dirpath, G_DIR_SEPARATOR_S, line_image, NULL);
259 gchar *absolute = file_realpath_dup ( full ); // resolved into the canonical name
260 vik_waypoint_set_image ( wp, absolute );
261 g_free ( absolute );
262 g_free ( full );
263 }
264 }
50a14534 265
acaf7113 266 if ( line_symbol )
acaf7113 267 vik_waypoint_set_symbol ( wp, line_symbol );
50a14534 268 }
0d2b891f 269 else if ((line_type == GPSPOINT_TYPE_TRACK || line_type == GPSPOINT_TYPE_ROUTE) && line_name)
50a14534 270 {
cb5ec7a8 271 have_read_something = TRUE;
50a14534 272 VikTrack *pl = vik_track_new();
387ff7ac
RN
273 // NB don't set defaults here as all properties are stored in the GPS_POINT format
274 //vik_track_set_defaults ( pl );
50a14534
EB
275
276 /* Thanks to Peter Jones for this Fix */
277 if (!line_name) line_name = g_strdup("UNK");
278
279 pl->visible = line_visible;
0d2b891f 280 pl->is_route = (line_type == GPSPOINT_TYPE_ROUTE);
50a14534 281
50a14534 282 if ( line_comment )
50a14534 283 vik_track_set_comment ( pl, line_comment );
50a14534 284
6b2f262e 285 if ( line_description )
6b2f262e 286 vik_track_set_description ( pl, line_description );
6b2f262e 287
be455472
RN
288 if ( line_source )
289 vik_track_set_source ( pl, line_source );
290
b1453c16
RN
291 if ( line_color )
292 {
293 if ( gdk_color_parse ( line_color, &(pl->color) ) )
294 pl->has_color = TRUE;
295 }
296
387ff7ac
RN
297 pl->draw_name_mode = line_name_label;
298 pl->max_number_dist_labels = line_dist_label;
299
50a14534 300 pl->trackpoints = NULL;
805d282e
EB
301 vik_trw_layer_filein_add_track ( trw, line_name, pl );
302 g_free ( line_name );
303 line_name = NULL;
50a14534
EB
304
305 current_track = pl;
306 }
0d2b891f 307 else if ((line_type == GPSPOINT_TYPE_TRACKPOINT || line_type == GPSPOINT_TYPE_ROUTEPOINT) && current_track)
50a14534 308 {
cb5ec7a8 309 have_read_something = TRUE;
a2817d3c 310 VikTrackpoint *tp = vik_trackpoint_new();
50a14534
EB
311 vik_coord_load_from_latlon ( &(tp->coord), coord_mode, &line_latlon );
312 tp->newsegment = line_newsegment;
313 tp->has_timestamp = line_has_timestamp;
314 tp->timestamp = line_timestamp;
315 tp->altitude = line_altitude;
b45865b4 316 vik_trackpoint_set_name ( tp, line_name );
a2817d3c 317 if (line_extended) {
a2817d3c
QT
318 tp->speed = line_speed;
319 tp->course = line_course;
320 tp->nsats = line_sat;
321 tp->fix_mode = line_fix;
9d342fdd
RN
322 tp->hdop = line_hdop;
323 tp->vdop = line_vdop;
324 tp->pdop = line_pdop;
a2817d3c 325 }
50a14534
EB
326 current_track->trackpoints = g_list_append ( current_track->trackpoints, tp );
327 }
328
329 if (line_name)
330 g_free ( line_name );
331 line_name = NULL;
6b2f262e 332 if (line_comment)
50a14534 333 g_free ( line_comment );
6b2f262e
RN
334 if (line_description)
335 g_free ( line_description );
be455472
RN
336 if (line_source)
337 g_free ( line_source );
b1453c16
RN
338 if (line_color)
339 g_free ( line_color );
50a14534
EB
340 if (line_image)
341 g_free ( line_image );
acaf7113
AF
342 if (line_symbol)
343 g_free ( line_symbol );
50a14534 344 line_comment = NULL;
6b2f262e 345 line_description = NULL;
be455472 346 line_source = NULL;
b1453c16 347 line_color = NULL;
50a14534 348 line_image = NULL;
acaf7113 349 line_symbol = NULL;
50a14534
EB
350 line_type = GPSPOINT_TYPE_NONE;
351 line_newsegment = FALSE;
352 line_has_timestamp = FALSE;
353 line_timestamp = 0;
354 line_altitude = VIK_DEFAULT_ALTITUDE;
355 line_visible = TRUE;
acaf7113 356 line_symbol = NULL;
a2817d3c
QT
357
358 line_extended = FALSE;
359 line_speed = NAN;
360 line_course = NAN;
361 line_sat = 0;
362 line_fix = 0;
9d342fdd
RN
363 line_hdop = VIK_DEFAULT_DOP;
364 line_vdop = VIK_DEFAULT_DOP;
365 line_pdop = VIK_DEFAULT_DOP;
387ff7ac
RN
366 line_name_label = 0;
367 line_dist_label = 0;
50a14534 368 }
cb5ec7a8
RN
369
370 return have_read_something;
50a14534
EB
371}
372
373/* Tag will be of a few defined forms:
374 ^[:alpha:]*=".*"$
375 ^[:alpha:]*=.*$
376
377 <invalid tag>
378
379So we must determine end of tag name, start of value, end of value.
380*/
7dfdf988 381static void gpspoint_process_tag ( const gchar *tag, guint len )
50a14534
EB
382{
383 const gchar *key_end, *value_start, *value_end;
384
385 /* Searching for key end */
386 key_end = tag;
387
388 while (++key_end - tag < len)
389 if (*key_end == '=')
390 break;
391
392 if (key_end - tag == len)
393 return; /* no good */
394
395 if (key_end - tag == len + 1)
396 value_start = value_end = 0; /* size = 0 */
397 else
398 {
399 value_start = key_end + 1; /* equal_sign plus one */
400
401 if (*value_start == '"')
402 {
403 value_start++;
404 if (*value_start == '"')
405 value_start = value_end = 0; /* size = 0 */
406 else
407 {
408 if (*(tag+len-1) == '"')
a81479e1 409 value_end = tag + len - 1;
50a14534
EB
410 else
411 return; /* bogus */
412 }
413 }
414 else
415 value_end = tag + len; /* value start really IS value start. */
416
a81479e1
RN
417 // Detect broken lines which end without any text or the enclosing ". i.e. like: comment="
418 if ( (value_end - value_start) < 0 )
419 return;
420
50a14534
EB
421 gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start);
422 }
423}
424
425/*
426value = NULL for none
427*/
7dfdf988 428static void gpspoint_process_key_and_value ( const gchar *key, guint key_len, const gchar *value, guint value_len )
50a14534
EB
429{
430 if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 )
431 {
432 if (value == NULL)
433 line_type = GPSPOINT_TYPE_NONE;
434 else if (value_len == 5 && strncasecmp( value, "track", value_len ) == 0 )
435 line_type = GPSPOINT_TYPE_TRACK;
436 else if (value_len == 10 && strncasecmp( value, "trackpoint", value_len ) == 0 )
437 line_type = GPSPOINT_TYPE_TRACKPOINT;
438 else if (value_len == 8 && strncasecmp( value, "waypoint", value_len ) == 0 )
439 line_type = GPSPOINT_TYPE_WAYPOINT;
0d2b891f
RN
440 else if (value_len == 5 && strncasecmp( value, "route", value_len ) == 0 )
441 line_type = GPSPOINT_TYPE_ROUTE;
442 else if (value_len == 10 && strncasecmp( value, "routepoint", value_len ) == 0 )
443 line_type = GPSPOINT_TYPE_ROUTEPOINT;
50a14534
EB
444 else
445 /* all others are ignored */
446 line_type = GPSPOINT_TYPE_NONE;
447 }
448 else if (key_len == 4 && strncasecmp( key, "name", key_len ) == 0 && value != NULL)
449 {
450 if (line_name == NULL)
451 {
81eb2211 452 line_name = deslashndup ( value, value_len );
50a14534
EB
453 }
454 }
455 else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL)
456 {
457 if (line_comment == NULL)
458 line_comment = deslashndup ( value, value_len );
459 }
6b2f262e
RN
460 else if (key_len == 11 && strncasecmp( key, "description", key_len ) == 0 && value != NULL)
461 {
462 if (line_description == NULL)
463 line_description = deslashndup ( value, value_len );
464 }
be455472
RN
465 else if (key_len == 6 && strncasecmp( key, "source", key_len ) == 0 && value != NULL)
466 {
467 if (line_source == NULL)
468 line_source = deslashndup ( value, value_len );
469 }
b1453c16
RN
470 else if (key_len == 5 && strncasecmp( key, "color", key_len ) == 0 && value != NULL)
471 {
472 if (line_color == NULL)
473 line_color = deslashndup ( value, value_len );
474 }
387ff7ac
RN
475 else if (key_len == 14 && strncasecmp( key, "draw_name_mode", key_len ) == 0 && value != NULL)
476 {
477 line_name_label = atoi(value);
478 }
479 else if (key_len == 18 && strncasecmp( key, "number_dist_labels", key_len ) == 0 && value != NULL)
480 {
481 line_dist_label = atoi(value);
482 }
50a14534
EB
483 else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL)
484 {
485 if (line_image == NULL)
486 line_image = deslashndup ( value, value_len );
487 }
488 else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL)
489 {
d1cd12c0 490 line_latlon.lat = g_ascii_strtod(value, NULL);
50a14534
EB
491 }
492 else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL)
493 {
d1cd12c0 494 line_latlon.lon = g_ascii_strtod(value, NULL);
50a14534
EB
495 }
496 else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL)
497 {
d1cd12c0 498 line_altitude = g_ascii_strtod(value, NULL);
50a14534 499 }
e69ac989 500 else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value != NULL && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T')
50a14534
EB
501 {
502 line_visible = FALSE;
503 }
acaf7113
AF
504 else if (key_len == 6 && strncasecmp( key, "symbol", key_len ) == 0 && value != NULL)
505 {
506 line_symbol = g_strndup ( value, value_len );
507 }
50a14534
EB
508 else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL)
509 {
d1cd12c0 510 line_timestamp = g_ascii_strtod(value, NULL);
50a14534
EB
511 if ( line_timestamp != 0x80000000 )
512 line_has_timestamp = TRUE;
513 }
514 else if (key_len == 10 && strncasecmp( key, "newsegment", key_len ) == 0 && value != NULL)
515 {
516 line_newsegment = TRUE;
517 }
a2817d3c
QT
518 else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL)
519 {
520 line_extended = TRUE;
521 }
522 else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL)
523 {
d1cd12c0 524 line_speed = g_ascii_strtod(value, NULL);
a2817d3c
QT
525 }
526 else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL)
527 {
d1cd12c0 528 line_course = g_ascii_strtod(value, NULL);
a2817d3c
QT
529 }
530 else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL)
531 {
532 line_sat = atoi(value);
533 }
534 else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL)
535 {
536 line_fix = atoi(value);
537 }
9d342fdd
RN
538 else if (key_len == 4 && strncasecmp( key, "hdop", key_len ) == 0 && value != NULL)
539 {
540 line_hdop = g_ascii_strtod(value, NULL);
541 }
542 else if (key_len == 4 && strncasecmp( key, "vdop", key_len ) == 0 && value != NULL)
543 {
544 line_vdop = g_ascii_strtod(value, NULL);
545 }
546 else if (key_len == 4 && strncasecmp( key, "pdop", key_len ) == 0 && value != NULL)
547 {
548 line_pdop = g_ascii_strtod(value, NULL);
549 }
50a14534
EB
550}
551
c9570f86 552static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f )
50a14534
EB
553{
554 static struct LatLon ll;
161aa492 555 gchar *s_lat, *s_lon;
e69ac989
RN
556 // Sanity clauses
557 if ( !wp )
c9570f86 558 return;
e69ac989
RN
559 if ( !(wp->name) )
560 return;
561
50a14534 562 vik_coord_to_latlon ( &(wp->coord), &ll );
161aa492
EB
563 s_lat = a_coords_dtostr(ll.lat);
564 s_lon = a_coords_dtostr(ll.lon);
81eb2211
RN
565 gchar *tmp_name = slashdup(wp->name);
566 fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, tmp_name );
567 g_free ( tmp_name );
161aa492
EB
568 g_free ( s_lat );
569 g_free ( s_lon );
570
571 if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) {
572 gchar *s_alt = a_coords_dtostr(wp->altitude);
573 fprintf ( f, " altitude=\"%s\"", s_alt );
574 g_free(s_alt);
575 }
a2d207d7
RN
576 if ( wp->has_timestamp )
577 fprintf ( f, " unixtime=\"%ld\"", wp->timestamp );
50a14534
EB
578 if ( wp->comment )
579 {
580 gchar *tmp_comment = slashdup(wp->comment);
581 fprintf ( f, " comment=\"%s\"", tmp_comment );
582 g_free ( tmp_comment );
583 }
6b2f262e
RN
584 if ( wp->description )
585 {
586 gchar *tmp_description = slashdup(wp->description);
587 fprintf ( f, " description=\"%s\"", tmp_description );
588 g_free ( tmp_description );
589 }
be455472
RN
590 if ( wp->source )
591 {
592 gchar *tmp_source = slashdup(wp->source);
593 fprintf ( f, " source=\"%s\"", tmp_source );
594 g_free ( tmp_source );
595 }
50a14534
EB
596 if ( wp->image )
597 {
88542aa9
RN
598 gchar *tmp_image = NULL;
599 gchar *cwd = NULL;
600 if ( a_vik_get_file_ref_format() == VIK_FILE_REF_FORMAT_RELATIVE ) {
601 cwd = g_get_current_dir();
602 if ( cwd )
603 tmp_image = g_strdup ( file_GetRelativeFilename ( cwd, wp->image ) );
604 }
605
606 // if cwd not available - use image filename as is
607 // this should be an absolute path as set in thumbnails
608 if ( !cwd )
609 tmp_image = slashdup(wp->image);
610
611 if ( tmp_image )
612 fprintf ( f, " image=\"%s\"", tmp_image );
613
614 g_free ( cwd );
50a14534
EB
615 g_free ( tmp_image );
616 }
acaf7113
AF
617 if ( wp->symbol )
618 {
f8c92775
RN
619 // Due to changes in garminsymbols - the symbol name is now in Title Case
620 // However to keep newly generated .vik files better compatible with older Viking versions
621 // The symbol names will always be lowercase
622 gchar *tmp_symbol = g_utf8_strdown(wp->symbol, -1);
623 fprintf ( f, " symbol=\"%s\"", tmp_symbol );
624 g_free ( tmp_symbol );
acaf7113 625 }
50a14534
EB
626 if ( ! wp->visible )
627 fprintf ( f, " visible=\"n\"" );
628 fprintf ( f, "\n" );
629}
630
0d2b891f 631static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info )
50a14534
EB
632{
633 static struct LatLon ll;
161aa492 634 gchar *s_lat, *s_lon;
50a14534
EB
635 vik_coord_to_latlon ( &(tp->coord), &ll );
636
0d2b891f
RN
637 FILE *f = write_info->f;
638
a2817d3c
QT
639 /* TODO: modify a_coords_dtostr() to accept (optional) buffer
640 * instead of doing malloc/free everytime */
161aa492
EB
641 s_lat = a_coords_dtostr(ll.lat);
642 s_lon = a_coords_dtostr(ll.lon);
0d2b891f 643 fprintf ( f, "type=\"%spoint\" latitude=\"%s\" longitude=\"%s\"", write_info->is_route ? "route" : "track", s_lat, s_lon );
161aa492
EB
644 g_free ( s_lat );
645 g_free ( s_lon );
50a14534 646
b45865b4
RN
647 if ( tp->name ) {
648 gchar *name = slashdup(tp->name);
649 fprintf ( f, " name=\"%s\"", name );
650 g_free(name);
651 }
652
161aa492
EB
653 if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
654 gchar *s_alt = a_coords_dtostr(tp->altitude);
655 fprintf ( f, " altitude=\"%s\"", s_alt );
656 g_free(s_alt);
657 }
50a14534
EB
658 if ( tp->has_timestamp )
659 fprintf ( f, " unixtime=\"%ld\"", tp->timestamp );
660 if ( tp->newsegment )
661 fprintf ( f, " newsegment=\"yes\"" );
a2817d3c 662
376c9177 663 if (!isnan(tp->speed) || !isnan(tp->course) || tp->nsats > 0) {
a2817d3c
QT
664 fprintf ( f, " extended=\"yes\"" );
665 if (!isnan(tp->speed)) {
666 gchar *s_speed = a_coords_dtostr(tp->speed);
667 fprintf ( f, " speed=\"%s\"", s_speed );
668 g_free(s_speed);
669 }
670 if (!isnan(tp->course)) {
671 gchar *s_course = a_coords_dtostr(tp->course);
672 fprintf ( f, " course=\"%s\"", s_course );
673 g_free(s_course);
674 }
675 if (tp->nsats > 0)
676 fprintf ( f, " sat=\"%d\"", tp->nsats );
677 if (tp->fix_mode > 0)
678 fprintf ( f, " fix=\"%d\"", tp->fix_mode );
9d342fdd
RN
679
680 if ( tp->hdop != VIK_DEFAULT_DOP ) {
681 gchar *ss = a_coords_dtostr(tp->hdop);
682 fprintf ( f, " hdop=\"%s\"", ss );
683 g_free(ss);
684 }
685 if ( tp->vdop != VIK_DEFAULT_DOP ) {
686 gchar *ss = a_coords_dtostr(tp->vdop);
687 fprintf ( f, " vdop=\"%s\"", ss );
688 g_free(ss);
689 }
690 if ( tp->pdop != VIK_DEFAULT_DOP ) {
691 gchar *ss = a_coords_dtostr(tp->pdop);
692 fprintf ( f, " pdop=\"%s\"", ss );
693 g_free(ss);
694 }
a2817d3c 695 }
50a14534
EB
696 fprintf ( f, "\n" );
697}
698
699
6b2f262e 700static void a_gpspoint_write_track ( const gpointer id, const VikTrack *trk, FILE *f )
50a14534 701{
e979bdab 702 // Sanity clauses
6b2f262e 703 if ( !trk )
ce4bd1cf 704 return;
6b2f262e 705 if ( !(trk->name) )
e979bdab
RN
706 return;
707
81eb2211
RN
708 gchar *tmp_name = slashdup(trk->name);
709 fprintf ( f, "type=\"%s\" name=\"%s\"", trk->is_route ? "route" : "track", tmp_name );
710 g_free ( tmp_name );
6b2f262e
RN
711
712 if ( trk->comment ) {
713 gchar *tmp = slashdup(trk->comment);
714 fprintf ( f, " comment=\"%s\"", tmp );
715 g_free ( tmp );
50a14534 716 }
6b2f262e
RN
717
718 if ( trk->description ) {
719 gchar *tmp = slashdup(trk->description);
720 fprintf ( f, " description=\"%s\"", tmp );
721 g_free ( tmp );
722 }
723
be455472
RN
724 if ( trk->source ) {
725 gchar *tmp = slashdup(trk->source);
726 fprintf ( f, " source=\"%s\"", tmp );
727 g_free ( tmp );
728 }
729
b1453c16
RN
730 if ( trk->has_color ) {
731 fprintf ( f, " color=#%.2x%.2x%.2x", (int)(trk->color.red/256),(int)(trk->color.green/256),(int)(trk->color.blue/256));
732 }
733
387ff7ac
RN
734 if ( trk->draw_name_mode > 0 )
735 fprintf ( f, " draw_name_mode=\"%d\"", trk->draw_name_mode );
736
737 if ( trk->max_number_dist_labels > 0 )
738 fprintf ( f, " number_dist_labels=\"%d\"", trk->max_number_dist_labels );
739
6b2f262e
RN
740 if ( ! trk->visible ) {
741 fprintf ( f, " visible=\"n\"" );
742 }
743 fprintf ( f, "\n" );
744
0d2b891f
RN
745 TP_write_info_type tp_write_info = { f, trk->is_route };
746 g_list_foreach ( trk->trackpoints, (GFunc) a_gpspoint_write_trackpoint, &tp_write_info );
747 fprintf ( f, "type=\"%send\"\n", trk->is_route ? "route" : "track" );
50a14534
EB
748}
749
750void a_gpspoint_write_file ( VikTrwLayer *trw, FILE *f )
751{
752 GHashTable *tracks = vik_trw_layer_get_tracks ( trw );
0d2b891f 753 GHashTable *routes = vik_trw_layer_get_routes ( trw );
50a14534
EB
754 GHashTable *waypoints = vik_trw_layer_get_waypoints ( trw );
755
756 fprintf ( f, "type=\"waypointlist\"\n" );
757 g_hash_table_foreach ( waypoints, (GHFunc) a_gpspoint_write_waypoint, f );
758 fprintf ( f, "type=\"waypointlistend\"\n" );
759 g_hash_table_foreach ( tracks, (GHFunc) a_gpspoint_write_track, f );
0d2b891f 760 g_hash_table_foreach ( routes, (GHFunc) a_gpspoint_write_track, f );
50a14534 761}