2 * This file is part of mapper
4 * Copyright (C) 2006-2007 John Costigan.
6 * POI and GPS-Info code originally written by Cezary Jackiewicz.
8 * Default map data provided by http://www.openstreetmap.org/
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.
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.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 #include <glib/gstdio.h>
36 #include <glib/gi18n.h>
39 #include <libgnomevfs/gnome-vfs.h>
40 #include <curl/multi.h>
41 #include <gconf/gconf-client.h>
42 #include <libxml/parser.h>
49 #include "ui-common.h"
53 #include "mapper-types.h"
58 #define XML_DATE_FORMAT "%FT%T"
60 #define WRITE_STRING(string) { \
61 GnomeVFSResult vfs_result; \
62 GnomeVFSFileSize size; \
63 if(GNOME_VFS_OK != (vfs_result = gnome_vfs_write( \
64 handle, (string), strlen((string)), &size))) \
66 gchar buffer[BUFFER_SIZE]; \
67 g_snprintf(buffer, sizeof(buffer), \
68 "%s:\n%s\n%s", _("Error while writing to file"), \
69 _("File is incomplete."), \
70 gnome_vfs_result_to_string(vfs_result)); \
71 popup_error(_window, buffer); \
76 /** This enum defines the states of the SAX parsing state machine. */
83 INSIDE_PATH_POINT_ELE,
84 INSIDE_PATH_POINT_TIME,
85 INSIDE_PATH_POINT_DESC,
91 /** Data used during the SAX parsing operation. */
92 typedef struct _SaxData SaxData;
98 gboolean at_least_one_trkpt;
110 localtime_r(&time1, &time2);
111 g_snprintf(XML_TZONE, sizeof(XML_TZONE), "%+03ld:%02ld",
112 (time2.tm_gmtoff / 60 / 60), (time2.tm_gmtoff / 60) % 60);
116 write_gpx(Path * path, GnomeVFSHandle * handle)
119 WayPoint *wcurr = NULL;
120 gboolean trkseg_break = FALSE;
122 /* Find first non-zero point. */
123 for (curr = path->head - 1, wcurr = path->whead; curr++ != path->tail;) {
126 else if (wcurr && curr == wcurr->point)
130 /* Write the header. */
131 WRITE_STRING("<?xml version=\"1.0\"?>\n"
132 "<gpx version=\"1.0\" creator=\"mapper\" "
133 "xmlns=\"http://www.topografix.com/GPX/1/0\">\n"
134 " <trk>\n" " <trkseg>\n");
136 /* Curr points to first non-zero point. */
137 for (curr--; curr++ != path->tail;) {
141 gboolean first_sub = TRUE;
143 /* First trkpt of the segment - write trkseg header. */
144 WRITE_STRING(" </trkseg>\n"
146 trkseg_break = FALSE;
148 unit2latlon(curr->unitx, curr->unity, lat, lon);
149 WRITE_STRING(" <trkpt lat=\"");
150 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lat);
151 WRITE_STRING(buffer);
152 WRITE_STRING("\" lon=\"");
153 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lon);
154 WRITE_STRING(buffer);
157 /* write the elevation */
158 if (!isnan(curr->altitude)) {
163 WRITE_STRING(" <ele>");
165 g_ascii_formatd(buffer, 80, "%.2f",
167 WRITE_STRING(buffer);
169 WRITE_STRING("</ele>\n");
178 WRITE_STRING(" <time>");
179 strftime(buffer, 80, XML_DATE_FORMAT,
180 localtime(&curr->time));
181 WRITE_STRING(buffer);
182 WRITE_STRING(XML_TZONE);
183 WRITE_STRING("</time>\n");
186 if (wcurr && curr == wcurr->point) {
191 WRITE_STRING(" <desc>");
192 WRITE_STRING(wcurr->desc);
193 WRITE_STRING("</desc>\n");
197 WRITE_STRING("/>\n");
199 WRITE_STRING(" </trkpt>\n");
205 /* Write the footer. */
206 WRITE_STRING(" </trkseg>\n" " </trk>\n" "</gpx>\n");
212 * Handle a start tag in the parsing of a GPX file.
214 #define MACRO_SET_UNKNOWN() { \
215 data->prev_state = data->state; \
216 data->state = UNKNOWN; \
217 data->unknown_depth = 1; \
221 gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
224 switch (data->state) {
228 if (!strcmp((gchar *) name, "gpx"))
229 data->state = INSIDE_GPX;
234 if (!strcmp((gchar *) name, "trk"))
235 data->state = INSIDE_PATH;
240 if (!strcmp((gchar *) name, "trkseg")) {
241 data->state = INSIDE_PATH_SEGMENT;
242 data->at_least_one_trkpt = FALSE;
246 case INSIDE_PATH_SEGMENT:
247 if (!strcmp((gchar *) name, "trkpt")) {
248 const xmlChar **curr_attr;
250 gdouble lat = 0.f, lon = 0.f;
251 gboolean has_lat, has_lon;
254 for (curr_attr = attrs; *curr_attr != NULL;) {
255 const gchar *attr_name = *curr_attr++;
256 const gchar *attr_val = *curr_attr++;
257 if (!strcmp(attr_name, "lat")) {
258 lat = g_ascii_strtod(attr_val, &error_check);
259 if (error_check != attr_val)
261 } else if (!strcmp(attr_name, "lon")) {
262 lon = g_ascii_strtod(attr_val, &error_check);
263 if (error_check != attr_val)
267 if (has_lat && has_lon) {
268 MACRO_PATH_INCREMENT_TAIL(data->path);
269 latlon2unit(lat, lon, data->path.tail->unitx, data->path.tail->unity);
270 data->path.tail->time = 0;
271 data->path.tail->altitude = NAN;
272 data->state = INSIDE_PATH_POINT;
278 case INSIDE_PATH_POINT:
279 if (!strcmp((gchar *) name, "time"))
280 data->state = INSIDE_PATH_POINT_TIME;
281 else if (!strcmp((gchar *) name, "ele"))
282 data->state = INSIDE_PATH_POINT_ELE;
283 else if (!strcmp((gchar *) name, "desc"))
284 data->state = INSIDE_PATH_POINT_DESC;
290 data->unknown_depth++;
299 * Handle an end tag in the parsing of a GPX file.
302 gpx_end_element(SaxData * data, const xmlChar * name)
305 switch (data->state) {
312 if (!strcmp((gchar *) name, "gpx"))
313 data->state = FINISH;
318 if (!strcmp((gchar *) name, "trk"))
319 data->state = INSIDE_GPX;
323 case INSIDE_PATH_SEGMENT:
324 if (!strcmp((gchar *) name, "trkseg")) {
325 if (data->at_least_one_trkpt) {
326 MACRO_PATH_INCREMENT_TAIL(data->path);
327 *data->path.tail = _point_null;
329 data->state = INSIDE_PATH;
333 case INSIDE_PATH_POINT:
334 if (!strcmp((gchar *) name, "trkpt")) {
335 data->state = INSIDE_PATH_SEGMENT;
336 data->at_least_one_trkpt = TRUE;
340 case INSIDE_PATH_POINT_ELE:
341 if (!strcmp((gchar *) name, "ele")) {
343 data->path.tail->altitude = g_ascii_strtod(data->chars->str, &error_check);
344 if (error_check == data->chars->str)
345 data->path.tail->altitude = NAN;
346 data->state = INSIDE_PATH_POINT;
347 g_string_free(data->chars, TRUE);
348 data->chars = g_string_new("");
352 case INSIDE_PATH_POINT_TIME:
353 if (!strcmp((gchar *) name, "time")) {
357 if (NULL == (ptr = strptime(data->chars->str,
358 XML_DATE_FORMAT, &time)))
359 /* Failed to parse dateTime format. */
362 /* Parse was successful. Now we have to parse timezone.
363 * From here on, if there is an error, I just assume local
364 * timezone. Yes, this is not proper XML, but I don't
368 /* First, set time in "local" time zone. */
369 data->path.tail->time = (mktime(&time));
371 /* Now, skip inconsequential characters */
372 while (*ptr && *ptr != 'Z' && *ptr != '-'
376 /* Check if we ran to the end of the string. */
378 /* Next character is either 'Z', '-', or '+' */
380 /* Zulu (UTC) time. Undo the local time zone's
382 data->path.tail->time += time.tm_gmtoff;
384 /* Not Zulu (UTC). Must parse hours and minutes. */
386 strtol(ptr, &error_check,
388 if (error_check != ptr
390 error_check) == ':') {
391 /* Parse of hours worked. Check minutes. */
398 /* Parse of minutes worked. Calculate. */
399 data->path.tail->time +=
401 (offhours * 60 * 60 + offmins * 60));
406 /* Successfully parsed dateTime. */
407 data->state = INSIDE_PATH_POINT;
410 g_string_free(data->chars, TRUE);
411 data->chars = g_string_new("");
415 case INSIDE_PATH_POINT_DESC:
416 /* only parse description for routes */
417 if (!strcmp((gchar *) name, "desc")) {
418 MACRO_PATH_INCREMENT_WTAIL(data->path);
419 data->path.wtail->point = data->path.tail;
420 data->path.wtail->desc
421 = g_string_free(data->chars, FALSE);
422 data->chars = g_string_new("");
423 data->state = INSIDE_PATH_POINT;
428 if (!--data->unknown_depth)
429 data->state = data->prev_state;
440 * Handle char data in the parsing of a GPX file.
443 gpx_chars(SaxData * data, const xmlChar * ch, int len)
447 switch (data->state) {
451 case INSIDE_PATH_POINT_ELE:
452 case INSIDE_PATH_POINT_TIME:
453 case INSIDE_PATH_POINT_DESC:
454 for (i = 0; i < len; i++)
455 data->chars = g_string_append_c(data->chars, ch[i]);
456 vprintf("%s\n", data->chars->str);
465 * Handle an entity in the parsing of a GPX file. We don't do anything
469 gpx_get_entity(SaxData * data, const xmlChar * name)
471 return xmlGetPredefinedEntity(name);
475 * Handle an error in the parsing of a GPX file.
478 gpx_error(SaxData * data, const gchar * msg, ...)
484 parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
487 xmlSAXHandler sax_handler;
489 MACRO_PATH_INIT(data.path);
491 data.chars = g_string_new("");
493 memset(&sax_handler, 0, sizeof(sax_handler));
494 sax_handler.characters = (charactersSAXFunc) gpx_chars;
495 sax_handler.startElement = (startElementSAXFunc) gpx_start_element;
496 sax_handler.endElement = (endElementSAXFunc) gpx_end_element;
497 sax_handler.entityDecl = (entityDeclSAXFunc) gpx_get_entity;
498 sax_handler.warning = (warningSAXFunc) gpx_error;
499 sax_handler.error = (errorSAXFunc) gpx_error;
500 sax_handler.fatalError = (fatalErrorSAXFunc) gpx_error;
502 xmlSAXUserParseMemory(&sax_handler, &data, buffer, size);
503 g_string_free(data.chars, TRUE);
505 if (data.state != FINISH) {
509 if (policy_old && to_replace->head != to_replace->tail) {
513 if (policy_old > 0) {
514 /* Append to current path. Make sure last path point is zero. */
515 if (to_replace->tail->unity != 0) {
516 MACRO_PATH_INCREMENT_TAIL((*to_replace));
517 *to_replace->tail = _point_null;
522 /* Prepend to current route. */
527 /* Find src_first non-zero point. */
528 for (src_first = src->head - 1; src_first++ != src->tail;)
529 if (src_first->unity)
532 /* Append route points from src to dest. */
533 if (src->tail >= src_first) {
535 guint num_dest_points = dest->tail - dest->head + 1;
536 guint num_src_points = src->tail - src_first + 1;
538 /* Adjust dest->tail to be able to fit src route data
539 * plus room for more route data. */
540 path_resize(dest, num_dest_points + num_src_points);
542 memcpy(dest->tail + 1, src_first,
543 num_src_points * sizeof(Point));
545 dest->tail += num_src_points;
547 /* Append waypoints from src to dest->. */
548 path_wresize(dest, (dest->wtail - dest->whead)
549 + (src->wtail - src->whead) + 2);
550 for (curr = src->whead - 1; curr++ != src->wtail;) {
551 (++(dest->wtail))->point =
552 dest->head + num_dest_points +
553 (curr->point - src_first);
554 dest->wtail->desc = curr->desc;
559 /* Kill old route - don't use MACRO_PATH_FREE(), because that
560 * would free the string desc's that we just moved to data.route. */
564 (*to_replace) = *dest;
566 MACRO_PATH_FREE((*to_replace));
567 /* Overwrite with data.route. */
568 (*to_replace) = data.path;
569 path_resize(to_replace,
570 to_replace->tail - to_replace->head + 1);
571 path_wresize(to_replace,
572 to_replace->wtail - to_replace->whead + 1);