]> git.street.me.uk Git - andy/viking.git/blame - src/gpx.c
Add Church as a Wikipedia waypoint feature type.
[andy/viking.git] / src / gpx.c
CommitLineData
8c4f1350
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
a482007a
GB
4 * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
6 * Copyright (C) 2008, Hein Ragas <viking@ragas.nl>
7 * Copyright (C) 2009, Tal B <tal.bav@gmail.com>
415b0e21 8 * Copyright (c) 2012-2014, Rob Norris <rw_norris@hotmail.com>
8c4f1350 9 *
bf35388d
EB
10 * Some of the code adapted from GPSBabel 1.2.7
11 * http://gpsbabel.sf.net/
12 * Copyright (C) 2002, 2003, 2004, 2005 Robert Lipe, robertlipe@usa.net
13 *
8c4f1350
EB
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
8c00358d
GB
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
8c4f1350
EB
32
33#define _XOPEN_SOURCE /* glibc2 needs this */
34
0b72c435 35#include "gpx.h"
8c4f1350
EB
36#include "viking.h"
37#include <expat.h>
8c00358d 38#ifdef HAVE_STRING_H
8c4f1350 39#include <string.h>
8c00358d 40#endif
8904c540 41#include <glib.h>
12ed2b58
RN
42#include <glib/gstdio.h>
43#include <glib/gi18n.h>
8c00358d 44#ifdef HAVE_MATH_H
8904c540 45#include <math.h>
8c00358d 46#endif
e3449376 47#include <time.h>
8c4f1350 48
8c4f1350
EB
49typedef enum {
50 tt_unknown = 0,
51
52 tt_gpx,
6ba42f1e
RN
53 tt_gpx_name,
54 tt_gpx_desc,
55 tt_gpx_author,
56 tt_gpx_time,
57 tt_gpx_keywords,
8c4f1350
EB
58
59 tt_wpt,
6b2f262e 60 tt_wpt_cmt,
8c4f1350 61 tt_wpt_desc,
be455472 62 tt_wpt_src,
01cfda98 63 tt_wpt_type,
8c4f1350
EB
64 tt_wpt_name,
65 tt_wpt_ele,
f2ef466d
RN
66 tt_wpt_sym,
67 tt_wpt_time,
415b0e21 68 tt_wpt_url,
8c4f1350
EB
69 tt_wpt_link, /* New in GPX 1.1 */
70
71 tt_trk,
6b2f262e 72 tt_trk_cmt,
8c4f1350 73 tt_trk_desc,
be455472 74 tt_trk_src,
01cfda98 75 tt_trk_type,
8c4f1350
EB
76 tt_trk_name,
77
0d2b891f
RN
78 tt_rte,
79
8c4f1350
EB
80 tt_trk_trkseg,
81 tt_trk_trkseg_trkpt,
82 tt_trk_trkseg_trkpt_ele,
83 tt_trk_trkseg_trkpt_time,
b45865b4 84 tt_trk_trkseg_trkpt_name,
6ba42f1e 85 /* extended */
a2817d3c
QT
86 tt_trk_trkseg_trkpt_course,
87 tt_trk_trkseg_trkpt_speed,
88 tt_trk_trkseg_trkpt_fix,
89 tt_trk_trkseg_trkpt_sat,
bf35388d 90
4a42b254
T
91 tt_trk_trkseg_trkpt_hdop,
92 tt_trk_trkseg_trkpt_vdop,
93 tt_trk_trkseg_trkpt_pdop,
94
bf35388d
EB
95 tt_waypoint,
96 tt_waypoint_coord,
97 tt_waypoint_name,
8c4f1350
EB
98} tag_type;
99
100typedef struct tag_mapping {
101 tag_type tag_type; /* enum from above for this tag */
102 const char *tag_name; /* xpath-ish tag name */
103} tag_mapping;
104
0b72c435
OK
105typedef struct {
106 GpxWritingOptions *options;
107 FILE *file;
108} GpxWritingContext;
109
8c4f1350 110/*
6ba42f1e 111 * xpath(ish) mappings between full tag paths and internal identifiers.
8c4f1350 112 * These appear in the order they appear in the GPX specification.
6ba42f1e 113 * If it's not a tag we explicitly handle, it doesn't go here.
8c4f1350
EB
114 */
115
116tag_mapping tag_path_map[] = {
8c4f1350 117
6ba42f1e
RN
118 { tt_gpx, "/gpx" },
119 { tt_gpx_name, "/gpx/name" },
120 { tt_gpx_desc, "/gpx/desc" },
121 { tt_gpx_time, "/gpx/time" },
122 { tt_gpx_author, "/gpx/author" },
123 { tt_gpx_keywords, "/gpx/keywords" },
124
125 // GPX 1.1 variant - basic properties moved into metadata namespace
126 { tt_gpx_name, "/gpx/metadata/name" },
127 { tt_gpx_desc, "/gpx/metadata/desc" },
128 { tt_gpx_time, "/gpx/metadata/time" },
129 { tt_gpx_author, "/gpx/metadata/author" },
130 { tt_gpx_keywords, "/gpx/metadata/keywords" },
131
8c4f1350 132 { tt_wpt, "/gpx/wpt" },
bf35388d
EB
133
134 { tt_waypoint, "/loc/waypoint" },
135 { tt_waypoint_coord, "/loc/waypoint/coord" },
136 { tt_waypoint_name, "/loc/waypoint/name" },
137
8c4f1350 138 { tt_wpt_ele, "/gpx/wpt/ele" },
f2ef466d 139 { tt_wpt_time, "/gpx/wpt/time" },
8c4f1350 140 { tt_wpt_name, "/gpx/wpt/name" },
6b2f262e 141 { tt_wpt_cmt, "/gpx/wpt/cmt" },
8c4f1350 142 { tt_wpt_desc, "/gpx/wpt/desc" },
be455472 143 { tt_wpt_src, "/gpx/wpt/src" },
01cfda98 144 { tt_wpt_type, "/gpx/wpt/type" },
42c2ac3e 145 { tt_wpt_sym, "/gpx/wpt/sym" },
08b251ec 146 { tt_wpt_sym, "/loc/waypoint/type" },
415b0e21 147 { tt_wpt_url, "/gpx/wpt/url" },
8c4f1350
EB
148 { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */
149
150 { tt_trk, "/gpx/trk" },
151 { tt_trk_name, "/gpx/trk/name" },
6b2f262e 152 { tt_trk_cmt, "/gpx/trk/cmt" },
8c4f1350 153 { tt_trk_desc, "/gpx/trk/desc" },
be455472 154 { tt_trk_src, "/gpx/trk/src" },
01cfda98 155 { tt_trk_type, "/gpx/trk/type" },
8c4f1350 156 { tt_trk_trkseg, "/gpx/trk/trkseg" },
8c4f1350
EB
157 { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
158 { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
159 { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
b45865b4 160 { tt_trk_trkseg_trkpt_name, "/gpx/trk/trkseg/trkpt/name" },
6ba42f1e
RN
161 /* extended */
162 { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" },
a2817d3c
QT
163 { tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" },
164 { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" },
165 { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" },
8c4f1350 166
4a42b254
T
167 { tt_trk_trkseg_trkpt_hdop, "/gpx/trk/trkseg/trkpt/hdop" },
168 { tt_trk_trkseg_trkpt_vdop, "/gpx/trk/trkseg/trkpt/vdop" },
169 { tt_trk_trkseg_trkpt_pdop, "/gpx/trk/trkseg/trkpt/pdop" },
0d2b891f
RN
170
171 { tt_rte, "/gpx/rte" },
172 // NB Route reuses track point feature tags
173 { tt_trk_name, "/gpx/rte/name" },
174 { tt_trk_cmt, "/gpx/rte/cmt" },
175 { tt_trk_desc, "/gpx/rte/desc" },
be455472 176 { tt_trk_src, "/gpx/rte/src" },
0d2b891f 177 { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
b45865b4 178 { tt_trk_trkseg_trkpt_name, "/gpx/rte/rtept/name" },
0d2b891f
RN
179 { tt_trk_trkseg_trkpt_ele, "/gpx/rte/rtept/ele" },
180
8c4f1350
EB
181 {0}
182};
183
184static tag_type get_tag(const char *t)
185{
186 tag_mapping *tm;
187 for (tm = tag_path_map; tm->tag_type != 0; tm++)
188 if (0 == strcmp(tm->tag_name, t))
189 return tm->tag_type;
190 return tt_unknown;
191}
192
193/******************************************/
194
195tag_type current_tag = tt_unknown;
196GString *xpath = NULL;
197GString *c_cdata = NULL;
198
199/* current ("c_") objects */
200VikTrackpoint *c_tp = NULL;
201VikWaypoint *c_wp = NULL;
202VikTrack *c_tr = NULL;
6ba42f1e 203VikTRWMetadata *c_md = NULL;
8c4f1350
EB
204
205gchar *c_wp_name = NULL;
206gchar *c_tr_name = NULL;
207
208/* temporary things so we don't have to create them lots of times */
209const gchar *c_slat, *c_slon;
210struct LatLon c_ll;
211
212/* specialty flags / etc */
213gboolean f_tr_newseg;
214guint unnamed_waypoints = 0;
215guint unnamed_tracks = 0;
43e2df3e 216guint unnamed_routes = 0;
8c4f1350
EB
217
218static const char *get_attr ( const char **attr, const char *key )
219{
220 while ( *attr ) {
221 if ( strcmp(*attr,key) == 0 )
222 return *(attr + 1);
223 attr += 2;
224 }
225 return NULL;
226}
227
228static gboolean set_c_ll ( const char **attr )
229{
230 if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) {
d1cd12c0
T
231 c_ll.lat = g_ascii_strtod(c_slat, NULL);
232 c_ll.lon = g_ascii_strtod(c_slon, NULL);
8c4f1350
EB
233 return TRUE;
234 }
235 return FALSE;
236}
237
238static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
239{
bf35388d
EB
240 static const gchar *tmp;
241
8c4f1350
EB
242 g_string_append_c ( xpath, '/' );
243 g_string_append ( xpath, el );
244 current_tag = get_tag ( xpath->str );
245
246 switch ( current_tag ) {
247
6ba42f1e
RN
248 case tt_gpx:
249 c_md = vik_trw_metadata_new();
250 break;
251
8c4f1350
EB
252 case tt_wpt:
253 if ( set_c_ll( attr ) ) {
254 c_wp = vik_waypoint_new ();
0b9f279d
RN
255 c_wp->visible = TRUE;
256 if ( get_attr ( attr, "hidden" ) )
257 c_wp->visible = FALSE;
8c4f1350
EB
258
259 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
260 }
261 break;
262
263 case tt_trk:
0d2b891f 264 case tt_rte:
8c4f1350 265 c_tr = vik_track_new ();
387ff7ac 266 vik_track_set_defaults ( c_tr );
0d2b891f 267 c_tr->is_route = (current_tag == tt_rte) ? TRUE : FALSE;
0b9f279d
RN
268 c_tr->visible = TRUE;
269 if ( get_attr ( attr, "hidden" ) )
270 c_tr->visible = FALSE;
8c4f1350
EB
271 break;
272
273 case tt_trk_trkseg:
274 f_tr_newseg = TRUE;
275 break;
276
277 case tt_trk_trkseg_trkpt:
278 if ( set_c_ll( attr ) ) {
279 c_tp = vik_trackpoint_new ();
8c4f1350
EB
280 vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
281 if ( f_tr_newseg ) {
282 c_tp->newsegment = TRUE;
283 f_tr_newseg = FALSE;
284 }
285 c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp );
286 }
287 break;
288
6ba42f1e
RN
289 case tt_gpx_name:
290 case tt_gpx_author:
291 case tt_gpx_desc:
292 case tt_gpx_keywords:
293 case tt_gpx_time:
b45865b4 294 case tt_trk_trkseg_trkpt_name:
8c4f1350
EB
295 case tt_trk_trkseg_trkpt_ele:
296 case tt_trk_trkseg_trkpt_time:
6b2f262e 297 case tt_wpt_cmt:
8c4f1350 298 case tt_wpt_desc:
be455472 299 case tt_wpt_src:
01cfda98 300 case tt_wpt_type:
8c4f1350
EB
301 case tt_wpt_name:
302 case tt_wpt_ele:
f2ef466d 303 case tt_wpt_time:
415b0e21 304 case tt_wpt_url:
8c4f1350 305 case tt_wpt_link:
6b2f262e 306 case tt_trk_cmt:
8c4f1350 307 case tt_trk_desc:
be455472 308 case tt_trk_src:
01cfda98 309 case tt_trk_type:
8c4f1350
EB
310 case tt_trk_name:
311 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */
bf35388d 312 break;
8c4f1350 313
bf35388d
EB
314 case tt_waypoint:
315 c_wp = vik_waypoint_new ();
bf35388d
EB
316 c_wp->visible = TRUE;
317 break;
318
319 case tt_waypoint_coord:
320 if ( set_c_ll( attr ) )
321 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
322 break;
323
324 case tt_waypoint_name:
325 if ( ( tmp = get_attr(attr, "id") ) ) {
326 if ( c_wp_name )
327 g_free ( c_wp_name );
328 c_wp_name = g_strdup ( tmp );
329 }
330 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */
331 break;
332
8c4f1350
EB
333 default: break;
334 }
335}
336
337static void gpx_end(VikTrwLayer *vtl, const char *el)
338{
dd81e762 339 static GTimeVal tp_time;
f2ef466d 340 static GTimeVal wp_time;
dd81e762 341
8c4f1350
EB
342 g_string_truncate ( xpath, xpath->len - strlen(el) - 1 );
343
344 switch ( current_tag ) {
345
6ba42f1e
RN
346 case tt_gpx:
347 vik_trw_layer_set_metadata ( vtl, c_md );
348 c_md = NULL;
349 break;
350
351 case tt_gpx_name:
352 vik_layer_rename ( VIK_LAYER(vtl), c_cdata->str );
353 g_string_erase ( c_cdata, 0, -1 );
354 break;
355
356 case tt_gpx_author:
357 if ( c_md->author )
e57fae85 358 g_free ( c_md->author );
6ba42f1e
RN
359 c_md->author = g_strdup ( c_cdata->str );
360 g_string_erase ( c_cdata, 0, -1 );
361 break;
362
363 case tt_gpx_desc:
364 if ( c_md->description )
365 g_free ( c_md->description );
366 c_md->description = g_strdup ( c_cdata->str );
367 g_string_erase ( c_cdata, 0, -1 );
368 break;
369
370 case tt_gpx_keywords:
371 if ( c_md->keywords )
372 g_free ( c_md->keywords );
373 c_md->keywords = g_strdup ( c_cdata->str );
374 g_string_erase ( c_cdata, 0, -1 );
375 break;
376
377 case tt_gpx_time:
378 if ( c_md->timestamp )
379 g_free ( c_md->timestamp );
380 c_md->timestamp = g_strdup ( c_cdata->str );
381 g_string_erase ( c_cdata, 0, -1 );
382 break;
383
bf35388d 384 case tt_waypoint:
8c4f1350
EB
385 case tt_wpt:
386 if ( ! c_wp_name )
43e2df3e 387 c_wp_name = g_strdup_printf("VIKING_WP%04d", unnamed_waypoints++);
805d282e
EB
388 vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp );
389 g_free ( c_wp_name );
8c4f1350
EB
390 c_wp = NULL;
391 c_wp_name = NULL;
392 break;
393
394 case tt_trk:
43e2df3e
RN
395 if ( ! c_tr_name )
396 c_tr_name = g_strdup_printf("VIKING_TR%03d", unnamed_tracks++);
397 // Delibrate fall through
0d2b891f 398 case tt_rte:
8c4f1350 399 if ( ! c_tr_name )
43e2df3e 400 c_tr_name = g_strdup_printf("VIKING_RT%03d", unnamed_routes++);
805d282e
EB
401 vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr );
402 g_free ( c_tr_name );
8c4f1350
EB
403 c_tr = NULL;
404 c_tr_name = NULL;
405 break;
406
407 case tt_wpt_name:
408 if ( c_wp_name )
409 g_free ( c_wp_name );
410 c_wp_name = g_strdup ( c_cdata->str );
411 g_string_erase ( c_cdata, 0, -1 );
412 break;
413
414 case tt_trk_name:
415 if ( c_tr_name )
416 g_free ( c_tr_name );
417 c_tr_name = g_strdup ( c_cdata->str );
418 g_string_erase ( c_cdata, 0, -1 );
419 break;
420
421 case tt_wpt_ele:
d1cd12c0 422 c_wp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
8c4f1350
EB
423 g_string_erase ( c_cdata, 0, -1 );
424 break;
425
426 case tt_trk_trkseg_trkpt_ele:
d1cd12c0 427 c_tp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
8c4f1350
EB
428 g_string_erase ( c_cdata, 0, -1 );
429 break;
430
bf35388d 431 case tt_waypoint_name: /* .loc name is really description. */
8c4f1350 432 case tt_wpt_desc:
6b2f262e
RN
433 vik_waypoint_set_description ( c_wp, c_cdata->str );
434 g_string_erase ( c_cdata, 0, -1 );
435 break;
436
437 case tt_wpt_cmt:
8c4f1350
EB
438 vik_waypoint_set_comment ( c_wp, c_cdata->str );
439 g_string_erase ( c_cdata, 0, -1 );
440 break;
441
be455472
RN
442 case tt_wpt_src:
443 vik_waypoint_set_source ( c_wp, c_cdata->str );
444 g_string_erase ( c_cdata, 0, -1 );
445 break;
446
01cfda98
RN
447 case tt_wpt_type:
448 vik_waypoint_set_type ( c_wp, c_cdata->str );
449 g_string_erase ( c_cdata, 0, -1 );
450 break;
451
415b0e21
RN
452 case tt_wpt_url:
453 vik_waypoint_set_url ( c_wp, c_cdata->str );
454 g_string_erase ( c_cdata, 0, -1 );
455 break;
456
8c4f1350
EB
457 case tt_wpt_link:
458 vik_waypoint_set_image ( c_wp, c_cdata->str );
459 g_string_erase ( c_cdata, 0, -1 );
460 break;
461
ea3933fc 462 case tt_wpt_sym: {
fffcc269 463 vik_waypoint_set_symbol ( c_wp, c_cdata->str );
42c2ac3e
AF
464 g_string_erase ( c_cdata, 0, -1 );
465 break;
ea3933fc 466 }
42c2ac3e 467
8c4f1350 468 case tt_trk_desc:
6b2f262e
RN
469 vik_track_set_description ( c_tr, c_cdata->str );
470 g_string_erase ( c_cdata, 0, -1 );
471 break;
472
be455472
RN
473 case tt_trk_src:
474 vik_track_set_source ( c_tr, c_cdata->str );
475 g_string_erase ( c_cdata, 0, -1 );
476 break;
477
01cfda98
RN
478 case tt_trk_type:
479 vik_track_set_type ( c_tr, c_cdata->str );
480 g_string_erase ( c_cdata, 0, -1 );
481 break;
482
6b2f262e 483 case tt_trk_cmt:
8c4f1350
EB
484 vik_track_set_comment ( c_tr, c_cdata->str );
485 g_string_erase ( c_cdata, 0, -1 );
486 break;
487
f2ef466d
RN
488 case tt_wpt_time:
489 if ( g_time_val_from_iso8601(c_cdata->str, &wp_time) ) {
490 c_wp->timestamp = wp_time.tv_sec;
491 c_wp->has_timestamp = TRUE;
492 }
493 g_string_erase ( c_cdata, 0, -1 );
494 break;
495
b45865b4
RN
496 case tt_trk_trkseg_trkpt_name:
497 vik_trackpoint_set_name ( c_tp, c_cdata->str );
498 g_string_erase ( c_cdata, 0, -1 );
499 break;
500
8c4f1350 501 case tt_trk_trkseg_trkpt_time:
dd81e762
GB
502 if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) {
503 c_tp->timestamp = tp_time.tv_sec;
8c4f1350
EB
504 c_tp->has_timestamp = TRUE;
505 }
506 g_string_erase ( c_cdata, 0, -1 );
bf35388d 507 break;
8c4f1350 508
a2817d3c 509 case tt_trk_trkseg_trkpt_course:
d1cd12c0 510 c_tp->course = g_ascii_strtod ( c_cdata->str, NULL );
a2817d3c
QT
511 g_string_erase ( c_cdata, 0, -1 );
512 break;
513
514 case tt_trk_trkseg_trkpt_speed:
d1cd12c0 515 c_tp->speed = g_ascii_strtod ( c_cdata->str, NULL );
a2817d3c
QT
516 g_string_erase ( c_cdata, 0, -1 );
517 break;
518
519 case tt_trk_trkseg_trkpt_fix:
a2817d3c
QT
520 if (!strcmp("2d", c_cdata->str))
521 c_tp->fix_mode = VIK_GPS_MODE_2D;
522 else if (!strcmp("3d", c_cdata->str))
523 c_tp->fix_mode = VIK_GPS_MODE_3D;
e29aad19
RN
524 else if (!strcmp("dgps", c_cdata->str))
525 c_tp->fix_mode = VIK_GPS_MODE_DGPS;
526 else if (!strcmp("pps", c_cdata->str))
527 c_tp->fix_mode = VIK_GPS_MODE_PPS;
528 else
a2817d3c
QT
529 c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN;
530 g_string_erase ( c_cdata, 0, -1 );
531 break;
532
533 case tt_trk_trkseg_trkpt_sat:
a2817d3c
QT
534 c_tp->nsats = atoi ( c_cdata->str );
535 g_string_erase ( c_cdata, 0, -1 );
536 break;
537
4a42b254 538 case tt_trk_trkseg_trkpt_hdop:
4a42b254
T
539 c_tp->hdop = g_strtod ( c_cdata->str, NULL );
540 g_string_erase ( c_cdata, 0, -1 );
541 break;
542
543 case tt_trk_trkseg_trkpt_vdop:
4a42b254
T
544 c_tp->vdop = g_strtod ( c_cdata->str, NULL );
545 g_string_erase ( c_cdata, 0, -1 );
546 break;
547
548 case tt_trk_trkseg_trkpt_pdop:
4a42b254
T
549 c_tp->pdop = g_strtod ( c_cdata->str, NULL );
550 g_string_erase ( c_cdata, 0, -1 );
551 break;
552
8c4f1350
EB
553 default: break;
554 }
555
556 current_tag = get_tag ( xpath->str );
557}
558
559static void gpx_cdata(void *dta, const XML_Char *s, int len)
560{
561 switch ( current_tag ) {
6ba42f1e
RN
562 case tt_gpx_name:
563 case tt_gpx_author:
564 case tt_gpx_desc:
565 case tt_gpx_keywords:
566 case tt_gpx_time:
8c4f1350
EB
567 case tt_wpt_name:
568 case tt_trk_name:
569 case tt_wpt_ele:
570 case tt_trk_trkseg_trkpt_ele:
6b2f262e 571 case tt_wpt_cmt:
8c4f1350 572 case tt_wpt_desc:
be455472 573 case tt_wpt_src:
01cfda98 574 case tt_wpt_type:
42c2ac3e 575 case tt_wpt_sym:
415b0e21 576 case tt_wpt_url:
8c4f1350 577 case tt_wpt_link:
6b2f262e 578 case tt_trk_cmt:
8c4f1350 579 case tt_trk_desc:
be455472 580 case tt_trk_src:
01cfda98 581 case tt_trk_type:
8c4f1350 582 case tt_trk_trkseg_trkpt_time:
f2ef466d 583 case tt_wpt_time:
b45865b4 584 case tt_trk_trkseg_trkpt_name:
a2817d3c
QT
585 case tt_trk_trkseg_trkpt_course:
586 case tt_trk_trkseg_trkpt_speed:
587 case tt_trk_trkseg_trkpt_fix:
588 case tt_trk_trkseg_trkpt_sat:
4a42b254
T
589 case tt_trk_trkseg_trkpt_hdop:
590 case tt_trk_trkseg_trkpt_vdop:
591 case tt_trk_trkseg_trkpt_pdop:
bf35388d 592 case tt_waypoint_name: /* .loc name is really description. */
8c4f1350
EB
593 g_string_append_len ( c_cdata, s, len );
594 break;
595
596 default: break; /* ignore cdata from other things */
597 }
598}
599
600// make like a "stack" of tag names
601// like gpspoint's separated like /gpx/wpt/whatever
602
efe9a9c3 603gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
8c4f1350
EB
604 XML_Parser parser = XML_ParserCreate(NULL);
605 int done=0, len;
efe9a9c3 606 enum XML_Status status = XML_STATUS_ERROR;
8c4f1350
EB
607
608 XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
609 XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
610 XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata);
611
612 gchar buf[4096];
613
bf35388d 614 g_assert ( f != NULL && vtl != NULL );
8c4f1350
EB
615
616 xpath = g_string_new ( "" );
617 c_cdata = g_string_new ( "" );
618
43e2df3e
RN
619 unnamed_waypoints = 1;
620 unnamed_tracks = 1;
621 unnamed_routes = 1;
8c4f1350
EB
622
623 while (!done) {
624 len = fread(buf, 1, sizeof(buf)-7, f);
625 done = feof(f) || !len;
efe9a9c3 626 status = XML_Parse(parser, buf, len, done);
8c4f1350
EB
627 }
628
6278ccc6 629 XML_ParserFree (parser);
8c4f1350
EB
630 g_string_free ( xpath, TRUE );
631 g_string_free ( c_cdata, TRUE );
efe9a9c3
GB
632
633 return status != XML_STATUS_ERROR;
8c4f1350 634}
561e6ad0
EB
635
636/**** entitize from GPSBabel ****/
637typedef struct {
638 const char * text;
639 const char * entity;
640 int not_html;
641} entity_types;
642
643static
644entity_types stdentities[] = {
645 { "&", "&amp;", 0 },
646 { "'", "&apos;", 1 },
647 { "<", "&lt;", 0 },
648 { ">", "&gt;", 0 },
649 { "\"", "&quot;", 0 },
650 { NULL, NULL, 0 }
651};
652
653void utf8_to_int( const char *cp, int *bytes, int *value )
654{
655 if ( (*cp & 0xe0) == 0xc0 ) {
656 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
657 *bytes = 2;
658 *value = ((*cp & 0x1f) << 6) |
659 (*(cp+1) & 0x3f);
660 }
661 else if ( (*cp & 0xf0) == 0xe0 ) {
662 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
663 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
664 *bytes = 3;
665 *value = ((*cp & 0x0f) << 12) |
666 ((*(cp+1) & 0x3f) << 6) |
667 (*(cp+2) & 0x3f);
668 }
669 else if ( (*cp & 0xf8) == 0xf0 ) {
670 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
671 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
672 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
673 *bytes = 4;
674 *value = ((*cp & 0x07) << 18) |
675 ((*(cp+1) & 0x3f) << 12) |
676 ((*(cp+2) & 0x3f) << 6) |
677 (*(cp+3) & 0x3f);
678 }
679 else if ( (*cp & 0xfc) == 0xf8 ) {
680 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
681 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
682 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
683 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
684 *bytes = 5;
685 *value = ((*cp & 0x03) << 24) |
686 ((*(cp+1) & 0x3f) << 18) |
687 ((*(cp+2) & 0x3f) << 12) |
688 ((*(cp+3) & 0x3f) << 6) |
689 (*(cp+4) & 0x3f);
690 }
691 else if ( (*cp & 0xfe) == 0xfc ) {
692 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
693 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
694 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
695 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
696 if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault;
697 *bytes = 6;
698 *value = ((*cp & 0x01) << 30) |
699 ((*(cp+1) & 0x3f) << 24) |
700 ((*(cp+2) & 0x3f) << 18) |
701 ((*(cp+3) & 0x3f) << 12) |
702 ((*(cp+4) & 0x3f) << 6) |
703 (*(cp+5) & 0x3f);
704 }
705 else {
706dodefault:
707 *bytes = 1;
708 *value = (unsigned char)*cp;
709 }
710}
711
712static
713char *
714entitize(const char * str)
715{
716 int elen, ecount, nsecount;
717 entity_types *ep;
718 const char * cp;
719 char * p, * tmp, * xstr;
720
721 char tmpsub[20];
722 int bytes = 0;
723 int value = 0;
724 ep = stdentities;
725 elen = ecount = nsecount = 0;
726
727 /* figure # of entity replacements and additional size. */
728 while (ep->text) {
729 cp = str;
730 while ((cp = strstr(cp, ep->text)) != NULL) {
731 elen += strlen(ep->entity) - strlen(ep->text);
732 ecount++;
733 cp += strlen(ep->text);
734 }
735 ep++;
736 }
737
738 /* figure the same for other than standard entities (i.e. anything
739 * that isn't in the range U+0000 to U+007F */
740 for ( cp = str; *cp; cp++ ) {
741 if ( *cp & 0x80 ) {
742
743 utf8_to_int( cp, &bytes, &value );
744 cp += bytes-1;
745 elen += sprintf( tmpsub, "&#x%x;", value ) - bytes;
746 nsecount++;
747 }
748 }
749
750 /* enough space for the whole string plus entity replacements, if any */
751 tmp = g_malloc((strlen(str) + elen + 1));
752 strcpy(tmp, str);
753
754 /* no entity replacements */
755 if (ecount == 0 && nsecount == 0)
756 return (tmp);
757
758 if ( ecount != 0 ) {
759 for (ep = stdentities; ep->text; ep++) {
760 p = tmp;
761 while ((p = strstr(p, ep->text)) != NULL) {
762 elen = strlen(ep->entity);
763
764 xstr = g_strdup(p + strlen(ep->text));
765
766 strcpy(p, ep->entity);
767 strcpy(p + elen, xstr);
768
769 g_free(xstr);
770
771 p += elen;
772 }
773 }
774 }
775
776 if ( nsecount != 0 ) {
777 p = tmp;
778 while (*p) {
779 if ( *p & 0x80 ) {
780 utf8_to_int( p, &bytes, &value );
781 if ( p[bytes] ) {
782 xstr = g_strdup( p + bytes );
783 }
784 else {
785 xstr = NULL;
786 }
787 sprintf( p, "&#x%x;", value );
788 p = p+strlen(p);
789 if ( xstr ) {
790 strcpy( p, xstr );
791 g_free(xstr);
792 }
793 }
794 else {
795 p++;
796 }
797 }
798 }
799 return (tmp);
800}
801/**** end GPSBabel code ****/
802
803/* export GPX */
8904c540 804
c9570f86 805static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
561e6ad0 806{
208d2084
RN
807 // Don't write invisible waypoints when specified
808 if (context->options && !context->options->hidden && !wp->visible)
809 return;
810
0b72c435 811 FILE *f = context->file;
e798750d 812 struct LatLon ll;
e778b260
RN
813 gchar s_lat[COORDS_STR_BUFFER_SIZE];
814 gchar s_lon[COORDS_STR_BUFFER_SIZE];
561e6ad0
EB
815 gchar *tmp;
816 vik_coord_to_latlon ( &(wp->coord), &ll );
e778b260
RN
817 a_coords_dtostr_buffer ( ll.lat, s_lat );
818 a_coords_dtostr_buffer ( ll.lon, s_lon );
208d2084
RN
819 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
820 // luckily most other GPX processing software ignores things they don't understand
8904c540
GB
821 fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
822 s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );
561e6ad0 823
c9570f86
RN
824 // Sanity clause
825 if ( wp->name )
826 tmp = entitize ( wp->name );
827 else
828 tmp = g_strdup ("waypoint");
829
561e6ad0
EB
830 fprintf ( f, " <name>%s</name>\n", tmp );
831 g_free ( tmp);
832
833 if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
8904c540 834 {
e778b260
RN
835 gchar s_alt[COORDS_STR_BUFFER_SIZE];
836 a_coords_dtostr_buffer ( wp->altitude, s_alt );
837 fprintf ( f, " <ele>%s</ele>\n", s_alt );
8904c540 838 }
f2ef466d
RN
839
840 if ( wp->has_timestamp ) {
841 GTimeVal timestamp;
842 timestamp.tv_sec = wp->timestamp;
843 timestamp.tv_usec = 0;
844
845 gchar *time_iso8601 = g_time_val_to_iso8601 ( &timestamp );
846 if ( time_iso8601 != NULL )
847 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
848 g_free ( time_iso8601 );
849 }
850
561e6ad0
EB
851 if ( wp->comment )
852 {
853 tmp = entitize(wp->comment);
6b2f262e
RN
854 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
855 g_free ( tmp );
856 }
857 if ( wp->description )
858 {
859 tmp = entitize(wp->description);
561e6ad0
EB
860 fprintf ( f, " <desc>%s</desc>\n", tmp );
861 g_free ( tmp );
862 }
be455472
RN
863 if ( wp->source )
864 {
865 tmp = entitize(wp->source);
866 fprintf ( f, " <src>%s</src>\n", tmp );
867 g_free ( tmp );
868 }
01cfda98
RN
869 if ( wp->type )
870 {
871 tmp = entitize(wp->type);
872 fprintf ( f, " <type>%s</type>\n", tmp );
873 g_free ( tmp );
874 }
415b0e21
RN
875 if ( wp->url )
876 {
877 tmp = entitize(wp->url);
878 fprintf ( f, " <url>%s</url>\n", tmp );
879 g_free ( tmp );
880 }
561e6ad0
EB
881 if ( wp->image )
882 {
883 tmp = entitize(wp->image);
884 fprintf ( f, " <link>%s</link>\n", tmp );
885 g_free ( tmp );
886 }
42c2ac3e
AF
887 if ( wp->symbol )
888 {
889 tmp = entitize(wp->symbol);
72f067ad
RN
890 if ( a_vik_gpx_export_wpt_sym_name ( ) ) {
891 // Lowercase the symbol name
892 gchar *tmp2 = g_utf8_strdown ( tmp, -1 );
893 fprintf ( f, " <sym>%s</sym>\n", tmp2 );
894 g_free ( tmp2 );
895 }
896 else
897 fprintf ( f, " <sym>%s</sym>\n", tmp);
42c2ac3e
AF
898 g_free ( tmp );
899 }
900
561e6ad0
EB
901 fprintf ( f, "</wpt>\n" );
902}
903
0b72c435 904static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context )
561e6ad0 905{
0b72c435 906 FILE *f = context->file;
e798750d 907 struct LatLon ll;
e778b260
RN
908 gchar s_lat[COORDS_STR_BUFFER_SIZE];
909 gchar s_lon[COORDS_STR_BUFFER_SIZE];
910 gchar s_alt[COORDS_STR_BUFFER_SIZE];
911 gchar s_dop[COORDS_STR_BUFFER_SIZE];
dd81e762 912 gchar *time_iso8601;
561e6ad0
EB
913 vik_coord_to_latlon ( &(tp->coord), &ll );
914
0d2b891f
RN
915 // No such thing as a rteseg! So make sure we don't put them in
916 if ( context->options && !context->options->is_route && tp->newsegment )
561e6ad0
EB
917 fprintf ( f, " </trkseg>\n <trkseg>\n" );
918
e778b260
RN
919 a_coords_dtostr_buffer ( ll.lat, s_lat );
920 a_coords_dtostr_buffer ( ll.lon, s_lon );
0d2b891f 921 fprintf ( f, " <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon );
561e6ad0 922
b45865b4
RN
923 if (tp->name) {
924 gchar *s_name = entitize(tp->name);
925 fprintf ( f, " <name>%s</name>\n", s_name );
926 g_free(s_name);
927 }
928
561e6ad0 929 if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
8904c540 930 {
e778b260 931 a_coords_dtostr_buffer ( tp->altitude, s_alt );
b537cdba 932 fprintf ( f, " <ele>%s</ele>\n", s_alt );
8904c540 933 }
0b72c435 934 else if ( context->options != NULL && context->options->force_ele )
36179a2e 935 {
b537cdba 936 fprintf ( f, " <ele>0</ele>\n" );
36179a2e 937 }
36179a2e 938
dd81e762 939 time_iso8601 = NULL;
561e6ad0 940 if ( tp->has_timestamp ) {
dd81e762
GB
941 GTimeVal timestamp;
942 timestamp.tv_sec = tp->timestamp;
61f37ac9 943 timestamp.tv_usec = 0;
dd81e762
GB
944
945 time_iso8601 = g_time_val_to_iso8601 ( &timestamp );
561e6ad0 946 }
0b72c435 947 else if ( context->options != NULL && context->options->force_time )
36179a2e 948 {
dd81e762
GB
949 GTimeVal current;
950 g_get_current_time ( &current );
36179a2e 951
dd81e762 952 time_iso8601 = g_time_val_to_iso8601 ( &current );
36179a2e 953 }
dd81e762
GB
954 if ( time_iso8601 != NULL )
955 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
956 g_free(time_iso8601);
957 time_iso8601 = NULL;
36179a2e 958
ad54cd32 959 if (!isnan(tp->course)) {
e778b260
RN
960 gchar s_course[COORDS_STR_BUFFER_SIZE];
961 a_coords_dtostr_buffer ( tp->course, s_course );
ad54cd32 962 fprintf ( f, " <course>%s</course>\n", s_course );
a2817d3c 963 }
ad54cd32 964 if (!isnan(tp->speed)) {
e778b260
RN
965 gchar s_speed[COORDS_STR_BUFFER_SIZE];
966 a_coords_dtostr_buffer ( tp->speed, s_speed );
ad54cd32 967 fprintf ( f, " <speed>%s</speed>\n", s_speed );
ad54cd32
T
968 }
969 if (tp->fix_mode == VIK_GPS_MODE_2D)
970 fprintf ( f, " <fix>2d</fix>\n");
971 if (tp->fix_mode == VIK_GPS_MODE_3D)
972 fprintf ( f, " <fix>3d</fix>\n");
e29aad19
RN
973 if (tp->fix_mode == VIK_GPS_MODE_DGPS)
974 fprintf ( f, " <fix>dgps</fix>\n");
975 if (tp->fix_mode == VIK_GPS_MODE_PPS)
976 fprintf ( f, " <fix>pps</fix>\n");
ad54cd32
T
977 if (tp->nsats > 0)
978 fprintf ( f, " <sat>%d</sat>\n", tp->nsats );
a2817d3c 979
e778b260
RN
980 if ( tp->hdop != VIK_DEFAULT_DOP ) {
981 a_coords_dtostr_buffer ( tp->hdop, s_dop );
4a42b254 982 fprintf ( f, " <hdop>%s</hdop>\n", s_dop );
4a42b254 983 }
4a42b254 984
e778b260
RN
985 if ( tp->vdop != VIK_DEFAULT_DOP ) {
986 a_coords_dtostr_buffer ( tp->vdop, s_dop );
987 fprintf ( f, " <vdop>%s</vdop>\n", s_dop );
4a42b254 988 }
e778b260
RN
989
990 if ( tp->pdop != VIK_DEFAULT_DOP ) {
991 a_coords_dtostr_buffer ( tp->pdop, s_dop );
4a42b254 992 fprintf ( f, " <pdop>%s</pdop>\n", s_dop );
e778b260 993 }
4a42b254 994
0d2b891f 995 fprintf ( f, " </%spt>\n", (context->options && context->options->is_route) ? "rte" : "trk" );
561e6ad0
EB
996}
997
998
ce4bd1cf 999static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
561e6ad0 1000{
208d2084
RN
1001 // Don't write invisible tracks when specified
1002 if (context->options && !context->options->hidden && !t->visible)
1003 return;
1004
0b72c435 1005 FILE *f = context->file;
561e6ad0 1006 gchar *tmp;
494eb388 1007 gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */
561e6ad0 1008
ce4bd1cf
RN
1009 // Sanity clause
1010 if ( t->name )
1011 tmp = entitize ( t->name );
1012 else
1013 tmp = g_strdup ("track");
1014
208d2084
RN
1015 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
1016 // luckily most other GPX processing software ignores things they don't understand
0d2b891f
RN
1017 fprintf ( f, "<%s%s>\n <name>%s</name>\n",
1018 t->is_route ? "rte" : "trk",
1019 t->visible ? "" : " hidden=\"hidden\"",
1020 tmp );
561e6ad0
EB
1021 g_free ( tmp );
1022
1023 if ( t->comment )
1024 {
1025 tmp = entitize ( t->comment );
6b2f262e
RN
1026 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
1027 g_free ( tmp );
1028 }
1029
1030 if ( t->description )
1031 {
1032 tmp = entitize ( t->description );
561e6ad0
EB
1033 fprintf ( f, " <desc>%s</desc>\n", tmp );
1034 g_free ( tmp );
1035 }
0d2b891f 1036
be455472
RN
1037 if ( t->source )
1038 {
1039 tmp = entitize ( t->source );
1040 fprintf ( f, " <src>%s</src>\n", tmp );
1041 g_free ( tmp );
1042 }
1043
01cfda98
RN
1044 if ( t->type )
1045 {
1046 tmp = entitize ( t->type );
1047 fprintf ( f, " <type>%s</type>\n", tmp );
1048 g_free ( tmp );
1049 }
1050
0d2b891f
RN
1051 /* No such thing as a rteseg! */
1052 if ( !t->is_route )
1053 fprintf ( f, " <trkseg>\n" );
561e6ad0
EB
1054
1055 if ( t->trackpoints && t->trackpoints->data ) {
1056 first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment;
1057 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */
0b72c435 1058 g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context );
561e6ad0 1059 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
494eb388 1060 }
561e6ad0 1061
0d2b891f
RN
1062 /* NB apparently no such thing as a rteseg! */
1063 if (!t->is_route)
1064 fprintf ( f, " </trkseg>\n");
1065
1066 fprintf ( f, "</%s>\n", t->is_route ? "rte" : "trk" );
561e6ad0
EB
1067}
1068
5092de80 1069static void gpx_write_header( FILE *f )
561e6ad0
EB
1070{
1071 fprintf(f, "<?xml version=\"1.0\"?>\n"
1072 "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
1073 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
1074 "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
1075 "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
5092de80
GB
1076}
1077
1078static void gpx_write_footer( FILE *f )
1079{
561e6ad0 1080 fprintf(f, "</gpx>\n");
5092de80 1081}
561e6ad0 1082
c9570f86 1083static int gpx_waypoint_compare(const void *x, const void *y)
eab858a1 1084{
c9570f86
RN
1085 VikWaypoint *a = (VikWaypoint *)x;
1086 VikWaypoint *b = (VikWaypoint *)y;
eab858a1
EB
1087 return strcmp(a->name,b->name);
1088}
1089
0d2b891f
RN
1090static int gpx_track_compare_name(const void *x, const void *y)
1091{
1092 VikTrack *a = (VikTrack *)x;
1093 VikTrack *b = (VikTrack *)y;
1094 return strcmp(a->name,b->name);
1095}
1096
208d2084 1097void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
0b72c435
OK
1098{
1099 GpxWritingContext context = { options, f };
eab858a1 1100
5092de80 1101 gpx_write_header ( f );
eab858a1 1102
6ba42f1e
RN
1103 gchar *tmp;
1104 const gchar *name = vik_layer_get_name(VIK_LAYER(vtl));
1105 if ( name ) {
1106 tmp = entitize ( name );
1107 fprintf ( f, " <name>%s</name>\n", tmp );
1108 g_free ( tmp );
1109 }
1110
1111 VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
1112 if ( md ) {
ae26a1d4 1113 if ( md->author && strlen(md->author) > 0 ) {
6ba42f1e
RN
1114 tmp = entitize ( md->author );
1115 fprintf ( f, " <author>%s</author>\n", tmp );
1116 g_free ( tmp );
1117 }
ae26a1d4 1118 if ( md->description && strlen(md->description) > 0) {
6ba42f1e
RN
1119 tmp = entitize ( md->description );
1120 fprintf ( f, " <desc>%s</desc>\n", tmp );
1121 g_free ( tmp );
1122 }
1123 if ( md->timestamp ) {
1124 tmp = entitize ( md->timestamp );
1125 fprintf ( f, " <time>%s</time>\n", tmp );
1126 g_free ( tmp );
1127 }
ae26a1d4 1128 if ( md->keywords && strlen(md->keywords) > 0) {
6ba42f1e
RN
1129 tmp = entitize ( md->keywords );
1130 fprintf ( f, " <keywords>%s</keywords>\n", tmp );
1131 g_free ( tmp );
1132 }
1133 }
1134
978cd696
RN
1135 if ( vik_trw_layer_get_waypoints_visibility(vtl) || (options && options->hidden) ) {
1136 // gather waypoints in a list, then sort
1137 GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
1138 gl = g_list_sort ( gl, gpx_waypoint_compare );
1139
1140 for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
1141 gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
1142 }
1143 g_list_free ( gl );
c9570f86 1144 }
eab858a1 1145
978cd696
RN
1146 GList *gl = NULL;
1147 if ( vik_trw_layer_get_tracks_visibility(vtl) || (options && options->hidden) ) {
1148 //gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
1149 // Forming the list manually seems to produce one that is more likely to be nearer to the creation order
1150 gpointer key, value;
1151 GHashTableIter ght_iter;
1152 g_hash_table_iter_init ( &ght_iter, vik_trw_layer_get_tracks ( vtl ) );
1153 while ( g_hash_table_iter_next (&ght_iter, &key, &value) ) {
1154 gl = g_list_prepend ( gl ,value );
1155 }
1156 gl = g_list_reverse ( gl );
d13f1a57 1157
978cd696
RN
1158 // Sort method determined by preference
1159 if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
1160 gl = g_list_sort ( gl, vik_track_compare_timestamp );
1161 else if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_ALPHA )
1162 gl = g_list_sort ( gl, gpx_track_compare_name );
b02cd3a3 1163 }
d13f1a57 1164
978cd696 1165 GList *glrte = NULL;
0d2b891f 1166 // Routes sorted by name
5466f124 1167 if ( vik_trw_layer_get_routes_visibility(vtl) || (options && options->hidden) ) {
978cd696
RN
1168 glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
1169 glrte = g_list_sort ( glrte, gpx_track_compare_name );
1170 }
ce4bd1cf 1171
0d2b891f
RN
1172 // g_list_concat doesn't copy memory properly
1173 // so process each list separately
1174
1175 GpxWritingContext context_tmp = context;
63959706 1176 GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE, FALSE };
0d2b891f
RN
1177 // Force trackpoints on tracks
1178 if ( !context.options )
1179 context_tmp.options = &opt_tmp;
1180 context_tmp.options->is_route = FALSE;
1181
1182 // Loop around each list and write each one
978cd696 1183 for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
0d2b891f
RN
1184 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
1185 }
1186
1187 // Routes (to get routepoints)
1188 context_tmp.options->is_route = TRUE;
978cd696 1189 for (GList *iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
0d2b891f 1190 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
e3449376 1191 }
ce4bd1cf
RN
1192
1193 g_list_free ( gl );
0d2b891f 1194 g_list_free ( glrte );
ce4bd1cf 1195
5092de80
GB
1196 gpx_write_footer ( f );
1197}
561e6ad0 1198
208d2084 1199void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
0b72c435
OK
1200{
1201 GpxWritingContext context = {options, f};
5092de80 1202 gpx_write_header ( f );
ce4bd1cf 1203 gpx_write_track ( trk, &context );
5092de80 1204 gpx_write_footer ( f );
561e6ad0 1205}
12ed2b58
RN
1206
1207/**
1208 * Common write of a temporary GPX file
1209 */
1210static gchar* write_tmp_file ( VikTrwLayer *vtl, VikTrack *trk, GpxWritingOptions *options )
1211{
1212 gchar *tmp_filename = NULL;
1213 GError *error = NULL;
1214 // Opening temporary file
1215 int fd = g_file_open_tmp("viking_XXXXXX.gpx", &tmp_filename, &error);
1216 if (fd < 0) {
1217 g_warning ( _("failed to open temporary file: %s"), error->message );
1218 g_clear_error ( &error );
1219 return NULL;
1220 }
1221 g_debug ("%s: temporary file = %s", __FUNCTION__, tmp_filename);
1222
1223 FILE *ff = fdopen (fd, "w");
1224
1225 if ( trk )
1226 a_gpx_write_track_file ( trk, ff, options );
1227 else
1228 a_gpx_write_file ( vtl, ff, options );
1229
1230 fclose (ff);
1231
1232 return tmp_filename;
1233}
1234
1235/*
1236 * a_gpx_write_tmp_file:
1237 * @vtl: The #VikTrwLayer to write
1238 * @options: Possible ways of writing the file data (can be NULL)
1239 *
1240 * Returns: The name of newly created temporary GPX file
1241 * This file should be removed once used and the string freed.
1242 * If NULL then the process failed.
1243 */
1244gchar* a_gpx_write_tmp_file ( VikTrwLayer *vtl, GpxWritingOptions *options )
1245{
1246 return write_tmp_file ( vtl, NULL, options );
1247}
1248
1249/*
1250 * a_gpx_write_track_tmp_file:
1251 * @trk: The #VikTrack to write
1252 * @options: Possible ways of writing the file data (can be NULL)
1253 *
1254 * Returns: The name of newly created temporary GPX file
1255 * This file should be removed once used and the string freed.
1256 * If NULL then the process failed.
1257 */
1258gchar* a_gpx_write_track_tmp_file ( VikTrack *trk, GpxWritingOptions *options )
1259{
1260 return write_tmp_file ( NULL, trk, options );
1261}