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"
54 #include "mapper-types.h"
58 #define XML_DATE_FORMAT "%FT%T"
68 localtime_r(&time1, &time2);
69 snprintf(XML_TZONE, sizeof(XML_TZONE), "%+03ld:%02ld",
70 (time2.tm_gmtoff / 60 / 60), (time2.tm_gmtoff / 60) % 60);
74 write_gpx(Path * path, GnomeVFSHandle * handle)
77 WayPoint *wcurr = NULL;
78 gboolean trkseg_break = FALSE;
80 /* Find first non-zero point. */
81 for (curr = path->head - 1, wcurr = path->whead; curr++ != path->tail;) {
84 else if (wcurr && curr == wcurr->point)
88 /* Write the header. */
89 WRITE_STRING("<?xml version=\"1.0\"?>\n"
90 "<gpx version=\"1.0\" creator=\"mapper\" "
91 "xmlns=\"http://www.topografix.com/GPX/1/0\">\n"
92 " <trk>\n" " <trkseg>\n");
94 /* Curr points to first non-zero point. */
95 for (curr--; curr++ != path->tail;) {
99 gboolean first_sub = TRUE;
101 /* First trkpt of the segment - write trkseg header. */
102 WRITE_STRING(" </trkseg>\n"
104 trkseg_break = FALSE;
106 unit2latlon(curr->unitx, curr->unity, lat, lon);
107 WRITE_STRING(" <trkpt lat=\"");
108 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lat);
109 WRITE_STRING(buffer);
110 WRITE_STRING("\" lon=\"");
111 g_ascii_formatd(buffer, sizeof(buffer), "%.06f", lon);
112 WRITE_STRING(buffer);
115 /* write the elevation */
116 if (!isnan(curr->altitude)) {
121 WRITE_STRING(" <ele>");
123 g_ascii_formatd(buffer, 80, "%.2f",
125 WRITE_STRING(buffer);
127 WRITE_STRING("</ele>\n");
136 WRITE_STRING(" <time>");
137 strftime(buffer, 80, XML_DATE_FORMAT,
138 localtime(&curr->time));
139 WRITE_STRING(buffer);
140 WRITE_STRING(XML_TZONE);
141 WRITE_STRING("</time>\n");
144 if (wcurr && curr == wcurr->point) {
149 WRITE_STRING(" <desc>");
150 WRITE_STRING(wcurr->desc);
151 WRITE_STRING("</desc>\n");
155 WRITE_STRING("/>\n");
157 WRITE_STRING(" </trkpt>\n");
163 /* Write the footer. */
164 WRITE_STRING(" </trkseg>\n" " </trk>\n" "</gpx>\n");
170 * Handle a start tag in the parsing of a GPX file.
172 #define MACRO_SET_UNKNOWN() { \
173 data->prev_state = data->state; \
174 data->state = UNKNOWN; \
175 data->unknown_depth = 1; \
179 gpx_start_element(SaxData * data, const xmlChar * name, const xmlChar ** attrs)
182 switch (data->state) {
186 if (!strcmp((gchar *) name, "gpx"))
187 data->state = INSIDE_GPX;
192 if (!strcmp((gchar *) name, "trk"))
193 data->state = INSIDE_PATH;
198 if (!strcmp((gchar *) name, "trkseg")) {
199 data->state = INSIDE_PATH_SEGMENT;
200 data->at_least_one_trkpt = FALSE;
204 case INSIDE_PATH_SEGMENT:
205 if (!strcmp((gchar *) name, "trkpt")) {
206 const xmlChar **curr_attr;
208 gdouble lat = 0.f, lon = 0.f;
209 gboolean has_lat, has_lon;
212 for (curr_attr = attrs; *curr_attr != NULL;) {
213 const gchar *attr_name = *curr_attr++;
214 const gchar *attr_val = *curr_attr++;
215 if (!strcmp(attr_name, "lat")) {
216 lat = g_ascii_strtod(attr_val, &error_check);
217 if (error_check != attr_val)
219 } else if (!strcmp(attr_name, "lon")) {
220 lon = g_ascii_strtod(attr_val, &error_check);
221 if (error_check != attr_val)
225 if (has_lat && has_lon) {
226 MACRO_PATH_INCREMENT_TAIL(data->path);
227 latlon2unit(lat, lon, data->path.tail->unitx, data->path.tail->unity);
228 data->path.tail->time = 0;
229 data->path.tail->altitude = NAN;
230 data->state = INSIDE_PATH_POINT;
236 case INSIDE_PATH_POINT:
237 if (!strcmp((gchar *) name, "time"))
238 data->state = INSIDE_PATH_POINT_TIME;
239 else if (!strcmp((gchar *) name, "ele"))
240 data->state = INSIDE_PATH_POINT_ELE;
241 else if (!strcmp((gchar *) name, "desc"))
242 data->state = INSIDE_PATH_POINT_DESC;
248 data->unknown_depth++;
257 * Handle an end tag in the parsing of a GPX file.
260 gpx_end_element(SaxData * data, const xmlChar * name)
263 switch (data->state) {
270 if (!strcmp((gchar *) name, "gpx"))
271 data->state = FINISH;
276 if (!strcmp((gchar *) name, "trk"))
277 data->state = INSIDE_GPX;
281 case INSIDE_PATH_SEGMENT:
282 if (!strcmp((gchar *) name, "trkseg")) {
283 if (data->at_least_one_trkpt) {
284 MACRO_PATH_INCREMENT_TAIL(data->path);
285 *data->path.tail = _point_null;
287 data->state = INSIDE_PATH;
291 case INSIDE_PATH_POINT:
292 if (!strcmp((gchar *) name, "trkpt")) {
293 data->state = INSIDE_PATH_SEGMENT;
294 data->at_least_one_trkpt = TRUE;
298 case INSIDE_PATH_POINT_ELE:
299 if (!strcmp((gchar *) name, "ele")) {
301 data->path.tail->altitude = g_ascii_strtod(data->chars->str, &error_check);
302 if (error_check == data->chars->str)
303 data->path.tail->altitude = NAN;
304 data->state = INSIDE_PATH_POINT;
305 g_string_free(data->chars, TRUE);
306 data->chars = g_string_new("");
310 case INSIDE_PATH_POINT_TIME:
311 if (!strcmp((gchar *) name, "time")) {
315 if (NULL == (ptr = strptime(data->chars->str,
316 XML_DATE_FORMAT, &time)))
317 /* Failed to parse dateTime format. */
320 /* Parse was successful. Now we have to parse timezone.
321 * From here on, if there is an error, I just assume local
322 * timezone. Yes, this is not proper XML, but I don't
326 /* First, set time in "local" time zone. */
327 data->path.tail->time = (mktime(&time));
329 /* Now, skip inconsequential characters */
330 while (*ptr && *ptr != 'Z' && *ptr != '-'
334 /* Check if we ran to the end of the string. */
336 /* Next character is either 'Z', '-', or '+' */
338 /* Zulu (UTC) time. Undo the local time zone's
340 data->path.tail->time += time.tm_gmtoff;
342 /* Not Zulu (UTC). Must parse hours and minutes. */
344 strtol(ptr, &error_check,
346 if (error_check != ptr
348 error_check) == ':') {
349 /* Parse of hours worked. Check minutes. */
356 /* Parse of minutes worked. Calculate. */
357 data->path.tail->time +=
359 (offhours * 60 * 60 + offmins * 60));
364 /* Successfully parsed dateTime. */
365 data->state = INSIDE_PATH_POINT;
368 g_string_free(data->chars, TRUE);
369 data->chars = g_string_new("");
373 case INSIDE_PATH_POINT_DESC:
374 /* only parse description for routes */
375 if (!strcmp((gchar *) name, "desc")) {
376 MACRO_PATH_INCREMENT_WTAIL(data->path);
377 data->path.wtail->point = data->path.tail;
378 data->path.wtail->desc
379 = g_string_free(data->chars, FALSE);
380 data->chars = g_string_new("");
381 data->state = INSIDE_PATH_POINT;
386 if (!--data->unknown_depth)
387 data->state = data->prev_state;
398 * Handle char data in the parsing of a GPX file.
401 gpx_chars(SaxData * data, const xmlChar * ch, int len)
405 switch (data->state) {
409 case INSIDE_PATH_POINT_ELE:
410 case INSIDE_PATH_POINT_TIME:
411 case INSIDE_PATH_POINT_DESC:
412 for (i = 0; i < len; i++)
413 data->chars = g_string_append_c(data->chars, ch[i]);
414 vprintf("%s\n", data->chars->str);
423 * Handle an entity in the parsing of a GPX file. We don't do anything
427 gpx_get_entity(SaxData * data, const xmlChar * name)
429 return xmlGetPredefinedEntity(name);
433 * Handle an error in the parsing of a GPX file.
436 gpx_error(SaxData * data, const gchar * msg, ...)
442 parse_gpx(Path * to_replace, gchar * buffer, gint size, gint policy_old)
445 xmlSAXHandler sax_handler;
447 MACRO_PATH_INIT(data.path);
449 data.chars = g_string_new("");
451 memset(&sax_handler, 0, sizeof(sax_handler));
452 sax_handler.characters = (charactersSAXFunc) gpx_chars;
453 sax_handler.startElement = (startElementSAXFunc) gpx_start_element;
454 sax_handler.endElement = (endElementSAXFunc) gpx_end_element;
455 sax_handler.entityDecl = (entityDeclSAXFunc) gpx_get_entity;
456 sax_handler.warning = (warningSAXFunc) gpx_error;
457 sax_handler.error = (errorSAXFunc) gpx_error;
458 sax_handler.fatalError = (fatalErrorSAXFunc) gpx_error;
460 xmlSAXUserParseMemory(&sax_handler, &data, buffer, size);
461 g_string_free(data.chars, TRUE);
463 if (data.state != FINISH) {
467 if (policy_old && to_replace->head != to_replace->tail) {
471 if (policy_old > 0) {
472 /* Append to current path. Make sure last path point is zero. */
473 if (to_replace->tail->unity != 0) {
474 MACRO_PATH_INCREMENT_TAIL((*to_replace));
475 *to_replace->tail = _point_null;
480 /* Prepend to current route. */
485 /* Find src_first non-zero point. */
486 for (src_first = src->head - 1; src_first++ != src->tail;)
487 if (src_first->unity)
490 /* Append route points from src to dest. */
491 if (src->tail >= src_first) {
493 guint num_dest_points = dest->tail - dest->head + 1;
494 guint num_src_points = src->tail - src_first + 1;
496 /* Adjust dest->tail to be able to fit src route data
497 * plus room for more route data. */
498 path_resize(dest, num_dest_points + num_src_points);
500 memcpy(dest->tail + 1, src_first,
501 num_src_points * sizeof(Point));
503 dest->tail += num_src_points;
505 /* Append waypoints from src to dest->. */
506 path_wresize(dest, (dest->wtail - dest->whead)
507 + (src->wtail - src->whead) + 2);
508 for (curr = src->whead - 1; curr++ != src->wtail;) {
509 (++(dest->wtail))->point =
510 dest->head + num_dest_points +
511 (curr->point - src_first);
512 dest->wtail->desc = curr->desc;
517 /* Kill old route - don't use MACRO_PATH_FREE(), because that
518 * would free the string desc's that we just moved to data.route. */
522 (*to_replace) = *dest;
524 MACRO_PATH_FREE((*to_replace));
525 /* Overwrite with data.route. */
526 (*to_replace) = data.path;
527 path_resize(to_replace,
528 to_replace->tail - to_replace->head + 1);
529 path_wresize(to_replace,
530 to_replace->wtail - to_replace->whead + 1);