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.
37 #include <glib/gstdio.h>
38 #include <glib/gi18n.h>
41 #include <libgnomevfs/gnome-vfs.h>
42 #include <curl/multi.h>
43 #include <gconf/gconf-client.h>
44 #include <libxml/parser.h>
51 #include "ui-common.h"
55 #include "mapper-types.h"
60 #define XML_DATE_FORMAT "%FT%T"
62 #define WRITE_STRING(string) { \
63 GnomeVFSResult vfs_result; \
64 GnomeVFSFileSize size; \
65 if(GNOME_VFS_OK != (vfs_result = gnome_vfs_write( \
66 handle, (string), strlen((string)), &size))) \
68 gchar buffer[BUFFER_SIZE]; \
69 g_snprintf(buffer, sizeof(buffer), \
70 "%s:\n%s\n%s", _("Error while writing to file"), \
71 _("File is incomplete."), \
72 gnome_vfs_result_to_string(vfs_result)); \
73 popup_error(_window, buffer); \
78 /** This enum defines the states of the SAX parsing state machine. */
85 INSIDE_PATH_POINT_ELE,
86 INSIDE_PATH_POINT_TIME,
87 INSIDE_PATH_POINT_DESC,
93 /** Data used during the SAX parsing operation. */
94 typedef struct _SaxData SaxData;
100 gboolean at_least_one_trkpt;
112 localtime_r(&time1, &time2);
113 g_snprintf(XML_TZONE, sizeof(XML_TZONE), "%+03ld:%02ld",
114 (time2.tm_gmtoff / 60 / 60), (time2.tm_gmtoff / 60) % 60);
118 write_gpx(Path * path, GnomeVFSHandle * handle)
121 WayPoint *wcurr = NULL;
122 gboolean trkseg_break = FALSE;
124 /* Find first non-zero point. */
125 for (curr = path->head - 1, wcurr = path->whead; curr++ != path->tail;) {
128 else if (wcurr && curr == wcurr->point)
132 /* Write the header. */
133 WRITE_STRING("<?xml version=\"1.0\"?>\n"
134 "<gpx version=\"1.0\" creator=\"mapper\" "
135 "xmlns=\"http://www.topografix.com/GPX/1/0\">\n"
136 " <trk>\n" " <trkseg>\n");
138 /* Curr points to first non-zero point. */
139 for (curr--; curr++ != path->tail;) {
143 gboolean first_sub = TRUE;
145 /* First trkpt of the segment - write trkseg header. */
146 WRITE_STRING(" </trkseg>\n"
148 trkseg_break = FALSE;
150 unit2latlon(curr->unitx, curr->unity, lat, lon);
151 WRITE_STRING(" <trkpt lat=\"");
152 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lat);
153 WRITE_STRING(buffer);
154 WRITE_STRING("\" lon=\"");
155 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lon);
156 WRITE_STRING(buffer);
159 /* write the elevation */
160 if (!isnan(curr->altitude)) {
165 WRITE_STRING(" <ele>");
167 g_ascii_formatd(buffer, 80, "%.2f",
169 WRITE_STRING(buffer);
171 WRITE_STRING("</ele>\n");
180 WRITE_STRING(" <time>");
181 strftime(buffer, 80, XML_DATE_FORMAT,
182 localtime(&curr->time));
183 WRITE_STRING(buffer);
184 WRITE_STRING(XML_TZONE);
185 WRITE_STRING("</time>\n");
188 if (wcurr && curr == wcurr->point) {
193 WRITE_STRING(" <desc>");
194 WRITE_STRING(wcurr->desc);
195 WRITE_STRING("</desc>\n");
199 WRITE_STRING("/>\n");
201 WRITE_STRING(" </trkpt>\n");
207 /* Write the footer. */
208 WRITE_STRING(" </trkseg>\n" " </trk>\n" "</gpx>\n");
214 * Handle a start tag in the parsing of a GPX file.
216 #define MACRO_SET_UNKNOWN() { \
217 data->prev_state = data->state; \
218 data->state = UNKNOWN; \
219 data->unknown_depth = 1; \
223 gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
226 switch (data->state) {
230 if (!strcmp((gchar *) name, "gpx"))
231 data->state = INSIDE_GPX;
236 if (!strcmp((gchar *) name, "trk"))
237 data->state = INSIDE_PATH;
242 if (!strcmp((gchar *) name, "trkseg")) {
243 data->state = INSIDE_PATH_SEGMENT;
244 data->at_least_one_trkpt = FALSE;
248 case INSIDE_PATH_SEGMENT:
249 if (!strcmp((gchar *) name, "trkpt")) {
250 const xmlChar **curr_attr;
252 gdouble lat = 0.f, lon = 0.f;
253 gboolean has_lat, has_lon;
256 for (curr_attr = attrs; *curr_attr != NULL;) {
257 const gchar *attr_name = *curr_attr++;
258 const gchar *attr_val = *curr_attr++;
259 if (!strcmp(attr_name, "lat")) {
260 lat = g_ascii_strtod(attr_val, &error_check);
261 if (error_check != attr_val)
263 } else if (!strcmp(attr_name, "lon")) {
264 lon = g_ascii_strtod(attr_val, &error_check);
265 if (error_check != attr_val)
269 if (has_lat && has_lon) {
270 MACRO_PATH_INCREMENT_TAIL(data->path);
271 latlon2unit(lat, lon, data->path.tail->unitx, data->path.tail->unity);
272 data->path.tail->time = 0;
273 data->path.tail->altitude = NAN;
274 data->state = INSIDE_PATH_POINT;
280 case INSIDE_PATH_POINT:
281 if (!strcmp((gchar *) name, "time"))
282 data->state = INSIDE_PATH_POINT_TIME;
283 else if (!strcmp((gchar *) name, "ele"))
284 data->state = INSIDE_PATH_POINT_ELE;
285 else if (!strcmp((gchar *) name, "desc"))
286 data->state = INSIDE_PATH_POINT_DESC;
292 data->unknown_depth++;
301 * Handle an end tag in the parsing of a GPX file.
304 gpx_end_element(SaxData * data, const xmlChar * name)
307 switch (data->state) {
314 if (!strcmp((gchar *) name, "gpx"))
315 data->state = FINISH;
320 if (!strcmp((gchar *) name, "trk"))
321 data->state = INSIDE_GPX;
325 case INSIDE_PATH_SEGMENT:
326 if (!strcmp((gchar *) name, "trkseg")) {
327 if (data->at_least_one_trkpt) {
328 MACRO_PATH_INCREMENT_TAIL(data->path);
329 *data->path.tail = _point_null;
331 data->state = INSIDE_PATH;
335 case INSIDE_PATH_POINT:
336 if (!strcmp((gchar *) name, "trkpt")) {
337 data->state = INSIDE_PATH_SEGMENT;
338 data->at_least_one_trkpt = TRUE;
342 case INSIDE_PATH_POINT_ELE:
343 if (!strcmp((gchar *) name, "ele")) {
345 data->path.tail->altitude = g_ascii_strtod(data->chars->str, &error_check);
346 if (error_check == data->chars->str)
347 data->path.tail->altitude = NAN;
348 data->state = INSIDE_PATH_POINT;
349 g_string_free(data->chars, TRUE);
350 data->chars = g_string_new("");
354 case INSIDE_PATH_POINT_TIME:
355 if (!strcmp((gchar *) name, "time")) {
359 if (NULL == (ptr = strptime(data->chars->str,
360 XML_DATE_FORMAT, &time)))
361 /* Failed to parse dateTime format. */
364 /* Parse was successful. Now we have to parse timezone.
365 * From here on, if there is an error, I just assume local
366 * timezone. Yes, this is not proper XML, but I don't
370 /* First, set time in "local" time zone. */
371 data->path.tail->time = (mktime(&time));
373 /* Now, skip inconsequential characters */
374 while (*ptr && *ptr != 'Z' && *ptr != '-'
378 /* Check if we ran to the end of the string. */
380 /* Next character is either 'Z', '-', or '+' */
382 /* Zulu (UTC) time. Undo the local time zone's
384 data->path.tail->time += time.tm_gmtoff;
386 /* Not Zulu (UTC). Must parse hours and minutes. */
388 strtol(ptr, &error_check,
390 if (error_check != ptr
392 error_check) == ':') {
393 /* Parse of hours worked. Check minutes. */
400 /* Parse of minutes worked. Calculate. */
401 data->path.tail->time +=
403 (offhours * 60 * 60 + offmins * 60));
408 /* Successfully parsed dateTime. */
409 data->state = INSIDE_PATH_POINT;
412 g_string_free(data->chars, TRUE);
413 data->chars = g_string_new("");
417 case INSIDE_PATH_POINT_DESC:
418 /* only parse description for routes */
419 if (!strcmp((gchar *) name, "desc")) {
420 MACRO_PATH_INCREMENT_WTAIL(data->path);
421 data->path.wtail->point = data->path.tail;
422 data->path.wtail->desc
423 = g_string_free(data->chars, FALSE);
424 data->chars = g_string_new("");
425 data->state = INSIDE_PATH_POINT;
430 if (!--data->unknown_depth)
431 data->state = data->prev_state;
442 * Handle char data in the parsing of a GPX file.
445 gpx_chars(SaxData * data, const xmlChar * ch, int len)
449 switch (data->state) {
453 case INSIDE_PATH_POINT_ELE:
454 case INSIDE_PATH_POINT_TIME:
455 case INSIDE_PATH_POINT_DESC:
456 for (i = 0; i < len; i++)
457 data->chars = g_string_append_c(data->chars, ch[i]);
458 vprintf("%s\n", data->chars->str);
467 * Handle an entity in the parsing of a GPX file. We don't do anything
471 gpx_get_entity(SaxData * data, const xmlChar * name)
473 return xmlGetPredefinedEntity(name);
477 * Handle an error in the parsing of a GPX file.
480 gpx_error(SaxData * data, const gchar * msg, ...)
486 parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
489 xmlSAXHandler sax_handler;
491 MACRO_PATH_INIT(data.path);
493 data.chars = g_string_new("");
495 memset(&sax_handler, 0, sizeof(sax_handler));
496 sax_handler.characters = (charactersSAXFunc) gpx_chars;
497 sax_handler.startElement = (startElementSAXFunc) gpx_start_element;
498 sax_handler.endElement = (endElementSAXFunc) gpx_end_element;
499 sax_handler.entityDecl = (entityDeclSAXFunc) gpx_get_entity;
500 sax_handler.warning = (warningSAXFunc) gpx_error;
501 sax_handler.error = (errorSAXFunc) gpx_error;
502 sax_handler.fatalError = (fatalErrorSAXFunc) gpx_error;
504 xmlSAXUserParseMemory(&sax_handler, &data, buffer, size);
505 g_string_free(data.chars, TRUE);
507 if (data.state != FINISH) {
511 if (policy_old && to_replace->head != to_replace->tail) {
515 if (policy_old > 0) {
516 /* Append to current path. Make sure last path point is zero. */
517 if (to_replace->tail->unity != 0) {
518 MACRO_PATH_INCREMENT_TAIL((*to_replace));
519 *to_replace->tail = _point_null;
524 /* Prepend to current route. */
529 /* Find src_first non-zero point. */
530 for (src_first = src->head - 1; src_first++ != src->tail;)
531 if (src_first->unity)
534 /* Append route points from src to dest. */
535 if (src->tail >= src_first) {
537 guint num_dest_points = dest->tail - dest->head + 1;
538 guint num_src_points = src->tail - src_first + 1;
540 /* Adjust dest->tail to be able to fit src route data
541 * plus room for more route data. */
542 path_resize(dest, num_dest_points + num_src_points);
544 memcpy(dest->tail + 1, src_first,
545 num_src_points * sizeof(Point));
547 dest->tail += num_src_points;
549 /* Append waypoints from src to dest->. */
550 path_wresize(dest, (dest->wtail - dest->whead)
551 + (src->wtail - src->whead) + 2);
552 for (curr = src->whead - 1; curr++ != src->wtail;) {
553 (++(dest->wtail))->point =
554 dest->head + num_dest_points +
555 (curr->point - src_first);
556 dest->wtail->desc = curr->desc;
561 /* Kill old route - don't use MACRO_PATH_FREE(), because that
562 * would free the string desc's that we just moved to data.route. */
566 (*to_replace) = *dest;
568 MACRO_PATH_FREE((*to_replace));
569 /* Overwrite with data.route. */
570 (*to_replace) = data.path;
571 path_resize(to_replace,
572 to_replace->tail - to_replace->head + 1);
573 path_wresize(to_replace,
574 to_replace->wtail - to_replace->whead + 1);