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>
69 tt_wpt_link, /* New in GPX 1.1 */
82 tt_trk_trkseg_trkpt_ele,
83 tt_trk_trkseg_trkpt_time,
84 tt_trk_trkseg_trkpt_name,
86 tt_trk_trkseg_trkpt_course,
87 tt_trk_trkseg_trkpt_speed,
88 tt_trk_trkseg_trkpt_fix,
89 tt_trk_trkseg_trkpt_sat,
91 tt_trk_trkseg_trkpt_hdop,
92 tt_trk_trkseg_trkpt_vdop,
93 tt_trk_trkseg_trkpt_pdop,
100 typedef struct tag_mapping {
101 tag_type tag_type; /* enum from above for this tag */
102 const char *tag_name; /* xpath-ish tag name */
106 GpxWritingOptions *options;
111 * xpath(ish) mappings between full tag paths and internal identifiers.
112 * These appear in the order they appear in the GPX specification.
113 * If it's not a tag we explicitly handle, it doesn't go here.
116 tag_mapping tag_path_map[] = {
119 { tt_gpx_name, "/gpx/name" },
120 { tt_gpx_desc, "/gpx/desc" },
121 { tt_gpx_time, "/gpx/time" },
122 { tt_gpx_author, "/gpx/author" },
123 { tt_gpx_keywords, "/gpx/keywords" },
125 // GPX 1.1 variant - basic properties moved into metadata namespace
126 { tt_gpx_name, "/gpx/metadata/name" },
127 { tt_gpx_desc, "/gpx/metadata/desc" },
128 { tt_gpx_time, "/gpx/metadata/time" },
129 { tt_gpx_author, "/gpx/metadata/author" },
130 { tt_gpx_keywords, "/gpx/metadata/keywords" },
132 { tt_wpt, "/gpx/wpt" },
134 { tt_waypoint, "/loc/waypoint" },
135 { tt_waypoint_coord, "/loc/waypoint/coord" },
136 { tt_waypoint_name, "/loc/waypoint/name" },
138 { tt_wpt_ele, "/gpx/wpt/ele" },
139 { tt_wpt_time, "/gpx/wpt/time" },
140 { tt_wpt_name, "/gpx/wpt/name" },
141 { tt_wpt_cmt, "/gpx/wpt/cmt" },
142 { tt_wpt_desc, "/gpx/wpt/desc" },
143 { tt_wpt_src, "/gpx/wpt/src" },
144 { tt_wpt_type, "/gpx/wpt/type" },
145 { tt_wpt_sym, "/gpx/wpt/sym" },
146 { tt_wpt_sym, "/loc/waypoint/type" },
147 { tt_wpt_url, "/gpx/wpt/url" },
148 { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */
150 { tt_trk, "/gpx/trk" },
151 { tt_trk_name, "/gpx/trk/name" },
152 { tt_trk_cmt, "/gpx/trk/cmt" },
153 { tt_trk_desc, "/gpx/trk/desc" },
154 { tt_trk_src, "/gpx/trk/src" },
155 { tt_trk_type, "/gpx/trk/type" },
156 { tt_trk_trkseg, "/gpx/trk/trkseg" },
157 { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
158 { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
159 { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
160 { tt_trk_trkseg_trkpt_name, "/gpx/trk/trkseg/trkpt/name" },
162 { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" },
163 { tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" },
164 { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" },
165 { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" },
167 { tt_trk_trkseg_trkpt_hdop, "/gpx/trk/trkseg/trkpt/hdop" },
168 { tt_trk_trkseg_trkpt_vdop, "/gpx/trk/trkseg/trkpt/vdop" },
169 { tt_trk_trkseg_trkpt_pdop, "/gpx/trk/trkseg/trkpt/pdop" },
171 { tt_rte, "/gpx/rte" },
172 // NB Route reuses track point feature tags
173 { tt_trk_name, "/gpx/rte/name" },
174 { tt_trk_cmt, "/gpx/rte/cmt" },
175 { tt_trk_desc, "/gpx/rte/desc" },
176 { tt_trk_src, "/gpx/rte/src" },
177 { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
178 { tt_trk_trkseg_trkpt_name, "/gpx/rte/rtept/name" },
179 { tt_trk_trkseg_trkpt_ele, "/gpx/rte/rtept/ele" },
184 static tag_type get_tag(const char *t)
187 for (tm = tag_path_map; tm->tag_type != 0; tm++)
188 if (0 == strcmp(tm->tag_name, t))
193 /******************************************/
195 tag_type current_tag = tt_unknown;
196 GString *xpath = NULL;
197 GString *c_cdata = NULL;
199 /* current ("c_") objects */
200 VikTrackpoint *c_tp = NULL;
201 VikWaypoint *c_wp = NULL;
202 VikTrack *c_tr = NULL;
203 VikTRWMetadata *c_md = NULL;
205 gchar *c_wp_name = NULL;
206 gchar *c_tr_name = NULL;
208 /* temporary things so we don't have to create them lots of times */
209 const gchar *c_slat, *c_slon;
212 /* specialty flags / etc */
213 gboolean f_tr_newseg;
214 guint unnamed_waypoints = 0;
215 guint unnamed_tracks = 0;
216 guint unnamed_routes = 0;
218 static const char *get_attr ( const char **attr, const char *key )
221 if ( strcmp(*attr,key) == 0 )
228 static gboolean set_c_ll ( const char **attr )
230 if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) {
231 c_ll.lat = g_ascii_strtod(c_slat, NULL);
232 c_ll.lon = g_ascii_strtod(c_slon, NULL);
238 static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
240 static const gchar *tmp;
242 g_string_append_c ( xpath, '/' );
243 g_string_append ( xpath, el );
244 current_tag = get_tag ( xpath->str );
246 switch ( current_tag ) {
249 c_md = vik_trw_metadata_new();
253 if ( set_c_ll( attr ) ) {
254 c_wp = vik_waypoint_new ();
255 c_wp->visible = TRUE;
256 if ( get_attr ( attr, "hidden" ) )
257 c_wp->visible = FALSE;
259 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
265 c_tr = vik_track_new ();
266 vik_track_set_defaults ( c_tr );
267 c_tr->is_route = (current_tag == tt_rte) ? TRUE : FALSE;
268 c_tr->visible = TRUE;
269 if ( get_attr ( attr, "hidden" ) )
270 c_tr->visible = FALSE;
277 case tt_trk_trkseg_trkpt:
278 if ( set_c_ll( attr ) ) {
279 c_tp = vik_trackpoint_new ();
280 vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
282 c_tp->newsegment = TRUE;
285 c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp );
292 case tt_gpx_keywords:
294 case tt_trk_trkseg_trkpt_name:
295 case tt_trk_trkseg_trkpt_ele:
296 case tt_trk_trkseg_trkpt_time:
311 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */
315 c_wp = vik_waypoint_new ();
316 c_wp->visible = TRUE;
319 case tt_waypoint_coord:
320 if ( set_c_ll( attr ) )
321 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
324 case tt_waypoint_name:
325 if ( ( tmp = get_attr(attr, "id") ) ) {
327 g_free ( c_wp_name );
328 c_wp_name = g_strdup ( tmp );
330 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */
337 static void gpx_end(VikTrwLayer *vtl, const char *el)
339 static GTimeVal tp_time;
340 static GTimeVal wp_time;
342 g_string_truncate ( xpath, xpath->len - strlen(el) - 1 );
344 switch ( current_tag ) {
347 vik_trw_layer_set_metadata ( vtl, c_md );
352 vik_layer_rename ( VIK_LAYER(vtl), c_cdata->str );
353 g_string_erase ( c_cdata, 0, -1 );
358 g_free ( c_md->author );
359 c_md->author = g_strdup ( c_cdata->str );
360 g_string_erase ( c_cdata, 0, -1 );
364 if ( c_md->description )
365 g_free ( c_md->description );
366 c_md->description = g_strdup ( c_cdata->str );
367 g_string_erase ( c_cdata, 0, -1 );
370 case tt_gpx_keywords:
371 if ( c_md->keywords )
372 g_free ( c_md->keywords );
373 c_md->keywords = g_strdup ( c_cdata->str );
374 g_string_erase ( c_cdata, 0, -1 );
378 if ( c_md->timestamp )
379 g_free ( c_md->timestamp );
380 c_md->timestamp = g_strdup ( c_cdata->str );
381 g_string_erase ( c_cdata, 0, -1 );
387 c_wp_name = g_strdup_printf("VIKING_WP%04d", unnamed_waypoints++);
388 vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp );
389 g_free ( c_wp_name );
396 c_tr_name = g_strdup_printf("VIKING_TR%03d", unnamed_tracks++);
397 // Delibrate fall through
400 c_tr_name = g_strdup_printf("VIKING_RT%03d", unnamed_routes++);
401 vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr );
402 g_free ( c_tr_name );
409 g_free ( c_wp_name );
410 c_wp_name = g_strdup ( c_cdata->str );
411 g_string_erase ( c_cdata, 0, -1 );
416 g_free ( c_tr_name );
417 c_tr_name = g_strdup ( c_cdata->str );
418 g_string_erase ( c_cdata, 0, -1 );
422 c_wp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
423 g_string_erase ( c_cdata, 0, -1 );
426 case tt_trk_trkseg_trkpt_ele:
427 c_tp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
428 g_string_erase ( c_cdata, 0, -1 );
431 case tt_waypoint_name: /* .loc name is really description. */
433 vik_waypoint_set_description ( c_wp, c_cdata->str );
434 g_string_erase ( c_cdata, 0, -1 );
438 vik_waypoint_set_comment ( c_wp, c_cdata->str );
439 g_string_erase ( c_cdata, 0, -1 );
443 vik_waypoint_set_source ( c_wp, c_cdata->str );
444 g_string_erase ( c_cdata, 0, -1 );
448 vik_waypoint_set_type ( c_wp, c_cdata->str );
449 g_string_erase ( c_cdata, 0, -1 );
453 vik_waypoint_set_url ( c_wp, c_cdata->str );
454 g_string_erase ( c_cdata, 0, -1 );
458 vik_waypoint_set_image ( c_wp, c_cdata->str );
459 g_string_erase ( c_cdata, 0, -1 );
463 vik_waypoint_set_symbol ( c_wp, c_cdata->str );
464 g_string_erase ( c_cdata, 0, -1 );
469 vik_track_set_description ( c_tr, c_cdata->str );
470 g_string_erase ( c_cdata, 0, -1 );
474 vik_track_set_source ( c_tr, c_cdata->str );
475 g_string_erase ( c_cdata, 0, -1 );
479 vik_track_set_type ( c_tr, c_cdata->str );
480 g_string_erase ( c_cdata, 0, -1 );
484 vik_track_set_comment ( c_tr, c_cdata->str );
485 g_string_erase ( c_cdata, 0, -1 );
489 if ( g_time_val_from_iso8601(c_cdata->str, &wp_time) ) {
490 c_wp->timestamp = wp_time.tv_sec;
491 c_wp->has_timestamp = TRUE;
493 g_string_erase ( c_cdata, 0, -1 );
496 case tt_trk_trkseg_trkpt_name:
497 vik_trackpoint_set_name ( c_tp, c_cdata->str );
498 g_string_erase ( c_cdata, 0, -1 );
501 case tt_trk_trkseg_trkpt_time:
502 if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) {
503 c_tp->timestamp = tp_time.tv_sec;
504 c_tp->has_timestamp = TRUE;
506 g_string_erase ( c_cdata, 0, -1 );
509 case tt_trk_trkseg_trkpt_course:
510 c_tp->course = g_ascii_strtod ( c_cdata->str, NULL );
511 g_string_erase ( c_cdata, 0, -1 );
514 case tt_trk_trkseg_trkpt_speed:
515 c_tp->speed = g_ascii_strtod ( c_cdata->str, NULL );
516 g_string_erase ( c_cdata, 0, -1 );
519 case tt_trk_trkseg_trkpt_fix:
520 if (!strcmp("2d", c_cdata->str))
521 c_tp->fix_mode = VIK_GPS_MODE_2D;
522 else if (!strcmp("3d", c_cdata->str))
523 c_tp->fix_mode = VIK_GPS_MODE_3D;
524 else if (!strcmp("dgps", c_cdata->str))
525 c_tp->fix_mode = VIK_GPS_MODE_DGPS;
526 else if (!strcmp("pps", c_cdata->str))
527 c_tp->fix_mode = VIK_GPS_MODE_PPS;
529 c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN;
530 g_string_erase ( c_cdata, 0, -1 );
533 case tt_trk_trkseg_trkpt_sat:
534 c_tp->nsats = atoi ( c_cdata->str );
535 g_string_erase ( c_cdata, 0, -1 );
538 case tt_trk_trkseg_trkpt_hdop:
539 c_tp->hdop = g_strtod ( c_cdata->str, NULL );
540 g_string_erase ( c_cdata, 0, -1 );
543 case tt_trk_trkseg_trkpt_vdop:
544 c_tp->vdop = g_strtod ( c_cdata->str, NULL );
545 g_string_erase ( c_cdata, 0, -1 );
548 case tt_trk_trkseg_trkpt_pdop:
549 c_tp->pdop = g_strtod ( c_cdata->str, NULL );
550 g_string_erase ( c_cdata, 0, -1 );
556 current_tag = get_tag ( xpath->str );
559 static void gpx_cdata(void *dta, const XML_Char *s, int len)
561 switch ( current_tag ) {
565 case tt_gpx_keywords:
570 case tt_trk_trkseg_trkpt_ele:
582 case tt_trk_trkseg_trkpt_time:
584 case tt_trk_trkseg_trkpt_name:
585 case tt_trk_trkseg_trkpt_course:
586 case tt_trk_trkseg_trkpt_speed:
587 case tt_trk_trkseg_trkpt_fix:
588 case tt_trk_trkseg_trkpt_sat:
589 case tt_trk_trkseg_trkpt_hdop:
590 case tt_trk_trkseg_trkpt_vdop:
591 case tt_trk_trkseg_trkpt_pdop:
592 case tt_waypoint_name: /* .loc name is really description. */
593 g_string_append_len ( c_cdata, s, len );
596 default: break; /* ignore cdata from other things */
600 // make like a "stack" of tag names
601 // like gpspoint's separated like /gpx/wpt/whatever
603 gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
604 XML_Parser parser = XML_ParserCreate(NULL);
606 enum XML_Status status = XML_STATUS_ERROR;
608 XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
609 XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
610 XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata);
614 g_assert ( f != NULL && vtl != NULL );
616 xpath = g_string_new ( "" );
617 c_cdata = g_string_new ( "" );
619 unnamed_waypoints = 1;
624 len = fread(buf, 1, sizeof(buf)-7, f);
625 done = feof(f) || !len;
626 status = XML_Parse(parser, buf, len, done);
629 XML_ParserFree (parser);
630 g_string_free ( xpath, TRUE );
631 g_string_free ( c_cdata, TRUE );
633 return status != XML_STATUS_ERROR;
636 /**** entitize from GPSBabel ****/
644 entity_types stdentities[] = {
646 { "'", "'", 1 },
649 { "\"", """, 0 },
653 void utf8_to_int( const char *cp, int *bytes, int *value )
655 if ( (*cp & 0xe0) == 0xc0 ) {
656 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
658 *value = ((*cp & 0x1f) << 6) |
661 else if ( (*cp & 0xf0) == 0xe0 ) {
662 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
663 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
665 *value = ((*cp & 0x0f) << 12) |
666 ((*(cp+1) & 0x3f) << 6) |
669 else if ( (*cp & 0xf8) == 0xf0 ) {
670 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
671 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
672 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
674 *value = ((*cp & 0x07) << 18) |
675 ((*(cp+1) & 0x3f) << 12) |
676 ((*(cp+2) & 0x3f) << 6) |
679 else if ( (*cp & 0xfc) == 0xf8 ) {
680 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
681 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
682 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
683 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
685 *value = ((*cp & 0x03) << 24) |
686 ((*(cp+1) & 0x3f) << 18) |
687 ((*(cp+2) & 0x3f) << 12) |
688 ((*(cp+3) & 0x3f) << 6) |
691 else if ( (*cp & 0xfe) == 0xfc ) {
692 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
693 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
694 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
695 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
696 if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault;
698 *value = ((*cp & 0x01) << 30) |
699 ((*(cp+1) & 0x3f) << 24) |
700 ((*(cp+2) & 0x3f) << 18) |
701 ((*(cp+3) & 0x3f) << 12) |
702 ((*(cp+4) & 0x3f) << 6) |
708 *value = (unsigned char)*cp;
714 entitize(const char * str)
716 int elen, ecount, nsecount;
719 char * p, * tmp, * xstr;
725 elen = ecount = nsecount = 0;
727 /* figure # of entity replacements and additional size. */
730 while ((cp = strstr(cp, ep->text)) != NULL) {
731 elen += strlen(ep->entity) - strlen(ep->text);
733 cp += strlen(ep->text);
738 /* figure the same for other than standard entities (i.e. anything
739 * that isn't in the range U+0000 to U+007F */
740 for ( cp = str; *cp; cp++ ) {
743 utf8_to_int( cp, &bytes, &value );
745 elen += sprintf( tmpsub, "&#x%x;", value ) - bytes;
750 /* enough space for the whole string plus entity replacements, if any */
751 tmp = g_malloc((strlen(str) + elen + 1));
754 /* no entity replacements */
755 if (ecount == 0 && nsecount == 0)
759 for (ep = stdentities; ep->text; ep++) {
761 while ((p = strstr(p, ep->text)) != NULL) {
762 elen = strlen(ep->entity);
764 xstr = g_strdup(p + strlen(ep->text));
766 strcpy(p, ep->entity);
767 strcpy(p + elen, xstr);
776 if ( nsecount != 0 ) {
780 utf8_to_int( p, &bytes, &value );
782 xstr = g_strdup( p + bytes );
787 sprintf( p, "&#x%x;", value );
801 /**** end GPSBabel code ****/
805 static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
807 // Don't write invisible waypoints when specified
808 if (context->options && !context->options->hidden && !wp->visible)
811 FILE *f = context->file;
812 static struct LatLon ll;
815 vik_coord_to_latlon ( &(wp->coord), &ll );
816 s_lat = a_coords_dtostr( ll.lat );
817 s_lon = a_coords_dtostr( ll.lon );
818 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
819 // luckily most other GPX processing software ignores things they don't understand
820 fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
821 s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );
827 tmp = entitize ( wp->name );
829 tmp = g_strdup ("waypoint");
831 fprintf ( f, " <name>%s</name>\n", tmp );
834 if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
836 tmp = a_coords_dtostr ( wp->altitude );
837 fprintf ( f, " <ele>%s</ele>\n", tmp );
841 if ( wp->has_timestamp ) {
843 timestamp.tv_sec = wp->timestamp;
844 timestamp.tv_usec = 0;
846 gchar *time_iso8601 = g_time_val_to_iso8601 ( ×tamp );
847 if ( time_iso8601 != NULL )
848 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
849 g_free ( time_iso8601 );
854 tmp = entitize(wp->comment);
855 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
858 if ( wp->description )
860 tmp = entitize(wp->description);
861 fprintf ( f, " <desc>%s</desc>\n", tmp );
866 tmp = entitize(wp->source);
867 fprintf ( f, " <src>%s</src>\n", tmp );
872 tmp = entitize(wp->type);
873 fprintf ( f, " <type>%s</type>\n", tmp );
878 tmp = entitize(wp->url);
879 fprintf ( f, " <url>%s</url>\n", tmp );
884 tmp = entitize(wp->image);
885 fprintf ( f, " <link>%s</link>\n", tmp );
890 tmp = entitize(wp->symbol);
891 if ( a_vik_gpx_export_wpt_sym_name ( ) ) {
892 // Lowercase the symbol name
893 gchar *tmp2 = g_utf8_strdown ( tmp, -1 );
894 fprintf ( f, " <sym>%s</sym>\n", tmp2 );
898 fprintf ( f, " <sym>%s</sym>\n", tmp);
902 fprintf ( f, "</wpt>\n" );
905 static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context )
907 FILE *f = context->file;
908 static struct LatLon ll;
909 gchar *s_lat,*s_lon, *s_alt, *s_dop;
911 vik_coord_to_latlon ( &(tp->coord), &ll );
913 // No such thing as a rteseg! So make sure we don't put them in
914 if ( context->options && !context->options->is_route && tp->newsegment )
915 fprintf ( f, " </trkseg>\n <trkseg>\n" );
917 s_lat = a_coords_dtostr( ll.lat );
918 s_lon = a_coords_dtostr( ll.lon );
919 fprintf ( f, " <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon );
920 g_free ( s_lat ); s_lat = NULL;
921 g_free ( s_lon ); s_lon = NULL;
924 gchar *s_name = entitize(tp->name);
925 fprintf ( f, " <name>%s</name>\n", s_name );
930 if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
932 s_alt = a_coords_dtostr ( tp->altitude );
934 else if ( context->options != NULL && context->options->force_ele )
936 s_alt = a_coords_dtostr ( 0 );
939 fprintf ( f, " <ele>%s</ele>\n", s_alt );
940 g_free ( s_alt ); s_alt = NULL;
943 if ( tp->has_timestamp ) {
945 timestamp.tv_sec = tp->timestamp;
946 timestamp.tv_usec = 0;
948 time_iso8601 = g_time_val_to_iso8601 ( ×tamp );
950 else if ( context->options != NULL && context->options->force_time )
953 g_get_current_time ( ¤t );
955 time_iso8601 = g_time_val_to_iso8601 ( ¤t );
957 if ( time_iso8601 != NULL )
958 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
959 g_free(time_iso8601);
962 if (!isnan(tp->course)) {
963 gchar *s_course = a_coords_dtostr(tp->course);
964 fprintf ( f, " <course>%s</course>\n", s_course );
967 if (!isnan(tp->speed)) {
968 gchar *s_speed = a_coords_dtostr(tp->speed);
969 fprintf ( f, " <speed>%s</speed>\n", s_speed );
972 if (tp->fix_mode == VIK_GPS_MODE_2D)
973 fprintf ( f, " <fix>2d</fix>\n");
974 if (tp->fix_mode == VIK_GPS_MODE_3D)
975 fprintf ( f, " <fix>3d</fix>\n");
976 if (tp->fix_mode == VIK_GPS_MODE_DGPS)
977 fprintf ( f, " <fix>dgps</fix>\n");
978 if (tp->fix_mode == VIK_GPS_MODE_PPS)
979 fprintf ( f, " <fix>pps</fix>\n");
981 fprintf ( f, " <sat>%d</sat>\n", tp->nsats );
984 if ( tp->hdop != VIK_DEFAULT_DOP )
986 s_dop = a_coords_dtostr ( tp->hdop );
989 fprintf ( f, " <hdop>%s</hdop>\n", s_dop );
990 g_free ( s_dop ); s_dop = NULL;
992 if ( tp->vdop != VIK_DEFAULT_DOP )
994 s_dop = a_coords_dtostr ( tp->vdop );
997 fprintf ( f, " <vdop>%s</vdop>\n", s_dop );
998 g_free ( s_dop ); s_dop = NULL;
1000 if ( tp->pdop != VIK_DEFAULT_DOP )
1002 s_dop = a_coords_dtostr ( tp->pdop );
1005 fprintf ( f, " <pdop>%s</pdop>\n", s_dop );
1006 g_free ( s_dop ); s_dop = NULL;
1008 fprintf ( f, " </%spt>\n", (context->options && context->options->is_route) ? "rte" : "trk" );
1012 static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
1014 // Don't write invisible tracks when specified
1015 if (context->options && !context->options->hidden && !t->visible)
1018 FILE *f = context->file;
1020 gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */
1024 tmp = entitize ( t->name );
1026 tmp = g_strdup ("track");
1028 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
1029 // luckily most other GPX processing software ignores things they don't understand
1030 fprintf ( f, "<%s%s>\n <name>%s</name>\n",
1031 t->is_route ? "rte" : "trk",
1032 t->visible ? "" : " hidden=\"hidden\"",
1038 tmp = entitize ( t->comment );
1039 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
1043 if ( t->description )
1045 tmp = entitize ( t->description );
1046 fprintf ( f, " <desc>%s</desc>\n", tmp );
1052 tmp = entitize ( t->source );
1053 fprintf ( f, " <src>%s</src>\n", tmp );
1059 tmp = entitize ( t->type );
1060 fprintf ( f, " <type>%s</type>\n", tmp );
1064 /* No such thing as a rteseg! */
1066 fprintf ( f, " <trkseg>\n" );
1068 if ( t->trackpoints && t->trackpoints->data ) {
1069 first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment;
1070 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */
1071 g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context );
1072 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
1075 /* NB apparently no such thing as a rteseg! */
1077 fprintf ( f, " </trkseg>\n");
1079 fprintf ( f, "</%s>\n", t->is_route ? "rte" : "trk" );
1082 static void gpx_write_header( FILE *f )
1084 fprintf(f, "<?xml version=\"1.0\"?>\n"
1085 "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
1086 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
1087 "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
1088 "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
1091 static void gpx_write_footer( FILE *f )
1093 fprintf(f, "</gpx>\n");
1096 static int gpx_waypoint_compare(const void *x, const void *y)
1098 VikWaypoint *a = (VikWaypoint *)x;
1099 VikWaypoint *b = (VikWaypoint *)y;
1100 return strcmp(a->name,b->name);
1103 static int gpx_track_compare_name(const void *x, const void *y)
1105 VikTrack *a = (VikTrack *)x;
1106 VikTrack *b = (VikTrack *)y;
1107 return strcmp(a->name,b->name);
1110 void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
1112 GpxWritingContext context = { options, f };
1114 gpx_write_header ( f );
1117 const gchar *name = vik_layer_get_name(VIK_LAYER(vtl));
1119 tmp = entitize ( name );
1120 fprintf ( f, " <name>%s</name>\n", tmp );
1124 VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
1127 tmp = entitize ( md->author );
1128 fprintf ( f, " <author>%s</author>\n", tmp );
1131 if ( md->description ) {
1132 tmp = entitize ( md->description );
1133 fprintf ( f, " <desc>%s</desc>\n", tmp );
1136 if ( md->timestamp ) {
1137 tmp = entitize ( md->timestamp );
1138 fprintf ( f, " <time>%s</time>\n", tmp );
1141 if ( md->keywords ) {
1142 tmp = entitize ( md->keywords );
1143 fprintf ( f, " <keywords>%s</keywords>\n", tmp );
1148 if ( vik_trw_layer_get_waypoints_visibility(vtl) || (options && options->hidden) ) {
1149 // gather waypoints in a list, then sort
1150 GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
1151 gl = g_list_sort ( gl, gpx_waypoint_compare );
1153 for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
1154 gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
1160 if ( vik_trw_layer_get_tracks_visibility(vtl) || (options && options->hidden) ) {
1161 //gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
1162 // Forming the list manually seems to produce one that is more likely to be nearer to the creation order
1163 gpointer key, value;
1164 GHashTableIter ght_iter;
1165 g_hash_table_iter_init ( &ght_iter, vik_trw_layer_get_tracks ( vtl ) );
1166 while ( g_hash_table_iter_next (&ght_iter, &key, &value) ) {
1167 gl = g_list_prepend ( gl ,value );
1169 gl = g_list_reverse ( gl );
1171 // Sort method determined by preference
1172 if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
1173 gl = g_list_sort ( gl, vik_track_compare_timestamp );
1174 else if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_ALPHA )
1175 gl = g_list_sort ( gl, gpx_track_compare_name );
1178 GList *glrte = NULL;
1179 // Routes sorted by name
1180 if ( vik_trw_layer_get_routes_visibility(vtl) || (options && options->hidden) ) {
1181 glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
1182 glrte = g_list_sort ( glrte, gpx_track_compare_name );
1185 // g_list_concat doesn't copy memory properly
1186 // so process each list separately
1188 GpxWritingContext context_tmp = context;
1189 GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE, FALSE };
1190 // Force trackpoints on tracks
1191 if ( !context.options )
1192 context_tmp.options = &opt_tmp;
1193 context_tmp.options->is_route = FALSE;
1195 // Loop around each list and write each one
1196 for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
1197 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
1200 // Routes (to get routepoints)
1201 context_tmp.options->is_route = TRUE;
1202 for (GList *iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
1203 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
1207 g_list_free ( glrte );
1209 gpx_write_footer ( f );
1212 void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
1214 GpxWritingContext context = {options, f};
1215 gpx_write_header ( f );
1216 gpx_write_track ( trk, &context );
1217 gpx_write_footer ( f );
1221 * Common write of a temporary GPX file
1223 static gchar* write_tmp_file ( VikTrwLayer *vtl, VikTrack *trk, GpxWritingOptions *options )
1225 gchar *tmp_filename = NULL;
1226 GError *error = NULL;
1227 // Opening temporary file
1228 int fd = g_file_open_tmp("viking_XXXXXX.gpx", &tmp_filename, &error);
1230 g_warning ( _("failed to open temporary file: %s"), error->message );
1231 g_clear_error ( &error );
1234 g_debug ("%s: temporary file = %s", __FUNCTION__, tmp_filename);
1236 FILE *ff = fdopen (fd, "w");
1239 a_gpx_write_track_file ( trk, ff, options );
1241 a_gpx_write_file ( vtl, ff, options );
1245 return tmp_filename;
1249 * a_gpx_write_tmp_file:
1250 * @vtl: The #VikTrwLayer to write
1251 * @options: Possible ways of writing the file data (can be NULL)
1253 * Returns: The name of newly created temporary GPX file
1254 * This file should be removed once used and the string freed.
1255 * If NULL then the process failed.
1257 gchar* a_gpx_write_tmp_file ( VikTrwLayer *vtl, GpxWritingOptions *options )
1259 return write_tmp_file ( vtl, NULL, options );
1263 * a_gpx_write_track_tmp_file:
1264 * @trk: The #VikTrack to write
1265 * @options: Possible ways of writing the file data (can be NULL)
1267 * Returns: The name of newly created temporary GPX file
1268 * This file should be removed once used and the string freed.
1269 * If NULL then the process failed.
1271 gchar* a_gpx_write_track_tmp_file ( VikTrack *trk, GpxWritingOptions *options )
1273 return write_tmp_file ( NULL, trk, options );