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"
58 #define XML_DATE_FORMAT "%FT%T"
60 /** This enum defines the states of the SAX parsing state machine. */
67 INSIDE_PATH_POINT_ELE,
68 INSIDE_PATH_POINT_TIME,
69 INSIDE_PATH_POINT_DESC,
75 /** Data used during the SAX parsing operation. */
76 typedef struct _SaxData SaxData;
82 gboolean at_least_one_trkpt;
94 localtime_r(&time1, &time2);
95 snprintf(XML_TZONE, sizeof(XML_TZONE), "%+03ld:%02ld",
96 (time2.tm_gmtoff / 60 / 60), (time2.tm_gmtoff / 60) % 60);
100 write_gpx(Path * path, GnomeVFSHandle * handle)
103 WayPoint *wcurr = NULL;
104 gboolean trkseg_break = FALSE;
106 /* Find first non-zero point. */
107 for (curr = path->head - 1, wcurr = path->whead; curr++ != path->tail;) {
110 else if (wcurr && curr == wcurr->point)
114 /* Write the header. */
115 WRITE_STRING("<?xml version=\"1.0\"?>\n"
116 "<gpx version=\"1.0\" creator=\"mapper\" "
117 "xmlns=\"http://www.topografix.com/GPX/1/0\">\n"
118 " <trk>\n" " <trkseg>\n");
120 /* Curr points to first non-zero point. */
121 for (curr--; curr++ != path->tail;) {
125 gboolean first_sub = TRUE;
127 /* First trkpt of the segment - write trkseg header. */
128 WRITE_STRING(" </trkseg>\n"
130 trkseg_break = FALSE;
132 unit2latlon(curr->unitx, curr->unity, lat, lon);
133 WRITE_STRING(" <trkpt lat=\"");
134 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lat);
135 WRITE_STRING(buffer);
136 WRITE_STRING("\" lon=\"");
137 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lon);
138 WRITE_STRING(buffer);
141 /* write the elevation */
142 if (!isnan(curr->altitude)) {
147 WRITE_STRING(" <ele>");
149 g_ascii_formatd(buffer, 80, "%.2f",
151 WRITE_STRING(buffer);
153 WRITE_STRING("</ele>\n");
162 WRITE_STRING(" <time>");
163 strftime(buffer, 80, XML_DATE_FORMAT,
164 localtime(&curr->time));
165 WRITE_STRING(buffer);
166 WRITE_STRING(XML_TZONE);
167 WRITE_STRING("</time>\n");
170 if (wcurr && curr == wcurr->point) {
175 WRITE_STRING(" <desc>");
176 WRITE_STRING(wcurr->desc);
177 WRITE_STRING("</desc>\n");
181 WRITE_STRING("/>\n");
183 WRITE_STRING(" </trkpt>\n");
189 /* Write the footer. */
190 WRITE_STRING(" </trkseg>\n" " </trk>\n" "</gpx>\n");
196 * Handle a start tag in the parsing of a GPX file.
198 #define MACRO_SET_UNKNOWN() { \
199 data->prev_state = data->state; \
200 data->state = UNKNOWN; \
201 data->unknown_depth = 1; \
205 gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
208 switch (data->state) {
212 if (!strcmp((gchar *) name, "gpx"))
213 data->state = INSIDE_GPX;
218 if (!strcmp((gchar *) name, "trk"))
219 data->state = INSIDE_PATH;
224 if (!strcmp((gchar *) name, "trkseg")) {
225 data->state = INSIDE_PATH_SEGMENT;
226 data->at_least_one_trkpt = FALSE;
230 case INSIDE_PATH_SEGMENT:
231 if (!strcmp((gchar *) name, "trkpt")) {
232 const xmlChar **curr_attr;
234 gdouble lat = 0.f, lon = 0.f;
235 gboolean has_lat, has_lon;
238 for (curr_attr = attrs; *curr_attr != NULL;) {
239 const gchar *attr_name = *curr_attr++;
240 const gchar *attr_val = *curr_attr++;
241 if (!strcmp(attr_name, "lat")) {
242 lat = g_ascii_strtod(attr_val, &error_check);
243 if (error_check != attr_val)
245 } else if (!strcmp(attr_name, "lon")) {
246 lon = g_ascii_strtod(attr_val, &error_check);
247 if (error_check != attr_val)
251 if (has_lat && has_lon) {
252 MACRO_PATH_INCREMENT_TAIL(data->path);
253 latlon2unit(lat, lon, data->path.tail->unitx, data->path.tail->unity);
254 data->path.tail->time = 0;
255 data->path.tail->altitude = NAN;
256 data->state = INSIDE_PATH_POINT;
262 case INSIDE_PATH_POINT:
263 if (!strcmp((gchar *) name, "time"))
264 data->state = INSIDE_PATH_POINT_TIME;
265 else if (!strcmp((gchar *) name, "ele"))
266 data->state = INSIDE_PATH_POINT_ELE;
267 else if (!strcmp((gchar *) name, "desc"))
268 data->state = INSIDE_PATH_POINT_DESC;
274 data->unknown_depth++;
283 * Handle an end tag in the parsing of a GPX file.
286 gpx_end_element(SaxData * data, const xmlChar * name)
289 switch (data->state) {
296 if (!strcmp((gchar *) name, "gpx"))
297 data->state = FINISH;
302 if (!strcmp((gchar *) name, "trk"))
303 data->state = INSIDE_GPX;
307 case INSIDE_PATH_SEGMENT:
308 if (!strcmp((gchar *) name, "trkseg")) {
309 if (data->at_least_one_trkpt) {
310 MACRO_PATH_INCREMENT_TAIL(data->path);
311 *data->path.tail = _point_null;
313 data->state = INSIDE_PATH;
317 case INSIDE_PATH_POINT:
318 if (!strcmp((gchar *) name, "trkpt")) {
319 data->state = INSIDE_PATH_SEGMENT;
320 data->at_least_one_trkpt = TRUE;
324 case INSIDE_PATH_POINT_ELE:
325 if (!strcmp((gchar *) name, "ele")) {
327 data->path.tail->altitude = g_ascii_strtod(data->chars->str, &error_check);
328 if (error_check == data->chars->str)
329 data->path.tail->altitude = NAN;
330 data->state = INSIDE_PATH_POINT;
331 g_string_free(data->chars, TRUE);
332 data->chars = g_string_new("");
336 case INSIDE_PATH_POINT_TIME:
337 if (!strcmp((gchar *) name, "time")) {
341 if (NULL == (ptr = strptime(data->chars->str,
342 XML_DATE_FORMAT, &time)))
343 /* Failed to parse dateTime format. */
346 /* Parse was successful. Now we have to parse timezone.
347 * From here on, if there is an error, I just assume local
348 * timezone. Yes, this is not proper XML, but I don't
352 /* First, set time in "local" time zone. */
353 data->path.tail->time = (mktime(&time));
355 /* Now, skip inconsequential characters */
356 while (*ptr && *ptr != 'Z' && *ptr != '-'
360 /* Check if we ran to the end of the string. */
362 /* Next character is either 'Z', '-', or '+' */
364 /* Zulu (UTC) time. Undo the local time zone's
366 data->path.tail->time += time.tm_gmtoff;
368 /* Not Zulu (UTC). Must parse hours and minutes. */
370 strtol(ptr, &error_check,
372 if (error_check != ptr
374 error_check) == ':') {
375 /* Parse of hours worked. Check minutes. */
382 /* Parse of minutes worked. Calculate. */
383 data->path.tail->time +=
385 (offhours * 60 * 60 + offmins * 60));
390 /* Successfully parsed dateTime. */
391 data->state = INSIDE_PATH_POINT;
394 g_string_free(data->chars, TRUE);
395 data->chars = g_string_new("");
399 case INSIDE_PATH_POINT_DESC:
400 /* only parse description for routes */
401 if (!strcmp((gchar *) name, "desc")) {
402 MACRO_PATH_INCREMENT_WTAIL(data->path);
403 data->path.wtail->point = data->path.tail;
404 data->path.wtail->desc
405 = g_string_free(data->chars, FALSE);
406 data->chars = g_string_new("");
407 data->state = INSIDE_PATH_POINT;
412 if (!--data->unknown_depth)
413 data->state = data->prev_state;
424 * Handle char data in the parsing of a GPX file.
427 gpx_chars(SaxData * data, const xmlChar * ch, int len)
431 switch (data->state) {
435 case INSIDE_PATH_POINT_ELE:
436 case INSIDE_PATH_POINT_TIME:
437 case INSIDE_PATH_POINT_DESC:
438 for (i = 0; i < len; i++)
439 data->chars = g_string_append_c(data->chars, ch[i]);
440 vprintf("%s\n", data->chars->str);
449 * Handle an entity in the parsing of a GPX file. We don't do anything
453 gpx_get_entity(SaxData * data, const xmlChar * name)
455 return xmlGetPredefinedEntity(name);
459 * Handle an error in the parsing of a GPX file.
462 gpx_error(SaxData * data, const gchar * msg, ...)
468 parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
471 xmlSAXHandler sax_handler;
473 MACRO_PATH_INIT(data.path);
475 data.chars = g_string_new("");
477 memset(&sax_handler, 0, sizeof(sax_handler));
478 sax_handler.characters = (charactersSAXFunc) gpx_chars;
479 sax_handler.startElement = (startElementSAXFunc) gpx_start_element;
480 sax_handler.endElement = (endElementSAXFunc) gpx_end_element;
481 sax_handler.entityDecl = (entityDeclSAXFunc) gpx_get_entity;
482 sax_handler.warning = (warningSAXFunc) gpx_error;
483 sax_handler.error = (errorSAXFunc) gpx_error;
484 sax_handler.fatalError = (fatalErrorSAXFunc) gpx_error;
486 xmlSAXUserParseMemory(&sax_handler, &data, buffer, size);
487 g_string_free(data.chars, TRUE);
489 if (data.state != FINISH) {
493 if (policy_old && to_replace->head != to_replace->tail) {
497 if (policy_old > 0) {
498 /* Append to current path. Make sure last path point is zero. */
499 if (to_replace->tail->unity != 0) {
500 MACRO_PATH_INCREMENT_TAIL((*to_replace));
501 *to_replace->tail = _point_null;
506 /* Prepend to current route. */
511 /* Find src_first non-zero point. */
512 for (src_first = src->head - 1; src_first++ != src->tail;)
513 if (src_first->unity)
516 /* Append route points from src to dest. */
517 if (src->tail >= src_first) {
519 guint num_dest_points = dest->tail - dest->head + 1;
520 guint num_src_points = src->tail - src_first + 1;
522 /* Adjust dest->tail to be able to fit src route data
523 * plus room for more route data. */
524 path_resize(dest, num_dest_points + num_src_points);
526 memcpy(dest->tail + 1, src_first,
527 num_src_points * sizeof(Point));
529 dest->tail += num_src_points;
531 /* Append waypoints from src to dest->. */
532 path_wresize(dest, (dest->wtail - dest->whead)
533 + (src->wtail - src->whead) + 2);
534 for (curr = src->whead - 1; curr++ != src->wtail;) {
535 (++(dest->wtail))->point =
536 dest->head + num_dest_points +
537 (curr->point - src_first);
538 dest->wtail->desc = curr->desc;
543 /* Kill old route - don't use MACRO_PATH_FREE(), because that
544 * would free the string desc's that we just moved to data.route. */
548 (*to_replace) = *dest;
550 MACRO_PATH_FREE((*to_replace));
551 /* Overwrite with data.route. */
552 (*to_replace) = data.path;
553 path_resize(to_replace,
554 to_replace->tail - to_replace->head + 1);
555 path_wresize(to_replace,
556 to_replace->wtail - to_replace->whead + 1);