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 */
58 tt_wpt_link, /* New in GPX 1.1 */
69 tt_trk_trkseg_trkpt_ele,
70 tt_trk_trkseg_trkpt_time,
72 tt_trk_trkseg_trkpt_course,
73 tt_trk_trkseg_trkpt_speed,
74 tt_trk_trkseg_trkpt_fix,
75 tt_trk_trkseg_trkpt_sat,
77 tt_trk_trkseg_trkpt_hdop,
78 tt_trk_trkseg_trkpt_vdop,
79 tt_trk_trkseg_trkpt_pdop,
86 typedef struct tag_mapping {
87 tag_type tag_type; /* enum from above for this tag */
88 const char *tag_name; /* xpath-ish tag name */
92 GpxWritingOptions *options;
97 * xpath(ish) mappings between full tag paths and internal identifers.
98 * These appear in the order they appear in the GPX specification.
99 * If it's not a tag we explictly handle, it doesn't go here.
102 tag_mapping tag_path_map[] = {
104 { tt_wpt, "/gpx/wpt" },
106 { tt_waypoint, "/loc/waypoint" },
107 { tt_waypoint_coord, "/loc/waypoint/coord" },
108 { tt_waypoint_name, "/loc/waypoint/name" },
110 { tt_wpt_ele, "/gpx/wpt/ele" },
111 { tt_wpt_name, "/gpx/wpt/name" },
112 { tt_wpt_cmt, "/gpx/wpt/cmt" },
113 { tt_wpt_desc, "/gpx/wpt/desc" },
114 { tt_wpt_sym, "/gpx/wpt/sym" },
115 { tt_wpt_sym, "/loc/waypoint/type" },
116 { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */
118 { tt_trk, "/gpx/trk" },
119 { tt_trk_name, "/gpx/trk/name" },
120 { tt_trk_cmt, "/gpx/trk/cmt" },
121 { tt_trk_desc, "/gpx/trk/desc" },
122 { tt_trk_trkseg, "/gpx/trk/trkseg" },
123 { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
124 { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
125 { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
127 { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" },
128 { tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" },
129 { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" },
130 { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" },
132 { tt_trk_trkseg_trkpt_hdop, "/gpx/trk/trkseg/trkpt/hdop" },
133 { tt_trk_trkseg_trkpt_vdop, "/gpx/trk/trkseg/trkpt/vdop" },
134 { tt_trk_trkseg_trkpt_pdop, "/gpx/trk/trkseg/trkpt/pdop" },
136 { tt_rte, "/gpx/rte" },
137 // NB Route reuses track point feature tags
138 { tt_trk_name, "/gpx/rte/name" },
139 { tt_trk_cmt, "/gpx/rte/cmt" },
140 { tt_trk_desc, "/gpx/rte/desc" },
141 { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
142 { tt_trk_trkseg_trkpt_ele, "/gpx/rte/rtept/ele" },
147 static tag_type get_tag(const char *t)
150 for (tm = tag_path_map; tm->tag_type != 0; tm++)
151 if (0 == strcmp(tm->tag_name, t))
156 /******************************************/
158 tag_type current_tag = tt_unknown;
159 GString *xpath = NULL;
160 GString *c_cdata = NULL;
162 /* current ("c_") objects */
163 VikTrackpoint *c_tp = NULL;
164 VikWaypoint *c_wp = NULL;
165 VikTrack *c_tr = NULL;
167 gchar *c_wp_name = NULL;
168 gchar *c_tr_name = NULL;
170 /* temporary things so we don't have to create them lots of times */
171 const gchar *c_slat, *c_slon;
174 /* specialty flags / etc */
175 gboolean f_tr_newseg;
176 guint unnamed_waypoints = 0;
177 guint unnamed_tracks = 0;
180 static const char *get_attr ( const char **attr, const char *key )
183 if ( strcmp(*attr,key) == 0 )
190 static gboolean set_c_ll ( const char **attr )
192 if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) {
193 c_ll.lat = g_ascii_strtod(c_slat, NULL);
194 c_ll.lon = g_ascii_strtod(c_slon, NULL);
200 static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
202 static const gchar *tmp;
204 g_string_append_c ( xpath, '/' );
205 g_string_append ( xpath, el );
206 current_tag = get_tag ( xpath->str );
208 switch ( current_tag ) {
211 if ( set_c_ll( attr ) ) {
212 c_wp = vik_waypoint_new ();
213 c_wp->visible = TRUE;
214 if ( get_attr ( attr, "hidden" ) )
215 c_wp->visible = FALSE;
217 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
223 c_tr = vik_track_new ();
224 c_tr->is_route = (current_tag == tt_rte) ? TRUE : FALSE;
225 c_tr->visible = TRUE;
226 if ( get_attr ( attr, "hidden" ) )
227 c_tr->visible = FALSE;
234 case tt_trk_trkseg_trkpt:
235 if ( set_c_ll( attr ) ) {
236 c_tp = vik_trackpoint_new ();
237 vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
239 c_tp->newsegment = TRUE;
242 c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp );
246 case tt_trk_trkseg_trkpt_ele:
247 case tt_trk_trkseg_trkpt_time:
256 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */
260 c_wp = vik_waypoint_new ();
261 c_wp->visible = TRUE;
264 case tt_waypoint_coord:
265 if ( set_c_ll( attr ) )
266 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
269 case tt_waypoint_name:
270 if ( ( tmp = get_attr(attr, "id") ) ) {
272 g_free ( c_wp_name );
273 c_wp_name = g_strdup ( tmp );
275 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */
282 static void gpx_end(VikTrwLayer *vtl, const char *el)
284 static GTimeVal tp_time;
286 g_string_truncate ( xpath, xpath->len - strlen(el) - 1 );
288 switch ( current_tag ) {
293 c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++);
294 vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp );
295 g_free ( c_wp_name );
303 c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_tracks++);
304 vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr );
305 g_free ( c_tr_name );
312 g_free ( c_wp_name );
313 c_wp_name = g_strdup ( c_cdata->str );
314 g_string_erase ( c_cdata, 0, -1 );
319 g_free ( c_tr_name );
320 c_tr_name = g_strdup ( c_cdata->str );
321 g_string_erase ( c_cdata, 0, -1 );
325 c_wp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
326 g_string_erase ( c_cdata, 0, -1 );
329 case tt_trk_trkseg_trkpt_ele:
330 c_tp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
331 g_string_erase ( c_cdata, 0, -1 );
334 case tt_waypoint_name: /* .loc name is really description. */
336 vik_waypoint_set_description ( c_wp, c_cdata->str );
337 g_string_erase ( c_cdata, 0, -1 );
341 vik_waypoint_set_comment ( c_wp, c_cdata->str );
342 g_string_erase ( c_cdata, 0, -1 );
346 vik_waypoint_set_image ( c_wp, c_cdata->str );
347 g_string_erase ( c_cdata, 0, -1 );
351 vik_waypoint_set_symbol ( c_wp, c_cdata->str );
352 g_string_erase ( c_cdata, 0, -1 );
357 vik_track_set_description ( c_tr, c_cdata->str );
358 g_string_erase ( c_cdata, 0, -1 );
362 vik_track_set_comment ( c_tr, c_cdata->str );
363 g_string_erase ( c_cdata, 0, -1 );
366 case tt_trk_trkseg_trkpt_time:
367 if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) {
368 c_tp->timestamp = tp_time.tv_sec;
369 c_tp->has_timestamp = TRUE;
371 g_string_erase ( c_cdata, 0, -1 );
374 case tt_trk_trkseg_trkpt_course:
375 c_tp->course = g_ascii_strtod ( c_cdata->str, NULL );
376 g_string_erase ( c_cdata, 0, -1 );
379 case tt_trk_trkseg_trkpt_speed:
380 c_tp->speed = g_ascii_strtod ( c_cdata->str, NULL );
381 g_string_erase ( c_cdata, 0, -1 );
384 case tt_trk_trkseg_trkpt_fix:
385 if (!strcmp("2d", c_cdata->str))
386 c_tp->fix_mode = VIK_GPS_MODE_2D;
387 else if (!strcmp("3d", c_cdata->str))
388 c_tp->fix_mode = VIK_GPS_MODE_3D;
389 else /* TODO: more fix modes here */
390 c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN;
391 g_string_erase ( c_cdata, 0, -1 );
394 case tt_trk_trkseg_trkpt_sat:
395 c_tp->nsats = atoi ( c_cdata->str );
396 g_string_erase ( c_cdata, 0, -1 );
399 case tt_trk_trkseg_trkpt_hdop:
400 c_tp->hdop = g_strtod ( c_cdata->str, NULL );
401 g_string_erase ( c_cdata, 0, -1 );
404 case tt_trk_trkseg_trkpt_vdop:
405 c_tp->vdop = g_strtod ( c_cdata->str, NULL );
406 g_string_erase ( c_cdata, 0, -1 );
409 case tt_trk_trkseg_trkpt_pdop:
410 c_tp->pdop = g_strtod ( c_cdata->str, NULL );
411 g_string_erase ( c_cdata, 0, -1 );
417 current_tag = get_tag ( xpath->str );
420 static void gpx_cdata(void *dta, const XML_Char *s, int len)
422 switch ( current_tag ) {
426 case tt_trk_trkseg_trkpt_ele:
433 case tt_trk_trkseg_trkpt_time:
434 case tt_trk_trkseg_trkpt_course:
435 case tt_trk_trkseg_trkpt_speed:
436 case tt_trk_trkseg_trkpt_fix:
437 case tt_trk_trkseg_trkpt_sat:
438 case tt_trk_trkseg_trkpt_hdop:
439 case tt_trk_trkseg_trkpt_vdop:
440 case tt_trk_trkseg_trkpt_pdop:
441 case tt_waypoint_name: /* .loc name is really description. */
442 g_string_append_len ( c_cdata, s, len );
445 default: break; /* ignore cdata from other things */
449 // make like a "stack" of tag names
450 // like gpspoint's separated like /gpx/wpt/whatever
452 gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
453 XML_Parser parser = XML_ParserCreate(NULL);
455 enum XML_Status status = XML_STATUS_ERROR;
457 XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
458 XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
459 XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata);
463 g_assert ( f != NULL && vtl != NULL );
465 xpath = g_string_new ( "" );
466 c_cdata = g_string_new ( "" );
468 unnamed_waypoints = 0;
472 len = fread(buf, 1, sizeof(buf)-7, f);
473 done = feof(f) || !len;
474 status = XML_Parse(parser, buf, len, done);
477 XML_ParserFree (parser);
478 g_string_free ( xpath, TRUE );
479 g_string_free ( c_cdata, TRUE );
481 return status != XML_STATUS_ERROR;
484 /**** entitize from GPSBabel ****/
492 entity_types stdentities[] = {
494 { "'", "'", 1 },
497 { "\"", """, 0 },
501 void utf8_to_int( const char *cp, int *bytes, int *value )
503 if ( (*cp & 0xe0) == 0xc0 ) {
504 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
506 *value = ((*cp & 0x1f) << 6) |
509 else if ( (*cp & 0xf0) == 0xe0 ) {
510 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
511 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
513 *value = ((*cp & 0x0f) << 12) |
514 ((*(cp+1) & 0x3f) << 6) |
517 else if ( (*cp & 0xf8) == 0xf0 ) {
518 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
519 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
520 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
522 *value = ((*cp & 0x07) << 18) |
523 ((*(cp+1) & 0x3f) << 12) |
524 ((*(cp+2) & 0x3f) << 6) |
527 else if ( (*cp & 0xfc) == 0xf8 ) {
528 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
529 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
530 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
531 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
533 *value = ((*cp & 0x03) << 24) |
534 ((*(cp+1) & 0x3f) << 18) |
535 ((*(cp+2) & 0x3f) << 12) |
536 ((*(cp+3) & 0x3f) << 6) |
539 else if ( (*cp & 0xfe) == 0xfc ) {
540 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
541 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
542 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
543 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
544 if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault;
546 *value = ((*cp & 0x01) << 30) |
547 ((*(cp+1) & 0x3f) << 24) |
548 ((*(cp+2) & 0x3f) << 18) |
549 ((*(cp+3) & 0x3f) << 12) |
550 ((*(cp+4) & 0x3f) << 6) |
556 *value = (unsigned char)*cp;
562 entitize(const char * str)
564 int elen, ecount, nsecount;
567 char * p, * tmp, * xstr;
573 elen = ecount = nsecount = 0;
575 /* figure # of entity replacements and additional size. */
578 while ((cp = strstr(cp, ep->text)) != NULL) {
579 elen += strlen(ep->entity) - strlen(ep->text);
581 cp += strlen(ep->text);
586 /* figure the same for other than standard entities (i.e. anything
587 * that isn't in the range U+0000 to U+007F */
588 for ( cp = str; *cp; cp++ ) {
591 utf8_to_int( cp, &bytes, &value );
593 elen += sprintf( tmpsub, "&#x%x;", value ) - bytes;
598 /* enough space for the whole string plus entity replacements, if any */
599 tmp = g_malloc((strlen(str) + elen + 1));
602 /* no entity replacements */
603 if (ecount == 0 && nsecount == 0)
607 for (ep = stdentities; ep->text; ep++) {
609 while ((p = strstr(p, ep->text)) != NULL) {
610 elen = strlen(ep->entity);
612 xstr = g_strdup(p + strlen(ep->text));
614 strcpy(p, ep->entity);
615 strcpy(p + elen, xstr);
624 if ( nsecount != 0 ) {
628 utf8_to_int( p, &bytes, &value );
630 xstr = g_strdup( p + bytes );
635 sprintf( p, "&#x%x;", value );
649 /**** end GPSBabel code ****/
653 static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
655 // Don't write invisible waypoints when specified
656 if (context->options && !context->options->hidden && !wp->visible)
659 FILE *f = context->file;
660 static struct LatLon ll;
663 vik_coord_to_latlon ( &(wp->coord), &ll );
664 s_lat = a_coords_dtostr( ll.lat );
665 s_lon = a_coords_dtostr( ll.lon );
666 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
667 // luckily most other GPX processing software ignores things they don't understand
668 fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
669 s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );
675 tmp = entitize ( wp->name );
677 tmp = g_strdup ("waypoint");
679 fprintf ( f, " <name>%s</name>\n", tmp );
682 if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
684 tmp = a_coords_dtostr ( wp->altitude );
685 fprintf ( f, " <ele>%s</ele>\n", tmp );
690 tmp = entitize(wp->comment);
691 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
694 if ( wp->description )
696 tmp = entitize(wp->description);
697 fprintf ( f, " <desc>%s</desc>\n", tmp );
702 tmp = entitize(wp->image);
703 fprintf ( f, " <link>%s</link>\n", tmp );
708 tmp = entitize(wp->symbol);
709 if ( a_vik_gpx_export_wpt_sym_name ( ) ) {
710 // Lowercase the symbol name
711 gchar *tmp2 = g_utf8_strdown ( tmp, -1 );
712 fprintf ( f, " <sym>%s</sym>\n", tmp2 );
716 fprintf ( f, " <sym>%s</sym>\n", tmp);
720 fprintf ( f, "</wpt>\n" );
723 static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context )
725 FILE *f = context->file;
726 static struct LatLon ll;
727 gchar *s_lat,*s_lon, *s_alt, *s_dop;
729 vik_coord_to_latlon ( &(tp->coord), &ll );
731 // No such thing as a rteseg! So make sure we don't put them in
732 if ( context->options && !context->options->is_route && tp->newsegment )
733 fprintf ( f, " </trkseg>\n <trkseg>\n" );
735 s_lat = a_coords_dtostr( ll.lat );
736 s_lon = a_coords_dtostr( ll.lon );
737 fprintf ( f, " <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon );
738 g_free ( s_lat ); s_lat = NULL;
739 g_free ( s_lon ); s_lon = NULL;
742 if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
744 s_alt = a_coords_dtostr ( tp->altitude );
746 else if ( context->options != NULL && context->options->force_ele )
748 s_alt = a_coords_dtostr ( 0 );
751 fprintf ( f, " <ele>%s</ele>\n", s_alt );
752 g_free ( s_alt ); s_alt = NULL;
755 if ( tp->has_timestamp ) {
757 timestamp.tv_sec = tp->timestamp;
758 timestamp.tv_usec = 0;
760 time_iso8601 = g_time_val_to_iso8601 ( ×tamp );
762 else if ( context->options != NULL && context->options->force_time )
765 g_get_current_time ( ¤t );
767 time_iso8601 = g_time_val_to_iso8601 ( ¤t );
769 if ( time_iso8601 != NULL )
770 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
771 g_free(time_iso8601);
774 if (!isnan(tp->course)) {
775 gchar *s_course = a_coords_dtostr(tp->course);
776 fprintf ( f, " <course>%s</course>\n", s_course );
779 if (!isnan(tp->speed)) {
780 gchar *s_speed = a_coords_dtostr(tp->speed);
781 fprintf ( f, " <speed>%s</speed>\n", s_speed );
784 if (tp->fix_mode == VIK_GPS_MODE_2D)
785 fprintf ( f, " <fix>2d</fix>\n");
786 if (tp->fix_mode == VIK_GPS_MODE_3D)
787 fprintf ( f, " <fix>3d</fix>\n");
789 fprintf ( f, " <sat>%d</sat>\n", tp->nsats );
792 if ( tp->hdop != VIK_DEFAULT_DOP )
794 s_dop = a_coords_dtostr ( tp->hdop );
797 fprintf ( f, " <hdop>%s</hdop>\n", s_dop );
798 g_free ( s_dop ); s_dop = NULL;
800 if ( tp->vdop != VIK_DEFAULT_DOP )
802 s_dop = a_coords_dtostr ( tp->vdop );
805 fprintf ( f, " <vdop>%s</vdop>\n", s_dop );
806 g_free ( s_dop ); s_dop = NULL;
808 if ( tp->pdop != VIK_DEFAULT_DOP )
810 s_dop = a_coords_dtostr ( tp->pdop );
813 fprintf ( f, " <pdop>%s</pdop>\n", s_dop );
814 g_free ( s_dop ); s_dop = NULL;
816 fprintf ( f, " </%spt>\n", (context->options && context->options->is_route) ? "rte" : "trk" );
820 static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
822 // Don't write invisible tracks when specified
823 if (context->options && !context->options->hidden && !t->visible)
826 FILE *f = context->file;
828 gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */
832 tmp = entitize ( t->name );
834 tmp = g_strdup ("track");
836 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
837 // luckily most other GPX processing software ignores things they don't understand
838 fprintf ( f, "<%s%s>\n <name>%s</name>\n",
839 t->is_route ? "rte" : "trk",
840 t->visible ? "" : " hidden=\"hidden\"",
846 tmp = entitize ( t->comment );
847 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
851 if ( t->description )
853 tmp = entitize ( t->description );
854 fprintf ( f, " <desc>%s</desc>\n", tmp );
858 /* No such thing as a rteseg! */
860 fprintf ( f, " <trkseg>\n" );
862 if ( t->trackpoints && t->trackpoints->data ) {
863 first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment;
864 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */
865 g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context );
866 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
869 /* NB apparently no such thing as a rteseg! */
871 fprintf ( f, " </trkseg>\n");
873 fprintf ( f, "</%s>\n", t->is_route ? "rte" : "trk" );
876 static void gpx_write_header( FILE *f )
878 fprintf(f, "<?xml version=\"1.0\"?>\n"
879 "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
880 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
881 "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
882 "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
885 static void gpx_write_footer( FILE *f )
887 fprintf(f, "</gpx>\n");
890 static int gpx_waypoint_compare(const void *x, const void *y)
892 VikWaypoint *a = (VikWaypoint *)x;
893 VikWaypoint *b = (VikWaypoint *)y;
894 return strcmp(a->name,b->name);
897 static int gpx_track_compare_name(const void *x, const void *y)
899 VikTrack *a = (VikTrack *)x;
900 VikTrack *b = (VikTrack *)y;
901 return strcmp(a->name,b->name);
904 /* Function to compare two tracks by their first timestamp */
905 static int gpx_track_compare_timestamp (const void *x, const void *y)
907 VikTrack *a = (VikTrack *)x;
908 VikTrack *b = (VikTrack *)y;
910 VikTrackpoint *tpa = NULL;
911 VikTrackpoint *tpb = NULL;
913 if ( a->trackpoints )
914 tpa = VIK_TRACKPOINT(g_list_first(a->trackpoints)->data);
916 if ( b->trackpoints )
917 tpb = VIK_TRACKPOINT(g_list_first(b->trackpoints)->data);
920 if ( tpa->timestamp < tpb->timestamp )
922 if ( tpa->timestamp > tpb->timestamp )
935 void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
937 GpxWritingContext context = { options, f };
939 gpx_write_header ( f );
941 // gather waypoints in a list, then sort
942 // g_hash_table_get_values: glib 2.14+
943 GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
944 gl = g_list_sort ( gl, gpx_waypoint_compare );
947 for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
948 gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
953 gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
954 // Sort method determined by preference
955 if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
956 gl = g_list_sort ( gl, gpx_track_compare_timestamp );
958 gl = g_list_sort ( gl, gpx_track_compare_name );
960 // Routes sorted by name
961 GList *glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
962 glrte = g_list_sort ( glrte, gpx_track_compare_name );
964 // g_list_concat doesn't copy memory properly
965 // so process each list separately
967 GpxWritingContext context_tmp = context;
968 GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE };
969 // Force trackpoints on tracks
970 if ( !context.options )
971 context_tmp.options = &opt_tmp;
972 context_tmp.options->is_route = FALSE;
974 // Loop around each list and write each one
975 for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
976 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
979 // Routes (to get routepoints)
980 context_tmp.options->is_route = TRUE;
981 for (iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
982 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
986 g_list_free ( glrte );
988 gpx_write_footer ( f );
991 void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
993 GpxWritingContext context = {options, f};
994 gpx_write_header ( f );
995 gpx_write_track ( trk, &context );
996 gpx_write_footer ( f );