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-2014, 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 */
42 #include <glib/gstdio.h>
43 #include <glib/gi18n.h>
68 tt_wpt_link, /* New in GPX 1.1 */
80 tt_trk_trkseg_trkpt_ele,
81 tt_trk_trkseg_trkpt_time,
82 tt_trk_trkseg_trkpt_name,
84 tt_trk_trkseg_trkpt_course,
85 tt_trk_trkseg_trkpt_speed,
86 tt_trk_trkseg_trkpt_fix,
87 tt_trk_trkseg_trkpt_sat,
89 tt_trk_trkseg_trkpt_hdop,
90 tt_trk_trkseg_trkpt_vdop,
91 tt_trk_trkseg_trkpt_pdop,
98 typedef struct tag_mapping {
99 tag_type tag_type; /* enum from above for this tag */
100 const char *tag_name; /* xpath-ish tag name */
104 GpxWritingOptions *options;
109 * xpath(ish) mappings between full tag paths and internal identifiers.
110 * These appear in the order they appear in the GPX specification.
111 * If it's not a tag we explicitly handle, it doesn't go here.
114 tag_mapping tag_path_map[] = {
117 { tt_gpx_name, "/gpx/name" },
118 { tt_gpx_desc, "/gpx/desc" },
119 { tt_gpx_time, "/gpx/time" },
120 { tt_gpx_author, "/gpx/author" },
121 { tt_gpx_keywords, "/gpx/keywords" },
123 // GPX 1.1 variant - basic properties moved into metadata namespace
124 { tt_gpx_name, "/gpx/metadata/name" },
125 { tt_gpx_desc, "/gpx/metadata/desc" },
126 { tt_gpx_time, "/gpx/metadata/time" },
127 { tt_gpx_author, "/gpx/metadata/author" },
128 { tt_gpx_keywords, "/gpx/metadata/keywords" },
130 { tt_wpt, "/gpx/wpt" },
132 { tt_waypoint, "/loc/waypoint" },
133 { tt_waypoint_coord, "/loc/waypoint/coord" },
134 { tt_waypoint_name, "/loc/waypoint/name" },
136 { tt_wpt_ele, "/gpx/wpt/ele" },
137 { tt_wpt_time, "/gpx/wpt/time" },
138 { tt_wpt_name, "/gpx/wpt/name" },
139 { tt_wpt_cmt, "/gpx/wpt/cmt" },
140 { tt_wpt_desc, "/gpx/wpt/desc" },
141 { tt_wpt_src, "/gpx/wpt/src" },
142 { tt_wpt_sym, "/gpx/wpt/sym" },
143 { tt_wpt_sym, "/loc/waypoint/type" },
144 { tt_wpt_url, "/gpx/wpt/url" },
145 { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */
147 { tt_trk, "/gpx/trk" },
148 { tt_trk_name, "/gpx/trk/name" },
149 { tt_trk_cmt, "/gpx/trk/cmt" },
150 { tt_trk_desc, "/gpx/trk/desc" },
151 { tt_trk_src, "/gpx/trk/src" },
152 { tt_trk_trkseg, "/gpx/trk/trkseg" },
153 { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
154 { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
155 { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
156 { tt_trk_trkseg_trkpt_name, "/gpx/trk/trkseg/trkpt/name" },
158 { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" },
159 { tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" },
160 { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" },
161 { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" },
163 { tt_trk_trkseg_trkpt_hdop, "/gpx/trk/trkseg/trkpt/hdop" },
164 { tt_trk_trkseg_trkpt_vdop, "/gpx/trk/trkseg/trkpt/vdop" },
165 { tt_trk_trkseg_trkpt_pdop, "/gpx/trk/trkseg/trkpt/pdop" },
167 { tt_rte, "/gpx/rte" },
168 // NB Route reuses track point feature tags
169 { tt_trk_name, "/gpx/rte/name" },
170 { tt_trk_cmt, "/gpx/rte/cmt" },
171 { tt_trk_desc, "/gpx/rte/desc" },
172 { tt_trk_src, "/gpx/rte/src" },
173 { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
174 { tt_trk_trkseg_trkpt_name, "/gpx/rte/rtept/name" },
175 { tt_trk_trkseg_trkpt_ele, "/gpx/rte/rtept/ele" },
180 static tag_type get_tag(const char *t)
183 for (tm = tag_path_map; tm->tag_type != 0; tm++)
184 if (0 == strcmp(tm->tag_name, t))
189 /******************************************/
191 tag_type current_tag = tt_unknown;
192 GString *xpath = NULL;
193 GString *c_cdata = NULL;
195 /* current ("c_") objects */
196 VikTrackpoint *c_tp = NULL;
197 VikWaypoint *c_wp = NULL;
198 VikTrack *c_tr = NULL;
199 VikTRWMetadata *c_md = NULL;
201 gchar *c_wp_name = NULL;
202 gchar *c_tr_name = NULL;
204 /* temporary things so we don't have to create them lots of times */
205 const gchar *c_slat, *c_slon;
208 /* specialty flags / etc */
209 gboolean f_tr_newseg;
210 guint unnamed_waypoints = 0;
211 guint unnamed_tracks = 0;
212 guint unnamed_routes = 0;
214 static const char *get_attr ( const char **attr, const char *key )
217 if ( strcmp(*attr,key) == 0 )
224 static gboolean set_c_ll ( const char **attr )
226 if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) {
227 c_ll.lat = g_ascii_strtod(c_slat, NULL);
228 c_ll.lon = g_ascii_strtod(c_slon, NULL);
234 static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
236 static const gchar *tmp;
238 g_string_append_c ( xpath, '/' );
239 g_string_append ( xpath, el );
240 current_tag = get_tag ( xpath->str );
242 switch ( current_tag ) {
245 c_md = vik_trw_metadata_new();
249 if ( set_c_ll( attr ) ) {
250 c_wp = vik_waypoint_new ();
251 c_wp->visible = TRUE;
252 if ( get_attr ( attr, "hidden" ) )
253 c_wp->visible = FALSE;
255 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
261 c_tr = vik_track_new ();
262 vik_track_set_defaults ( c_tr );
263 c_tr->is_route = (current_tag == tt_rte) ? TRUE : FALSE;
264 c_tr->visible = TRUE;
265 if ( get_attr ( attr, "hidden" ) )
266 c_tr->visible = FALSE;
273 case tt_trk_trkseg_trkpt:
274 if ( set_c_ll( attr ) ) {
275 c_tp = vik_trackpoint_new ();
276 vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
278 c_tp->newsegment = TRUE;
281 c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp );
288 case tt_gpx_keywords:
290 case tt_trk_trkseg_trkpt_name:
291 case tt_trk_trkseg_trkpt_ele:
292 case tt_trk_trkseg_trkpt_time:
305 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */
309 c_wp = vik_waypoint_new ();
310 c_wp->visible = TRUE;
313 case tt_waypoint_coord:
314 if ( set_c_ll( attr ) )
315 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
318 case tt_waypoint_name:
319 if ( ( tmp = get_attr(attr, "id") ) ) {
321 g_free ( c_wp_name );
322 c_wp_name = g_strdup ( tmp );
324 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */
331 static void gpx_end(VikTrwLayer *vtl, const char *el)
333 static GTimeVal tp_time;
334 static GTimeVal wp_time;
336 g_string_truncate ( xpath, xpath->len - strlen(el) - 1 );
338 switch ( current_tag ) {
341 vik_trw_layer_set_metadata ( vtl, c_md );
346 vik_layer_rename ( VIK_LAYER(vtl), c_cdata->str );
347 g_string_erase ( c_cdata, 0, -1 );
352 g_free ( c_md->author );
353 c_md->author = g_strdup ( c_cdata->str );
354 g_string_erase ( c_cdata, 0, -1 );
358 if ( c_md->description )
359 g_free ( c_md->description );
360 c_md->description = g_strdup ( c_cdata->str );
361 g_string_erase ( c_cdata, 0, -1 );
364 case tt_gpx_keywords:
365 if ( c_md->keywords )
366 g_free ( c_md->keywords );
367 c_md->keywords = g_strdup ( c_cdata->str );
368 g_string_erase ( c_cdata, 0, -1 );
372 if ( c_md->timestamp )
373 g_free ( c_md->timestamp );
374 c_md->timestamp = g_strdup ( c_cdata->str );
375 g_string_erase ( c_cdata, 0, -1 );
381 c_wp_name = g_strdup_printf("VIKING_WP%04d", unnamed_waypoints++);
382 vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp );
383 g_free ( c_wp_name );
390 c_tr_name = g_strdup_printf("VIKING_TR%03d", unnamed_tracks++);
391 // Delibrate fall through
394 c_tr_name = g_strdup_printf("VIKING_RT%03d", unnamed_routes++);
395 vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr );
396 g_free ( c_tr_name );
403 g_free ( c_wp_name );
404 c_wp_name = g_strdup ( c_cdata->str );
405 g_string_erase ( c_cdata, 0, -1 );
410 g_free ( c_tr_name );
411 c_tr_name = g_strdup ( c_cdata->str );
412 g_string_erase ( c_cdata, 0, -1 );
416 c_wp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
417 g_string_erase ( c_cdata, 0, -1 );
420 case tt_trk_trkseg_trkpt_ele:
421 c_tp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
422 g_string_erase ( c_cdata, 0, -1 );
425 case tt_waypoint_name: /* .loc name is really description. */
427 vik_waypoint_set_description ( c_wp, c_cdata->str );
428 g_string_erase ( c_cdata, 0, -1 );
432 vik_waypoint_set_comment ( c_wp, c_cdata->str );
433 g_string_erase ( c_cdata, 0, -1 );
437 vik_waypoint_set_source ( c_wp, c_cdata->str );
438 g_string_erase ( c_cdata, 0, -1 );
442 vik_waypoint_set_url ( c_wp, c_cdata->str );
443 g_string_erase ( c_cdata, 0, -1 );
447 vik_waypoint_set_image ( c_wp, c_cdata->str );
448 g_string_erase ( c_cdata, 0, -1 );
452 vik_waypoint_set_symbol ( c_wp, c_cdata->str );
453 g_string_erase ( c_cdata, 0, -1 );
458 vik_track_set_description ( c_tr, c_cdata->str );
459 g_string_erase ( c_cdata, 0, -1 );
463 vik_track_set_source ( c_tr, c_cdata->str );
464 g_string_erase ( c_cdata, 0, -1 );
468 vik_track_set_comment ( c_tr, c_cdata->str );
469 g_string_erase ( c_cdata, 0, -1 );
473 if ( g_time_val_from_iso8601(c_cdata->str, &wp_time) ) {
474 c_wp->timestamp = wp_time.tv_sec;
475 c_wp->has_timestamp = TRUE;
477 g_string_erase ( c_cdata, 0, -1 );
480 case tt_trk_trkseg_trkpt_name:
481 vik_trackpoint_set_name ( c_tp, c_cdata->str );
482 g_string_erase ( c_cdata, 0, -1 );
485 case tt_trk_trkseg_trkpt_time:
486 if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) {
487 c_tp->timestamp = tp_time.tv_sec;
488 c_tp->has_timestamp = TRUE;
490 g_string_erase ( c_cdata, 0, -1 );
493 case tt_trk_trkseg_trkpt_course:
494 c_tp->course = g_ascii_strtod ( c_cdata->str, NULL );
495 g_string_erase ( c_cdata, 0, -1 );
498 case tt_trk_trkseg_trkpt_speed:
499 c_tp->speed = g_ascii_strtod ( c_cdata->str, NULL );
500 g_string_erase ( c_cdata, 0, -1 );
503 case tt_trk_trkseg_trkpt_fix:
504 if (!strcmp("2d", c_cdata->str))
505 c_tp->fix_mode = VIK_GPS_MODE_2D;
506 else if (!strcmp("3d", c_cdata->str))
507 c_tp->fix_mode = VIK_GPS_MODE_3D;
508 else if (!strcmp("dgps", c_cdata->str))
509 c_tp->fix_mode = VIK_GPS_MODE_DGPS;
510 else if (!strcmp("pps", c_cdata->str))
511 c_tp->fix_mode = VIK_GPS_MODE_PPS;
513 c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN;
514 g_string_erase ( c_cdata, 0, -1 );
517 case tt_trk_trkseg_trkpt_sat:
518 c_tp->nsats = atoi ( c_cdata->str );
519 g_string_erase ( c_cdata, 0, -1 );
522 case tt_trk_trkseg_trkpt_hdop:
523 c_tp->hdop = g_strtod ( c_cdata->str, NULL );
524 g_string_erase ( c_cdata, 0, -1 );
527 case tt_trk_trkseg_trkpt_vdop:
528 c_tp->vdop = g_strtod ( c_cdata->str, NULL );
529 g_string_erase ( c_cdata, 0, -1 );
532 case tt_trk_trkseg_trkpt_pdop:
533 c_tp->pdop = g_strtod ( c_cdata->str, NULL );
534 g_string_erase ( c_cdata, 0, -1 );
540 current_tag = get_tag ( xpath->str );
543 static void gpx_cdata(void *dta, const XML_Char *s, int len)
545 switch ( current_tag ) {
549 case tt_gpx_keywords:
554 case tt_trk_trkseg_trkpt_ele:
564 case tt_trk_trkseg_trkpt_time:
566 case tt_trk_trkseg_trkpt_name:
567 case tt_trk_trkseg_trkpt_course:
568 case tt_trk_trkseg_trkpt_speed:
569 case tt_trk_trkseg_trkpt_fix:
570 case tt_trk_trkseg_trkpt_sat:
571 case tt_trk_trkseg_trkpt_hdop:
572 case tt_trk_trkseg_trkpt_vdop:
573 case tt_trk_trkseg_trkpt_pdop:
574 case tt_waypoint_name: /* .loc name is really description. */
575 g_string_append_len ( c_cdata, s, len );
578 default: break; /* ignore cdata from other things */
582 // make like a "stack" of tag names
583 // like gpspoint's separated like /gpx/wpt/whatever
585 gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
586 XML_Parser parser = XML_ParserCreate(NULL);
588 enum XML_Status status = XML_STATUS_ERROR;
590 XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
591 XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
592 XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata);
596 g_assert ( f != NULL && vtl != NULL );
598 xpath = g_string_new ( "" );
599 c_cdata = g_string_new ( "" );
601 unnamed_waypoints = 1;
606 len = fread(buf, 1, sizeof(buf)-7, f);
607 done = feof(f) || !len;
608 status = XML_Parse(parser, buf, len, done);
611 XML_ParserFree (parser);
612 g_string_free ( xpath, TRUE );
613 g_string_free ( c_cdata, TRUE );
615 return status != XML_STATUS_ERROR;
618 /**** entitize from GPSBabel ****/
626 entity_types stdentities[] = {
628 { "'", "'", 1 },
631 { "\"", """, 0 },
635 void utf8_to_int( const char *cp, int *bytes, int *value )
637 if ( (*cp & 0xe0) == 0xc0 ) {
638 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
640 *value = ((*cp & 0x1f) << 6) |
643 else if ( (*cp & 0xf0) == 0xe0 ) {
644 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
645 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
647 *value = ((*cp & 0x0f) << 12) |
648 ((*(cp+1) & 0x3f) << 6) |
651 else if ( (*cp & 0xf8) == 0xf0 ) {
652 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
653 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
654 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
656 *value = ((*cp & 0x07) << 18) |
657 ((*(cp+1) & 0x3f) << 12) |
658 ((*(cp+2) & 0x3f) << 6) |
661 else if ( (*cp & 0xfc) == 0xf8 ) {
662 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
663 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
664 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
665 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
667 *value = ((*cp & 0x03) << 24) |
668 ((*(cp+1) & 0x3f) << 18) |
669 ((*(cp+2) & 0x3f) << 12) |
670 ((*(cp+3) & 0x3f) << 6) |
673 else if ( (*cp & 0xfe) == 0xfc ) {
674 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
675 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
676 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
677 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
678 if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault;
680 *value = ((*cp & 0x01) << 30) |
681 ((*(cp+1) & 0x3f) << 24) |
682 ((*(cp+2) & 0x3f) << 18) |
683 ((*(cp+3) & 0x3f) << 12) |
684 ((*(cp+4) & 0x3f) << 6) |
690 *value = (unsigned char)*cp;
696 entitize(const char * str)
698 int elen, ecount, nsecount;
701 char * p, * tmp, * xstr;
707 elen = ecount = nsecount = 0;
709 /* figure # of entity replacements and additional size. */
712 while ((cp = strstr(cp, ep->text)) != NULL) {
713 elen += strlen(ep->entity) - strlen(ep->text);
715 cp += strlen(ep->text);
720 /* figure the same for other than standard entities (i.e. anything
721 * that isn't in the range U+0000 to U+007F */
722 for ( cp = str; *cp; cp++ ) {
725 utf8_to_int( cp, &bytes, &value );
727 elen += sprintf( tmpsub, "&#x%x;", value ) - bytes;
732 /* enough space for the whole string plus entity replacements, if any */
733 tmp = g_malloc((strlen(str) + elen + 1));
736 /* no entity replacements */
737 if (ecount == 0 && nsecount == 0)
741 for (ep = stdentities; ep->text; ep++) {
743 while ((p = strstr(p, ep->text)) != NULL) {
744 elen = strlen(ep->entity);
746 xstr = g_strdup(p + strlen(ep->text));
748 strcpy(p, ep->entity);
749 strcpy(p + elen, xstr);
758 if ( nsecount != 0 ) {
762 utf8_to_int( p, &bytes, &value );
764 xstr = g_strdup( p + bytes );
769 sprintf( p, "&#x%x;", value );
783 /**** end GPSBabel code ****/
787 static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
789 // Don't write invisible waypoints when specified
790 if (context->options && !context->options->hidden && !wp->visible)
793 FILE *f = context->file;
794 static struct LatLon ll;
797 vik_coord_to_latlon ( &(wp->coord), &ll );
798 s_lat = a_coords_dtostr( ll.lat );
799 s_lon = a_coords_dtostr( ll.lon );
800 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
801 // luckily most other GPX processing software ignores things they don't understand
802 fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
803 s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );
809 tmp = entitize ( wp->name );
811 tmp = g_strdup ("waypoint");
813 fprintf ( f, " <name>%s</name>\n", tmp );
816 if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
818 tmp = a_coords_dtostr ( wp->altitude );
819 fprintf ( f, " <ele>%s</ele>\n", tmp );
823 if ( wp->has_timestamp ) {
825 timestamp.tv_sec = wp->timestamp;
826 timestamp.tv_usec = 0;
828 gchar *time_iso8601 = g_time_val_to_iso8601 ( ×tamp );
829 if ( time_iso8601 != NULL )
830 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
831 g_free ( time_iso8601 );
836 tmp = entitize(wp->comment);
837 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
840 if ( wp->description )
842 tmp = entitize(wp->description);
843 fprintf ( f, " <desc>%s</desc>\n", tmp );
848 tmp = entitize(wp->source);
849 fprintf ( f, " <src>%s</src>\n", tmp );
854 tmp = entitize(wp->url);
855 fprintf ( f, " <url>%s</url>\n", tmp );
860 tmp = entitize(wp->image);
861 fprintf ( f, " <link>%s</link>\n", tmp );
866 tmp = entitize(wp->symbol);
867 if ( a_vik_gpx_export_wpt_sym_name ( ) ) {
868 // Lowercase the symbol name
869 gchar *tmp2 = g_utf8_strdown ( tmp, -1 );
870 fprintf ( f, " <sym>%s</sym>\n", tmp2 );
874 fprintf ( f, " <sym>%s</sym>\n", tmp);
878 fprintf ( f, "</wpt>\n" );
881 static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context )
883 FILE *f = context->file;
884 static struct LatLon ll;
885 gchar *s_lat,*s_lon, *s_alt, *s_dop;
887 vik_coord_to_latlon ( &(tp->coord), &ll );
889 // No such thing as a rteseg! So make sure we don't put them in
890 if ( context->options && !context->options->is_route && tp->newsegment )
891 fprintf ( f, " </trkseg>\n <trkseg>\n" );
893 s_lat = a_coords_dtostr( ll.lat );
894 s_lon = a_coords_dtostr( ll.lon );
895 fprintf ( f, " <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon );
896 g_free ( s_lat ); s_lat = NULL;
897 g_free ( s_lon ); s_lon = NULL;
900 gchar *s_name = entitize(tp->name);
901 fprintf ( f, " <name>%s</name>\n", s_name );
906 if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
908 s_alt = a_coords_dtostr ( tp->altitude );
910 else if ( context->options != NULL && context->options->force_ele )
912 s_alt = a_coords_dtostr ( 0 );
915 fprintf ( f, " <ele>%s</ele>\n", s_alt );
916 g_free ( s_alt ); s_alt = NULL;
919 if ( tp->has_timestamp ) {
921 timestamp.tv_sec = tp->timestamp;
922 timestamp.tv_usec = 0;
924 time_iso8601 = g_time_val_to_iso8601 ( ×tamp );
926 else if ( context->options != NULL && context->options->force_time )
929 g_get_current_time ( ¤t );
931 time_iso8601 = g_time_val_to_iso8601 ( ¤t );
933 if ( time_iso8601 != NULL )
934 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
935 g_free(time_iso8601);
938 if (!isnan(tp->course)) {
939 gchar *s_course = a_coords_dtostr(tp->course);
940 fprintf ( f, " <course>%s</course>\n", s_course );
943 if (!isnan(tp->speed)) {
944 gchar *s_speed = a_coords_dtostr(tp->speed);
945 fprintf ( f, " <speed>%s</speed>\n", s_speed );
948 if (tp->fix_mode == VIK_GPS_MODE_2D)
949 fprintf ( f, " <fix>2d</fix>\n");
950 if (tp->fix_mode == VIK_GPS_MODE_3D)
951 fprintf ( f, " <fix>3d</fix>\n");
952 if (tp->fix_mode == VIK_GPS_MODE_DGPS)
953 fprintf ( f, " <fix>dgps</fix>\n");
954 if (tp->fix_mode == VIK_GPS_MODE_PPS)
955 fprintf ( f, " <fix>pps</fix>\n");
957 fprintf ( f, " <sat>%d</sat>\n", tp->nsats );
960 if ( tp->hdop != VIK_DEFAULT_DOP )
962 s_dop = a_coords_dtostr ( tp->hdop );
965 fprintf ( f, " <hdop>%s</hdop>\n", s_dop );
966 g_free ( s_dop ); s_dop = NULL;
968 if ( tp->vdop != VIK_DEFAULT_DOP )
970 s_dop = a_coords_dtostr ( tp->vdop );
973 fprintf ( f, " <vdop>%s</vdop>\n", s_dop );
974 g_free ( s_dop ); s_dop = NULL;
976 if ( tp->pdop != VIK_DEFAULT_DOP )
978 s_dop = a_coords_dtostr ( tp->pdop );
981 fprintf ( f, " <pdop>%s</pdop>\n", s_dop );
982 g_free ( s_dop ); s_dop = NULL;
984 fprintf ( f, " </%spt>\n", (context->options && context->options->is_route) ? "rte" : "trk" );
988 static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
990 // Don't write invisible tracks when specified
991 if (context->options && !context->options->hidden && !t->visible)
994 FILE *f = context->file;
996 gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */
1000 tmp = entitize ( t->name );
1002 tmp = g_strdup ("track");
1004 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
1005 // luckily most other GPX processing software ignores things they don't understand
1006 fprintf ( f, "<%s%s>\n <name>%s</name>\n",
1007 t->is_route ? "rte" : "trk",
1008 t->visible ? "" : " hidden=\"hidden\"",
1014 tmp = entitize ( t->comment );
1015 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
1019 if ( t->description )
1021 tmp = entitize ( t->description );
1022 fprintf ( f, " <desc>%s</desc>\n", tmp );
1028 tmp = entitize ( t->source );
1029 fprintf ( f, " <src>%s</src>\n", tmp );
1033 /* No such thing as a rteseg! */
1035 fprintf ( f, " <trkseg>\n" );
1037 if ( t->trackpoints && t->trackpoints->data ) {
1038 first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment;
1039 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */
1040 g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context );
1041 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
1044 /* NB apparently no such thing as a rteseg! */
1046 fprintf ( f, " </trkseg>\n");
1048 fprintf ( f, "</%s>\n", t->is_route ? "rte" : "trk" );
1051 static void gpx_write_header( FILE *f )
1053 fprintf(f, "<?xml version=\"1.0\"?>\n"
1054 "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
1055 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
1056 "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
1057 "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
1060 static void gpx_write_footer( FILE *f )
1062 fprintf(f, "</gpx>\n");
1065 static int gpx_waypoint_compare(const void *x, const void *y)
1067 VikWaypoint *a = (VikWaypoint *)x;
1068 VikWaypoint *b = (VikWaypoint *)y;
1069 return strcmp(a->name,b->name);
1072 static int gpx_track_compare_name(const void *x, const void *y)
1074 VikTrack *a = (VikTrack *)x;
1075 VikTrack *b = (VikTrack *)y;
1076 return strcmp(a->name,b->name);
1079 void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
1081 GpxWritingContext context = { options, f };
1083 gpx_write_header ( f );
1086 const gchar *name = vik_layer_get_name(VIK_LAYER(vtl));
1088 tmp = entitize ( name );
1089 fprintf ( f, " <name>%s</name>\n", tmp );
1093 VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
1096 tmp = entitize ( md->author );
1097 fprintf ( f, " <author>%s</author>\n", tmp );
1100 if ( md->description ) {
1101 tmp = entitize ( md->description );
1102 fprintf ( f, " <desc>%s</desc>\n", tmp );
1105 if ( md->timestamp ) {
1106 tmp = entitize ( md->timestamp );
1107 fprintf ( f, " <time>%s</time>\n", tmp );
1110 if ( md->keywords ) {
1111 tmp = entitize ( md->keywords );
1112 fprintf ( f, " <keywords>%s</keywords>\n", tmp );
1117 if ( vik_trw_layer_get_waypoints_visibility(vtl) || (options && options->hidden) ) {
1118 // gather waypoints in a list, then sort
1119 GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
1120 gl = g_list_sort ( gl, gpx_waypoint_compare );
1122 for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
1123 gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
1129 if ( vik_trw_layer_get_tracks_visibility(vtl) || (options && options->hidden) ) {
1130 //gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
1131 // Forming the list manually seems to produce one that is more likely to be nearer to the creation order
1132 gpointer key, value;
1133 GHashTableIter ght_iter;
1134 g_hash_table_iter_init ( &ght_iter, vik_trw_layer_get_tracks ( vtl ) );
1135 while ( g_hash_table_iter_next (&ght_iter, &key, &value) ) {
1136 gl = g_list_prepend ( gl ,value );
1138 gl = g_list_reverse ( gl );
1140 // Sort method determined by preference
1141 if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
1142 gl = g_list_sort ( gl, vik_track_compare_timestamp );
1143 else if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_ALPHA )
1144 gl = g_list_sort ( gl, gpx_track_compare_name );
1147 GList *glrte = NULL;
1148 // Routes sorted by name
1149 if ( vik_trw_layer_get_routes_visibility(vtl) || (options && options->hidden) ) {
1150 glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
1151 glrte = g_list_sort ( glrte, gpx_track_compare_name );
1154 // g_list_concat doesn't copy memory properly
1155 // so process each list separately
1157 GpxWritingContext context_tmp = context;
1158 GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE, FALSE };
1159 // Force trackpoints on tracks
1160 if ( !context.options )
1161 context_tmp.options = &opt_tmp;
1162 context_tmp.options->is_route = FALSE;
1164 // Loop around each list and write each one
1165 for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
1166 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
1169 // Routes (to get routepoints)
1170 context_tmp.options->is_route = TRUE;
1171 for (GList *iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
1172 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
1176 g_list_free ( glrte );
1178 gpx_write_footer ( f );
1181 void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
1183 GpxWritingContext context = {options, f};
1184 gpx_write_header ( f );
1185 gpx_write_track ( trk, &context );
1186 gpx_write_footer ( f );
1190 * Common write of a temporary GPX file
1192 static gchar* write_tmp_file ( VikTrwLayer *vtl, VikTrack *trk, GpxWritingOptions *options )
1194 gchar *tmp_filename = NULL;
1195 GError *error = NULL;
1196 // Opening temporary file
1197 int fd = g_file_open_tmp("viking_XXXXXX.gpx", &tmp_filename, &error);
1199 g_warning ( _("failed to open temporary file: %s"), error->message );
1200 g_clear_error ( &error );
1203 g_debug ("%s: temporary file = %s", __FUNCTION__, tmp_filename);
1205 FILE *ff = fdopen (fd, "w");
1208 a_gpx_write_track_file ( trk, ff, options );
1210 a_gpx_write_file ( vtl, ff, options );
1214 return tmp_filename;
1218 * a_gpx_write_tmp_file:
1219 * @vtl: The #VikTrwLayer to write
1220 * @options: Possible ways of writing the file data (can be NULL)
1222 * Returns: The name of newly created temporary GPX file
1223 * This file should be removed once used and the string freed.
1224 * If NULL then the process failed.
1226 gchar* a_gpx_write_tmp_file ( VikTrwLayer *vtl, GpxWritingOptions *options )
1228 return write_tmp_file ( vtl, NULL, options );
1232 * a_gpx_write_track_tmp_file:
1233 * @trk: The #VikTrack to write
1234 * @options: Possible ways of writing the file data (can be NULL)
1236 * Returns: The name of newly created temporary GPX file
1237 * This file should be removed once used and the string freed.
1238 * If NULL then the process failed.
1240 gchar* a_gpx_write_track_tmp_file ( VikTrack *trk, GpxWritingOptions *options )
1242 return write_tmp_file ( NULL, trk, options );