]>
Commit | Line | Data |
---|---|---|
8c4f1350 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
a482007a GB |
4 | * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net> |
5 | * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com> | |
6 | * Copyright (C) 2008, Hein Ragas <viking@ragas.nl> | |
7 | * Copyright (C) 2009, Tal B <tal.bav@gmail.com> | |
415b0e21 | 8 | * Copyright (c) 2012-2014, Rob Norris <rw_norris@hotmail.com> |
8c4f1350 | 9 | * |
bf35388d EB |
10 | * Some of the code adapted from GPSBabel 1.2.7 |
11 | * http://gpsbabel.sf.net/ | |
12 | * Copyright (C) 2002, 2003, 2004, 2005 Robert Lipe, robertlipe@usa.net | |
13 | * | |
8c4f1350 EB |
14 | * This program is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
27 | * | |
28 | */ | |
8c00358d GB |
29 | #ifdef HAVE_CONFIG_H |
30 | #include "config.h" | |
31 | #endif | |
8c4f1350 EB |
32 | |
33 | #define _XOPEN_SOURCE /* glibc2 needs this */ | |
34 | ||
0b72c435 | 35 | #include "gpx.h" |
8c4f1350 EB |
36 | #include "viking.h" |
37 | #include <expat.h> | |
8c00358d | 38 | #ifdef HAVE_STRING_H |
8c4f1350 | 39 | #include <string.h> |
8c00358d | 40 | #endif |
8904c540 | 41 | #include <glib.h> |
12ed2b58 RN |
42 | #include <glib/gstdio.h> |
43 | #include <glib/gi18n.h> | |
8c00358d | 44 | #ifdef HAVE_MATH_H |
8904c540 | 45 | #include <math.h> |
8c00358d | 46 | #endif |
e3449376 | 47 | #include <time.h> |
8c4f1350 | 48 | |
8c4f1350 EB |
49 | typedef enum { |
50 | tt_unknown = 0, | |
51 | ||
52 | tt_gpx, | |
6ba42f1e RN |
53 | tt_gpx_name, |
54 | tt_gpx_desc, | |
55 | tt_gpx_author, | |
56 | tt_gpx_time, | |
57 | tt_gpx_keywords, | |
8c4f1350 EB |
58 | |
59 | tt_wpt, | |
6b2f262e | 60 | tt_wpt_cmt, |
8c4f1350 EB |
61 | tt_wpt_desc, |
62 | tt_wpt_name, | |
63 | tt_wpt_ele, | |
f2ef466d RN |
64 | tt_wpt_sym, |
65 | tt_wpt_time, | |
415b0e21 | 66 | tt_wpt_url, |
8c4f1350 EB |
67 | tt_wpt_link, /* New in GPX 1.1 */ |
68 | ||
69 | tt_trk, | |
6b2f262e | 70 | tt_trk_cmt, |
8c4f1350 EB |
71 | tt_trk_desc, |
72 | tt_trk_name, | |
73 | ||
0d2b891f RN |
74 | tt_rte, |
75 | ||
8c4f1350 EB |
76 | tt_trk_trkseg, |
77 | tt_trk_trkseg_trkpt, | |
78 | tt_trk_trkseg_trkpt_ele, | |
79 | tt_trk_trkseg_trkpt_time, | |
b45865b4 | 80 | tt_trk_trkseg_trkpt_name, |
6ba42f1e | 81 | /* extended */ |
a2817d3c QT |
82 | tt_trk_trkseg_trkpt_course, |
83 | tt_trk_trkseg_trkpt_speed, | |
84 | tt_trk_trkseg_trkpt_fix, | |
85 | tt_trk_trkseg_trkpt_sat, | |
bf35388d | 86 | |
4a42b254 T |
87 | tt_trk_trkseg_trkpt_hdop, |
88 | tt_trk_trkseg_trkpt_vdop, | |
89 | tt_trk_trkseg_trkpt_pdop, | |
90 | ||
bf35388d EB |
91 | tt_waypoint, |
92 | tt_waypoint_coord, | |
93 | tt_waypoint_name, | |
8c4f1350 EB |
94 | } tag_type; |
95 | ||
96 | typedef struct tag_mapping { | |
97 | tag_type tag_type; /* enum from above for this tag */ | |
98 | const char *tag_name; /* xpath-ish tag name */ | |
99 | } tag_mapping; | |
100 | ||
0b72c435 OK |
101 | typedef struct { |
102 | GpxWritingOptions *options; | |
103 | FILE *file; | |
104 | } GpxWritingContext; | |
105 | ||
8c4f1350 | 106 | /* |
6ba42f1e | 107 | * xpath(ish) mappings between full tag paths and internal identifiers. |
8c4f1350 | 108 | * These appear in the order they appear in the GPX specification. |
6ba42f1e | 109 | * If it's not a tag we explicitly handle, it doesn't go here. |
8c4f1350 EB |
110 | */ |
111 | ||
112 | tag_mapping tag_path_map[] = { | |
8c4f1350 | 113 | |
6ba42f1e RN |
114 | { tt_gpx, "/gpx" }, |
115 | { tt_gpx_name, "/gpx/name" }, | |
116 | { tt_gpx_desc, "/gpx/desc" }, | |
117 | { tt_gpx_time, "/gpx/time" }, | |
118 | { tt_gpx_author, "/gpx/author" }, | |
119 | { tt_gpx_keywords, "/gpx/keywords" }, | |
120 | ||
121 | // GPX 1.1 variant - basic properties moved into metadata namespace | |
122 | { tt_gpx_name, "/gpx/metadata/name" }, | |
123 | { tt_gpx_desc, "/gpx/metadata/desc" }, | |
124 | { tt_gpx_time, "/gpx/metadata/time" }, | |
125 | { tt_gpx_author, "/gpx/metadata/author" }, | |
126 | { tt_gpx_keywords, "/gpx/metadata/keywords" }, | |
127 | ||
8c4f1350 | 128 | { tt_wpt, "/gpx/wpt" }, |
bf35388d EB |
129 | |
130 | { tt_waypoint, "/loc/waypoint" }, | |
131 | { tt_waypoint_coord, "/loc/waypoint/coord" }, | |
132 | { tt_waypoint_name, "/loc/waypoint/name" }, | |
133 | ||
8c4f1350 | 134 | { tt_wpt_ele, "/gpx/wpt/ele" }, |
f2ef466d | 135 | { tt_wpt_time, "/gpx/wpt/time" }, |
8c4f1350 | 136 | { tt_wpt_name, "/gpx/wpt/name" }, |
6b2f262e | 137 | { tt_wpt_cmt, "/gpx/wpt/cmt" }, |
8c4f1350 | 138 | { tt_wpt_desc, "/gpx/wpt/desc" }, |
42c2ac3e | 139 | { tt_wpt_sym, "/gpx/wpt/sym" }, |
08b251ec | 140 | { tt_wpt_sym, "/loc/waypoint/type" }, |
415b0e21 | 141 | { tt_wpt_url, "/gpx/wpt/url" }, |
8c4f1350 EB |
142 | { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */ |
143 | ||
144 | { tt_trk, "/gpx/trk" }, | |
145 | { tt_trk_name, "/gpx/trk/name" }, | |
6b2f262e | 146 | { tt_trk_cmt, "/gpx/trk/cmt" }, |
8c4f1350 EB |
147 | { tt_trk_desc, "/gpx/trk/desc" }, |
148 | { tt_trk_trkseg, "/gpx/trk/trkseg" }, | |
8c4f1350 EB |
149 | { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" }, |
150 | { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" }, | |
151 | { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" }, | |
b45865b4 | 152 | { tt_trk_trkseg_trkpt_name, "/gpx/trk/trkseg/trkpt/name" }, |
6ba42f1e RN |
153 | /* extended */ |
154 | { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" }, | |
a2817d3c QT |
155 | { tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" }, |
156 | { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" }, | |
157 | { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" }, | |
8c4f1350 | 158 | |
4a42b254 T |
159 | { tt_trk_trkseg_trkpt_hdop, "/gpx/trk/trkseg/trkpt/hdop" }, |
160 | { tt_trk_trkseg_trkpt_vdop, "/gpx/trk/trkseg/trkpt/vdop" }, | |
161 | { tt_trk_trkseg_trkpt_pdop, "/gpx/trk/trkseg/trkpt/pdop" }, | |
0d2b891f RN |
162 | |
163 | { tt_rte, "/gpx/rte" }, | |
164 | // NB Route reuses track point feature tags | |
165 | { tt_trk_name, "/gpx/rte/name" }, | |
166 | { tt_trk_cmt, "/gpx/rte/cmt" }, | |
167 | { tt_trk_desc, "/gpx/rte/desc" }, | |
168 | { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" }, | |
b45865b4 | 169 | { tt_trk_trkseg_trkpt_name, "/gpx/rte/rtept/name" }, |
0d2b891f RN |
170 | { tt_trk_trkseg_trkpt_ele, "/gpx/rte/rtept/ele" }, |
171 | ||
8c4f1350 EB |
172 | {0} |
173 | }; | |
174 | ||
175 | static tag_type get_tag(const char *t) | |
176 | { | |
177 | tag_mapping *tm; | |
178 | for (tm = tag_path_map; tm->tag_type != 0; tm++) | |
179 | if (0 == strcmp(tm->tag_name, t)) | |
180 | return tm->tag_type; | |
181 | return tt_unknown; | |
182 | } | |
183 | ||
184 | /******************************************/ | |
185 | ||
186 | tag_type current_tag = tt_unknown; | |
187 | GString *xpath = NULL; | |
188 | GString *c_cdata = NULL; | |
189 | ||
190 | /* current ("c_") objects */ | |
191 | VikTrackpoint *c_tp = NULL; | |
192 | VikWaypoint *c_wp = NULL; | |
193 | VikTrack *c_tr = NULL; | |
6ba42f1e | 194 | VikTRWMetadata *c_md = NULL; |
8c4f1350 EB |
195 | |
196 | gchar *c_wp_name = NULL; | |
197 | gchar *c_tr_name = NULL; | |
198 | ||
199 | /* temporary things so we don't have to create them lots of times */ | |
200 | const gchar *c_slat, *c_slon; | |
201 | struct LatLon c_ll; | |
202 | ||
203 | /* specialty flags / etc */ | |
204 | gboolean f_tr_newseg; | |
205 | guint unnamed_waypoints = 0; | |
206 | guint unnamed_tracks = 0; | |
43e2df3e | 207 | guint unnamed_routes = 0; |
8c4f1350 EB |
208 | |
209 | static const char *get_attr ( const char **attr, const char *key ) | |
210 | { | |
211 | while ( *attr ) { | |
212 | if ( strcmp(*attr,key) == 0 ) | |
213 | return *(attr + 1); | |
214 | attr += 2; | |
215 | } | |
216 | return NULL; | |
217 | } | |
218 | ||
219 | static gboolean set_c_ll ( const char **attr ) | |
220 | { | |
221 | if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) { | |
d1cd12c0 T |
222 | c_ll.lat = g_ascii_strtod(c_slat, NULL); |
223 | c_ll.lon = g_ascii_strtod(c_slon, NULL); | |
8c4f1350 EB |
224 | return TRUE; |
225 | } | |
226 | return FALSE; | |
227 | } | |
228 | ||
229 | static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) | |
230 | { | |
bf35388d EB |
231 | static const gchar *tmp; |
232 | ||
8c4f1350 EB |
233 | g_string_append_c ( xpath, '/' ); |
234 | g_string_append ( xpath, el ); | |
235 | current_tag = get_tag ( xpath->str ); | |
236 | ||
237 | switch ( current_tag ) { | |
238 | ||
6ba42f1e RN |
239 | case tt_gpx: |
240 | c_md = vik_trw_metadata_new(); | |
241 | break; | |
242 | ||
8c4f1350 EB |
243 | case tt_wpt: |
244 | if ( set_c_ll( attr ) ) { | |
245 | c_wp = vik_waypoint_new (); | |
0b9f279d RN |
246 | c_wp->visible = TRUE; |
247 | if ( get_attr ( attr, "hidden" ) ) | |
248 | c_wp->visible = FALSE; | |
8c4f1350 EB |
249 | |
250 | vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); | |
251 | } | |
252 | break; | |
253 | ||
254 | case tt_trk: | |
0d2b891f | 255 | case tt_rte: |
8c4f1350 | 256 | c_tr = vik_track_new (); |
387ff7ac | 257 | vik_track_set_defaults ( c_tr ); |
0d2b891f | 258 | c_tr->is_route = (current_tag == tt_rte) ? TRUE : FALSE; |
0b9f279d RN |
259 | c_tr->visible = TRUE; |
260 | if ( get_attr ( attr, "hidden" ) ) | |
261 | c_tr->visible = FALSE; | |
8c4f1350 EB |
262 | break; |
263 | ||
264 | case tt_trk_trkseg: | |
265 | f_tr_newseg = TRUE; | |
266 | break; | |
267 | ||
268 | case tt_trk_trkseg_trkpt: | |
269 | if ( set_c_ll( attr ) ) { | |
270 | c_tp = vik_trackpoint_new (); | |
8c4f1350 EB |
271 | vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); |
272 | if ( f_tr_newseg ) { | |
273 | c_tp->newsegment = TRUE; | |
274 | f_tr_newseg = FALSE; | |
275 | } | |
276 | c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp ); | |
277 | } | |
278 | break; | |
279 | ||
6ba42f1e RN |
280 | case tt_gpx_name: |
281 | case tt_gpx_author: | |
282 | case tt_gpx_desc: | |
283 | case tt_gpx_keywords: | |
284 | case tt_gpx_time: | |
b45865b4 | 285 | case tt_trk_trkseg_trkpt_name: |
8c4f1350 EB |
286 | case tt_trk_trkseg_trkpt_ele: |
287 | case tt_trk_trkseg_trkpt_time: | |
6b2f262e | 288 | case tt_wpt_cmt: |
8c4f1350 EB |
289 | case tt_wpt_desc: |
290 | case tt_wpt_name: | |
291 | case tt_wpt_ele: | |
f2ef466d | 292 | case tt_wpt_time: |
415b0e21 | 293 | case tt_wpt_url: |
8c4f1350 | 294 | case tt_wpt_link: |
6b2f262e | 295 | case tt_trk_cmt: |
8c4f1350 EB |
296 | case tt_trk_desc: |
297 | case tt_trk_name: | |
298 | g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */ | |
bf35388d | 299 | break; |
8c4f1350 | 300 | |
bf35388d EB |
301 | case tt_waypoint: |
302 | c_wp = vik_waypoint_new (); | |
bf35388d EB |
303 | c_wp->visible = TRUE; |
304 | break; | |
305 | ||
306 | case tt_waypoint_coord: | |
307 | if ( set_c_ll( attr ) ) | |
308 | vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); | |
309 | break; | |
310 | ||
311 | case tt_waypoint_name: | |
312 | if ( ( tmp = get_attr(attr, "id") ) ) { | |
313 | if ( c_wp_name ) | |
314 | g_free ( c_wp_name ); | |
315 | c_wp_name = g_strdup ( tmp ); | |
316 | } | |
317 | g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */ | |
318 | break; | |
319 | ||
8c4f1350 EB |
320 | default: break; |
321 | } | |
322 | } | |
323 | ||
324 | static void gpx_end(VikTrwLayer *vtl, const char *el) | |
325 | { | |
dd81e762 | 326 | static GTimeVal tp_time; |
f2ef466d | 327 | static GTimeVal wp_time; |
dd81e762 | 328 | |
8c4f1350 EB |
329 | g_string_truncate ( xpath, xpath->len - strlen(el) - 1 ); |
330 | ||
331 | switch ( current_tag ) { | |
332 | ||
6ba42f1e RN |
333 | case tt_gpx: |
334 | vik_trw_layer_set_metadata ( vtl, c_md ); | |
335 | c_md = NULL; | |
336 | break; | |
337 | ||
338 | case tt_gpx_name: | |
339 | vik_layer_rename ( VIK_LAYER(vtl), c_cdata->str ); | |
340 | g_string_erase ( c_cdata, 0, -1 ); | |
341 | break; | |
342 | ||
343 | case tt_gpx_author: | |
344 | if ( c_md->author ) | |
345 | g_free ( c_md->description ); | |
346 | c_md->author = g_strdup ( c_cdata->str ); | |
347 | g_string_erase ( c_cdata, 0, -1 ); | |
348 | break; | |
349 | ||
350 | case tt_gpx_desc: | |
351 | if ( c_md->description ) | |
352 | g_free ( c_md->description ); | |
353 | c_md->description = g_strdup ( c_cdata->str ); | |
354 | g_string_erase ( c_cdata, 0, -1 ); | |
355 | break; | |
356 | ||
357 | case tt_gpx_keywords: | |
358 | if ( c_md->keywords ) | |
359 | g_free ( c_md->keywords ); | |
360 | c_md->keywords = g_strdup ( c_cdata->str ); | |
361 | g_string_erase ( c_cdata, 0, -1 ); | |
362 | break; | |
363 | ||
364 | case tt_gpx_time: | |
365 | if ( c_md->timestamp ) | |
366 | g_free ( c_md->timestamp ); | |
367 | c_md->timestamp = g_strdup ( c_cdata->str ); | |
368 | g_string_erase ( c_cdata, 0, -1 ); | |
369 | break; | |
370 | ||
bf35388d | 371 | case tt_waypoint: |
8c4f1350 EB |
372 | case tt_wpt: |
373 | if ( ! c_wp_name ) | |
43e2df3e | 374 | c_wp_name = g_strdup_printf("VIKING_WP%04d", unnamed_waypoints++); |
805d282e EB |
375 | vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp ); |
376 | g_free ( c_wp_name ); | |
8c4f1350 EB |
377 | c_wp = NULL; |
378 | c_wp_name = NULL; | |
379 | break; | |
380 | ||
381 | case tt_trk: | |
43e2df3e RN |
382 | if ( ! c_tr_name ) |
383 | c_tr_name = g_strdup_printf("VIKING_TR%03d", unnamed_tracks++); | |
384 | // Delibrate fall through | |
0d2b891f | 385 | case tt_rte: |
8c4f1350 | 386 | if ( ! c_tr_name ) |
43e2df3e | 387 | c_tr_name = g_strdup_printf("VIKING_RT%03d", unnamed_routes++); |
805d282e EB |
388 | vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr ); |
389 | g_free ( c_tr_name ); | |
8c4f1350 EB |
390 | c_tr = NULL; |
391 | c_tr_name = NULL; | |
392 | break; | |
393 | ||
394 | case tt_wpt_name: | |
395 | if ( c_wp_name ) | |
396 | g_free ( c_wp_name ); | |
397 | c_wp_name = g_strdup ( c_cdata->str ); | |
398 | g_string_erase ( c_cdata, 0, -1 ); | |
399 | break; | |
400 | ||
401 | case tt_trk_name: | |
402 | if ( c_tr_name ) | |
403 | g_free ( c_tr_name ); | |
404 | c_tr_name = g_strdup ( c_cdata->str ); | |
405 | g_string_erase ( c_cdata, 0, -1 ); | |
406 | break; | |
407 | ||
408 | case tt_wpt_ele: | |
d1cd12c0 | 409 | c_wp->altitude = g_ascii_strtod ( c_cdata->str, NULL ); |
8c4f1350 EB |
410 | g_string_erase ( c_cdata, 0, -1 ); |
411 | break; | |
412 | ||
413 | case tt_trk_trkseg_trkpt_ele: | |
d1cd12c0 | 414 | c_tp->altitude = g_ascii_strtod ( c_cdata->str, NULL ); |
8c4f1350 EB |
415 | g_string_erase ( c_cdata, 0, -1 ); |
416 | break; | |
417 | ||
bf35388d | 418 | case tt_waypoint_name: /* .loc name is really description. */ |
8c4f1350 | 419 | case tt_wpt_desc: |
6b2f262e RN |
420 | vik_waypoint_set_description ( c_wp, c_cdata->str ); |
421 | g_string_erase ( c_cdata, 0, -1 ); | |
422 | break; | |
423 | ||
424 | case tt_wpt_cmt: | |
8c4f1350 EB |
425 | vik_waypoint_set_comment ( c_wp, c_cdata->str ); |
426 | g_string_erase ( c_cdata, 0, -1 ); | |
427 | break; | |
428 | ||
415b0e21 RN |
429 | case tt_wpt_url: |
430 | vik_waypoint_set_url ( c_wp, c_cdata->str ); | |
431 | g_string_erase ( c_cdata, 0, -1 ); | |
432 | break; | |
433 | ||
8c4f1350 EB |
434 | case tt_wpt_link: |
435 | vik_waypoint_set_image ( c_wp, c_cdata->str ); | |
436 | g_string_erase ( c_cdata, 0, -1 ); | |
437 | break; | |
438 | ||
ea3933fc | 439 | case tt_wpt_sym: { |
fffcc269 | 440 | vik_waypoint_set_symbol ( c_wp, c_cdata->str ); |
42c2ac3e AF |
441 | g_string_erase ( c_cdata, 0, -1 ); |
442 | break; | |
ea3933fc | 443 | } |
42c2ac3e | 444 | |
8c4f1350 | 445 | case tt_trk_desc: |
6b2f262e RN |
446 | vik_track_set_description ( c_tr, c_cdata->str ); |
447 | g_string_erase ( c_cdata, 0, -1 ); | |
448 | break; | |
449 | ||
450 | case tt_trk_cmt: | |
8c4f1350 EB |
451 | vik_track_set_comment ( c_tr, c_cdata->str ); |
452 | g_string_erase ( c_cdata, 0, -1 ); | |
453 | break; | |
454 | ||
f2ef466d RN |
455 | case tt_wpt_time: |
456 | if ( g_time_val_from_iso8601(c_cdata->str, &wp_time) ) { | |
457 | c_wp->timestamp = wp_time.tv_sec; | |
458 | c_wp->has_timestamp = TRUE; | |
459 | } | |
460 | g_string_erase ( c_cdata, 0, -1 ); | |
461 | break; | |
462 | ||
b45865b4 RN |
463 | case tt_trk_trkseg_trkpt_name: |
464 | vik_trackpoint_set_name ( c_tp, c_cdata->str ); | |
465 | g_string_erase ( c_cdata, 0, -1 ); | |
466 | break; | |
467 | ||
8c4f1350 | 468 | case tt_trk_trkseg_trkpt_time: |
dd81e762 GB |
469 | if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) { |
470 | c_tp->timestamp = tp_time.tv_sec; | |
8c4f1350 EB |
471 | c_tp->has_timestamp = TRUE; |
472 | } | |
473 | g_string_erase ( c_cdata, 0, -1 ); | |
bf35388d | 474 | break; |
8c4f1350 | 475 | |
a2817d3c | 476 | case tt_trk_trkseg_trkpt_course: |
d1cd12c0 | 477 | c_tp->course = g_ascii_strtod ( c_cdata->str, NULL ); |
a2817d3c QT |
478 | g_string_erase ( c_cdata, 0, -1 ); |
479 | break; | |
480 | ||
481 | case tt_trk_trkseg_trkpt_speed: | |
d1cd12c0 | 482 | c_tp->speed = g_ascii_strtod ( c_cdata->str, NULL ); |
a2817d3c QT |
483 | g_string_erase ( c_cdata, 0, -1 ); |
484 | break; | |
485 | ||
486 | case tt_trk_trkseg_trkpt_fix: | |
a2817d3c QT |
487 | if (!strcmp("2d", c_cdata->str)) |
488 | c_tp->fix_mode = VIK_GPS_MODE_2D; | |
489 | else if (!strcmp("3d", c_cdata->str)) | |
490 | c_tp->fix_mode = VIK_GPS_MODE_3D; | |
491 | else /* TODO: more fix modes here */ | |
492 | c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN; | |
493 | g_string_erase ( c_cdata, 0, -1 ); | |
494 | break; | |
495 | ||
496 | case tt_trk_trkseg_trkpt_sat: | |
a2817d3c QT |
497 | c_tp->nsats = atoi ( c_cdata->str ); |
498 | g_string_erase ( c_cdata, 0, -1 ); | |
499 | break; | |
500 | ||
4a42b254 | 501 | case tt_trk_trkseg_trkpt_hdop: |
4a42b254 T |
502 | c_tp->hdop = g_strtod ( c_cdata->str, NULL ); |
503 | g_string_erase ( c_cdata, 0, -1 ); | |
504 | break; | |
505 | ||
506 | case tt_trk_trkseg_trkpt_vdop: | |
4a42b254 T |
507 | c_tp->vdop = g_strtod ( c_cdata->str, NULL ); |
508 | g_string_erase ( c_cdata, 0, -1 ); | |
509 | break; | |
510 | ||
511 | case tt_trk_trkseg_trkpt_pdop: | |
4a42b254 T |
512 | c_tp->pdop = g_strtod ( c_cdata->str, NULL ); |
513 | g_string_erase ( c_cdata, 0, -1 ); | |
514 | break; | |
515 | ||
8c4f1350 EB |
516 | default: break; |
517 | } | |
518 | ||
519 | current_tag = get_tag ( xpath->str ); | |
520 | } | |
521 | ||
522 | static void gpx_cdata(void *dta, const XML_Char *s, int len) | |
523 | { | |
524 | switch ( current_tag ) { | |
6ba42f1e RN |
525 | case tt_gpx_name: |
526 | case tt_gpx_author: | |
527 | case tt_gpx_desc: | |
528 | case tt_gpx_keywords: | |
529 | case tt_gpx_time: | |
8c4f1350 EB |
530 | case tt_wpt_name: |
531 | case tt_trk_name: | |
532 | case tt_wpt_ele: | |
533 | case tt_trk_trkseg_trkpt_ele: | |
6b2f262e | 534 | case tt_wpt_cmt: |
8c4f1350 | 535 | case tt_wpt_desc: |
42c2ac3e | 536 | case tt_wpt_sym: |
415b0e21 | 537 | case tt_wpt_url: |
8c4f1350 | 538 | case tt_wpt_link: |
6b2f262e | 539 | case tt_trk_cmt: |
8c4f1350 EB |
540 | case tt_trk_desc: |
541 | case tt_trk_trkseg_trkpt_time: | |
f2ef466d | 542 | case tt_wpt_time: |
b45865b4 | 543 | case tt_trk_trkseg_trkpt_name: |
a2817d3c QT |
544 | case tt_trk_trkseg_trkpt_course: |
545 | case tt_trk_trkseg_trkpt_speed: | |
546 | case tt_trk_trkseg_trkpt_fix: | |
547 | case tt_trk_trkseg_trkpt_sat: | |
4a42b254 T |
548 | case tt_trk_trkseg_trkpt_hdop: |
549 | case tt_trk_trkseg_trkpt_vdop: | |
550 | case tt_trk_trkseg_trkpt_pdop: | |
bf35388d | 551 | case tt_waypoint_name: /* .loc name is really description. */ |
8c4f1350 EB |
552 | g_string_append_len ( c_cdata, s, len ); |
553 | break; | |
554 | ||
555 | default: break; /* ignore cdata from other things */ | |
556 | } | |
557 | } | |
558 | ||
559 | // make like a "stack" of tag names | |
560 | // like gpspoint's separated like /gpx/wpt/whatever | |
561 | ||
efe9a9c3 | 562 | gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) { |
8c4f1350 EB |
563 | XML_Parser parser = XML_ParserCreate(NULL); |
564 | int done=0, len; | |
efe9a9c3 | 565 | enum XML_Status status = XML_STATUS_ERROR; |
8c4f1350 EB |
566 | |
567 | XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end); | |
568 | XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */ | |
569 | XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata); | |
570 | ||
571 | gchar buf[4096]; | |
572 | ||
bf35388d | 573 | g_assert ( f != NULL && vtl != NULL ); |
8c4f1350 EB |
574 | |
575 | xpath = g_string_new ( "" ); | |
576 | c_cdata = g_string_new ( "" ); | |
577 | ||
43e2df3e RN |
578 | unnamed_waypoints = 1; |
579 | unnamed_tracks = 1; | |
580 | unnamed_routes = 1; | |
8c4f1350 EB |
581 | |
582 | while (!done) { | |
583 | len = fread(buf, 1, sizeof(buf)-7, f); | |
584 | done = feof(f) || !len; | |
efe9a9c3 | 585 | status = XML_Parse(parser, buf, len, done); |
8c4f1350 EB |
586 | } |
587 | ||
6278ccc6 | 588 | XML_ParserFree (parser); |
8c4f1350 EB |
589 | g_string_free ( xpath, TRUE ); |
590 | g_string_free ( c_cdata, TRUE ); | |
efe9a9c3 GB |
591 | |
592 | return status != XML_STATUS_ERROR; | |
8c4f1350 | 593 | } |
561e6ad0 EB |
594 | |
595 | /**** entitize from GPSBabel ****/ | |
596 | typedef struct { | |
597 | const char * text; | |
598 | const char * entity; | |
599 | int not_html; | |
600 | } entity_types; | |
601 | ||
602 | static | |
603 | entity_types stdentities[] = { | |
604 | { "&", "&", 0 }, | |
605 | { "'", "'", 1 }, | |
606 | { "<", "<", 0 }, | |
607 | { ">", ">", 0 }, | |
608 | { "\"", """, 0 }, | |
609 | { NULL, NULL, 0 } | |
610 | }; | |
611 | ||
612 | void utf8_to_int( const char *cp, int *bytes, int *value ) | |
613 | { | |
614 | if ( (*cp & 0xe0) == 0xc0 ) { | |
615 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
616 | *bytes = 2; | |
617 | *value = ((*cp & 0x1f) << 6) | | |
618 | (*(cp+1) & 0x3f); | |
619 | } | |
620 | else if ( (*cp & 0xf0) == 0xe0 ) { | |
621 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
622 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
623 | *bytes = 3; | |
624 | *value = ((*cp & 0x0f) << 12) | | |
625 | ((*(cp+1) & 0x3f) << 6) | | |
626 | (*(cp+2) & 0x3f); | |
627 | } | |
628 | else if ( (*cp & 0xf8) == 0xf0 ) { | |
629 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
630 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
631 | if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; | |
632 | *bytes = 4; | |
633 | *value = ((*cp & 0x07) << 18) | | |
634 | ((*(cp+1) & 0x3f) << 12) | | |
635 | ((*(cp+2) & 0x3f) << 6) | | |
636 | (*(cp+3) & 0x3f); | |
637 | } | |
638 | else if ( (*cp & 0xfc) == 0xf8 ) { | |
639 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
640 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
641 | if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; | |
642 | if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault; | |
643 | *bytes = 5; | |
644 | *value = ((*cp & 0x03) << 24) | | |
645 | ((*(cp+1) & 0x3f) << 18) | | |
646 | ((*(cp+2) & 0x3f) << 12) | | |
647 | ((*(cp+3) & 0x3f) << 6) | | |
648 | (*(cp+4) & 0x3f); | |
649 | } | |
650 | else if ( (*cp & 0xfe) == 0xfc ) { | |
651 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
652 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
653 | if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; | |
654 | if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault; | |
655 | if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault; | |
656 | *bytes = 6; | |
657 | *value = ((*cp & 0x01) << 30) | | |
658 | ((*(cp+1) & 0x3f) << 24) | | |
659 | ((*(cp+2) & 0x3f) << 18) | | |
660 | ((*(cp+3) & 0x3f) << 12) | | |
661 | ((*(cp+4) & 0x3f) << 6) | | |
662 | (*(cp+5) & 0x3f); | |
663 | } | |
664 | else { | |
665 | dodefault: | |
666 | *bytes = 1; | |
667 | *value = (unsigned char)*cp; | |
668 | } | |
669 | } | |
670 | ||
671 | static | |
672 | char * | |
673 | entitize(const char * str) | |
674 | { | |
675 | int elen, ecount, nsecount; | |
676 | entity_types *ep; | |
677 | const char * cp; | |
678 | char * p, * tmp, * xstr; | |
679 | ||
680 | char tmpsub[20]; | |
681 | int bytes = 0; | |
682 | int value = 0; | |
683 | ep = stdentities; | |
684 | elen = ecount = nsecount = 0; | |
685 | ||
686 | /* figure # of entity replacements and additional size. */ | |
687 | while (ep->text) { | |
688 | cp = str; | |
689 | while ((cp = strstr(cp, ep->text)) != NULL) { | |
690 | elen += strlen(ep->entity) - strlen(ep->text); | |
691 | ecount++; | |
692 | cp += strlen(ep->text); | |
693 | } | |
694 | ep++; | |
695 | } | |
696 | ||
697 | /* figure the same for other than standard entities (i.e. anything | |
698 | * that isn't in the range U+0000 to U+007F */ | |
699 | for ( cp = str; *cp; cp++ ) { | |
700 | if ( *cp & 0x80 ) { | |
701 | ||
702 | utf8_to_int( cp, &bytes, &value ); | |
703 | cp += bytes-1; | |
704 | elen += sprintf( tmpsub, "&#x%x;", value ) - bytes; | |
705 | nsecount++; | |
706 | } | |
707 | } | |
708 | ||
709 | /* enough space for the whole string plus entity replacements, if any */ | |
710 | tmp = g_malloc((strlen(str) + elen + 1)); | |
711 | strcpy(tmp, str); | |
712 | ||
713 | /* no entity replacements */ | |
714 | if (ecount == 0 && nsecount == 0) | |
715 | return (tmp); | |
716 | ||
717 | if ( ecount != 0 ) { | |
718 | for (ep = stdentities; ep->text; ep++) { | |
719 | p = tmp; | |
720 | while ((p = strstr(p, ep->text)) != NULL) { | |
721 | elen = strlen(ep->entity); | |
722 | ||
723 | xstr = g_strdup(p + strlen(ep->text)); | |
724 | ||
725 | strcpy(p, ep->entity); | |
726 | strcpy(p + elen, xstr); | |
727 | ||
728 | g_free(xstr); | |
729 | ||
730 | p += elen; | |
731 | } | |
732 | } | |
733 | } | |
734 | ||
735 | if ( nsecount != 0 ) { | |
736 | p = tmp; | |
737 | while (*p) { | |
738 | if ( *p & 0x80 ) { | |
739 | utf8_to_int( p, &bytes, &value ); | |
740 | if ( p[bytes] ) { | |
741 | xstr = g_strdup( p + bytes ); | |
742 | } | |
743 | else { | |
744 | xstr = NULL; | |
745 | } | |
746 | sprintf( p, "&#x%x;", value ); | |
747 | p = p+strlen(p); | |
748 | if ( xstr ) { | |
749 | strcpy( p, xstr ); | |
750 | g_free(xstr); | |
751 | } | |
752 | } | |
753 | else { | |
754 | p++; | |
755 | } | |
756 | } | |
757 | } | |
758 | return (tmp); | |
759 | } | |
760 | /**** end GPSBabel code ****/ | |
761 | ||
762 | /* export GPX */ | |
8904c540 | 763 | |
c9570f86 | 764 | static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context ) |
561e6ad0 | 765 | { |
208d2084 RN |
766 | // Don't write invisible waypoints when specified |
767 | if (context->options && !context->options->hidden && !wp->visible) | |
768 | return; | |
769 | ||
0b72c435 | 770 | FILE *f = context->file; |
561e6ad0 | 771 | static struct LatLon ll; |
8904c540 | 772 | gchar *s_lat,*s_lon; |
561e6ad0 EB |
773 | gchar *tmp; |
774 | vik_coord_to_latlon ( &(wp->coord), &ll ); | |
161aa492 EB |
775 | s_lat = a_coords_dtostr( ll.lat ); |
776 | s_lon = a_coords_dtostr( ll.lon ); | |
208d2084 RN |
777 | // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension' |
778 | // luckily most other GPX processing software ignores things they don't understand | |
8904c540 GB |
779 | fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n", |
780 | s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" ); | |
781 | g_free ( s_lat ); | |
782 | g_free ( s_lon ); | |
561e6ad0 | 783 | |
c9570f86 RN |
784 | // Sanity clause |
785 | if ( wp->name ) | |
786 | tmp = entitize ( wp->name ); | |
787 | else | |
788 | tmp = g_strdup ("waypoint"); | |
789 | ||
561e6ad0 EB |
790 | fprintf ( f, " <name>%s</name>\n", tmp ); |
791 | g_free ( tmp); | |
792 | ||
793 | if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) | |
8904c540 | 794 | { |
161aa492 | 795 | tmp = a_coords_dtostr ( wp->altitude ); |
8904c540 GB |
796 | fprintf ( f, " <ele>%s</ele>\n", tmp ); |
797 | g_free ( tmp ); | |
798 | } | |
f2ef466d RN |
799 | |
800 | if ( wp->has_timestamp ) { | |
801 | GTimeVal timestamp; | |
802 | timestamp.tv_sec = wp->timestamp; | |
803 | timestamp.tv_usec = 0; | |
804 | ||
805 | gchar *time_iso8601 = g_time_val_to_iso8601 ( ×tamp ); | |
806 | if ( time_iso8601 != NULL ) | |
807 | fprintf ( f, " <time>%s</time>\n", time_iso8601 ); | |
808 | g_free ( time_iso8601 ); | |
809 | } | |
810 | ||
561e6ad0 EB |
811 | if ( wp->comment ) |
812 | { | |
813 | tmp = entitize(wp->comment); | |
6b2f262e RN |
814 | fprintf ( f, " <cmt>%s</cmt>\n", tmp ); |
815 | g_free ( tmp ); | |
816 | } | |
817 | if ( wp->description ) | |
818 | { | |
819 | tmp = entitize(wp->description); | |
561e6ad0 EB |
820 | fprintf ( f, " <desc>%s</desc>\n", tmp ); |
821 | g_free ( tmp ); | |
822 | } | |
415b0e21 RN |
823 | if ( wp->url ) |
824 | { | |
825 | tmp = entitize(wp->url); | |
826 | fprintf ( f, " <url>%s</url>\n", tmp ); | |
827 | g_free ( tmp ); | |
828 | } | |
561e6ad0 EB |
829 | if ( wp->image ) |
830 | { | |
831 | tmp = entitize(wp->image); | |
832 | fprintf ( f, " <link>%s</link>\n", tmp ); | |
833 | g_free ( tmp ); | |
834 | } | |
42c2ac3e AF |
835 | if ( wp->symbol ) |
836 | { | |
837 | tmp = entitize(wp->symbol); | |
72f067ad RN |
838 | if ( a_vik_gpx_export_wpt_sym_name ( ) ) { |
839 | // Lowercase the symbol name | |
840 | gchar *tmp2 = g_utf8_strdown ( tmp, -1 ); | |
841 | fprintf ( f, " <sym>%s</sym>\n", tmp2 ); | |
842 | g_free ( tmp2 ); | |
843 | } | |
844 | else | |
845 | fprintf ( f, " <sym>%s</sym>\n", tmp); | |
42c2ac3e AF |
846 | g_free ( tmp ); |
847 | } | |
848 | ||
561e6ad0 EB |
849 | fprintf ( f, "</wpt>\n" ); |
850 | } | |
851 | ||
0b72c435 | 852 | static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context ) |
561e6ad0 | 853 | { |
0b72c435 | 854 | FILE *f = context->file; |
561e6ad0 | 855 | static struct LatLon ll; |
4a42b254 | 856 | gchar *s_lat,*s_lon, *s_alt, *s_dop; |
dd81e762 | 857 | gchar *time_iso8601; |
561e6ad0 EB |
858 | vik_coord_to_latlon ( &(tp->coord), &ll ); |
859 | ||
0d2b891f RN |
860 | // No such thing as a rteseg! So make sure we don't put them in |
861 | if ( context->options && !context->options->is_route && tp->newsegment ) | |
561e6ad0 EB |
862 | fprintf ( f, " </trkseg>\n <trkseg>\n" ); |
863 | ||
161aa492 EB |
864 | s_lat = a_coords_dtostr( ll.lat ); |
865 | s_lon = a_coords_dtostr( ll.lon ); | |
0d2b891f | 866 | fprintf ( f, " <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon ); |
0b72c435 OK |
867 | g_free ( s_lat ); s_lat = NULL; |
868 | g_free ( s_lon ); s_lon = NULL; | |
561e6ad0 | 869 | |
b45865b4 RN |
870 | if (tp->name) { |
871 | gchar *s_name = entitize(tp->name); | |
872 | fprintf ( f, " <name>%s</name>\n", s_name ); | |
873 | g_free(s_name); | |
874 | } | |
875 | ||
0b72c435 | 876 | s_alt = NULL; |
561e6ad0 | 877 | if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) |
8904c540 | 878 | { |
161aa492 | 879 | s_alt = a_coords_dtostr ( tp->altitude ); |
8904c540 | 880 | } |
0b72c435 | 881 | else if ( context->options != NULL && context->options->force_ele ) |
36179a2e OK |
882 | { |
883 | s_alt = a_coords_dtostr ( 0 ); | |
884 | } | |
0b72c435 OK |
885 | if (s_alt != NULL) |
886 | fprintf ( f, " <ele>%s</ele>\n", s_alt ); | |
887 | g_free ( s_alt ); s_alt = NULL; | |
36179a2e | 888 | |
dd81e762 | 889 | time_iso8601 = NULL; |
561e6ad0 | 890 | if ( tp->has_timestamp ) { |
dd81e762 GB |
891 | GTimeVal timestamp; |
892 | timestamp.tv_sec = tp->timestamp; | |
61f37ac9 | 893 | timestamp.tv_usec = 0; |
dd81e762 GB |
894 | |
895 | time_iso8601 = g_time_val_to_iso8601 ( ×tamp ); | |
561e6ad0 | 896 | } |
0b72c435 | 897 | else if ( context->options != NULL && context->options->force_time ) |
36179a2e | 898 | { |
dd81e762 GB |
899 | GTimeVal current; |
900 | g_get_current_time ( ¤t ); | |
36179a2e | 901 | |
dd81e762 | 902 | time_iso8601 = g_time_val_to_iso8601 ( ¤t ); |
36179a2e | 903 | } |
dd81e762 GB |
904 | if ( time_iso8601 != NULL ) |
905 | fprintf ( f, " <time>%s</time>\n", time_iso8601 ); | |
906 | g_free(time_iso8601); | |
907 | time_iso8601 = NULL; | |
36179a2e | 908 | |
ad54cd32 T |
909 | if (!isnan(tp->course)) { |
910 | gchar *s_course = a_coords_dtostr(tp->course); | |
911 | fprintf ( f, " <course>%s</course>\n", s_course ); | |
912 | g_free(s_course); | |
a2817d3c | 913 | } |
ad54cd32 T |
914 | if (!isnan(tp->speed)) { |
915 | gchar *s_speed = a_coords_dtostr(tp->speed); | |
916 | fprintf ( f, " <speed>%s</speed>\n", s_speed ); | |
917 | g_free(s_speed); | |
918 | } | |
919 | if (tp->fix_mode == VIK_GPS_MODE_2D) | |
920 | fprintf ( f, " <fix>2d</fix>\n"); | |
921 | if (tp->fix_mode == VIK_GPS_MODE_3D) | |
922 | fprintf ( f, " <fix>3d</fix>\n"); | |
923 | if (tp->nsats > 0) | |
924 | fprintf ( f, " <sat>%d</sat>\n", tp->nsats ); | |
a2817d3c | 925 | |
4a42b254 T |
926 | s_dop = NULL; |
927 | if ( tp->hdop != VIK_DEFAULT_DOP ) | |
928 | { | |
929 | s_dop = a_coords_dtostr ( tp->hdop ); | |
930 | } | |
931 | if (s_dop != NULL) | |
932 | fprintf ( f, " <hdop>%s</hdop>\n", s_dop ); | |
933 | g_free ( s_dop ); s_dop = NULL; | |
934 | ||
935 | if ( tp->vdop != VIK_DEFAULT_DOP ) | |
936 | { | |
937 | s_dop = a_coords_dtostr ( tp->vdop ); | |
938 | } | |
939 | if (s_dop != NULL) | |
940 | fprintf ( f, " <vdop>%s</vdop>\n", s_dop ); | |
941 | g_free ( s_dop ); s_dop = NULL; | |
942 | ||
943 | if ( tp->pdop != VIK_DEFAULT_DOP ) | |
944 | { | |
945 | s_dop = a_coords_dtostr ( tp->pdop ); | |
946 | } | |
947 | if (s_dop != NULL) | |
948 | fprintf ( f, " <pdop>%s</pdop>\n", s_dop ); | |
949 | g_free ( s_dop ); s_dop = NULL; | |
950 | ||
0d2b891f | 951 | fprintf ( f, " </%spt>\n", (context->options && context->options->is_route) ? "rte" : "trk" ); |
561e6ad0 EB |
952 | } |
953 | ||
954 | ||
ce4bd1cf | 955 | static void gpx_write_track ( VikTrack *t, GpxWritingContext *context ) |
561e6ad0 | 956 | { |
208d2084 RN |
957 | // Don't write invisible tracks when specified |
958 | if (context->options && !context->options->hidden && !t->visible) | |
959 | return; | |
960 | ||
0b72c435 | 961 | FILE *f = context->file; |
561e6ad0 | 962 | gchar *tmp; |
494eb388 | 963 | gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */ |
561e6ad0 | 964 | |
ce4bd1cf RN |
965 | // Sanity clause |
966 | if ( t->name ) | |
967 | tmp = entitize ( t->name ); | |
968 | else | |
969 | tmp = g_strdup ("track"); | |
970 | ||
208d2084 RN |
971 | // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension' |
972 | // luckily most other GPX processing software ignores things they don't understand | |
0d2b891f RN |
973 | fprintf ( f, "<%s%s>\n <name>%s</name>\n", |
974 | t->is_route ? "rte" : "trk", | |
975 | t->visible ? "" : " hidden=\"hidden\"", | |
976 | tmp ); | |
561e6ad0 EB |
977 | g_free ( tmp ); |
978 | ||
979 | if ( t->comment ) | |
980 | { | |
981 | tmp = entitize ( t->comment ); | |
6b2f262e RN |
982 | fprintf ( f, " <cmt>%s</cmt>\n", tmp ); |
983 | g_free ( tmp ); | |
984 | } | |
985 | ||
986 | if ( t->description ) | |
987 | { | |
988 | tmp = entitize ( t->description ); | |
561e6ad0 EB |
989 | fprintf ( f, " <desc>%s</desc>\n", tmp ); |
990 | g_free ( tmp ); | |
991 | } | |
0d2b891f RN |
992 | |
993 | /* No such thing as a rteseg! */ | |
994 | if ( !t->is_route ) | |
995 | fprintf ( f, " <trkseg>\n" ); | |
561e6ad0 EB |
996 | |
997 | if ( t->trackpoints && t->trackpoints->data ) { | |
998 | first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment; | |
999 | VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */ | |
0b72c435 | 1000 | g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context ); |
561e6ad0 | 1001 | VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */ |
494eb388 | 1002 | } |
561e6ad0 | 1003 | |
0d2b891f RN |
1004 | /* NB apparently no such thing as a rteseg! */ |
1005 | if (!t->is_route) | |
1006 | fprintf ( f, " </trkseg>\n"); | |
1007 | ||
1008 | fprintf ( f, "</%s>\n", t->is_route ? "rte" : "trk" ); | |
561e6ad0 EB |
1009 | } |
1010 | ||
5092de80 | 1011 | static void gpx_write_header( FILE *f ) |
561e6ad0 EB |
1012 | { |
1013 | fprintf(f, "<?xml version=\"1.0\"?>\n" | |
1014 | "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n" | |
1015 | "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" | |
1016 | "xmlns=\"http://www.topografix.com/GPX/1/0\"\n" | |
1017 | "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n"); | |
5092de80 GB |
1018 | } |
1019 | ||
1020 | static void gpx_write_footer( FILE *f ) | |
1021 | { | |
561e6ad0 | 1022 | fprintf(f, "</gpx>\n"); |
5092de80 | 1023 | } |
561e6ad0 | 1024 | |
c9570f86 | 1025 | static int gpx_waypoint_compare(const void *x, const void *y) |
eab858a1 | 1026 | { |
c9570f86 RN |
1027 | VikWaypoint *a = (VikWaypoint *)x; |
1028 | VikWaypoint *b = (VikWaypoint *)y; | |
eab858a1 EB |
1029 | return strcmp(a->name,b->name); |
1030 | } | |
1031 | ||
0d2b891f RN |
1032 | static int gpx_track_compare_name(const void *x, const void *y) |
1033 | { | |
1034 | VikTrack *a = (VikTrack *)x; | |
1035 | VikTrack *b = (VikTrack *)y; | |
1036 | return strcmp(a->name,b->name); | |
1037 | } | |
1038 | ||
208d2084 | 1039 | void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options ) |
0b72c435 OK |
1040 | { |
1041 | GpxWritingContext context = { options, f }; | |
eab858a1 | 1042 | |
5092de80 | 1043 | gpx_write_header ( f ); |
eab858a1 | 1044 | |
6ba42f1e RN |
1045 | gchar *tmp; |
1046 | const gchar *name = vik_layer_get_name(VIK_LAYER(vtl)); | |
1047 | if ( name ) { | |
1048 | tmp = entitize ( name ); | |
1049 | fprintf ( f, " <name>%s</name>\n", tmp ); | |
1050 | g_free ( tmp ); | |
1051 | } | |
1052 | ||
1053 | VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl); | |
1054 | if ( md ) { | |
1055 | if ( md->author ) { | |
1056 | tmp = entitize ( md->author ); | |
1057 | fprintf ( f, " <author>%s</author>\n", tmp ); | |
1058 | g_free ( tmp ); | |
1059 | } | |
1060 | if ( md->description ) { | |
1061 | tmp = entitize ( md->description ); | |
1062 | fprintf ( f, " <desc>%s</desc>\n", tmp ); | |
1063 | g_free ( tmp ); | |
1064 | } | |
1065 | if ( md->timestamp ) { | |
1066 | tmp = entitize ( md->timestamp ); | |
1067 | fprintf ( f, " <time>%s</time>\n", tmp ); | |
1068 | g_free ( tmp ); | |
1069 | } | |
1070 | if ( md->keywords ) { | |
1071 | tmp = entitize ( md->keywords ); | |
1072 | fprintf ( f, " <keywords>%s</keywords>\n", tmp ); | |
1073 | g_free ( tmp ); | |
1074 | } | |
1075 | } | |
1076 | ||
c9570f86 RN |
1077 | // gather waypoints in a list, then sort |
1078 | // g_hash_table_get_values: glib 2.14+ | |
1079 | GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) ); | |
1080 | gl = g_list_sort ( gl, gpx_waypoint_compare ); | |
0b72c435 | 1081 | |
c9570f86 RN |
1082 | GList *iter; |
1083 | for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) { | |
1084 | gpx_write_waypoint ( (VikWaypoint*)iter->data, &context ); | |
1085 | } | |
eab858a1 | 1086 | |
c9570f86 | 1087 | g_list_free ( gl ); |
d13f1a57 | 1088 | |
b02cd3a3 RN |
1089 | //gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) ); |
1090 | // Forming the list manually seems to produce one that is more likely to be nearer to the creation order | |
62e2f612 | 1091 | gl = NULL; |
b02cd3a3 RN |
1092 | gpointer key, value; |
1093 | GHashTableIter ght_iter; | |
1094 | g_hash_table_iter_init ( &ght_iter, vik_trw_layer_get_tracks ( vtl ) ); | |
1095 | while ( g_hash_table_iter_next (&ght_iter, &key, &value) ) { | |
1096 | gl = g_list_prepend ( gl ,value ); | |
1097 | } | |
1098 | gl = g_list_reverse ( gl ); | |
1099 | ||
d13f1a57 RN |
1100 | // Sort method determined by preference |
1101 | if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME ) | |
5c97b8a7 | 1102 | gl = g_list_sort ( gl, vik_track_compare_timestamp ); |
b02cd3a3 | 1103 | else if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_ALPHA ) |
d13f1a57 RN |
1104 | gl = g_list_sort ( gl, gpx_track_compare_name ); |
1105 | ||
0d2b891f RN |
1106 | // Routes sorted by name |
1107 | GList *glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) ); | |
1108 | glrte = g_list_sort ( glrte, gpx_track_compare_name ); | |
ce4bd1cf | 1109 | |
0d2b891f RN |
1110 | // g_list_concat doesn't copy memory properly |
1111 | // so process each list separately | |
1112 | ||
1113 | GpxWritingContext context_tmp = context; | |
63959706 | 1114 | GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE, FALSE }; |
0d2b891f RN |
1115 | // Force trackpoints on tracks |
1116 | if ( !context.options ) | |
1117 | context_tmp.options = &opt_tmp; | |
1118 | context_tmp.options->is_route = FALSE; | |
1119 | ||
1120 | // Loop around each list and write each one | |
ce4bd1cf | 1121 | for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) { |
0d2b891f RN |
1122 | gpx_write_track ( (VikTrack*)iter->data, &context_tmp ); |
1123 | } | |
1124 | ||
1125 | // Routes (to get routepoints) | |
1126 | context_tmp.options->is_route = TRUE; | |
1127 | for (iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) { | |
1128 | gpx_write_track ( (VikTrack*)iter->data, &context_tmp ); | |
e3449376 | 1129 | } |
ce4bd1cf RN |
1130 | |
1131 | g_list_free ( gl ); | |
0d2b891f | 1132 | g_list_free ( glrte ); |
ce4bd1cf | 1133 | |
5092de80 GB |
1134 | gpx_write_footer ( f ); |
1135 | } | |
561e6ad0 | 1136 | |
208d2084 | 1137 | void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options ) |
0b72c435 OK |
1138 | { |
1139 | GpxWritingContext context = {options, f}; | |
5092de80 | 1140 | gpx_write_header ( f ); |
ce4bd1cf | 1141 | gpx_write_track ( trk, &context ); |
5092de80 | 1142 | gpx_write_footer ( f ); |
561e6ad0 | 1143 | } |
12ed2b58 RN |
1144 | |
1145 | /** | |
1146 | * Common write of a temporary GPX file | |
1147 | */ | |
1148 | static gchar* write_tmp_file ( VikTrwLayer *vtl, VikTrack *trk, GpxWritingOptions *options ) | |
1149 | { | |
1150 | gchar *tmp_filename = NULL; | |
1151 | GError *error = NULL; | |
1152 | // Opening temporary file | |
1153 | int fd = g_file_open_tmp("viking_XXXXXX.gpx", &tmp_filename, &error); | |
1154 | if (fd < 0) { | |
1155 | g_warning ( _("failed to open temporary file: %s"), error->message ); | |
1156 | g_clear_error ( &error ); | |
1157 | return NULL; | |
1158 | } | |
1159 | g_debug ("%s: temporary file = %s", __FUNCTION__, tmp_filename); | |
1160 | ||
1161 | FILE *ff = fdopen (fd, "w"); | |
1162 | ||
1163 | if ( trk ) | |
1164 | a_gpx_write_track_file ( trk, ff, options ); | |
1165 | else | |
1166 | a_gpx_write_file ( vtl, ff, options ); | |
1167 | ||
1168 | fclose (ff); | |
1169 | ||
1170 | return tmp_filename; | |
1171 | } | |
1172 | ||
1173 | /* | |
1174 | * a_gpx_write_tmp_file: | |
1175 | * @vtl: The #VikTrwLayer to write | |
1176 | * @options: Possible ways of writing the file data (can be NULL) | |
1177 | * | |
1178 | * Returns: The name of newly created temporary GPX file | |
1179 | * This file should be removed once used and the string freed. | |
1180 | * If NULL then the process failed. | |
1181 | */ | |
1182 | gchar* a_gpx_write_tmp_file ( VikTrwLayer *vtl, GpxWritingOptions *options ) | |
1183 | { | |
1184 | return write_tmp_file ( vtl, NULL, options ); | |
1185 | } | |
1186 | ||
1187 | /* | |
1188 | * a_gpx_write_track_tmp_file: | |
1189 | * @trk: The #VikTrack to write | |
1190 | * @options: Possible ways of writing the file data (can be NULL) | |
1191 | * | |
1192 | * Returns: The name of newly created temporary GPX file | |
1193 | * This file should be removed once used and the string freed. | |
1194 | * If NULL then the process failed. | |
1195 | */ | |
1196 | gchar* a_gpx_write_track_tmp_file ( VikTrack *trk, GpxWritingOptions *options ) | |
1197 | { | |
1198 | return write_tmp_file ( NULL, trk, options ); | |
1199 | } |