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