]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * Some of the code adapted from GPSBabel 1.2.7 | |
7 | * http://gpsbabel.sf.net/ | |
8 | * Copyright (C) 2002, 2003, 2004, 2005 Robert Lipe, robertlipe@usa.net | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 | * | |
24 | */ | |
25 | ||
26 | ||
27 | #define _XOPEN_SOURCE /* glibc2 needs this */ | |
28 | ||
29 | #include "viking.h" | |
30 | #include <expat.h> | |
31 | #include <string.h> | |
32 | ||
33 | #define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ" | |
34 | ||
35 | typedef enum { | |
36 | tt_unknown = 0, | |
37 | ||
38 | tt_gpx, | |
39 | ||
40 | tt_wpt, | |
41 | tt_wpt_desc, | |
42 | tt_wpt_name, | |
43 | tt_wpt_ele, | |
44 | tt_wpt_link, /* New in GPX 1.1 */ | |
45 | ||
46 | tt_trk, | |
47 | tt_trk_desc, | |
48 | tt_trk_name, | |
49 | ||
50 | tt_trk_trkseg, | |
51 | tt_trk_trkseg_trkpt, | |
52 | tt_trk_trkseg_trkpt_ele, | |
53 | tt_trk_trkseg_trkpt_time, | |
54 | ||
55 | tt_waypoint, | |
56 | tt_waypoint_coord, | |
57 | tt_waypoint_name, | |
58 | } tag_type; | |
59 | ||
60 | typedef struct tag_mapping { | |
61 | tag_type tag_type; /* enum from above for this tag */ | |
62 | const char *tag_name; /* xpath-ish tag name */ | |
63 | } tag_mapping; | |
64 | ||
65 | /* | |
66 | * xpath(ish) mappings between full tag paths and internal identifers. | |
67 | * These appear in the order they appear in the GPX specification. | |
68 | * If it's not a tag we explictly handle, it doesn't go here. | |
69 | */ | |
70 | ||
71 | tag_mapping tag_path_map[] = { | |
72 | ||
73 | { tt_wpt, "/gpx/wpt" }, | |
74 | ||
75 | { tt_waypoint, "/loc/waypoint" }, | |
76 | { tt_waypoint_coord, "/loc/waypoint/coord" }, | |
77 | { tt_waypoint_name, "/loc/waypoint/name" }, | |
78 | ||
79 | { tt_wpt_ele, "/gpx/wpt/ele" }, | |
80 | { tt_wpt_name, "/gpx/wpt/name" }, | |
81 | { tt_wpt_desc, "/gpx/wpt/desc" }, | |
82 | { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */ | |
83 | ||
84 | { tt_trk, "/gpx/trk" }, | |
85 | { tt_trk, "/gpx/rte" }, | |
86 | { tt_trk_name, "/gpx/trk/name" }, | |
87 | { tt_trk_desc, "/gpx/trk/desc" }, | |
88 | { tt_trk_trkseg, "/gpx/trk/trkseg" }, | |
89 | { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" }, | |
90 | { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" }, | |
91 | { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" }, | |
92 | { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" }, | |
93 | ||
94 | {0} | |
95 | }; | |
96 | ||
97 | static tag_type get_tag(const char *t) | |
98 | { | |
99 | tag_mapping *tm; | |
100 | for (tm = tag_path_map; tm->tag_type != 0; tm++) | |
101 | if (0 == strcmp(tm->tag_name, t)) | |
102 | return tm->tag_type; | |
103 | return tt_unknown; | |
104 | } | |
105 | ||
106 | /******************************************/ | |
107 | ||
108 | tag_type current_tag = tt_unknown; | |
109 | GString *xpath = NULL; | |
110 | GString *c_cdata = NULL; | |
111 | ||
112 | /* current ("c_") objects */ | |
113 | VikTrackpoint *c_tp = NULL; | |
114 | VikWaypoint *c_wp = NULL; | |
115 | VikTrack *c_tr = NULL; | |
116 | ||
117 | gchar *c_wp_name = NULL; | |
118 | gchar *c_tr_name = NULL; | |
119 | ||
120 | /* temporary things so we don't have to create them lots of times */ | |
121 | const gchar *c_slat, *c_slon; | |
122 | struct LatLon c_ll; | |
123 | ||
124 | /* specialty flags / etc */ | |
125 | gboolean f_tr_newseg; | |
126 | guint unnamed_waypoints = 0; | |
127 | guint unnamed_tracks = 0; | |
128 | ||
129 | ||
130 | static const char *get_attr ( const char **attr, const char *key ) | |
131 | { | |
132 | while ( *attr ) { | |
133 | if ( strcmp(*attr,key) == 0 ) | |
134 | return *(attr + 1); | |
135 | attr += 2; | |
136 | } | |
137 | return NULL; | |
138 | } | |
139 | ||
140 | static gboolean set_c_ll ( const char **attr ) | |
141 | { | |
142 | if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) { | |
143 | c_ll.lat = strtod(c_slat, NULL); | |
144 | c_ll.lon = strtod(c_slon, NULL); | |
145 | return TRUE; | |
146 | } | |
147 | return FALSE; | |
148 | } | |
149 | ||
150 | static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) | |
151 | { | |
152 | static const gchar *tmp; | |
153 | ||
154 | g_string_append_c ( xpath, '/' ); | |
155 | g_string_append ( xpath, el ); | |
156 | current_tag = get_tag ( xpath->str ); | |
157 | ||
158 | switch ( current_tag ) { | |
159 | ||
160 | case tt_wpt: | |
161 | if ( set_c_ll( attr ) ) { | |
162 | c_wp = vik_waypoint_new (); | |
163 | c_wp->altitude = VIK_DEFAULT_ALTITUDE; | |
164 | if ( ! get_attr ( attr, "hidden" ) ) | |
165 | c_wp->visible = TRUE; | |
166 | ||
167 | vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); | |
168 | } | |
169 | break; | |
170 | ||
171 | case tt_trk: | |
172 | c_tr = vik_track_new (); | |
173 | if ( ! get_attr ( attr, "hidden" ) ) | |
174 | c_tr->visible = TRUE; | |
175 | break; | |
176 | ||
177 | case tt_trk_trkseg: | |
178 | f_tr_newseg = TRUE; | |
179 | break; | |
180 | ||
181 | case tt_trk_trkseg_trkpt: | |
182 | if ( set_c_ll( attr ) ) { | |
183 | c_tp = vik_trackpoint_new (); | |
184 | c_tp->altitude = VIK_DEFAULT_ALTITUDE; | |
185 | vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); | |
186 | if ( f_tr_newseg ) { | |
187 | c_tp->newsegment = TRUE; | |
188 | f_tr_newseg = FALSE; | |
189 | } | |
190 | c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp ); | |
191 | } | |
192 | break; | |
193 | ||
194 | case tt_trk_trkseg_trkpt_ele: | |
195 | case tt_trk_trkseg_trkpt_time: | |
196 | case tt_wpt_desc: | |
197 | case tt_wpt_name: | |
198 | case tt_wpt_ele: | |
199 | case tt_wpt_link: | |
200 | case tt_trk_desc: | |
201 | case tt_trk_name: | |
202 | g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */ | |
203 | break; | |
204 | ||
205 | case tt_waypoint: | |
206 | c_wp = vik_waypoint_new (); | |
207 | c_wp->altitude = VIK_DEFAULT_ALTITUDE; | |
208 | c_wp->visible = TRUE; | |
209 | break; | |
210 | ||
211 | case tt_waypoint_coord: | |
212 | if ( set_c_ll( attr ) ) | |
213 | vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); | |
214 | break; | |
215 | ||
216 | case tt_waypoint_name: | |
217 | if ( ( tmp = get_attr(attr, "id") ) ) { | |
218 | if ( c_wp_name ) | |
219 | g_free ( c_wp_name ); | |
220 | c_wp_name = g_strdup ( tmp ); | |
221 | } | |
222 | g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */ | |
223 | break; | |
224 | ||
225 | default: break; | |
226 | } | |
227 | } | |
228 | ||
229 | static void gpx_end(VikTrwLayer *vtl, const char *el) | |
230 | { | |
231 | static struct tm tm; | |
232 | g_string_truncate ( xpath, xpath->len - strlen(el) - 1 ); | |
233 | ||
234 | switch ( current_tag ) { | |
235 | ||
236 | case tt_waypoint: | |
237 | case tt_wpt: | |
238 | if ( ! c_wp_name ) | |
239 | c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++); | |
240 | g_hash_table_insert ( vik_trw_layer_get_waypoints ( vtl ), c_wp_name, c_wp ); | |
241 | c_wp = NULL; | |
242 | c_wp_name = NULL; | |
243 | break; | |
244 | ||
245 | case tt_trk: | |
246 | if ( ! c_tr_name ) | |
247 | c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_waypoints++); | |
248 | g_hash_table_insert ( vik_trw_layer_get_tracks ( vtl ), c_tr_name, c_tr ); | |
249 | c_tr = NULL; | |
250 | c_tr_name = NULL; | |
251 | break; | |
252 | ||
253 | case tt_wpt_name: | |
254 | if ( c_wp_name ) | |
255 | g_free ( c_wp_name ); | |
256 | c_wp_name = g_strdup ( c_cdata->str ); | |
257 | g_string_erase ( c_cdata, 0, -1 ); | |
258 | break; | |
259 | ||
260 | case tt_trk_name: | |
261 | if ( c_tr_name ) | |
262 | g_free ( c_tr_name ); | |
263 | c_tr_name = g_strdup ( c_cdata->str ); | |
264 | g_string_erase ( c_cdata, 0, -1 ); | |
265 | break; | |
266 | ||
267 | case tt_wpt_ele: | |
268 | c_wp->altitude = strtod ( c_cdata->str, NULL ); | |
269 | g_string_erase ( c_cdata, 0, -1 ); | |
270 | break; | |
271 | ||
272 | case tt_trk_trkseg_trkpt_ele: | |
273 | c_tp->altitude = strtod ( c_cdata->str, NULL ); | |
274 | g_string_erase ( c_cdata, 0, -1 ); | |
275 | break; | |
276 | ||
277 | case tt_waypoint_name: /* .loc name is really description. */ | |
278 | case tt_wpt_desc: | |
279 | vik_waypoint_set_comment ( c_wp, c_cdata->str ); | |
280 | g_string_erase ( c_cdata, 0, -1 ); | |
281 | break; | |
282 | ||
283 | case tt_wpt_link: | |
284 | vik_waypoint_set_image ( c_wp, c_cdata->str ); | |
285 | g_string_erase ( c_cdata, 0, -1 ); | |
286 | break; | |
287 | ||
288 | case tt_trk_desc: | |
289 | vik_track_set_comment ( c_tr, c_cdata->str ); | |
290 | g_string_erase ( c_cdata, 0, -1 ); | |
291 | break; | |
292 | ||
293 | case tt_trk_trkseg_trkpt_time: | |
294 | if ( strptime(c_cdata->str, GPX_TIME_FORMAT, &tm) != c_cdata->str ) { /* it read at least one char */ | |
295 | c_tp->timestamp = mktime(&tm); | |
296 | c_tp->has_timestamp = TRUE; | |
297 | } | |
298 | g_string_erase ( c_cdata, 0, -1 ); | |
299 | break; | |
300 | ||
301 | default: break; | |
302 | } | |
303 | ||
304 | current_tag = get_tag ( xpath->str ); | |
305 | } | |
306 | ||
307 | static void gpx_cdata(void *dta, const XML_Char *s, int len) | |
308 | { | |
309 | switch ( current_tag ) { | |
310 | case tt_wpt_name: | |
311 | case tt_trk_name: | |
312 | case tt_wpt_ele: | |
313 | case tt_trk_trkseg_trkpt_ele: | |
314 | case tt_wpt_desc: | |
315 | case tt_wpt_link: | |
316 | case tt_trk_desc: | |
317 | case tt_trk_trkseg_trkpt_time: | |
318 | case tt_waypoint_name: /* .loc name is really description. */ | |
319 | g_string_append_len ( c_cdata, s, len ); | |
320 | break; | |
321 | ||
322 | default: break; /* ignore cdata from other things */ | |
323 | } | |
324 | } | |
325 | ||
326 | // make like a "stack" of tag names | |
327 | // like gpspoint's separated like /gpx/wpt/whatever | |
328 | ||
329 | void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) { | |
330 | XML_Parser parser = XML_ParserCreate(NULL); | |
331 | int done=0, len; | |
332 | ||
333 | XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end); | |
334 | XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */ | |
335 | XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata); | |
336 | ||
337 | gchar buf[4096]; | |
338 | ||
339 | g_assert ( f != NULL && vtl != NULL ); | |
340 | ||
341 | xpath = g_string_new ( "" ); | |
342 | c_cdata = g_string_new ( "" ); | |
343 | ||
344 | unnamed_waypoints = 0; | |
345 | unnamed_tracks = 0; | |
346 | ||
347 | while (!done) { | |
348 | len = fread(buf, 1, sizeof(buf)-7, f); | |
349 | done = feof(f) || !len; | |
350 | XML_Parse(parser, buf, len, done); | |
351 | } | |
352 | ||
353 | g_string_free ( xpath, TRUE ); | |
354 | g_string_free ( c_cdata, TRUE ); | |
355 | } | |
356 | ||
357 | /**** entitize from GPSBabel ****/ | |
358 | typedef struct { | |
359 | const char * text; | |
360 | const char * entity; | |
361 | int not_html; | |
362 | } entity_types; | |
363 | ||
364 | static | |
365 | entity_types stdentities[] = { | |
366 | { "&", "&", 0 }, | |
367 | { "'", "'", 1 }, | |
368 | { "<", "<", 0 }, | |
369 | { ">", ">", 0 }, | |
370 | { "\"", """, 0 }, | |
371 | { NULL, NULL, 0 } | |
372 | }; | |
373 | ||
374 | void utf8_to_int( const char *cp, int *bytes, int *value ) | |
375 | { | |
376 | if ( (*cp & 0xe0) == 0xc0 ) { | |
377 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
378 | *bytes = 2; | |
379 | *value = ((*cp & 0x1f) << 6) | | |
380 | (*(cp+1) & 0x3f); | |
381 | } | |
382 | else if ( (*cp & 0xf0) == 0xe0 ) { | |
383 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
384 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
385 | *bytes = 3; | |
386 | *value = ((*cp & 0x0f) << 12) | | |
387 | ((*(cp+1) & 0x3f) << 6) | | |
388 | (*(cp+2) & 0x3f); | |
389 | } | |
390 | else if ( (*cp & 0xf8) == 0xf0 ) { | |
391 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
392 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
393 | if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; | |
394 | *bytes = 4; | |
395 | *value = ((*cp & 0x07) << 18) | | |
396 | ((*(cp+1) & 0x3f) << 12) | | |
397 | ((*(cp+2) & 0x3f) << 6) | | |
398 | (*(cp+3) & 0x3f); | |
399 | } | |
400 | else if ( (*cp & 0xfc) == 0xf8 ) { | |
401 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
402 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
403 | if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; | |
404 | if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault; | |
405 | *bytes = 5; | |
406 | *value = ((*cp & 0x03) << 24) | | |
407 | ((*(cp+1) & 0x3f) << 18) | | |
408 | ((*(cp+2) & 0x3f) << 12) | | |
409 | ((*(cp+3) & 0x3f) << 6) | | |
410 | (*(cp+4) & 0x3f); | |
411 | } | |
412 | else if ( (*cp & 0xfe) == 0xfc ) { | |
413 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
414 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
415 | if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; | |
416 | if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault; | |
417 | if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault; | |
418 | *bytes = 6; | |
419 | *value = ((*cp & 0x01) << 30) | | |
420 | ((*(cp+1) & 0x3f) << 24) | | |
421 | ((*(cp+2) & 0x3f) << 18) | | |
422 | ((*(cp+3) & 0x3f) << 12) | | |
423 | ((*(cp+4) & 0x3f) << 6) | | |
424 | (*(cp+5) & 0x3f); | |
425 | } | |
426 | else { | |
427 | dodefault: | |
428 | *bytes = 1; | |
429 | *value = (unsigned char)*cp; | |
430 | } | |
431 | } | |
432 | ||
433 | static | |
434 | char * | |
435 | entitize(const char * str) | |
436 | { | |
437 | int elen, ecount, nsecount; | |
438 | entity_types *ep; | |
439 | const char * cp; | |
440 | char * p, * tmp, * xstr; | |
441 | ||
442 | char tmpsub[20]; | |
443 | int bytes = 0; | |
444 | int value = 0; | |
445 | ep = stdentities; | |
446 | elen = ecount = nsecount = 0; | |
447 | ||
448 | /* figure # of entity replacements and additional size. */ | |
449 | while (ep->text) { | |
450 | cp = str; | |
451 | while ((cp = strstr(cp, ep->text)) != NULL) { | |
452 | elen += strlen(ep->entity) - strlen(ep->text); | |
453 | ecount++; | |
454 | cp += strlen(ep->text); | |
455 | } | |
456 | ep++; | |
457 | } | |
458 | ||
459 | /* figure the same for other than standard entities (i.e. anything | |
460 | * that isn't in the range U+0000 to U+007F */ | |
461 | for ( cp = str; *cp; cp++ ) { | |
462 | if ( *cp & 0x80 ) { | |
463 | ||
464 | utf8_to_int( cp, &bytes, &value ); | |
465 | cp += bytes-1; | |
466 | elen += sprintf( tmpsub, "&#x%x;", value ) - bytes; | |
467 | nsecount++; | |
468 | } | |
469 | } | |
470 | ||
471 | /* enough space for the whole string plus entity replacements, if any */ | |
472 | tmp = g_malloc((strlen(str) + elen + 1)); | |
473 | strcpy(tmp, str); | |
474 | ||
475 | /* no entity replacements */ | |
476 | if (ecount == 0 && nsecount == 0) | |
477 | return (tmp); | |
478 | ||
479 | if ( ecount != 0 ) { | |
480 | for (ep = stdentities; ep->text; ep++) { | |
481 | p = tmp; | |
482 | while ((p = strstr(p, ep->text)) != NULL) { | |
483 | elen = strlen(ep->entity); | |
484 | ||
485 | xstr = g_strdup(p + strlen(ep->text)); | |
486 | ||
487 | strcpy(p, ep->entity); | |
488 | strcpy(p + elen, xstr); | |
489 | ||
490 | g_free(xstr); | |
491 | ||
492 | p += elen; | |
493 | } | |
494 | } | |
495 | } | |
496 | ||
497 | if ( nsecount != 0 ) { | |
498 | p = tmp; | |
499 | while (*p) { | |
500 | if ( *p & 0x80 ) { | |
501 | utf8_to_int( p, &bytes, &value ); | |
502 | if ( p[bytes] ) { | |
503 | xstr = g_strdup( p + bytes ); | |
504 | } | |
505 | else { | |
506 | xstr = NULL; | |
507 | } | |
508 | sprintf( p, "&#x%x;", value ); | |
509 | p = p+strlen(p); | |
510 | if ( xstr ) { | |
511 | strcpy( p, xstr ); | |
512 | g_free(xstr); | |
513 | } | |
514 | } | |
515 | else { | |
516 | p++; | |
517 | } | |
518 | } | |
519 | } | |
520 | return (tmp); | |
521 | } | |
522 | /**** end GPSBabel code ****/ | |
523 | ||
524 | /* export GPX */ | |
525 | static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f ) | |
526 | { | |
527 | static struct LatLon ll; | |
528 | gchar *tmp; | |
529 | vik_coord_to_latlon ( &(wp->coord), &ll ); | |
530 | fprintf ( f, "<wpt lat=\"%f\" lon=\"%f\"%s>\n", | |
531 | ll.lat, ll.lon, wp->visible ? "" : " hidden=\"hidden\"" ); | |
532 | ||
533 | tmp = entitize ( name ); | |
534 | fprintf ( f, " <name>%s</name>\n", tmp ); | |
535 | g_free ( tmp); | |
536 | ||
537 | if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) | |
538 | fprintf ( f, " <ele>%f</ele>\n", wp->altitude ); | |
539 | if ( wp->comment ) | |
540 | { | |
541 | tmp = entitize(wp->comment); | |
542 | fprintf ( f, " <desc>%s</desc>\n", tmp ); | |
543 | g_free ( tmp ); | |
544 | } | |
545 | if ( wp->image ) | |
546 | { | |
547 | tmp = entitize(wp->image); | |
548 | fprintf ( f, " <link>%s</link>\n", tmp ); | |
549 | g_free ( tmp ); | |
550 | } | |
551 | fprintf ( f, "</wpt>\n" ); | |
552 | } | |
553 | ||
554 | static void gpx_write_trackpoint ( VikTrackpoint *tp, FILE *f ) | |
555 | { | |
556 | static struct LatLon ll; | |
557 | static gchar time_buf[30]; | |
558 | vik_coord_to_latlon ( &(tp->coord), &ll ); | |
559 | ||
560 | if ( tp->newsegment ) | |
561 | fprintf ( f, " </trkseg>\n <trkseg>\n" ); | |
562 | ||
563 | fprintf ( f, " <trkpt lat=\"%f\" lon=\"%f\">\n", ll.lat, ll.lon ); | |
564 | ||
565 | if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) | |
566 | fprintf ( f, " <ele>%f</ele>\n", tp->altitude ); | |
567 | if ( tp->has_timestamp ) { | |
568 | time_buf [ strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&(tp->timestamp)) ) ] = '\0'; | |
569 | fprintf ( f, " <time>%s</time>\n", time_buf ); | |
570 | } | |
571 | fprintf ( f, " </trkpt>\n" ); | |
572 | } | |
573 | ||
574 | ||
575 | static void gpx_write_track ( const gchar *name, VikTrack *t, FILE *f ) | |
576 | { | |
577 | gchar *tmp; | |
578 | gboolean first_tp_is_newsegment; /* must temporarily make it not so, but we want to restore state. not that it matters. */ | |
579 | ||
580 | tmp = entitize ( name ); | |
581 | fprintf ( f, "<trk%s>\n <name>%s</name>\n", t->visible ? "" : " hidden=\"hidden\"", tmp ); | |
582 | g_free ( tmp ); | |
583 | ||
584 | if ( t->comment ) | |
585 | { | |
586 | tmp = entitize ( t->comment ); | |
587 | fprintf ( f, " <desc>%s</desc>\n", tmp ); | |
588 | g_free ( tmp ); | |
589 | } | |
590 | ||
591 | fprintf ( f, " <trkseg>\n" ); | |
592 | ||
593 | if ( t->trackpoints && t->trackpoints->data ) { | |
594 | first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment; | |
595 | VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */ | |
596 | } | |
597 | g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, f ); | |
598 | if ( t->trackpoints && t->trackpoints->data ) | |
599 | VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */ | |
600 | ||
601 | fprintf ( f, "</trkseg>\n</trk>\n" ); | |
602 | } | |
603 | ||
604 | void a_gpx_write_file( VikTrwLayer *vtl, FILE *f ) | |
605 | { | |
606 | fprintf(f, "<?xml version=\"1.0\"?>\n" | |
607 | "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n" | |
608 | "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" | |
609 | "xmlns=\"http://www.topografix.com/GPX/1/0\"\n" | |
610 | "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n"); | |
611 | g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_write_waypoint, f ); | |
612 | g_hash_table_foreach ( vik_trw_layer_get_tracks ( vtl ), (GHFunc) gpx_write_track, f ); | |
613 | fprintf(f, "</gpx>\n"); | |
614 | ||
615 | ||
616 | } |