]> err.no Git - mapper/blob - src/path.c
Add missing GPL header
[mapper] / src / path.c
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2008 Kaj-Michael Lang
5  *
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.
10  *
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.
15  *
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.
19  */
20 #include <config.h>
21
22 #include <gtk/gtk.h>
23
24 #include "mapper-types.h"
25 #include "path.h"
26 #include "utils.h"
27 #include "map.h"
28 #include "track.h"
29 #include "route.h"
30 #include "gps.h"
31 #include "settings.h"
32 #include "latlon.h"
33 #include "gpx.h"
34
35 struct sql_select_stmt {
36     sqlite3_stmt *select_path;
37     sqlite3_stmt *select_path_nodes;
38     sqlite3_stmt *insert_path;
39     sqlite3_stmt *insert_path_node;
40     sqlite3_stmt *delete_path_nodes;
41     sqlite3_stmt *delete_path;
42 };
43 static struct sql_select_stmt sql;
44
45 /* Path point add sensitivity */
46 static gint sensitivity=3;
47
48 Path *
49 path_new(void)
50 {
51 Path *p;
52
53 p=g_slice_new0(Path);
54 MACRO_PATH_INIT(*p);
55 return p;
56 }
57
58 void
59 path_free(Path *p)
60 {
61 g_return_if_fail(p);
62 MACRO_PATH_FREE(*p);
63 g_slice_free(Path, p);
64 }
65
66 void
67 path_clear(Path *p)
68 {
69 g_return_if_fail(p);
70 MACRO_PATH_FREE(*p);
71 p->length=p->avgspeed=0.0;
72 p->points=0;
73 }
74
75 gboolean 
76 path_resize(Path *path, guint size)
77 {
78 if (path->head + size != path->cap) {
79         Point *old_head = path->head;
80         WayPoint *curr;
81         path->head = g_renew(Point, old_head, size);
82         path->cap = path->head + size;
83         if (path->head != old_head) {
84                 path->tail = path->head + (path->tail - old_head);
85
86                 /* Adjust all of the waypoints. */
87                 for (curr = path->whead - 1; curr++ != path->wtail;)
88                         curr->point = path->head + (curr->point - old_head);
89         }
90         return TRUE;
91 }
92 return FALSE;
93 }
94
95 gboolean 
96 path_wresize(Path *path, guint wsize)
97 {
98 if (path->whead + wsize != path->wcap) {
99         WayPoint *old_whead = path->whead;
100         path->whead = g_renew(WayPoint, old_whead, wsize);
101         path->wtail = path->whead + (path->wtail - old_whead);
102         path->wcap = path->whead + wsize;
103         return TRUE;
104 }
105 return FALSE;
106 }
107
108 gboolean
109 path_add_point(Path *path, GpsData *gps)
110 {
111 if (!gps) {
112         MACRO_PATH_INCREMENT_TAIL(*path);
113         *path->tail=_point_null;
114         return FALSE;
115 }
116
117 if (abs((gint)gps->unitx-path->tail->unitx) > sensitivity || abs((gint)gps->unity-path->tail->unity) > sensitivity) {
118         if (path->tail->unity && path->tail->unitx) {
119                 gdouble lat, lon;
120
121                 unit2latlon(path->tail->unitx, path->tail->unity, lat, lon);
122                 path->length+=calculate_distance(lat, lon, gps->lat, gps->lon);
123         }
124         MACRO_PATH_INCREMENT_TAIL(*path);
125         path->tail->unitx=gps->unitx;
126         path->tail->unity=gps->unity;
127         path->tail->time=gps->time;
128         path->tail->altitude=gps->altitude;
129         path->maxspeed=gps->maxspeed;
130         path->tspeed+=gps->speed;
131         path->avgspeed=(path->points>0) ? path->tspeed/path->points : 0.0;
132         path->points++;
133         g_debug("TRACK: %f %f (%d)", path->length, path->avgspeed, path->points);
134 }
135
136 return TRUE;
137 }
138
139 gboolean 
140 path_insert_break(Path *path)
141 {
142 if (!path)
143         return FALSE;
144
145 if (!path->tail)
146         return FALSE;
147
148 if (path->tail->unity) {
149         guint x1, y1;
150
151         /* To mark a "waypoint" in a track, we'll add a (0, 0) point and then
152          * another instance of the most recent track point. */
153         MACRO_PATH_INCREMENT_TAIL(*path);
154         *path->tail=_point_null;
155         MACRO_PATH_INCREMENT_TAIL(*path);
156         *path->tail=path->tail[-2];
157
158 #if 0
159         /* Instead of calling map_render_paths(), we'll just draw the waypoint ourselves. */
160         x1 = unit2bufx(path->tail->unitx);
161         y1 = unit2bufy(path->tail->unity);
162         map_render_waypoint(x1, y1, _gc[COLORABLE_TRACK_BREAK]);
163 #endif
164         return TRUE;
165 }
166 return FALSE;
167 }
168
169 Point *
170 path_find_last_point(Path *path)
171 {
172 Point *p=NULL;
173
174 if (path->head == path->tail)
175         return p;
176
177 for (p=path->tail; !p->unity; p--) {
178 }
179 return p;
180 }
181
182 gdouble
183 path_get_distance_to(Path *path, Point *point, gdouble lat, gdouble lon)
184 {
185 gdouble lat1, lon1, lat2, lon2;
186 gdouble sum=0.0;
187
188 /* If point is NULL, use the next waypoint. */
189 if (point == NULL && path->next_way)
190         point = path->next_way->point;
191
192 /* If point is still NULL, return an error. */
193 if (point==NULL)
194         return FALSE;
195
196 lat1=lat;
197 lon1=lon;
198
199 if (point > path->near_point) {
200         Point *curr;
201         /* Skip _near_point in case we have already passed it. */
202         for (curr = path->near_point + 1; curr <= point; ++curr) {
203                 if (curr->unity) {
204                         unit2latlon(curr->unitx, curr->unity, lat2, lon2);
205                         sum += calculate_distance(lat1, lon1, lat2, lon2);
206                         lat1 = lat2;
207                         lon1 = lon2;
208                 }
209         }
210 } else if (point < path->near_point) {
211         Point *curr;
212         /* Skip near_point in case we have already passed it. */
213         for (curr = path->near_point - 1; curr >= point; --curr) {
214                 if (curr->unity) {
215                         unit2latlon(curr->unitx, curr->unity, lat2, lon2);
216                         sum += calculate_distance(lat1, lon1, lat2, lon2);
217                         lat1 = lat2;
218                         lon1 = lon2;
219                 }
220         }
221 } else {
222         /* Waypoint _is_ the nearest point. */
223         unit2latlon(path->near_point->unitx, path->near_point->unity, lat2, lon2);
224         sum += calculate_distance(lat1, lon1, lat2, lon2);
225 }
226 return sum;
227 }
228
229 gboolean
230 path_load(Path *path, const gchar *config_dir, const gchar *file)
231 {
232 gchar *pfile;
233 gchar *bytes;
234 gint size;
235
236 pfile = gnome_vfs_uri_make_full_from_relative(config_dir, file);
237 if (gnome_vfs_read_entire_file(pfile, &size, &bytes)==GNOME_VFS_OK)
238         gpx_parse(path, bytes, size, GPX_PATH_NEW);
239 g_free(pfile);
240 return TRUE;
241 }
242
243 gboolean 
244 path_save(Path *path, const gchar *config_dir, const gchar *file)
245 {
246 GnomeVFSHandle *handle;
247 gchar *tfile;
248 tfile=gnome_vfs_uri_make_full_from_relative(config_dir, file);
249 if (gnome_vfs_create(&handle, tfile, GNOME_VFS_OPEN_WRITE, FALSE, 0600)==GNOME_VFS_OK) {
250         gpx_write(path, handle);
251         gnome_vfs_close(handle);
252 }
253 g_free(tfile);
254 return TRUE;
255 }
256
257 /**
258  * Add a text description at current point
259  * 
260  */
261 void 
262 path_insert_mark_text(Path *path, gchar *text)
263 {
264 g_assert(path);
265 MACRO_PATH_INCREMENT_WTAIL(*path);
266 path->wtail->point=path->tail;
267 if (text) {
268         path->wtail->desc=text;
269 } else {
270         path->wpcnt++;
271         path->wtail->desc=g_strdup_printf("WP: %u", path->wpcnt);
272 }
273 }
274
275 GtkListStore *
276 path_generate_store(Path *path)
277 {
278 WayPoint *wcurr;
279 GtkTreeIter iter;
280 GtkListStore *store;
281 gchar buffer1[80];
282 gchar buffer2[32];
283 gchar tmp1[16], tmp2[16];
284 gdouble lat1, lon1, lat2, lon2;
285 gdouble sum=0.0;
286
287 if (path->whead==path->wtail)
288         return NULL;
289
290 wcurr=path->whead;
291
292 if (!wcurr)
293         return NULL;
294
295 if (!wcurr->point)
296         return NULL;
297
298 unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat1, lon1);
299
300 store=gtk_list_store_new(ROUTE_NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
301
302 while (wcurr!=path->wtail) {
303         if (!wcurr) {
304                 break;
305         }
306
307         if (!wcurr->point) {
308                 g_debug("No point for waypoint (%s) skipping", wcurr->desc);
309                 wcurr++;
310                 continue;
311         }
312
313         unit2latlon(wcurr->point->unitx, wcurr->point->unity, lat2, lon2);
314         lat_format(_degformat, lat2, tmp1);
315         lon_format(_degformat, lon2, tmp2);
316
317         g_snprintf(buffer1, sizeof(buffer1), "%s,%s", tmp1, tmp2);
318         sum += calculate_distance(lat1, lon1, lat2, lon2);
319         g_snprintf(buffer2, sizeof(buffer2), "%.02f %s", sum * UNITS_CONVERT[_units], UNITS_TEXT[_units]);
320
321         gtk_list_store_append(store, &iter);
322         gtk_list_store_set(store, &iter,
323         ROUTE_LATLON, buffer1,
324                 ROUTE_DISTANCE, buffer2,
325                 ROUTE_WAYPOINT, wcurr->desc,
326                 ROUTE_LAT, lat2,
327                 ROUTE_LON, lon2,
328                 -1);
329
330         lat1=lat2;
331         lon1=lon2;
332
333         wcurr++;
334 }
335
336 return store;
337 }
338
339 void
340 position_set(Position *pos, gboolean valid, gdouble lat, gdouble lon)
341 {
342 pos->valid=valid;
343 pos->lat=valid ? lat : NAN;
344 pos->lon=valid ? lon : NAN;
345 }
346
347 void
348 position_update(Position *pos, GpsData *data)
349 {
350 if (!pos->valid)
351         return;
352 }