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"
59 #define XML_DATE_FORMAT "%FT%T"
61 #define WRITE_STRING(string) { \
62 GnomeVFSResult vfs_result; \
63 GnomeVFSFileSize size; \
64 if(GNOME_VFS_OK != (vfs_result = gnome_vfs_write( \
65 handle, (string), strlen((string)), &size))) \
67 gchar buffer[BUFFER_SIZE]; \
68 snprintf(buffer, sizeof(buffer), \
69 "%s:\n%s\n%s", _("Error while writing to file"), \
70 _("File is incomplete."), \
71 gnome_vfs_result_to_string(vfs_result)); \
72 popup_error(_window, buffer); \
77 /** This enum defines the states of the SAX parsing state machine. */
84 INSIDE_PATH_POINT_ELE,
85 INSIDE_PATH_POINT_TIME,
86 INSIDE_PATH_POINT_DESC,
92 /** Data used during the SAX parsing operation. */
93 typedef struct _SaxData SaxData;
99 gboolean at_least_one_trkpt;
111 localtime_r(&time1, &time2);
112 snprintf(XML_TZONE, sizeof(XML_TZONE), "%+03ld:%02ld",
113 (time2.tm_gmtoff / 60 / 60), (time2.tm_gmtoff / 60) % 60);
117 write_gpx(Path * path, GnomeVFSHandle * handle)
120 WayPoint *wcurr = NULL;
121 gboolean trkseg_break = FALSE;
123 /* Find first non-zero point. */
124 for (curr = path->head - 1, wcurr = path->whead; curr++ != path->tail;) {
127 else if (wcurr && curr == wcurr->point)
131 /* Write the header. */
132 WRITE_STRING("<?xml version=\"1.0\"?>\n"
133 "<gpx version=\"1.0\" creator=\"mapper\" "
134 "xmlns=\"http://www.topografix.com/GPX/1/0\">\n"
135 " <trk>\n" " <trkseg>\n");
137 /* Curr points to first non-zero point. */
138 for (curr--; curr++ != path->tail;) {
142 gboolean first_sub = TRUE;
144 /* First trkpt of the segment - write trkseg header. */
145 WRITE_STRING(" </trkseg>\n"
147 trkseg_break = FALSE;
149 unit2latlon(curr->unitx, curr->unity, lat, lon);
150 WRITE_STRING(" <trkpt lat=\"");
151 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lat);
152 WRITE_STRING(buffer);
153 WRITE_STRING("\" lon=\"");
154 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lon);
155 WRITE_STRING(buffer);
158 /* write the elevation */
159 if (!isnan(curr->altitude)) {
164 WRITE_STRING(" <ele>");
166 g_ascii_formatd(buffer, 80, "%.2f",
168 WRITE_STRING(buffer);
170 WRITE_STRING("</ele>\n");
179 WRITE_STRING(" <time>");
180 strftime(buffer, 80, XML_DATE_FORMAT,
181 localtime(&curr->time));
182 WRITE_STRING(buffer);
183 WRITE_STRING(XML_TZONE);
184 WRITE_STRING("</time>\n");
187 if (wcurr && curr == wcurr->point) {
192 WRITE_STRING(" <desc>");
193 WRITE_STRING(wcurr->desc);
194 WRITE_STRING("</desc>\n");
198 WRITE_STRING("/>\n");
200 WRITE_STRING(" </trkpt>\n");
206 /* Write the footer. */
207 WRITE_STRING(" </trkseg>\n" " </trk>\n" "</gpx>\n");
213 * Handle a start tag in the parsing of a GPX file.
215 #define MACRO_SET_UNKNOWN() { \
216 data->prev_state = data->state; \
217 data->state = UNKNOWN; \
218 data->unknown_depth = 1; \
222 gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
225 switch (data->state) {
229 if (!strcmp((gchar *) name, "gpx"))
230 data->state = INSIDE_GPX;
235 if (!strcmp((gchar *) name, "trk"))
236 data->state = INSIDE_PATH;
241 if (!strcmp((gchar *) name, "trkseg")) {
242 data->state = INSIDE_PATH_SEGMENT;
243 data->at_least_one_trkpt = FALSE;
247 case INSIDE_PATH_SEGMENT:
248 if (!strcmp((gchar *) name, "trkpt")) {
249 const xmlChar **curr_attr;
251 gdouble lat = 0.f, lon = 0.f;
252 gboolean has_lat, has_lon;
255 for (curr_attr = attrs; *curr_attr != NULL;) {
256 const gchar *attr_name = *curr_attr++;
257 const gchar *attr_val = *curr_attr++;
258 if (!strcmp(attr_name, "lat")) {
259 lat = g_ascii_strtod(attr_val, &error_check);
260 if (error_check != attr_val)
262 } else if (!strcmp(attr_name, "lon")) {
263 lon = g_ascii_strtod(attr_val, &error_check);
264 if (error_check != attr_val)
268 if (has_lat && has_lon) {
269 MACRO_PATH_INCREMENT_TAIL(data->path);
270 latlon2unit(lat, lon, data->path.tail->unitx, data->path.tail->unity);
271 data->path.tail->time = 0;
272 data->path.tail->altitude = NAN;
273 data->state = INSIDE_PATH_POINT;
279 case INSIDE_PATH_POINT:
280 if (!strcmp((gchar *) name, "time"))
281 data->state = INSIDE_PATH_POINT_TIME;
282 else if (!strcmp((gchar *) name, "ele"))
283 data->state = INSIDE_PATH_POINT_ELE;
284 else if (!strcmp((gchar *) name, "desc"))
285 data->state = INSIDE_PATH_POINT_DESC;
291 data->unknown_depth++;
300 * Handle an end tag in the parsing of a GPX file.
303 gpx_end_element(SaxData * data, const xmlChar * name)
306 switch (data->state) {
313 if (!strcmp((gchar *) name, "gpx"))
314 data->state = FINISH;
319 if (!strcmp((gchar *) name, "trk"))
320 data->state = INSIDE_GPX;
324 case INSIDE_PATH_SEGMENT:
325 if (!strcmp((gchar *) name, "trkseg")) {
326 if (data->at_least_one_trkpt) {
327 MACRO_PATH_INCREMENT_TAIL(data->path);
328 *data->path.tail = _point_null;
330 data->state = INSIDE_PATH;
334 case INSIDE_PATH_POINT:
335 if (!strcmp((gchar *) name, "trkpt")) {
336 data->state = INSIDE_PATH_SEGMENT;
337 data->at_least_one_trkpt = TRUE;
341 case INSIDE_PATH_POINT_ELE:
342 if (!strcmp((gchar *) name, "ele")) {
344 data->path.tail->altitude = g_ascii_strtod(data->chars->str, &error_check);
345 if (error_check == data->chars->str)
346 data->path.tail->altitude = NAN;
347 data->state = INSIDE_PATH_POINT;
348 g_string_free(data->chars, TRUE);
349 data->chars = g_string_new("");
353 case INSIDE_PATH_POINT_TIME:
354 if (!strcmp((gchar *) name, "time")) {
358 if (NULL == (ptr = strptime(data->chars->str,
359 XML_DATE_FORMAT, &time)))
360 /* Failed to parse dateTime format. */
363 /* Parse was successful. Now we have to parse timezone.
364 * From here on, if there is an error, I just assume local
365 * timezone. Yes, this is not proper XML, but I don't
369 /* First, set time in "local" time zone. */
370 data->path.tail->time = (mktime(&time));
372 /* Now, skip inconsequential characters */
373 while (*ptr && *ptr != 'Z' && *ptr != '-'
377 /* Check if we ran to the end of the string. */
379 /* Next character is either 'Z', '-', or '+' */
381 /* Zulu (UTC) time. Undo the local time zone's
383 data->path.tail->time += time.tm_gmtoff;
385 /* Not Zulu (UTC). Must parse hours and minutes. */
387 strtol(ptr, &error_check,
389 if (error_check != ptr
391 error_check) == ':') {
392 /* Parse of hours worked. Check minutes. */
399 /* Parse of minutes worked. Calculate. */
400 data->path.tail->time +=
402 (offhours * 60 * 60 + offmins * 60));
407 /* Successfully parsed dateTime. */
408 data->state = INSIDE_PATH_POINT;
411 g_string_free(data->chars, TRUE);
412 data->chars = g_string_new("");
416 case INSIDE_PATH_POINT_DESC:
417 /* only parse description for routes */
418 if (!strcmp((gchar *) name, "desc")) {
419 MACRO_PATH_INCREMENT_WTAIL(data->path);
420 data->path.wtail->point = data->path.tail;
421 data->path.wtail->desc
422 = g_string_free(data->chars, FALSE);
423 data->chars = g_string_new("");
424 data->state = INSIDE_PATH_POINT;
429 if (!--data->unknown_depth)
430 data->state = data->prev_state;
441 * Handle char data in the parsing of a GPX file.
444 gpx_chars(SaxData * data, const xmlChar * ch, int len)
448 switch (data->state) {
452 case INSIDE_PATH_POINT_ELE:
453 case INSIDE_PATH_POINT_TIME:
454 case INSIDE_PATH_POINT_DESC:
455 for (i = 0; i < len; i++)
456 data->chars = g_string_append_c(data->chars, ch[i]);
457 vprintf("%s\n", data->chars->str);
466 * Handle an entity in the parsing of a GPX file. We don't do anything
470 gpx_get_entity(SaxData * data, const xmlChar * name)
472 return xmlGetPredefinedEntity(name);
476 * Handle an error in the parsing of a GPX file.
479 gpx_error(SaxData * data, const gchar * msg, ...)
485 parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
488 xmlSAXHandler sax_handler;
490 MACRO_PATH_INIT(data.path);
492 data.chars = g_string_new("");
494 memset(&sax_handler, 0, sizeof(sax_handler));
495 sax_handler.characters = (charactersSAXFunc) gpx_chars;
496 sax_handler.startElement = (startElementSAXFunc) gpx_start_element;
497 sax_handler.endElement = (endElementSAXFunc) gpx_end_element;
498 sax_handler.entityDecl = (entityDeclSAXFunc) gpx_get_entity;
499 sax_handler.warning = (warningSAXFunc) gpx_error;
500 sax_handler.error = (errorSAXFunc) gpx_error;
501 sax_handler.fatalError = (fatalErrorSAXFunc) gpx_error;
503 xmlSAXUserParseMemory(&sax_handler, &data, buffer, size);
504 g_string_free(data.chars, TRUE);
506 if (data.state != FINISH) {
510 if (policy_old && to_replace->head != to_replace->tail) {
514 if (policy_old > 0) {
515 /* Append to current path. Make sure last path point is zero. */
516 if (to_replace->tail->unity != 0) {
517 MACRO_PATH_INCREMENT_TAIL((*to_replace));
518 *to_replace->tail = _point_null;
523 /* Prepend to current route. */
528 /* Find src_first non-zero point. */
529 for (src_first = src->head - 1; src_first++ != src->tail;)
530 if (src_first->unity)
533 /* Append route points from src to dest. */
534 if (src->tail >= src_first) {
536 guint num_dest_points = dest->tail - dest->head + 1;
537 guint num_src_points = src->tail - src_first + 1;
539 /* Adjust dest->tail to be able to fit src route data
540 * plus room for more route data. */
541 path_resize(dest, num_dest_points + num_src_points);
543 memcpy(dest->tail + 1, src_first,
544 num_src_points * sizeof(Point));
546 dest->tail += num_src_points;
548 /* Append waypoints from src to dest->. */
549 path_wresize(dest, (dest->wtail - dest->whead)
550 + (src->wtail - src->whead) + 2);
551 for (curr = src->whead - 1; curr++ != src->wtail;) {
552 (++(dest->wtail))->point =
553 dest->head + num_dest_points +
554 (curr->point - src_first);
555 dest->wtail->desc = curr->desc;
560 /* Kill old route - don't use MACRO_PATH_FREE(), because that
561 * would free the string desc's that we just moved to data.route. */
565 (*to_replace) = *dest;
567 MACRO_PATH_FREE((*to_replace));
568 /* Overwrite with data.route. */
569 (*to_replace) = data.path;
570 path_resize(to_replace,
571 to_replace->tail - to_replace->head + 1);
572 path_wresize(to_replace,
573 to_replace->wtail - to_replace->whead + 1);