2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
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>
8 * Copyright (c) 2012, Rob Norris <rw_norris@hotmail.com>
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
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.
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.
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
33 #define _XOPEN_SOURCE /* glibc2 needs this */
64 tt_wpt_link, /* New in GPX 1.1 */
75 tt_trk_trkseg_trkpt_ele,
76 tt_trk_trkseg_trkpt_time,
77 tt_trk_trkseg_trkpt_name,
79 tt_trk_trkseg_trkpt_course,
80 tt_trk_trkseg_trkpt_speed,
81 tt_trk_trkseg_trkpt_fix,
82 tt_trk_trkseg_trkpt_sat,
84 tt_trk_trkseg_trkpt_hdop,
85 tt_trk_trkseg_trkpt_vdop,
86 tt_trk_trkseg_trkpt_pdop,
93 typedef struct tag_mapping {
94 tag_type tag_type; /* enum from above for this tag */
95 const char *tag_name; /* xpath-ish tag name */
99 GpxWritingOptions *options;
104 * xpath(ish) mappings between full tag paths and internal identifiers.
105 * These appear in the order they appear in the GPX specification.
106 * If it's not a tag we explicitly handle, it doesn't go here.
109 tag_mapping tag_path_map[] = {
112 { tt_gpx_name, "/gpx/name" },
113 { tt_gpx_desc, "/gpx/desc" },
114 { tt_gpx_time, "/gpx/time" },
115 { tt_gpx_author, "/gpx/author" },
116 { tt_gpx_keywords, "/gpx/keywords" },
118 // GPX 1.1 variant - basic properties moved into metadata namespace
119 { tt_gpx_name, "/gpx/metadata/name" },
120 { tt_gpx_desc, "/gpx/metadata/desc" },
121 { tt_gpx_time, "/gpx/metadata/time" },
122 { tt_gpx_author, "/gpx/metadata/author" },
123 { tt_gpx_keywords, "/gpx/metadata/keywords" },
125 { tt_wpt, "/gpx/wpt" },
127 { tt_waypoint, "/loc/waypoint" },
128 { tt_waypoint_coord, "/loc/waypoint/coord" },
129 { tt_waypoint_name, "/loc/waypoint/name" },
131 { tt_wpt_ele, "/gpx/wpt/ele" },
132 { tt_wpt_time, "/gpx/wpt/time" },
133 { tt_wpt_name, "/gpx/wpt/name" },
134 { tt_wpt_cmt, "/gpx/wpt/cmt" },
135 { tt_wpt_desc, "/gpx/wpt/desc" },
136 { tt_wpt_sym, "/gpx/wpt/sym" },
137 { tt_wpt_sym, "/loc/waypoint/type" },
138 { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */
140 { tt_trk, "/gpx/trk" },
141 { tt_trk_name, "/gpx/trk/name" },
142 { tt_trk_cmt, "/gpx/trk/cmt" },
143 { tt_trk_desc, "/gpx/trk/desc" },
144 { tt_trk_trkseg, "/gpx/trk/trkseg" },
145 { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
146 { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
147 { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
148 { tt_trk_trkseg_trkpt_name, "/gpx/trk/trkseg/trkpt/name" },
150 { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" },
151 { tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" },
152 { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" },
153 { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" },
155 { tt_trk_trkseg_trkpt_hdop, "/gpx/trk/trkseg/trkpt/hdop" },
156 { tt_trk_trkseg_trkpt_vdop, "/gpx/trk/trkseg/trkpt/vdop" },
157 { tt_trk_trkseg_trkpt_pdop, "/gpx/trk/trkseg/trkpt/pdop" },
159 { tt_rte, "/gpx/rte" },
160 // NB Route reuses track point feature tags
161 { tt_trk_name, "/gpx/rte/name" },
162 { tt_trk_cmt, "/gpx/rte/cmt" },
163 { tt_trk_desc, "/gpx/rte/desc" },
164 { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
165 { tt_trk_trkseg_trkpt_name, "/gpx/rte/rtept/name" },
166 { tt_trk_trkseg_trkpt_ele, "/gpx/rte/rtept/ele" },
171 static tag_type get_tag(const char *t)
174 for (tm = tag_path_map; tm->tag_type != 0; tm++)
175 if (0 == strcmp(tm->tag_name, t))
180 /******************************************/
182 tag_type current_tag = tt_unknown;
183 GString *xpath = NULL;
184 GString *c_cdata = NULL;
186 /* current ("c_") objects */
187 VikTrackpoint *c_tp = NULL;
188 VikWaypoint *c_wp = NULL;
189 VikTrack *c_tr = NULL;
190 VikTRWMetadata *c_md = NULL;
192 gchar *c_wp_name = NULL;
193 gchar *c_tr_name = NULL;
195 /* temporary things so we don't have to create them lots of times */
196 const gchar *c_slat, *c_slon;
199 /* specialty flags / etc */
200 gboolean f_tr_newseg;
201 guint unnamed_waypoints = 0;
202 guint unnamed_tracks = 0;
203 guint unnamed_routes = 0;
205 static const char *get_attr ( const char **attr, const char *key )
208 if ( strcmp(*attr,key) == 0 )
215 static gboolean set_c_ll ( const char **attr )
217 if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) {
218 c_ll.lat = g_ascii_strtod(c_slat, NULL);
219 c_ll.lon = g_ascii_strtod(c_slon, NULL);
225 static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
227 static const gchar *tmp;
229 g_string_append_c ( xpath, '/' );
230 g_string_append ( xpath, el );
231 current_tag = get_tag ( xpath->str );
233 switch ( current_tag ) {
236 c_md = vik_trw_metadata_new();
240 if ( set_c_ll( attr ) ) {
241 c_wp = vik_waypoint_new ();
242 c_wp->visible = TRUE;
243 if ( get_attr ( attr, "hidden" ) )
244 c_wp->visible = FALSE;
246 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
252 c_tr = vik_track_new ();
253 vik_track_set_defaults ( c_tr );
254 c_tr->is_route = (current_tag == tt_rte) ? TRUE : FALSE;
255 c_tr->visible = TRUE;
256 if ( get_attr ( attr, "hidden" ) )
257 c_tr->visible = FALSE;
264 case tt_trk_trkseg_trkpt:
265 if ( set_c_ll( attr ) ) {
266 c_tp = vik_trackpoint_new ();
267 vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
269 c_tp->newsegment = TRUE;
272 c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp );
279 case tt_gpx_keywords:
281 case tt_trk_trkseg_trkpt_name:
282 case tt_trk_trkseg_trkpt_ele:
283 case tt_trk_trkseg_trkpt_time:
293 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */
297 c_wp = vik_waypoint_new ();
298 c_wp->visible = TRUE;
301 case tt_waypoint_coord:
302 if ( set_c_ll( attr ) )
303 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
306 case tt_waypoint_name:
307 if ( ( tmp = get_attr(attr, "id") ) ) {
309 g_free ( c_wp_name );
310 c_wp_name = g_strdup ( tmp );
312 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */
319 static void gpx_end(VikTrwLayer *vtl, const char *el)
321 static GTimeVal tp_time;
322 static GTimeVal wp_time;
324 g_string_truncate ( xpath, xpath->len - strlen(el) - 1 );
326 switch ( current_tag ) {
329 vik_trw_layer_set_metadata ( vtl, c_md );
334 vik_layer_rename ( VIK_LAYER(vtl), c_cdata->str );
335 g_string_erase ( c_cdata, 0, -1 );
340 g_free ( c_md->description );
341 c_md->author = g_strdup ( c_cdata->str );
342 g_string_erase ( c_cdata, 0, -1 );
346 if ( c_md->description )
347 g_free ( c_md->description );
348 c_md->description = g_strdup ( c_cdata->str );
349 g_string_erase ( c_cdata, 0, -1 );
352 case tt_gpx_keywords:
353 if ( c_md->keywords )
354 g_free ( c_md->keywords );
355 c_md->keywords = g_strdup ( c_cdata->str );
356 g_string_erase ( c_cdata, 0, -1 );
360 if ( c_md->timestamp )
361 g_free ( c_md->timestamp );
362 c_md->timestamp = g_strdup ( c_cdata->str );
363 g_string_erase ( c_cdata, 0, -1 );
369 c_wp_name = g_strdup_printf("VIKING_WP%04d", unnamed_waypoints++);
370 vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp );
371 g_free ( c_wp_name );
378 c_tr_name = g_strdup_printf("VIKING_TR%03d", unnamed_tracks++);
379 // Delibrate fall through
382 c_tr_name = g_strdup_printf("VIKING_RT%03d", unnamed_routes++);
383 vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr );
384 g_free ( c_tr_name );
391 g_free ( c_wp_name );
392 c_wp_name = g_strdup ( c_cdata->str );
393 g_string_erase ( c_cdata, 0, -1 );
398 g_free ( c_tr_name );
399 c_tr_name = g_strdup ( c_cdata->str );
400 g_string_erase ( c_cdata, 0, -1 );
404 c_wp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
405 g_string_erase ( c_cdata, 0, -1 );
408 case tt_trk_trkseg_trkpt_ele:
409 c_tp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
410 g_string_erase ( c_cdata, 0, -1 );
413 case tt_waypoint_name: /* .loc name is really description. */
415 vik_waypoint_set_description ( c_wp, c_cdata->str );
416 g_string_erase ( c_cdata, 0, -1 );
420 vik_waypoint_set_comment ( c_wp, c_cdata->str );
421 g_string_erase ( c_cdata, 0, -1 );
425 vik_waypoint_set_image ( c_wp, c_cdata->str );
426 g_string_erase ( c_cdata, 0, -1 );
430 vik_waypoint_set_symbol ( c_wp, c_cdata->str );
431 g_string_erase ( c_cdata, 0, -1 );
436 vik_track_set_description ( c_tr, c_cdata->str );
437 g_string_erase ( c_cdata, 0, -1 );
441 vik_track_set_comment ( c_tr, c_cdata->str );
442 g_string_erase ( c_cdata, 0, -1 );
446 if ( g_time_val_from_iso8601(c_cdata->str, &wp_time) ) {
447 c_wp->timestamp = wp_time.tv_sec;
448 c_wp->has_timestamp = TRUE;
450 g_string_erase ( c_cdata, 0, -1 );
453 case tt_trk_trkseg_trkpt_name:
454 vik_trackpoint_set_name ( c_tp, c_cdata->str );
455 g_string_erase ( c_cdata, 0, -1 );
458 case tt_trk_trkseg_trkpt_time:
459 if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) {
460 c_tp->timestamp = tp_time.tv_sec;
461 c_tp->has_timestamp = TRUE;
463 g_string_erase ( c_cdata, 0, -1 );
466 case tt_trk_trkseg_trkpt_course:
467 c_tp->course = g_ascii_strtod ( c_cdata->str, NULL );
468 g_string_erase ( c_cdata, 0, -1 );
471 case tt_trk_trkseg_trkpt_speed:
472 c_tp->speed = g_ascii_strtod ( c_cdata->str, NULL );
473 g_string_erase ( c_cdata, 0, -1 );
476 case tt_trk_trkseg_trkpt_fix:
477 if (!strcmp("2d", c_cdata->str))
478 c_tp->fix_mode = VIK_GPS_MODE_2D;
479 else if (!strcmp("3d", c_cdata->str))
480 c_tp->fix_mode = VIK_GPS_MODE_3D;
481 else /* TODO: more fix modes here */
482 c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN;
483 g_string_erase ( c_cdata, 0, -1 );
486 case tt_trk_trkseg_trkpt_sat:
487 c_tp->nsats = atoi ( c_cdata->str );
488 g_string_erase ( c_cdata, 0, -1 );
491 case tt_trk_trkseg_trkpt_hdop:
492 c_tp->hdop = g_strtod ( c_cdata->str, NULL );
493 g_string_erase ( c_cdata, 0, -1 );
496 case tt_trk_trkseg_trkpt_vdop:
497 c_tp->vdop = g_strtod ( c_cdata->str, NULL );
498 g_string_erase ( c_cdata, 0, -1 );
501 case tt_trk_trkseg_trkpt_pdop:
502 c_tp->pdop = g_strtod ( c_cdata->str, NULL );
503 g_string_erase ( c_cdata, 0, -1 );
509 current_tag = get_tag ( xpath->str );
512 static void gpx_cdata(void *dta, const XML_Char *s, int len)
514 switch ( current_tag ) {
518 case tt_gpx_keywords:
523 case tt_trk_trkseg_trkpt_ele:
530 case tt_trk_trkseg_trkpt_time:
532 case tt_trk_trkseg_trkpt_name:
533 case tt_trk_trkseg_trkpt_course:
534 case tt_trk_trkseg_trkpt_speed:
535 case tt_trk_trkseg_trkpt_fix:
536 case tt_trk_trkseg_trkpt_sat:
537 case tt_trk_trkseg_trkpt_hdop:
538 case tt_trk_trkseg_trkpt_vdop:
539 case tt_trk_trkseg_trkpt_pdop:
540 case tt_waypoint_name: /* .loc name is really description. */
541 g_string_append_len ( c_cdata, s, len );
544 default: break; /* ignore cdata from other things */
548 // make like a "stack" of tag names
549 // like gpspoint's separated like /gpx/wpt/whatever
551 gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
552 XML_Parser parser = XML_ParserCreate(NULL);
554 enum XML_Status status = XML_STATUS_ERROR;
556 XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
557 XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
558 XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata);
562 g_assert ( f != NULL && vtl != NULL );
564 xpath = g_string_new ( "" );
565 c_cdata = g_string_new ( "" );
567 unnamed_waypoints = 1;
572 len = fread(buf, 1, sizeof(buf)-7, f);
573 done = feof(f) || !len;
574 status = XML_Parse(parser, buf, len, done);
577 XML_ParserFree (parser);
578 g_string_free ( xpath, TRUE );
579 g_string_free ( c_cdata, TRUE );
581 return status != XML_STATUS_ERROR;
584 /**** entitize from GPSBabel ****/
592 entity_types stdentities[] = {
594 { "'", "'", 1 },
597 { "\"", """, 0 },
601 void utf8_to_int( const char *cp, int *bytes, int *value )
603 if ( (*cp & 0xe0) == 0xc0 ) {
604 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
606 *value = ((*cp & 0x1f) << 6) |
609 else if ( (*cp & 0xf0) == 0xe0 ) {
610 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
611 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
613 *value = ((*cp & 0x0f) << 12) |
614 ((*(cp+1) & 0x3f) << 6) |
617 else if ( (*cp & 0xf8) == 0xf0 ) {
618 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
619 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
620 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
622 *value = ((*cp & 0x07) << 18) |
623 ((*(cp+1) & 0x3f) << 12) |
624 ((*(cp+2) & 0x3f) << 6) |
627 else if ( (*cp & 0xfc) == 0xf8 ) {
628 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
629 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
630 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
631 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
633 *value = ((*cp & 0x03) << 24) |
634 ((*(cp+1) & 0x3f) << 18) |
635 ((*(cp+2) & 0x3f) << 12) |
636 ((*(cp+3) & 0x3f) << 6) |
639 else if ( (*cp & 0xfe) == 0xfc ) {
640 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
641 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
642 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
643 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
644 if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault;
646 *value = ((*cp & 0x01) << 30) |
647 ((*(cp+1) & 0x3f) << 24) |
648 ((*(cp+2) & 0x3f) << 18) |
649 ((*(cp+3) & 0x3f) << 12) |
650 ((*(cp+4) & 0x3f) << 6) |
656 *value = (unsigned char)*cp;
662 entitize(const char * str)
664 int elen, ecount, nsecount;
667 char * p, * tmp, * xstr;
673 elen = ecount = nsecount = 0;
675 /* figure # of entity replacements and additional size. */
678 while ((cp = strstr(cp, ep->text)) != NULL) {
679 elen += strlen(ep->entity) - strlen(ep->text);
681 cp += strlen(ep->text);
686 /* figure the same for other than standard entities (i.e. anything
687 * that isn't in the range U+0000 to U+007F */
688 for ( cp = str; *cp; cp++ ) {
691 utf8_to_int( cp, &bytes, &value );
693 elen += sprintf( tmpsub, "&#x%x;", value ) - bytes;
698 /* enough space for the whole string plus entity replacements, if any */
699 tmp = g_malloc((strlen(str) + elen + 1));
702 /* no entity replacements */
703 if (ecount == 0 && nsecount == 0)
707 for (ep = stdentities; ep->text; ep++) {
709 while ((p = strstr(p, ep->text)) != NULL) {
710 elen = strlen(ep->entity);
712 xstr = g_strdup(p + strlen(ep->text));
714 strcpy(p, ep->entity);
715 strcpy(p + elen, xstr);
724 if ( nsecount != 0 ) {
728 utf8_to_int( p, &bytes, &value );
730 xstr = g_strdup( p + bytes );
735 sprintf( p, "&#x%x;", value );
749 /**** end GPSBabel code ****/
753 static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
755 // Don't write invisible waypoints when specified
756 if (context->options && !context->options->hidden && !wp->visible)
759 FILE *f = context->file;
760 static struct LatLon ll;
763 vik_coord_to_latlon ( &(wp->coord), &ll );
764 s_lat = a_coords_dtostr( ll.lat );
765 s_lon = a_coords_dtostr( ll.lon );
766 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
767 // luckily most other GPX processing software ignores things they don't understand
768 fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
769 s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );
775 tmp = entitize ( wp->name );
777 tmp = g_strdup ("waypoint");
779 fprintf ( f, " <name>%s</name>\n", tmp );
782 if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
784 tmp = a_coords_dtostr ( wp->altitude );
785 fprintf ( f, " <ele>%s</ele>\n", tmp );
789 if ( wp->has_timestamp ) {
791 timestamp.tv_sec = wp->timestamp;
792 timestamp.tv_usec = 0;
794 gchar *time_iso8601 = g_time_val_to_iso8601 ( ×tamp );
795 if ( time_iso8601 != NULL )
796 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
797 g_free ( time_iso8601 );
802 tmp = entitize(wp->comment);
803 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
806 if ( wp->description )
808 tmp = entitize(wp->description);
809 fprintf ( f, " <desc>%s</desc>\n", tmp );
814 tmp = entitize(wp->image);
815 fprintf ( f, " <link>%s</link>\n", tmp );
820 tmp = entitize(wp->symbol);
821 if ( a_vik_gpx_export_wpt_sym_name ( ) ) {
822 // Lowercase the symbol name
823 gchar *tmp2 = g_utf8_strdown ( tmp, -1 );
824 fprintf ( f, " <sym>%s</sym>\n", tmp2 );
828 fprintf ( f, " <sym>%s</sym>\n", tmp);
832 fprintf ( f, "</wpt>\n" );
835 static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context )
837 FILE *f = context->file;
838 static struct LatLon ll;
839 gchar *s_lat,*s_lon, *s_alt, *s_dop;
841 vik_coord_to_latlon ( &(tp->coord), &ll );
843 // No such thing as a rteseg! So make sure we don't put them in
844 if ( context->options && !context->options->is_route && tp->newsegment )
845 fprintf ( f, " </trkseg>\n <trkseg>\n" );
847 s_lat = a_coords_dtostr( ll.lat );
848 s_lon = a_coords_dtostr( ll.lon );
849 fprintf ( f, " <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon );
850 g_free ( s_lat ); s_lat = NULL;
851 g_free ( s_lon ); s_lon = NULL;
854 gchar *s_name = entitize(tp->name);
855 fprintf ( f, " <name>%s</name>\n", s_name );
860 if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
862 s_alt = a_coords_dtostr ( tp->altitude );
864 else if ( context->options != NULL && context->options->force_ele )
866 s_alt = a_coords_dtostr ( 0 );
869 fprintf ( f, " <ele>%s</ele>\n", s_alt );
870 g_free ( s_alt ); s_alt = NULL;
873 if ( tp->has_timestamp ) {
875 timestamp.tv_sec = tp->timestamp;
876 timestamp.tv_usec = 0;
878 time_iso8601 = g_time_val_to_iso8601 ( ×tamp );
880 else if ( context->options != NULL && context->options->force_time )
883 g_get_current_time ( ¤t );
885 time_iso8601 = g_time_val_to_iso8601 ( ¤t );
887 if ( time_iso8601 != NULL )
888 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
889 g_free(time_iso8601);
892 if (!isnan(tp->course)) {
893 gchar *s_course = a_coords_dtostr(tp->course);
894 fprintf ( f, " <course>%s</course>\n", s_course );
897 if (!isnan(tp->speed)) {
898 gchar *s_speed = a_coords_dtostr(tp->speed);
899 fprintf ( f, " <speed>%s</speed>\n", s_speed );
902 if (tp->fix_mode == VIK_GPS_MODE_2D)
903 fprintf ( f, " <fix>2d</fix>\n");
904 if (tp->fix_mode == VIK_GPS_MODE_3D)
905 fprintf ( f, " <fix>3d</fix>\n");
907 fprintf ( f, " <sat>%d</sat>\n", tp->nsats );
910 if ( tp->hdop != VIK_DEFAULT_DOP )
912 s_dop = a_coords_dtostr ( tp->hdop );
915 fprintf ( f, " <hdop>%s</hdop>\n", s_dop );
916 g_free ( s_dop ); s_dop = NULL;
918 if ( tp->vdop != VIK_DEFAULT_DOP )
920 s_dop = a_coords_dtostr ( tp->vdop );
923 fprintf ( f, " <vdop>%s</vdop>\n", s_dop );
924 g_free ( s_dop ); s_dop = NULL;
926 if ( tp->pdop != VIK_DEFAULT_DOP )
928 s_dop = a_coords_dtostr ( tp->pdop );
931 fprintf ( f, " <pdop>%s</pdop>\n", s_dop );
932 g_free ( s_dop ); s_dop = NULL;
934 fprintf ( f, " </%spt>\n", (context->options && context->options->is_route) ? "rte" : "trk" );
938 static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
940 // Don't write invisible tracks when specified
941 if (context->options && !context->options->hidden && !t->visible)
944 FILE *f = context->file;
946 gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */
950 tmp = entitize ( t->name );
952 tmp = g_strdup ("track");
954 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
955 // luckily most other GPX processing software ignores things they don't understand
956 fprintf ( f, "<%s%s>\n <name>%s</name>\n",
957 t->is_route ? "rte" : "trk",
958 t->visible ? "" : " hidden=\"hidden\"",
964 tmp = entitize ( t->comment );
965 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
969 if ( t->description )
971 tmp = entitize ( t->description );
972 fprintf ( f, " <desc>%s</desc>\n", tmp );
976 /* No such thing as a rteseg! */
978 fprintf ( f, " <trkseg>\n" );
980 if ( t->trackpoints && t->trackpoints->data ) {
981 first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment;
982 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */
983 g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context );
984 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
987 /* NB apparently no such thing as a rteseg! */
989 fprintf ( f, " </trkseg>\n");
991 fprintf ( f, "</%s>\n", t->is_route ? "rte" : "trk" );
994 static void gpx_write_header( FILE *f )
996 fprintf(f, "<?xml version=\"1.0\"?>\n"
997 "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
998 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
999 "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
1000 "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
1003 static void gpx_write_footer( FILE *f )
1005 fprintf(f, "</gpx>\n");
1008 static int gpx_waypoint_compare(const void *x, const void *y)
1010 VikWaypoint *a = (VikWaypoint *)x;
1011 VikWaypoint *b = (VikWaypoint *)y;
1012 return strcmp(a->name,b->name);
1015 static int gpx_track_compare_name(const void *x, const void *y)
1017 VikTrack *a = (VikTrack *)x;
1018 VikTrack *b = (VikTrack *)y;
1019 return strcmp(a->name,b->name);
1022 void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
1024 GpxWritingContext context = { options, f };
1026 gpx_write_header ( f );
1029 const gchar *name = vik_layer_get_name(VIK_LAYER(vtl));
1031 tmp = entitize ( name );
1032 fprintf ( f, " <name>%s</name>\n", tmp );
1036 VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
1039 tmp = entitize ( md->author );
1040 fprintf ( f, " <author>%s</author>\n", tmp );
1043 if ( md->description ) {
1044 tmp = entitize ( md->description );
1045 fprintf ( f, " <desc>%s</desc>\n", tmp );
1048 if ( md->timestamp ) {
1049 tmp = entitize ( md->timestamp );
1050 fprintf ( f, " <time>%s</time>\n", tmp );
1053 if ( md->keywords ) {
1054 tmp = entitize ( md->keywords );
1055 fprintf ( f, " <keywords>%s</keywords>\n", tmp );
1060 // gather waypoints in a list, then sort
1061 // g_hash_table_get_values: glib 2.14+
1062 GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
1063 gl = g_list_sort ( gl, gpx_waypoint_compare );
1066 for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
1067 gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
1072 //gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
1073 // Forming the list manually seems to produce one that is more likely to be nearer to the creation order
1075 gpointer key, value;
1076 GHashTableIter ght_iter;
1077 g_hash_table_iter_init ( &ght_iter, vik_trw_layer_get_tracks ( vtl ) );
1078 while ( g_hash_table_iter_next (&ght_iter, &key, &value) ) {
1079 gl = g_list_prepend ( gl ,value );
1081 gl = g_list_reverse ( gl );
1083 // Sort method determined by preference
1084 if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
1085 gl = g_list_sort ( gl, vik_track_compare_timestamp );
1086 else if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_ALPHA )
1087 gl = g_list_sort ( gl, gpx_track_compare_name );
1089 // Routes sorted by name
1090 GList *glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
1091 glrte = g_list_sort ( glrte, gpx_track_compare_name );
1093 // g_list_concat doesn't copy memory properly
1094 // so process each list separately
1096 GpxWritingContext context_tmp = context;
1097 GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE };
1098 // Force trackpoints on tracks
1099 if ( !context.options )
1100 context_tmp.options = &opt_tmp;
1101 context_tmp.options->is_route = FALSE;
1103 // Loop around each list and write each one
1104 for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
1105 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
1108 // Routes (to get routepoints)
1109 context_tmp.options->is_route = TRUE;
1110 for (iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
1111 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
1115 g_list_free ( glrte );
1117 gpx_write_footer ( f );
1120 void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
1122 GpxWritingContext context = {options, f};
1123 gpx_write_header ( f );
1124 gpx_write_track ( trk, &context );
1125 gpx_write_footer ( f );