]> git.street.me.uk Git - andy/viking.git/blame - src/gpx.c
Add simple user feedback about the image generation process.
[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>
8c4f1350 8 *
bf35388d
EB
9 * Some of the code adapted from GPSBabel 1.2.7
10 * http://gpsbabel.sf.net/
11 * Copyright (C) 2002, 2003, 2004, 2005 Robert Lipe, robertlipe@usa.net
12 *
8c4f1350
EB
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 */
8c00358d
GB
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
8c4f1350
EB
31
32#define _XOPEN_SOURCE /* glibc2 needs this */
33
0b72c435 34#include "gpx.h"
8c4f1350
EB
35#include "viking.h"
36#include <expat.h>
8c00358d 37#ifdef HAVE_STRING_H
8c4f1350 38#include <string.h>
8c00358d 39#endif
8904c540 40#include <glib.h>
8c00358d 41#ifdef HAVE_MATH_H
8904c540 42#include <math.h>
8c00358d 43#endif
e3449376 44#include <time.h>
8c4f1350 45
8c4f1350
EB
46typedef enum {
47 tt_unknown = 0,
48
49 tt_gpx,
50
51 tt_wpt,
52 tt_wpt_desc,
53 tt_wpt_name,
54 tt_wpt_ele,
42c2ac3e 55 tt_wpt_sym,
8c4f1350
EB
56 tt_wpt_link, /* New in GPX 1.1 */
57
58 tt_trk,
59 tt_trk_desc,
60 tt_trk_name,
61
62 tt_trk_trkseg,
63 tt_trk_trkseg_trkpt,
64 tt_trk_trkseg_trkpt_ele,
65 tt_trk_trkseg_trkpt_time,
a2817d3c
QT
66 /* extended */
67 tt_trk_trkseg_trkpt_course,
68 tt_trk_trkseg_trkpt_speed,
69 tt_trk_trkseg_trkpt_fix,
70 tt_trk_trkseg_trkpt_sat,
bf35388d 71
4a42b254
T
72 tt_trk_trkseg_trkpt_hdop,
73 tt_trk_trkseg_trkpt_vdop,
74 tt_trk_trkseg_trkpt_pdop,
75
bf35388d
EB
76 tt_waypoint,
77 tt_waypoint_coord,
78 tt_waypoint_name,
8c4f1350
EB
79} tag_type;
80
81typedef struct tag_mapping {
82 tag_type tag_type; /* enum from above for this tag */
83 const char *tag_name; /* xpath-ish tag name */
84} tag_mapping;
85
0b72c435
OK
86typedef struct {
87 GpxWritingOptions *options;
88 FILE *file;
89} GpxWritingContext;
90
8c4f1350
EB
91/*
92 * xpath(ish) mappings between full tag paths and internal identifers.
93 * These appear in the order they appear in the GPX specification.
94 * If it's not a tag we explictly handle, it doesn't go here.
95 */
96
97tag_mapping tag_path_map[] = {
8c4f1350
EB
98
99 { tt_wpt, "/gpx/wpt" },
bf35388d
EB
100
101 { tt_waypoint, "/loc/waypoint" },
102 { tt_waypoint_coord, "/loc/waypoint/coord" },
103 { tt_waypoint_name, "/loc/waypoint/name" },
104
8c4f1350
EB
105 { tt_wpt_ele, "/gpx/wpt/ele" },
106 { tt_wpt_name, "/gpx/wpt/name" },
107 { tt_wpt_desc, "/gpx/wpt/desc" },
42c2ac3e 108 { tt_wpt_sym, "/gpx/wpt/sym" },
08b251ec 109 { tt_wpt_sym, "/loc/waypoint/type" },
8c4f1350
EB
110 { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */
111
112 { tt_trk, "/gpx/trk" },
561e6ad0 113 { tt_trk, "/gpx/rte" },
8c4f1350
EB
114 { tt_trk_name, "/gpx/trk/name" },
115 { tt_trk_desc, "/gpx/trk/desc" },
116 { tt_trk_trkseg, "/gpx/trk/trkseg" },
8c4f1350 117 { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
561e6ad0 118 { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
8c4f1350
EB
119 { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
120 { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
a2817d3c
QT
121 /* extended */
122 { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" },
123 { tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" },
124 { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" },
125 { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" },
8c4f1350 126
4a42b254
T
127 { tt_trk_trkseg_trkpt_hdop, "/gpx/trk/trkseg/trkpt/hdop" },
128 { tt_trk_trkseg_trkpt_vdop, "/gpx/trk/trkseg/trkpt/vdop" },
129 { tt_trk_trkseg_trkpt_pdop, "/gpx/trk/trkseg/trkpt/pdop" },
8c4f1350
EB
130 {0}
131};
132
133static tag_type get_tag(const char *t)
134{
135 tag_mapping *tm;
136 for (tm = tag_path_map; tm->tag_type != 0; tm++)
137 if (0 == strcmp(tm->tag_name, t))
138 return tm->tag_type;
139 return tt_unknown;
140}
141
142/******************************************/
143
144tag_type current_tag = tt_unknown;
145GString *xpath = NULL;
146GString *c_cdata = NULL;
147
148/* current ("c_") objects */
149VikTrackpoint *c_tp = NULL;
150VikWaypoint *c_wp = NULL;
151VikTrack *c_tr = NULL;
152
153gchar *c_wp_name = NULL;
154gchar *c_tr_name = NULL;
155
156/* temporary things so we don't have to create them lots of times */
157const gchar *c_slat, *c_slon;
158struct LatLon c_ll;
159
160/* specialty flags / etc */
161gboolean f_tr_newseg;
162guint unnamed_waypoints = 0;
163guint unnamed_tracks = 0;
164
165
166static const char *get_attr ( const char **attr, const char *key )
167{
168 while ( *attr ) {
169 if ( strcmp(*attr,key) == 0 )
170 return *(attr + 1);
171 attr += 2;
172 }
173 return NULL;
174}
175
176static gboolean set_c_ll ( const char **attr )
177{
178 if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) {
d1cd12c0
T
179 c_ll.lat = g_ascii_strtod(c_slat, NULL);
180 c_ll.lon = g_ascii_strtod(c_slon, NULL);
8c4f1350
EB
181 return TRUE;
182 }
183 return FALSE;
184}
185
186static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
187{
bf35388d
EB
188 static const gchar *tmp;
189
8c4f1350
EB
190 g_string_append_c ( xpath, '/' );
191 g_string_append ( xpath, el );
192 current_tag = get_tag ( xpath->str );
193
194 switch ( current_tag ) {
195
196 case tt_wpt:
197 if ( set_c_ll( attr ) ) {
198 c_wp = vik_waypoint_new ();
0b9f279d
RN
199 c_wp->visible = TRUE;
200 if ( get_attr ( attr, "hidden" ) )
201 c_wp->visible = FALSE;
8c4f1350
EB
202
203 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
204 }
205 break;
206
207 case tt_trk:
208 c_tr = vik_track_new ();
0b9f279d
RN
209 c_tr->visible = TRUE;
210 if ( get_attr ( attr, "hidden" ) )
211 c_tr->visible = FALSE;
8c4f1350
EB
212 break;
213
214 case tt_trk_trkseg:
215 f_tr_newseg = TRUE;
216 break;
217
218 case tt_trk_trkseg_trkpt:
219 if ( set_c_ll( attr ) ) {
220 c_tp = vik_trackpoint_new ();
8c4f1350
EB
221 vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
222 if ( f_tr_newseg ) {
223 c_tp->newsegment = TRUE;
224 f_tr_newseg = FALSE;
225 }
226 c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp );
227 }
228 break;
229
230 case tt_trk_trkseg_trkpt_ele:
231 case tt_trk_trkseg_trkpt_time:
232 case tt_wpt_desc:
233 case tt_wpt_name:
234 case tt_wpt_ele:
235 case tt_wpt_link:
236 case tt_trk_desc:
237 case tt_trk_name:
238 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */
bf35388d 239 break;
8c4f1350 240
bf35388d
EB
241 case tt_waypoint:
242 c_wp = vik_waypoint_new ();
bf35388d
EB
243 c_wp->visible = TRUE;
244 break;
245
246 case tt_waypoint_coord:
247 if ( set_c_ll( attr ) )
248 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
249 break;
250
251 case tt_waypoint_name:
252 if ( ( tmp = get_attr(attr, "id") ) ) {
253 if ( c_wp_name )
254 g_free ( c_wp_name );
255 c_wp_name = g_strdup ( tmp );
256 }
257 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */
258 break;
259
8c4f1350
EB
260 default: break;
261 }
262}
263
264static void gpx_end(VikTrwLayer *vtl, const char *el)
265{
dd81e762
GB
266 static GTimeVal tp_time;
267
8c4f1350
EB
268 g_string_truncate ( xpath, xpath->len - strlen(el) - 1 );
269
270 switch ( current_tag ) {
271
bf35388d 272 case tt_waypoint:
8c4f1350
EB
273 case tt_wpt:
274 if ( ! c_wp_name )
275 c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++);
805d282e
EB
276 vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp );
277 g_free ( c_wp_name );
8c4f1350
EB
278 c_wp = NULL;
279 c_wp_name = NULL;
280 break;
281
282 case tt_trk:
283 if ( ! c_tr_name )
a20877b7 284 c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_tracks++);
805d282e
EB
285 vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr );
286 g_free ( c_tr_name );
8c4f1350
EB
287 c_tr = NULL;
288 c_tr_name = NULL;
289 break;
290
291 case tt_wpt_name:
292 if ( c_wp_name )
293 g_free ( c_wp_name );
294 c_wp_name = g_strdup ( c_cdata->str );
295 g_string_erase ( c_cdata, 0, -1 );
296 break;
297
298 case tt_trk_name:
299 if ( c_tr_name )
300 g_free ( c_tr_name );
301 c_tr_name = g_strdup ( c_cdata->str );
302 g_string_erase ( c_cdata, 0, -1 );
303 break;
304
305 case tt_wpt_ele:
d1cd12c0 306 c_wp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
8c4f1350
EB
307 g_string_erase ( c_cdata, 0, -1 );
308 break;
309
310 case tt_trk_trkseg_trkpt_ele:
d1cd12c0 311 c_tp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
8c4f1350
EB
312 g_string_erase ( c_cdata, 0, -1 );
313 break;
314
bf35388d 315 case tt_waypoint_name: /* .loc name is really description. */
8c4f1350
EB
316 case tt_wpt_desc:
317 vik_waypoint_set_comment ( c_wp, c_cdata->str );
318 g_string_erase ( c_cdata, 0, -1 );
319 break;
320
321 case tt_wpt_link:
322 vik_waypoint_set_image ( c_wp, c_cdata->str );
323 g_string_erase ( c_cdata, 0, -1 );
324 break;
325
ea3933fc
EB
326 case tt_wpt_sym: {
327 gchar *tmp_lower = g_utf8_strdown(c_cdata->str, -1); /* for things like <type>Geocache</type> */
328 vik_waypoint_set_symbol ( c_wp, tmp_lower );
329 g_free ( tmp_lower );
42c2ac3e
AF
330 g_string_erase ( c_cdata, 0, -1 );
331 break;
ea3933fc 332 }
42c2ac3e 333
8c4f1350
EB
334 case tt_trk_desc:
335 vik_track_set_comment ( c_tr, c_cdata->str );
336 g_string_erase ( c_cdata, 0, -1 );
337 break;
338
339 case tt_trk_trkseg_trkpt_time:
dd81e762
GB
340 if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) {
341 c_tp->timestamp = tp_time.tv_sec;
8c4f1350
EB
342 c_tp->has_timestamp = TRUE;
343 }
344 g_string_erase ( c_cdata, 0, -1 );
bf35388d 345 break;
8c4f1350 346
a2817d3c 347 case tt_trk_trkseg_trkpt_course:
d1cd12c0 348 c_tp->course = g_ascii_strtod ( c_cdata->str, NULL );
a2817d3c
QT
349 g_string_erase ( c_cdata, 0, -1 );
350 break;
351
352 case tt_trk_trkseg_trkpt_speed:
d1cd12c0 353 c_tp->speed = g_ascii_strtod ( c_cdata->str, NULL );
a2817d3c
QT
354 g_string_erase ( c_cdata, 0, -1 );
355 break;
356
357 case tt_trk_trkseg_trkpt_fix:
a2817d3c
QT
358 if (!strcmp("2d", c_cdata->str))
359 c_tp->fix_mode = VIK_GPS_MODE_2D;
360 else if (!strcmp("3d", c_cdata->str))
361 c_tp->fix_mode = VIK_GPS_MODE_3D;
362 else /* TODO: more fix modes here */
363 c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN;
364 g_string_erase ( c_cdata, 0, -1 );
365 break;
366
367 case tt_trk_trkseg_trkpt_sat:
a2817d3c
QT
368 c_tp->nsats = atoi ( c_cdata->str );
369 g_string_erase ( c_cdata, 0, -1 );
370 break;
371
4a42b254 372 case tt_trk_trkseg_trkpt_hdop:
4a42b254
T
373 c_tp->hdop = g_strtod ( c_cdata->str, NULL );
374 g_string_erase ( c_cdata, 0, -1 );
375 break;
376
377 case tt_trk_trkseg_trkpt_vdop:
4a42b254
T
378 c_tp->vdop = g_strtod ( c_cdata->str, NULL );
379 g_string_erase ( c_cdata, 0, -1 );
380 break;
381
382 case tt_trk_trkseg_trkpt_pdop:
4a42b254
T
383 c_tp->pdop = g_strtod ( c_cdata->str, NULL );
384 g_string_erase ( c_cdata, 0, -1 );
385 break;
386
8c4f1350
EB
387 default: break;
388 }
389
390 current_tag = get_tag ( xpath->str );
391}
392
393static void gpx_cdata(void *dta, const XML_Char *s, int len)
394{
395 switch ( current_tag ) {
396 case tt_wpt_name:
397 case tt_trk_name:
398 case tt_wpt_ele:
399 case tt_trk_trkseg_trkpt_ele:
400 case tt_wpt_desc:
42c2ac3e 401 case tt_wpt_sym:
8c4f1350
EB
402 case tt_wpt_link:
403 case tt_trk_desc:
404 case tt_trk_trkseg_trkpt_time:
a2817d3c
QT
405 case tt_trk_trkseg_trkpt_course:
406 case tt_trk_trkseg_trkpt_speed:
407 case tt_trk_trkseg_trkpt_fix:
408 case tt_trk_trkseg_trkpt_sat:
4a42b254
T
409 case tt_trk_trkseg_trkpt_hdop:
410 case tt_trk_trkseg_trkpt_vdop:
411 case tt_trk_trkseg_trkpt_pdop:
bf35388d 412 case tt_waypoint_name: /* .loc name is really description. */
8c4f1350
EB
413 g_string_append_len ( c_cdata, s, len );
414 break;
415
416 default: break; /* ignore cdata from other things */
417 }
418}
419
420// make like a "stack" of tag names
421// like gpspoint's separated like /gpx/wpt/whatever
422
efe9a9c3 423gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
8c4f1350
EB
424 XML_Parser parser = XML_ParserCreate(NULL);
425 int done=0, len;
efe9a9c3 426 enum XML_Status status = XML_STATUS_ERROR;
8c4f1350
EB
427
428 XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
429 XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
430 XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata);
431
432 gchar buf[4096];
433
bf35388d 434 g_assert ( f != NULL && vtl != NULL );
8c4f1350
EB
435
436 xpath = g_string_new ( "" );
437 c_cdata = g_string_new ( "" );
438
439 unnamed_waypoints = 0;
440 unnamed_tracks = 0;
441
442 while (!done) {
443 len = fread(buf, 1, sizeof(buf)-7, f);
444 done = feof(f) || !len;
efe9a9c3 445 status = XML_Parse(parser, buf, len, done);
8c4f1350
EB
446 }
447
6278ccc6 448 XML_ParserFree (parser);
8c4f1350
EB
449 g_string_free ( xpath, TRUE );
450 g_string_free ( c_cdata, TRUE );
efe9a9c3
GB
451
452 return status != XML_STATUS_ERROR;
8c4f1350 453}
561e6ad0
EB
454
455/**** entitize from GPSBabel ****/
456typedef struct {
457 const char * text;
458 const char * entity;
459 int not_html;
460} entity_types;
461
462static
463entity_types stdentities[] = {
464 { "&", "&amp;", 0 },
465 { "'", "&apos;", 1 },
466 { "<", "&lt;", 0 },
467 { ">", "&gt;", 0 },
468 { "\"", "&quot;", 0 },
469 { NULL, NULL, 0 }
470};
471
472void utf8_to_int( const char *cp, int *bytes, int *value )
473{
474 if ( (*cp & 0xe0) == 0xc0 ) {
475 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
476 *bytes = 2;
477 *value = ((*cp & 0x1f) << 6) |
478 (*(cp+1) & 0x3f);
479 }
480 else if ( (*cp & 0xf0) == 0xe0 ) {
481 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
482 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
483 *bytes = 3;
484 *value = ((*cp & 0x0f) << 12) |
485 ((*(cp+1) & 0x3f) << 6) |
486 (*(cp+2) & 0x3f);
487 }
488 else if ( (*cp & 0xf8) == 0xf0 ) {
489 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
490 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
491 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
492 *bytes = 4;
493 *value = ((*cp & 0x07) << 18) |
494 ((*(cp+1) & 0x3f) << 12) |
495 ((*(cp+2) & 0x3f) << 6) |
496 (*(cp+3) & 0x3f);
497 }
498 else if ( (*cp & 0xfc) == 0xf8 ) {
499 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
500 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
501 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
502 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
503 *bytes = 5;
504 *value = ((*cp & 0x03) << 24) |
505 ((*(cp+1) & 0x3f) << 18) |
506 ((*(cp+2) & 0x3f) << 12) |
507 ((*(cp+3) & 0x3f) << 6) |
508 (*(cp+4) & 0x3f);
509 }
510 else if ( (*cp & 0xfe) == 0xfc ) {
511 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
512 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
513 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
514 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
515 if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault;
516 *bytes = 6;
517 *value = ((*cp & 0x01) << 30) |
518 ((*(cp+1) & 0x3f) << 24) |
519 ((*(cp+2) & 0x3f) << 18) |
520 ((*(cp+3) & 0x3f) << 12) |
521 ((*(cp+4) & 0x3f) << 6) |
522 (*(cp+5) & 0x3f);
523 }
524 else {
525dodefault:
526 *bytes = 1;
527 *value = (unsigned char)*cp;
528 }
529}
530
531static
532char *
533entitize(const char * str)
534{
535 int elen, ecount, nsecount;
536 entity_types *ep;
537 const char * cp;
538 char * p, * tmp, * xstr;
539
540 char tmpsub[20];
541 int bytes = 0;
542 int value = 0;
543 ep = stdentities;
544 elen = ecount = nsecount = 0;
545
546 /* figure # of entity replacements and additional size. */
547 while (ep->text) {
548 cp = str;
549 while ((cp = strstr(cp, ep->text)) != NULL) {
550 elen += strlen(ep->entity) - strlen(ep->text);
551 ecount++;
552 cp += strlen(ep->text);
553 }
554 ep++;
555 }
556
557 /* figure the same for other than standard entities (i.e. anything
558 * that isn't in the range U+0000 to U+007F */
559 for ( cp = str; *cp; cp++ ) {
560 if ( *cp & 0x80 ) {
561
562 utf8_to_int( cp, &bytes, &value );
563 cp += bytes-1;
564 elen += sprintf( tmpsub, "&#x%x;", value ) - bytes;
565 nsecount++;
566 }
567 }
568
569 /* enough space for the whole string plus entity replacements, if any */
570 tmp = g_malloc((strlen(str) + elen + 1));
571 strcpy(tmp, str);
572
573 /* no entity replacements */
574 if (ecount == 0 && nsecount == 0)
575 return (tmp);
576
577 if ( ecount != 0 ) {
578 for (ep = stdentities; ep->text; ep++) {
579 p = tmp;
580 while ((p = strstr(p, ep->text)) != NULL) {
581 elen = strlen(ep->entity);
582
583 xstr = g_strdup(p + strlen(ep->text));
584
585 strcpy(p, ep->entity);
586 strcpy(p + elen, xstr);
587
588 g_free(xstr);
589
590 p += elen;
591 }
592 }
593 }
594
595 if ( nsecount != 0 ) {
596 p = tmp;
597 while (*p) {
598 if ( *p & 0x80 ) {
599 utf8_to_int( p, &bytes, &value );
600 if ( p[bytes] ) {
601 xstr = g_strdup( p + bytes );
602 }
603 else {
604 xstr = NULL;
605 }
606 sprintf( p, "&#x%x;", value );
607 p = p+strlen(p);
608 if ( xstr ) {
609 strcpy( p, xstr );
610 g_free(xstr);
611 }
612 }
613 else {
614 p++;
615 }
616 }
617 }
618 return (tmp);
619}
620/**** end GPSBabel code ****/
621
622/* export GPX */
8904c540 623
c9570f86 624static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
561e6ad0 625{
208d2084
RN
626 // Don't write invisible waypoints when specified
627 if (context->options && !context->options->hidden && !wp->visible)
628 return;
629
0b72c435 630 FILE *f = context->file;
561e6ad0 631 static struct LatLon ll;
8904c540 632 gchar *s_lat,*s_lon;
561e6ad0
EB
633 gchar *tmp;
634 vik_coord_to_latlon ( &(wp->coord), &ll );
161aa492
EB
635 s_lat = a_coords_dtostr( ll.lat );
636 s_lon = a_coords_dtostr( ll.lon );
208d2084
RN
637 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
638 // luckily most other GPX processing software ignores things they don't understand
8904c540
GB
639 fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
640 s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );
641 g_free ( s_lat );
642 g_free ( s_lon );
561e6ad0 643
c9570f86
RN
644 // Sanity clause
645 if ( wp->name )
646 tmp = entitize ( wp->name );
647 else
648 tmp = g_strdup ("waypoint");
649
561e6ad0
EB
650 fprintf ( f, " <name>%s</name>\n", tmp );
651 g_free ( tmp);
652
653 if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
8904c540 654 {
161aa492 655 tmp = a_coords_dtostr ( wp->altitude );
8904c540
GB
656 fprintf ( f, " <ele>%s</ele>\n", tmp );
657 g_free ( tmp );
658 }
561e6ad0
EB
659 if ( wp->comment )
660 {
661 tmp = entitize(wp->comment);
662 fprintf ( f, " <desc>%s</desc>\n", tmp );
663 g_free ( tmp );
664 }
665 if ( wp->image )
666 {
667 tmp = entitize(wp->image);
668 fprintf ( f, " <link>%s</link>\n", tmp );
669 g_free ( tmp );
670 }
42c2ac3e
AF
671 if ( wp->symbol )
672 {
673 tmp = entitize(wp->symbol);
674 fprintf ( f, " <sym>%s</sym>\n", tmp);
675 g_free ( tmp );
676 }
677
561e6ad0
EB
678 fprintf ( f, "</wpt>\n" );
679}
680
0b72c435 681static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context )
561e6ad0 682{
0b72c435 683 FILE *f = context->file;
561e6ad0 684 static struct LatLon ll;
4a42b254 685 gchar *s_lat,*s_lon, *s_alt, *s_dop;
dd81e762 686 gchar *time_iso8601;
561e6ad0
EB
687 vik_coord_to_latlon ( &(tp->coord), &ll );
688
689 if ( tp->newsegment )
690 fprintf ( f, " </trkseg>\n <trkseg>\n" );
691
161aa492
EB
692 s_lat = a_coords_dtostr( ll.lat );
693 s_lon = a_coords_dtostr( ll.lon );
8904c540 694 fprintf ( f, " <trkpt lat=\"%s\" lon=\"%s\">\n", s_lat, s_lon );
0b72c435
OK
695 g_free ( s_lat ); s_lat = NULL;
696 g_free ( s_lon ); s_lon = NULL;
561e6ad0 697
0b72c435 698 s_alt = NULL;
561e6ad0 699 if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
8904c540 700 {
161aa492 701 s_alt = a_coords_dtostr ( tp->altitude );
8904c540 702 }
0b72c435 703 else if ( context->options != NULL && context->options->force_ele )
36179a2e
OK
704 {
705 s_alt = a_coords_dtostr ( 0 );
706 }
0b72c435
OK
707 if (s_alt != NULL)
708 fprintf ( f, " <ele>%s</ele>\n", s_alt );
709 g_free ( s_alt ); s_alt = NULL;
36179a2e 710
dd81e762 711 time_iso8601 = NULL;
561e6ad0 712 if ( tp->has_timestamp ) {
dd81e762
GB
713 GTimeVal timestamp;
714 timestamp.tv_sec = tp->timestamp;
61f37ac9 715 timestamp.tv_usec = 0;
dd81e762
GB
716
717 time_iso8601 = g_time_val_to_iso8601 ( &timestamp );
561e6ad0 718 }
0b72c435 719 else if ( context->options != NULL && context->options->force_time )
36179a2e 720 {
dd81e762
GB
721 GTimeVal current;
722 g_get_current_time ( &current );
36179a2e 723
dd81e762 724 time_iso8601 = g_time_val_to_iso8601 ( &current );
36179a2e 725 }
dd81e762
GB
726 if ( time_iso8601 != NULL )
727 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
728 g_free(time_iso8601);
729 time_iso8601 = NULL;
36179a2e 730
ad54cd32
T
731 if (!isnan(tp->course)) {
732 gchar *s_course = a_coords_dtostr(tp->course);
733 fprintf ( f, " <course>%s</course>\n", s_course );
734 g_free(s_course);
a2817d3c 735 }
ad54cd32
T
736 if (!isnan(tp->speed)) {
737 gchar *s_speed = a_coords_dtostr(tp->speed);
738 fprintf ( f, " <speed>%s</speed>\n", s_speed );
739 g_free(s_speed);
740 }
741 if (tp->fix_mode == VIK_GPS_MODE_2D)
742 fprintf ( f, " <fix>2d</fix>\n");
743 if (tp->fix_mode == VIK_GPS_MODE_3D)
744 fprintf ( f, " <fix>3d</fix>\n");
745 if (tp->nsats > 0)
746 fprintf ( f, " <sat>%d</sat>\n", tp->nsats );
a2817d3c 747
4a42b254
T
748 s_dop = NULL;
749 if ( tp->hdop != VIK_DEFAULT_DOP )
750 {
751 s_dop = a_coords_dtostr ( tp->hdop );
752 }
753 if (s_dop != NULL)
754 fprintf ( f, " <hdop>%s</hdop>\n", s_dop );
755 g_free ( s_dop ); s_dop = NULL;
756
757 if ( tp->vdop != VIK_DEFAULT_DOP )
758 {
759 s_dop = a_coords_dtostr ( tp->vdop );
760 }
761 if (s_dop != NULL)
762 fprintf ( f, " <vdop>%s</vdop>\n", s_dop );
763 g_free ( s_dop ); s_dop = NULL;
764
765 if ( tp->pdop != VIK_DEFAULT_DOP )
766 {
767 s_dop = a_coords_dtostr ( tp->pdop );
768 }
769 if (s_dop != NULL)
770 fprintf ( f, " <pdop>%s</pdop>\n", s_dop );
771 g_free ( s_dop ); s_dop = NULL;
772
773
561e6ad0
EB
774 fprintf ( f, " </trkpt>\n" );
775}
776
777
ce4bd1cf 778static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
561e6ad0 779{
208d2084
RN
780 // Don't write invisible tracks when specified
781 if (context->options && !context->options->hidden && !t->visible)
782 return;
783
0b72c435 784 FILE *f = context->file;
561e6ad0 785 gchar *tmp;
494eb388 786 gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */
561e6ad0 787
ce4bd1cf
RN
788 // Sanity clause
789 if ( t->name )
790 tmp = entitize ( t->name );
791 else
792 tmp = g_strdup ("track");
793
208d2084
RN
794 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
795 // luckily most other GPX processing software ignores things they don't understand
561e6ad0
EB
796 fprintf ( f, "<trk%s>\n <name>%s</name>\n", t->visible ? "" : " hidden=\"hidden\"", tmp );
797 g_free ( tmp );
798
799 if ( t->comment )
800 {
801 tmp = entitize ( t->comment );
802 fprintf ( f, " <desc>%s</desc>\n", tmp );
803 g_free ( tmp );
804 }
561e6ad0
EB
805 fprintf ( f, " <trkseg>\n" );
806
807 if ( t->trackpoints && t->trackpoints->data ) {
808 first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment;
809 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */
0b72c435 810 g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context );
561e6ad0 811 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
494eb388 812 }
561e6ad0
EB
813
814 fprintf ( f, "</trkseg>\n</trk>\n" );
815}
816
5092de80 817static void gpx_write_header( FILE *f )
561e6ad0
EB
818{
819 fprintf(f, "<?xml version=\"1.0\"?>\n"
820 "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
821 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
822 "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
823 "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
5092de80
GB
824}
825
826static void gpx_write_footer( FILE *f )
827{
561e6ad0 828 fprintf(f, "</gpx>\n");
5092de80 829}
561e6ad0 830
c9570f86 831static int gpx_waypoint_compare(const void *x, const void *y)
eab858a1 832{
c9570f86
RN
833 VikWaypoint *a = (VikWaypoint *)x;
834 VikWaypoint *b = (VikWaypoint *)y;
eab858a1
EB
835 return strcmp(a->name,b->name);
836}
837
e3449376 838/* Function to compare two tracks by their first timestamp */
0dabdc4f 839static int gpx_track_compare_timestamp (const void *x, const void *y)
e3449376 840{
0dabdc4f
RN
841 VikTrack *a = (VikTrack *)x;
842 VikTrack *b = (VikTrack *)y;
843
844 VikTrackpoint *tpa = NULL;
845 VikTrackpoint *tpb = NULL;
846
847 if ( a->trackpoints )
848 tpa = VIK_TRACKPOINT(g_list_first(a->trackpoints)->data);
849
850 if ( b->trackpoints )
851 tpb = VIK_TRACKPOINT(g_list_first(b->trackpoints)->data);
852
853 if ( tpa && tpb ) {
854 if ( tpa->timestamp < tpb->timestamp )
855 return -1;
856 if ( tpa->timestamp > tpb->timestamp )
857 return 1;
e3449376 858 }
0dabdc4f
RN
859
860 if ( tpa && !tpb )
e3449376 861 return 1;
0dabdc4f
RN
862
863 if ( !tpa && tpb )
864 return -1;
865
e3449376
HR
866 return 0;
867}
868
208d2084 869void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
0b72c435
OK
870{
871 GpxWritingContext context = { options, f };
eab858a1 872
5092de80 873 gpx_write_header ( f );
eab858a1 874
c9570f86
RN
875 // gather waypoints in a list, then sort
876 // g_hash_table_get_values: glib 2.14+
877 GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
878 gl = g_list_sort ( gl, gpx_waypoint_compare );
0b72c435 879
c9570f86
RN
880 GList *iter;
881 for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
882 gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
883 }
eab858a1 884
c9570f86 885 g_list_free ( gl );
e3449376 886 /* Sort by timestamp */
ce4bd1cf 887 gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
0dabdc4f 888 gl = g_list_sort ( gl, gpx_track_compare_timestamp );
ce4bd1cf
RN
889
890 for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
891 gpx_write_track ( (VikTrack*)iter->data, &context );
e3449376 892 }
ce4bd1cf
RN
893
894 g_list_free ( gl );
895
5092de80
GB
896 gpx_write_footer ( f );
897}
561e6ad0 898
208d2084 899void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
0b72c435
OK
900{
901 GpxWritingContext context = {options, f};
5092de80 902 gpx_write_header ( f );
ce4bd1cf 903 gpx_write_track ( trk, &context );
5092de80 904 gpx_write_footer ( f );
561e6ad0 905}