2 * This file is part of mapper
4 * Copyright (C) 2008 Kaj-Michael Lang
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 struct sql_select_stmt {
35 sqlite3_stmt *select_paths;
36 sqlite3_stmt *select_path_nodes;
38 sqlite3_stmt *insert_path;
39 sqlite3_stmt *insert_path_node;
41 sqlite3_stmt *delete_path;
42 sqlite3_stmt *delete_path_nodes;
44 static struct sql_select_stmt sql;
46 #define PATH_TABLE_PATHS "create table IF NOT EXISTS paths ( \
47 nid int primary key, \
52 #define PATH_TABLE_NODES "create table IF NOT EXISTS path_nodes ( \
62 name text default null, \
66 NEW_POINT, /* A new point was appended to track track */
67 NEW_BREAK, /* A break was appended to the track */
68 NEW_WAYPOINT, /* A new waypoint/marker has been added */
69 NEAR_WAYPOINT, /* We are near the next route waypoint */
70 REACHED_WAYPOINT, /* We have reached the next route waypoint */
71 REACHED_DESTINATION, /* We have reached the last route waypoint */
72 CLEARED, /* Path was cleared */
75 static guint32 signals[LAST_SIGNAL] = {0};
77 G_DEFINE_TYPE(Path, path, G_TYPE_OBJECT);
79 /* #define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PATH_TYPE, PathPrivate)) */
82 path_dispose(GObject *object)
84 g_debug("path_dispose");
86 G_OBJECT_CLASS(path_parent_class)->dispose(object);
90 path_finalize(GObject *object)
92 Path *path=PATH(object);
94 g_debug("path_finalize");
95 MACRO_PATH_FREE(*path);
102 g_free(path->author);
104 g_free(path->keywords);
106 g_free(path->copyright);
112 path_class_init(PathClass *klass)
114 GObjectClass *object_class=G_OBJECT_CLASS(klass);
116 g_debug("path_class_init");
118 object_class->dispose=path_dispose;
119 object_class->finalize=path_finalize;
120 /* g_type_class_add_private (klass, sizeof(PathPrivate)); */
122 signals[NEW_POINT]=g_signal_new("new-point", G_OBJECT_CLASS_TYPE(object_class),
123 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET(PathClass, new_point),
124 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
125 signals[NEW_BREAK]=g_signal_new("new-break", G_OBJECT_CLASS_TYPE(object_class),
126 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET(PathClass, new_break),
127 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
128 signals[NEW_WAYPOINT]=g_signal_new("new-waypoint", G_OBJECT_CLASS_TYPE(object_class),
129 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET(PathClass, new_waypoint),
130 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
134 path_init(Path *path)
136 g_debug("path_init");
137 MACRO_PATH_INIT(*path);
141 path_new(PathType type, guint id)
145 p=g_object_new(PATH_TYPE, NULL);
153 path_free(Path *path)
155 g_return_if_fail(path);
156 g_object_unref(path);
160 path_clear(Path *path)
162 g_return_if_fail(path);
163 MACRO_PATH_FREE(*path);
164 path->length=path->avgspeed=0.0;
169 path_resize(Path *path, guint size)
171 g_return_val_if_fail(path, FALSE);
173 if (path->head + size != path->cap) {
175 Point *old_head = path->head;
177 path->head = g_renew(Point, old_head, size);
178 g_assert(path->head);
179 path->cap = path->head + size;
180 if (path->head != old_head) {
181 path->tail = path->head + (path->tail - old_head);
183 /* Adjust all of the waypoints. */
184 for (curr = path->whead - 1; curr++ != path->wtail;)
185 curr->point = path->head + (curr->point - old_head);
193 path_wresize(Path *path, guint wsize)
195 g_return_val_if_fail(path, FALSE);
197 if (path->whead + wsize != path->wcap) {
198 WayPoint *old_whead = path->whead;
200 path->whead = g_renew(WayPoint, old_whead, wsize);
201 path->wtail = path->whead + (path->wtail - old_whead);
202 path->wcap = path->whead + wsize;
210 * Append a waypoint to path at given lat, lon with description desc
213 path_add_waypoint(Path *path, gdouble lat, gdouble lon, gchar *desc)
217 latlon2unit(lat, lon, unitx, unity);
218 MACRO_PATH_INCREMENT_TAIL(*path);
219 path->tail->unitx=unitx;
220 path->tail->unity=unity;
222 path->tail->altitude=NAN;
224 MACRO_PATH_INCREMENT_WTAIL(*path);
225 path->wtail->point=path->tail;
226 path->wtail->desc=desc;
228 path_find_nearest_point(path);
230 g_signal_emit(G_OBJECT(path), signals[NEW_WAYPOINT], 0, NULL);
236 * Append a path point to path
238 * Returns: TRUE if the new point was added. FALSE is returned if new point distance was under sensitivity setting.
241 path_add_point(Path *path, GpsData *gps)
245 g_return_val_if_fail(path, FALSE);
248 path_add_break(path);
252 latlon2unit(gps->lat, gps->lon, unitx, unity);
254 if (abs((gint)unitx-path->tail->unitx) > path->sensitivity || abs((gint)unity-path->tail->unity) > path->sensitivity) {
255 if (path->tail->unity && path->tail->unitx) {
258 unit2latlon(path->tail->unitx, path->tail->unity, lat, lon);
259 path->length+=calculate_distance(lat, lon, gps->lat, gps->lon);
261 MACRO_PATH_INCREMENT_TAIL(*path);
262 path->tail->unitx=unitx;
263 path->tail->unity=unity;
264 path->tail->time=gps->time;
265 path->tail->altitude=gps->altitude;
266 path->maxspeed=gps->maxspeed;
267 path->tspeed+=gps->speed;
268 path->avgspeed=(path->points>0) ? path->tspeed/path->points : 0.0;
270 g_debug("TRACK: %f %f (%d)", path->length, path->avgspeed, path->points);
272 g_signal_emit(G_OBJECT(path), signals[NEW_POINT], 0, NULL);
280 path_add_break(Path *path)
282 g_return_val_if_fail(path, FALSE);
283 g_return_val_if_fail(path->tail, FALSE);
285 if (path->tail->unity && path->tail->unitx) {
288 /* To mark a "break" in a track, we'll add a (0, 0) point and then
289 another instance of the most recent track point. */
291 MACRO_PATH_INCREMENT_TAIL(*path);
292 *path->tail=_point_null;
293 MACRO_PATH_INCREMENT_TAIL(*path);
294 *path->tail=path->tail[-2];
296 g_signal_emit(G_OBJECT(path), signals[NEW_BREAK], 0, NULL);
304 path_has_points(Path *path)
306 g_return_val_if_fail(path, FALSE);
307 return path->head==path->tail ? FALSE : TRUE;
311 path_has_waypoints(Path *path)
313 g_return_val_if_fail(path, FALSE);
314 return path->whead==path->wtail ? FALSE : TRUE;
318 path_find_last_point(Path *path)
322 g_return_val_if_fail(path, NULL);
324 if (!path_has_points(path))
327 for (p=path->tail; !p->unity; p--) {
333 * Updates near_point, next_way, and next_wpt. If quick is FALSE (as
334 * it is when this function is called from route_find_nearest_point), then
335 * the entire list (starting from near_point) is searched. Otherwise, we
336 * stop searching when we find a point that is farther away.
339 path_update_nears(Path *path, Point *point, gboolean quick)
341 gboolean ret = FALSE;
343 WayPoint *wcurr, *wnext;
344 guint64 near_dist_squared;
346 g_return_val_if_fail(path, FALSE);
348 /* If we have waypoints (_next_way != NULL), then determine the "next
349 * waypoint", which is defined as the waypoint after the nearest point,
350 * UNLESS we've passed that waypoint, in which case the waypoint after
351 * that waypoint becomes the "next" waypoint. */
352 if (path->next_way) {
353 /* First, set near_dist_squared with the new distance from
355 near = path->near_point;
356 near_dist_squared = DISTANCE_SQUARED(*point, *near);
358 /* Now, search path for a closer point. If quick is TRUE, then we'll
359 * only search forward, only as long as we keep finding closer points.
361 for (curr = path->near_point; curr++ != path->tail;) {
363 guint dist_squared = DISTANCE_SQUARED(*point, *curr);
364 if (dist_squared <= near_dist_squared) {
366 near_dist_squared = dist_squared;
372 /* Update _near_point. */
373 path->near_point = near;
374 path->near_point_dist_squared = near_dist_squared;
376 for (wnext = wcurr = path->next_way; wcurr != path->wtail; wcurr++) {
377 if (wcurr->point < near || (wcurr->point == near && quick
378 && (path->next_wpt && (DISTANCE_SQUARED(*point, *near) > path->next_way_dist_squared
379 && DISTANCE_SQUARED(*point, *path->next_wpt) < path->next_wpt_dist_squared))))
380 /* Okay, this else if expression warrants explanation. If the
381 * nearest track point happens to be a waypoint, then we want to
382 * check if we have "passed" that waypoint. To check this, we
383 * test the distance from _gps to the waypoint and from _gps to
384 * _next_wpt, and if the former is increasing and the latter is
385 * decreasing, then we have passed the waypoint, and thus we
386 * should skip it. Note that if there is no _next_wpt, then
387 * there is no next waypoint, so we do not skip it in that case. */
393 if (wnext == path->wtail && (wnext->point < near || (wnext->point == near && quick
394 && (path->next_wpt && (DISTANCE_SQUARED (*point, *near) > path->next_way_dist_squared
395 && DISTANCE_SQUARED(*point, *path->next_wpt) < path->next_wpt_dist_squared)))))
397 path->next_way = NULL;
398 path->next_wpt = NULL;
399 path->next_way_dist_squared = -1;
400 path->next_wpt_dist_squared = -1;
403 /* Only update next_way (and consequently _next_wpt) if _next_way is
404 * different, and record that fact for return. */
406 if (!quick || path->next_way != wnext) {
407 path->next_way = wnext;
408 path->next_wpt = wnext->point;
409 if (path->next_wpt == path->tail)
410 path->next_wpt = NULL;
412 while (!(++path->next_wpt)->unity) {
413 if (path->next_wpt == path->tail) {
414 path->next_wpt = NULL;
421 path->next_way_dist_squared = DISTANCE_SQUARED(*point, *wnext->point);
423 path->next_wpt_dist_squared = DISTANCE_SQUARED(*point, *path->next_wpt);
430 * Reset the near_point data by searching the entire path for the nearest point and waypoint.
433 path_find_nearest_point(Path *path)
435 g_return_if_fail(path);
437 /* Initialize near_point to first non-zero point. */
438 path->near_point=path->head;
439 while (!path->near_point->unity && path->near_point != path->tail)
442 /* Initialize next_way */
443 if (path->wtail==path->whead)
446 path->next_way=path->whead;
447 path->next_way_dist_squared=-1;
449 /* Initialize next_wpt */
451 path->next_wpt_dist_squared=-1;
456 path_find_nearest_waypoint(Path *path, guint unitx, guint unity)
460 guint64 nearest_squared;
461 Point pos = { unitx, unity, 0, NAN };
463 g_return_val_if_fail(path, NULL);
465 wcurr=wnear=path->whead;
466 if (wcurr && wcurr->point && wcurr!=path->wtail) {
467 nearest_squared=DISTANCE_SQUARED(pos, *(wcurr->point));
469 while (wcurr++!=path->wtail) {
470 guint64 test_squared=DISTANCE_SQUARED(pos, *(wcurr->point));
471 if (test_squared < nearest_squared) {
473 nearest_squared=test_squared;
478 if (wnear && wnear->point) {
479 /* Only use the waypoint if it is within a 6*_draw_width square drawn
480 * around the position. This is consistent with select_poi(). */
481 if (abs(unitx - wnear->point->unitx) < pixel2unit(3 * path->sensitivity) && abs(unity - wnear->point->unity) < pixel2unit(3 * path->sensitivity))
488 /******************************************************************************/
491 path_get_distance_to(Path *path, Point *point, gdouble lat, gdouble lon)
493 gdouble lat1, lon1, lat2, lon2;
496 g_return_val_if_fail(path, 0.0);
498 /* If point is NULL, use the next waypoint. */
499 if (point == NULL && path->next_way)
500 point = path->next_way->point;
502 /* If point is still NULL, return an error. */
509 if (point > path->near_point) {
511 /* Skip _near_point in case we have already passed it. */
512 for (curr = path->near_point + 1; curr <= point; ++curr) {
514 unit2latlon(curr->unitx, curr->unity, lat2, lon2);
515 sum += calculate_distance(lat1, lon1, lat2, lon2);
520 } else if (point < path->near_point) {
522 /* Skip near_point in case we have already passed it. */
523 for (curr = path->near_point - 1; curr >= point; --curr) {
525 unit2latlon(curr->unitx, curr->unity, lat2, lon2);
526 sum += calculate_distance(lat1, lon1, lat2, lon2);
532 /* Waypoint _is_ the nearest point. */
533 unit2latlon(path->near_point->unitx, path->near_point->unity, lat2, lon2);
534 sum += calculate_distance(lat1, lon1, lat2, lon2);
539 /******************************************************************************/
542 path_load(Path *path, const gchar *config_dir, const gchar *file)
548 g_return_val_if_fail(path, FALSE);
549 g_return_val_if_fail(config_dir, FALSE);
550 g_return_val_if_fail(file, FALSE);
552 pfile = gnome_vfs_uri_make_full_from_relative(config_dir, file);
553 if (gnome_vfs_read_entire_file(pfile, &size, &bytes)==GNOME_VFS_OK)
554 gpx_parse(path, bytes, size, GPX_PATH_NEW);
560 path_save(Path *path, const gchar *config_dir, const gchar *file)
562 GnomeVFSHandle *handle;
565 g_return_val_if_fail(path, FALSE);
566 g_return_val_if_fail(config_dir, FALSE);
567 g_return_val_if_fail(file, FALSE);
569 tfile=gnome_vfs_uri_make_full_from_relative(config_dir, file);
570 if (gnome_vfs_create(&handle, tfile, GNOME_VFS_OPEN_WRITE, FALSE, 0600)==GNOME_VFS_OK) {
571 gpx_write(path, handle);
572 gnome_vfs_close(handle);
578 /******************************************************************************/
581 path_set_destination_from_last(Path *path, Position *pos)
586 g_return_val_if_fail(path, FALSE);
587 g_return_val_if_fail(pos, FALSE);
589 if (path->head==path->tail) {
590 position_set(pos, FALSE, NAN, NAN, NAN);
594 p=path_find_last_point(path);
596 unit2latlon(p->unitx, p->unity, &lat, &lon);
597 position_set(pos, TRUE, lat, lon, 0);
599 position_set(pos, FALSE, NAN, NAN, NAN);
606 path_get_waypoint_latlon(WayPoint *way, gdouble *lat, gdouble *lon)
610 g_return_val_if_fail(way, FALSE);
611 g_return_val_if_fail(way->point, FALSE);
612 g_return_val_if_fail(lat, FALSE);
613 g_return_val_if_fail(lon, FALSE);
615 unit2latlon(way->point->unitx, way->point->unity, tlat, tlon);
624 * Add a text description at current point
628 path_insert_mark_text(Path *path, gchar *text)
630 g_return_if_fail(path);
631 MACRO_PATH_INCREMENT_WTAIL(*path);
632 path->wtail->point=path->tail;
634 path->wtail->desc=text;
637 path->wtail->desc=g_strdup_printf("WPT:#%u", path->wpcnt);
639 g_signal_emit(G_OBJECT(path), signals[NEW_WAYPOINT], 0, NULL);
643 path_insert_mark_autonumber(Path *path)
645 path_insert_mark_text(path, NULL);
648 /******************************************************************************/
651 path_store_append_waypoint(GtkListStore *, WayPoint *w)
653 gchar tmp1[16], tmp2[16];
656 unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat2, lon2);
657 lat_format(_degformat, lat2, tmp1);
658 lon_format(_degformat, lon2, tmp2);
660 g_snprintf(buffer1, sizeof(buffer1), "%s,%s", tmp1, tmp2);
661 sum += calculate_distance(lat1, lon1, lat2, lon2);
662 g_snprintf(buffer2, sizeof(buffer2), "%.02f %s", sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
664 gtk_list_store_append(store, &iter);
665 gtk_list_store_set(store, &iter,
666 PATH_LATLON, buffer1,
667 PATH_DISTANCE, buffer2,
668 PATH_WAYPOINT, wcurr->desc,
679 * Generate a GtkListStore with information about path waypoints (location, distance)
682 path_get_waypoints_store(Path *path)
689 gchar tmp1[16], tmp2[16];
690 gdouble lat1, lon1, lat2, lon2;
693 g_return_val_if_fail(path!=NULL, NULL);
697 g_return_val_if_fail(wcurr!=NULL, NULL);
698 g_return_val_if_fail(wcurr->point!=NULL, NULL);
699 if (!path_has_waypoints(path))
702 store=gtk_list_store_new(PATH_NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
704 unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat1, lon1);
706 while (wcurr!=path->wtail) {
711 g_debug("PSTORE: No point for waypoint (%s) skipping", wcurr->desc);
716 unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat2, lon2);
717 lat_format(_degformat, lat2, tmp1);
718 lon_format(_degformat, lon2, tmp2);
720 g_snprintf(buffer1, sizeof(buffer1), "%s,%s", tmp1, tmp2);
721 sum += calculate_distance(lat1, lon1, lat2, lon2);
722 g_snprintf(buffer2, sizeof(buffer2), "%.02f %s", sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
724 gtk_list_store_append(store, &iter);
725 gtk_list_store_set(store, &iter,
726 PATH_LATLON, buffer1,
727 PATH_DISTANCE, buffer2,
728 PATH_WAYPOINT, wcurr->desc,