2 * This file is part of mapper
4 * Copyright (C) 2007 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.
25 #include <sys/types.h>
28 #include <glib/gstdio.h>
29 #include <glib/gi18n.h>
38 #include "osm-sql-tables.h"
40 /* #define DEBUG_OSM */
41 #define DEBUG_OSM_TIME
42 #define OSM_PLACE_CACHE_MAX_ITEMS (64)
44 #define OSM_DB_PROGRESS_NUM (30000)
46 /* Node search ranges */
47 #define OSM_RANGE_START (16384*2)
48 #define OSM_RANGE_STEP (8192)
49 #define OSM_RANGE_STOP (65535)
51 /* Way range for searching again */
52 #define OSM_RANGE_WAY (12000)
55 #define QT_START { g_timer_start(dbt); }
56 #define QT_END { g_timer_stop(dbt); g_debug("Query took: %f sec", g_timer_elapsed(dbt, &tms)); }
62 static sqlite3 *osmdb;
63 static gboolean osm_db_ok;
65 struct sql_select_stmt {
66 sqlite3_stmt *select_way;
67 sqlite3_stmt *select_way2;
68 sqlite3_stmt *select_way_next_seg;
69 sqlite3_stmt *select_way_prev_seg;
71 sqlite3_stmt *select_way_nodes;
72 sqlite3_stmt *select_way_name;
73 sqlite3_stmt *select_way_name_nls;
74 sqlite3_stmt *select_way_name_search;
75 sqlite3_stmt *select_way_ref;
76 sqlite3_stmt *select_place;
77 sqlite3_stmt *select_place_near;
78 sqlite3_stmt *select_place_search;
80 sqlite3_stmt *select_node_next;
81 sqlite3_stmt *select_node_prev;
83 static struct sql_select_stmt sql;
86 static GtkProgressBar *dbpw=NULL;
88 static const gchar *osm_tables[]={
93 OSM_TABLE_WAY_UPDATES,
97 OSM_TABLE_WAY_NAMES_NLS,
101 OSM_TABLE_POI_CATEGORY,
104 static const gchar *osm_indexes[]={
122 /* Cache hash tables */
123 struct osm_place_cache {
128 static struct osm_place_cache pcache;
130 static guint way_dist_range=OSM_RANGE_WAY;
133 osm_way_node *osm_way_get_prev_node(osm_way *w);
134 osm_way_node *osm_way_get_next_node(osm_way *w);
136 /*****************************************************************************/
139 osm_db_create(sqlite3 *db)
141 return db_exec_sql_array(db, osm_tables) && db_exec_sql_array(db, osm_indexes);
144 /*****************************************************************************/
147 osm_set_way_range(guint sr)
150 g_debug("way_range set to %d", sr);
154 osm_set_way_range_from_speed(gfloat speed)
156 osm_set_way_range(speed>54.0 ? OSM_RANGE_WAY : (OSM_RANGE_WAY-lrint((speed/4)*1000)));
159 /*****************************************************************************/
162 osm_progress_pulse(void)
166 gtk_progress_bar_pulse(dbpw);
171 osm_progress(void *ud)
173 g_debug("SQL: Running gtk mainloop");
175 while (gtk_events_pending())
176 gtk_main_iteration();
178 gtk_main_iteration_do(FALSE);
184 osm_progress_hide(sqlite3 *db)
188 gtk_progress_bar_set_text(dbpw, "");
189 gtk_progress_bar_set_fraction(dbpw, 0.0);
193 osm_progress_show(sqlite3 *db)
197 gtk_progress_bar_set_text(dbpw, _("Searching..."));
198 gtk_progress_bar_pulse(dbpw);
199 gtk_main_iteration_do(FALSE);
203 osm_progress_set_widget(sqlite3 *db, GtkProgressBar *w)
205 if (dbpw!=NULL && w==NULL) {
206 osm_progress_hide(db);
208 g_source_remove(sid);
215 osm_progress_show(db);
216 sid=g_timeout_add(330, (GSourceFunc)osm_progress_pulse, NULL);
220 /*****************************************************************************/
223 osm_db_prepare(sqlite3 *db)
229 /* Select nearest place inside lat,lon+-range */
230 DB_PREP(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
231 " ilat,ilon,places.nid,isin_p,isin_c "
232 " from places,nodes where type=$TYPE "
233 " and nodes.nid=places.nid "
234 " and ilat between $LAT-$RANGE and $LAT+$RANGE "
235 " and ilon between $LON-$RANGE and $LON+$RANGE "
236 " order by d limit 1",
237 sql.select_place_near);
239 /* Select place name, distance, location, parent-place and type with given ID */
240 DB_PREP(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
241 " ilat,ilon,type,isin_p,isin_c "
242 " from places,nodes where "
243 " nodes.nid=places.nid "
244 " and places.nid=$NID order by d limit 1",
247 /* Search place names */
248 DB_PREP(db, "select places.nid,name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
249 " rlat,rlon,type,isin_p,isin_c "
250 " from places,nodes where "
251 " nodes.nid=places.nid "
252 " and name like $NAME order by d limit 200",
253 sql.select_place_search);
255 /***********************
256 * Way related queries *
257 ***********************/
259 /* Select nearest ways inside lat,lon+-range */
260 DB_PREP(db, "select w.wid,type,nodes,flags,"
261 "(($LAT-n.ilat)*($LAT-n.ilat))+(($LON-n.ilon)*($LON-n.ilon)) as d,wn.f,wn.t,n.ilat,n.ilon "
262 " from way as w,way_n2n as wn,nodes as n "
263 " where w.wid=wn.wid and wn.f=n.nid "
264 " and n.ilat between $LAT-$RANGE and $LAT+$RANGE "
265 " and n.ilon between $LON-$RANGE and $LON+$RANGE "
266 " and w.type between $WTS and $WTY "
270 /* Search way names inside bbox, using both primary and secondary language names */
271 DB_PREP(db, "select w.wid,w.name as name,"
272 "(($LAT-ww.lat)*($LAT-ww.lat))+(($LON-ww.lon)*($LON-ww.lon)) as d,ww.lat,ww.lon "
273 " from way_names as w,way as ww where "
274 " ww.type between $WTS and $WTY and w.wid=ww.wid and w.name like $NAME "
275 " and ww.lat between $LAT-$RANGE and $LAT+$RANGE "
276 " and ww.lon between $LON-$RANGE and $LON+$RANGE "
278 " select w.wid,n.name as name,"
279 "(($LAT-ww.lat)*($LAT-ww.lat))+(($LON-ww.lon)*($LON-ww.lon)) as d,ww.lat,ww.lon "
280 " from way_names as w, way as ww,way_names_nls as n on w.wid=n.wid where "
281 " ww.type between $WTS and $WTY and w.wid=ww.wid and n.name like $NAME "
282 " and ww.lat between $LAT-$RANGE and $LAT+$RANGE "
283 " and ww.lon between $LON-$RANGE and $LON+$RANGE "
284 " order by d, name limit 500",
285 sql.select_way_name_search);
287 /* Get way segment */
288 DB_PREP(db, "select wn.t,ilat,ilon from way_n2n as wn,nodes where wid=? and wn.f=? and wn.t=nodes.nid limit 1",
289 sql.select_way_next_seg);
291 /* Get way segment */
292 DB_PREP(db, "select wn.f,ilat,ilon from way_n2n as wn,nodes where wid=? and wn.t=? and wn.f=nodes.nid limit 1",
293 sql.select_way_prev_seg);
296 DB_PREP(db, "select name from way_names where wid=?", sql.select_way_name);
298 /* Get Way ref and int_ref */
299 DB_PREP(db, "select ref,int_ref from way_ref where rid=?", sql.select_way_ref);
301 /* Get way all way nodes */
302 DB_PREP(db, "select wn.f,ilat,ilon from way_n2n as wn,nodes where wid=? and wn.f=nodes.nid", sql.select_way_nodes);
304 /* Get next, prev node + way information. For routing. */
305 DB_PREP(db, "select w.wid,w.type,w.flags,w.speed,n.nid,n.rlat,n.rlon,nn.f,nn.t,n.f,n.l "
306 "from way as w, nodes as n, way_n2n as nn where w.wid=nn.wid and nn.f=n.nid and nn.f=?",
307 sql.select_node_next);
309 DB_PREP(db, "select w.wid,w.type,w.flags,w.speed,n.nid,n.rlat,n.rlon,nn.f,nn.t,n.f,n.l "
310 "from way as w, nodes as n, way_n2n as nn where w.wid=nn.wid and nn.f=n.nid and nn.t=?",
311 sql.select_node_prev);
320 if (sql.select_way_ref)
321 sqlite3_finalize(sql.select_way_ref);
322 if (sql.select_way_name)
323 sqlite3_finalize(sql.select_way_name);
324 if (sql.select_way_next_seg)
325 sqlite3_finalize(sql.select_way_next_seg);
326 if (sql.select_way_prev_seg)
327 sqlite3_finalize(sql.select_way_prev_seg);
328 if (sql.select_way_name_search)
329 sqlite3_finalize(sql.select_way_name_search);
331 sqlite3_finalize(sql.select_way2);
332 if (sql.select_place)
333 sqlite3_finalize(sql.select_place);
334 if (sql.select_place_near)
335 sqlite3_finalize(sql.select_place_near);
336 if (sql.select_node_next)
337 sqlite3_finalize(sql.select_node_next);
338 if (sql.select_node_prev)
339 sqlite3_finalize(sql.select_node_prev);
343 memset(&sql, 0, sizeof(sql));
344 g_hash_table_destroy(pcache.cache);
348 g_timer_destroy(dbt);
352 osm_init(sqlite3 **db)
355 pcache.cache=g_hash_table_new(g_direct_hash, g_direct_equal);
364 memset(&sql, 0, sizeof(sql));
365 if (osm_db_create(osmdb)==FALSE) {
366 g_printerr("Failed to create OSM tables or indexes: %s", sqlite3_errmsg(osmdb));
370 if (osm_db_prepare(osmdb)==FALSE) {
371 g_printerr("Failed to prepare OSM SQL statements: %s", sqlite3_errmsg(osmdb));
379 osm_db_enable_mainloop(sqlite3 *db, gboolean eml)
382 sqlite3_progress_handler(db, OSM_DB_PROGRESS_NUM, NULL, NULL);
384 sqlite3_progress_handler(db, OSM_DB_PROGRESS_NUM, osm_progress, NULL);
387 /*****************************************************************************/
390 osm_way_node_new(guint id, gint lat, gint lon, gint flags)
392 osm_way_node *n=g_slice_new(osm_way_node);
402 osm_way_node_free(osm_way_node *n)
405 g_slice_free(osm_way_node, n);
409 * Free way nodes list
412 osm_way_nodes_free(osm_way *w)
419 for (iter=w->nodes; iter!=NULL; iter=iter->next)
420 g_slice_free(osm_way_node, (osm_way_node*)iter->data);
422 g_list_free(w->nodes);
426 * Free a osm_way structure
429 osm_way_free(osm_way *w)
433 osm_way_nodes_free(w);
440 g_slice_free(osm_way, w);
443 /*****************************************************************************/
446 osm_place_free(osm_place *p)
450 g_slice_free(osm_place, p);
454 osm_place_remove(gpointer k, gpointer v, gpointer ud)
456 osm_place_free((osm_place *)v);
463 return g_slice_new0(osm_place);
467 osm_place_cache_lookup(guint32 id)
470 r=g_hash_table_lookup(pcache.cache, GINT_TO_POINTER(id));
476 g_debug("OSM: Cache %d/%d", pcache.hit, pcache.miss);
481 osm_place_cache_add(osm_place *p)
483 if (osm_place_cache_lookup(p->id)==NULL)
484 g_hash_table_insert(pcache.cache, GINT_TO_POINTER(p->id), p);
488 osm_place_cache_gc(void)
492 r=g_hash_table_foreach_remove(pcache.cache, osm_place_remove, NULL);
493 g_debug("OSM: Cache cleared (%d)", r);
499 osm_place_update_distance(osm_place *p, gint lat, gint lon)
503 lam=(gdouble)((lat-p->lat)*(lat-p->lat));
504 lom=(gdouble)((lon-p->lon)*(lon-p->lon));
506 p->dist=sqrt(lam+lom);
510 * Get place with given id and distance to current location
513 osm_place_get(guint32 id, gint lat, gint lon, osm_place **nr)
517 g_return_val_if_fail(sql.select_place, FALSE);
520 n=osm_place_cache_lookup(id);
522 osm_place_update_distance(n, lat, lon);
527 /* XXX: better place for this */
528 if (g_hash_table_size(pcache.cache)>OSM_PLACE_CACHE_MAX_ITEMS)
529 osm_place_cache_gc();
531 sqlite3_clear_bindings(sql.select_place);
532 sqlite3_reset(sql.select_place);
534 if (SQLITE_OK != sqlite3_bind_int(sql.select_place, 1, lat) ||
535 SQLITE_OK != sqlite3_bind_int(sql.select_place, 2, lon) ||
536 SQLITE_OK != sqlite3_bind_int(sql.select_place, 3, id)) {
537 g_warning("Failed to bind values for place");
542 if (SQLITE_ROW == sqlite3_step(sql.select_place)) {
547 place=sqlite3_column_text(sql.select_place, 0);
548 n->name=g_strdup(place);
549 dist=sqlite3_column_int(sql.select_place, 1);
550 n->dist=sqrt((double)dist);
551 n->lat=sqlite3_column_int(sql.select_place, 2);
552 n->lon=sqlite3_column_int(sql.select_place, 3);
553 n->type=sqlite3_column_int(sql.select_place, 4);
554 n->isin_p=sqlite3_column_int(sql.select_place, 5);
555 /* n->isin_c=sqlite3_column_int(sql.select_place, 6); */
563 * Search for the nearest place with given type
566 osm_find_nearest_place(node_type_t type, gint lat, gint lon, osm_place **nr)
571 g_return_val_if_fail(sql.select_place_near, FALSE);
574 case NODE_PLACE_SUBURB:
577 case NODE_PLACE_CITY:
578 case NODE_PLACE_TOWN:
581 case NODE_PLACE_HAMLET:
582 case NODE_PLACE_VILLAGE:
590 sqlite3_clear_bindings(sql.select_place_near);
591 sqlite3_reset(sql.select_place_near);
593 if (SQLITE_OK != sqlite3_bind_int(sql.select_place_near, 1, lat) ||
594 SQLITE_OK != sqlite3_bind_int(sql.select_place_near, 2, lon) ||
595 SQLITE_OK != sqlite3_bind_int(sql.select_place_near, 3, type) ||
596 SQLITE_OK != sqlite3_bind_int(sql.select_place_near, 4, range)) {
597 g_warning("Failed to bind values for near place");
602 n->isin_p=n->lat=n->lon=n->dist=0;
604 if (SQLITE_ROW == sqlite3_step(sql.select_place_near)) {
608 place=sqlite3_column_text(sql.select_place_near, 0);
609 n->name=g_strdup(place);
610 dist=sqlite3_column_int(sql.select_place_near, 1);
611 n->dist=sqrt((double)dist);
612 n->lat=sqlite3_column_int(sql.select_place_near, 2);
613 n->lon=sqlite3_column_int(sql.select_place_near, 3);
614 n->id=sqlite3_column_int(sql.select_place_near, 4);
615 n->isin_p=sqlite3_column_int(sql.select_place_near, 5);
616 /* n->isin_c=sqlite3_column_int(sql.select_place_near, 6); */
620 osm_place_cache_add(n);
634 osm_find_nearest_way_nodes(gint lat, gint lon, guint range)
640 g_return_val_if_fail(sql.select_way2, NULL);
642 sqlite3_reset(sql.select_way2);
643 sqlite3_clear_bindings(sql.select_way2);
645 if (SQLITE_OK != sqlite3_bind_int(sql.select_way2, 1, lat) ||
646 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 2, lon) ||
647 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 3, range) ||
648 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 4, WAY_ROAD_START) ||
649 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 5, WAY_ROAD_END)) {
650 g_warning("Failed to bind values for way");
655 while (SQLITE_ROW == sqlite3_step(sql.select_way2)) {
660 w=g_slice_new0(osm_way);
661 w->id=sqlite3_column_int(sql.select_way2, 0);
662 w->type=sqlite3_column_int(sql.select_way2, 1);
663 w->nodecnt=sqlite3_column_int(sql.select_way2, 2);
664 w->flags=sqlite3_column_int(sql.select_way2, 3);
665 dist=sqlite3_column_int(sql.select_way2, 4);
666 w->dist=sqrt((gdouble)dist);
667 w->f=sqlite3_column_int(sql.select_way2, 5);
668 w->t=sqlite3_column_int(sql.select_way2, 6);
670 lat=sqlite3_column_int(sql.select_way2, 7);
671 lon=sqlite3_column_int(sql.select_way2, 8);
673 w->node_f=osm_way_node_new(w->f, lat, lon, 0);
675 ways=g_list_prepend(ways, w);
682 /*****************************************************************************/
685 osm_get_route_node(guint nid, osm_node_direction d)
690 sqlite3_stmt *psql=NULL;
692 g_return_val_if_fail(sql.select_node_next, FALSE);
693 g_return_val_if_fail(sql.select_node_prev, FALSE);
697 psql=sql.select_node_next;
700 psql=sql.select_node_prev;
703 g_assert_not_reached();
708 sqlite3_clear_bindings(psql);
710 if (SQLITE_OK != sqlite3_bind_int(psql, 1, nid)) {
711 g_warning("Failed to bind values for route node");
715 while (SQLITE_ROW == sqlite3_step(psql)) {
719 w=g_slice_new0(osm_way);
720 w->id=sqlite3_column_int(psql, 0);
721 w->type=sqlite3_column_int(psql, 1);
722 w->flags=sqlite3_column_int(psql, 2);
723 w->speed=sqlite3_column_int(psql, 3);
725 lat=sqlite3_column_double(psql, 5);
726 lon=sqlite3_column_double(psql, 6);
728 w->f=sqlite3_column_int(psql, 7);
729 w->t=sqlite3_column_int(psql, 8);
732 w->node->flags=sqlite3_column_int(psql, 9);
733 w->node->links=sqlite3_column_int(psql, 10);
736 r=g_slist_prepend(r, w);
743 /*****************************************************************************/
745 osm_way_distance(gint lat, gint lon, osm_way_node *f, osm_way_node *t, gdouble *d)
755 return distance_point_to_line((gdouble)lon, (gdouble)lat, (gdouble)f->lon, (gdouble)f->lat, (gdouble)t->lon, (gdouble)t->lat, d);
759 * Search for the nearest way (road)
760 * - First search for ways with nearest node
761 * - If only one is returned then we are done.
762 * - If more than one, go trough the results:
763 * - Load nodes for the way, check prev/next nodes
764 * - Store result if closer than before
765 * - Return closest way
768 #define START_DIST (900000.0)
771 osm_find_nearest_way(gint lat, gint lon)
775 guint range=OSM_RANGE_START;
778 gdouble pdist=START_DIST, dist_n, dist_p;
780 while ((w=osm_find_nearest_way_nodes(lat, lon, range))==NULL && range<=OSM_RANGE_STOP) {
781 range+=OSM_RANGE_STEP;
782 g_debug("Trying with range: %d", range);
784 found_ways=g_list_length(w);
785 g_debug("Found %d ways withing range %d", found_ways, range);
790 for (iter=w; iter!=NULL; iter=iter->next) {
793 osm_way *way=(osm_way*)iter->data;
797 wnn=osm_way_get_next_node(way);
798 if (osm_way_distance(lat, lon, way->node_f, wnn, &dist_n)==FALSE) {
799 osm_way_node_free(wnn);
801 } else if (dist_n<pdist) { /* Is it near ?*/
804 way->distance=dist_n;
810 g_debug("WayN: #%d (T:%d), %d nodes, near [%d-%d], D: %.2f (Valid: %s)",
811 way->id, way->type, way->nodecnt, way->f, way->t, dist_n, dist_n==START_DIST ? "F" : "T");
813 wnp=osm_way_get_prev_node(way);
814 if (osm_way_distance(lat, lon, way->node_f, wnp, &dist_p)==FALSE) {
815 osm_way_node_free(wnp);
817 } else if (dist_p<pdist) {
820 way->distance=dist_n;
822 osm_way_node_free(wnn);
829 g_debug("WayP: #%d (T:%d), %d nodes, near [%d-%d], D: %.2f (Valid: %s)",
830 way->id, way->type, way->nodecnt, way->f, way->t, dist_p, dist_p==START_DIST ? "F" : "T");
842 osm_way_get_name(cw);
843 if (cw->type==WAY_MOTORWAY || cw->type==WAY_TRUNK ||
844 cw->type==WAY_PRIMARY || cw->type==WAY_SECONDARY || cw->type==WAY_TERTIARY) {
847 osm_way_get_nodes(cw);
849 g_debug("Found: (#%d): [%s]:[%s][%s]", cw->id, cw->name, cw->ref, cw->int_ref);
850 g_debug("T: %d F: %d N#: %d D: %.2f", cw->type, cw->flags, cw->nodecnt, cw->dist);
851 g_debug("NF#: %d NT#: %d (DC: %.2f)", cw->f, cw->t, cw->distance);
856 /* XXX: These two should be combined to save memory */
858 * Get previous node/segment of given way node
862 osm_way_get_prev_node(osm_way *w)
864 g_return_val_if_fail(sql.select_way_prev_seg, NULL);
866 sqlite3_reset(sql.select_way_prev_seg);
867 sqlite3_clear_bindings(sql.select_way_prev_seg);
869 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_prev_seg, 1, w->id) ||
870 SQLITE_OK != sqlite3_bind_int(sql.select_way_prev_seg, 2, w->f) ) {
871 g_warning("Failed to bind values for prev seg");
875 if (SQLITE_ROW == sqlite3_step(sql.select_way_prev_seg)) {
876 return osm_way_node_new(
877 sqlite3_column_int(sql.select_way_prev_seg, 0),
878 sqlite3_column_int(sql.select_way_prev_seg, 1),
879 sqlite3_column_int(sql.select_way_prev_seg, 2),
887 * Get next node/segment of given way node
891 osm_way_get_next_node(osm_way *w)
893 g_return_val_if_fail(sql.select_way_next_seg, NULL);
895 sqlite3_reset(sql.select_way_next_seg);
896 sqlite3_clear_bindings(sql.select_way_next_seg);
898 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_next_seg, 1, w->id) ||
899 SQLITE_OK != sqlite3_bind_int(sql.select_way_next_seg, 2, w->f) ) {
900 g_warning("Failed to bind values for next seg");
904 if (SQLITE_ROW == sqlite3_step(sql.select_way_next_seg)) {
905 return osm_way_node_new(
906 sqlite3_column_int(sql.select_way_next_seg, 0),
907 sqlite3_column_int(sql.select_way_next_seg, 1),
908 sqlite3_column_int(sql.select_way_next_seg, 2),
916 * Get list of nodes for given way
920 osm_way_get_nodes(osm_way *w)
925 g_return_val_if_fail(sql.select_way_nodes, FALSE);
927 sqlite3_reset(sql.select_way_nodes);
928 sqlite3_clear_bindings(sql.select_way_nodes);
930 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_nodes, 1, w->id)) {
931 g_warning("Failed to bind values for way nodes");
935 while (SQLITE_ROW == sqlite3_step(sql.select_way_nodes)) {
938 n=g_slice_new(osm_way_node);
939 n->id=sqlite3_column_int(sql.select_way_nodes, 0);
940 n->lat=sqlite3_column_int(sql.select_way_nodes, 1);
941 n->lon=sqlite3_column_int(sql.select_way_nodes, 2);
942 w->nodes=g_list_append(w->nodes, n);
945 return (w->nodes==NULL) ? FALSE : TRUE;
953 osm_way_get_name(osm_way *w)
955 g_return_val_if_fail(sql.select_way_name, FALSE);
957 sqlite3_reset(sql.select_way_name);
958 sqlite3_clear_bindings(sql.select_way_name);
960 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_name, 1, w->id)) {
961 g_warning("Failed to bind values for way name");
965 if (SQLITE_ROW == sqlite3_step(sql.select_way_name)) {
967 place=sqlite3_column_text(sql.select_way_name, 0);
968 w->name=g_strdup(place);
975 osm_way_get_name_nls(osm_way *w)
977 g_return_val_if_fail(sql.select_way_name_nls, FALSE);
979 sqlite3_reset(sql.select_way_name_nls);
980 sqlite3_clear_bindings(sql.select_way_name_nls);
982 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_name_nls, 1, w->id) ||
983 SQLITE_OK != sqlite3_bind_) {
984 g_warning("Failed to bind values for way name nls");
988 if (SQLITE_ROW == sqlite3_step(sql.select_way_name_nls)) {
990 place=sqlite3_column_text(sql.select_way_name_nls, 0);
991 w->name=g_strdup(place);
999 * Get Way ref and int_ref
1003 osm_way_get_ref(osm_way *w)
1005 g_return_val_if_fail(sql.select_way_ref, FALSE);
1007 sqlite3_reset(sql.select_way_ref);
1008 sqlite3_clear_bindings(sql.select_way_ref);
1010 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_ref, 1, w->id)) {
1011 g_warning("Failed to bind values for way ref");
1015 if (SQLITE_ROW == sqlite3_step(sql.select_way_ref)) {
1016 const gchar *ref, *int_ref;
1017 ref=sqlite3_column_text(sql.select_way_ref, 0);
1018 int_ref=sqlite3_column_text(sql.select_way_ref, 1);
1019 w->ref=g_strdup(ref);
1020 w->int_ref=g_strdup(int_ref);
1025 /******************************************************************************/
1028 * Check if we have moved away from the last known street
1031 osm_check_location(osm_location *map_loc, gint lat, gint lon)
1034 if (map_loc->street && osm_way_distance(lat, lon, map_loc->street->node_f, map_loc->street->node_t, &dist)==TRUE) {
1035 return (dist>(gdouble)way_dist_range) ? FALSE : TRUE;
1041 * Try to figure out where the given lat,lon is. Fills in the given struct,
1042 * with street, secondary (suburb) and primary (city,town,village) location.
1043 * Will try to minimize the amount of database access by skipping queries
1044 * if we haven't moved or if we don't know where we are.
1048 osm_get_location_data(gint lat, gint lon, gfloat heading, osm_location *map_loc)
1051 gboolean check_place=FALSE;
1054 /* Check 10 times, if location is not known then assume we are in here-be-dragons land */
1055 if (map_loc->nfcnt>10) {
1057 t=time(NULL)-map_loc->last_valid;
1059 g_debug("Here be dragons");
1062 g_debug("Trying again");
1063 map_loc->last_valid=time(NULL);
1065 map_loc->valid=FALSE;
1069 if (map_loc->valid==FALSE) {
1072 map_loc->valid=TRUE;
1075 d=calculate_idistance(lat,lon,map_loc->lat,map_loc->lon);
1078 /* Check if we are still near the same way as last time */
1079 if (map_loc->street && osm_way_distance(lat, lon, map_loc->street->node_f, map_loc->street->node_t, &dist)==TRUE) {
1080 /* We are probably on the same way as last time,
1081 but check if the distance is over a limit or our heading has changed significantly.
1083 if ( (dist>(gdouble)way_dist_range) || (fabs(angle_diff(heading, map_loc->heading))>15.0)) {
1084 /* We have moved a large amount, check way again */
1085 g_debug("Distance %.2f > %.2f range or angle %f > 15.0, checking location",
1086 dist, (gdouble)way_dist_range, fabs(heading-map_loc->heading));
1087 osm_way_free(map_loc->street);
1088 map_loc->street=osm_find_nearest_way(lat, lon);
1090 map_loc->changed=TRUE;
1092 g_debug("*** No change in location: %f %d", dist, way_dist_range);
1093 /* We are still on the same way as last time */
1095 map_loc->changed=FALSE;
1100 /* We didn't know our location, so check it, but only if we have moved */
1101 if (d>way_dist_range) {
1102 g_debug("*** Must check location");
1104 osm_way_free(map_loc->street);
1105 map_loc->street=osm_find_nearest_way(lat, lon);
1112 if (!map_loc->street) {
1113 g_debug("*** Street not known");
1115 map_loc->changed=TRUE;
1117 g_debug("*** Street known");
1119 map_loc->last_valid=time(NULL);
1120 map_loc->changed=TRUE;
1125 if (map_loc->changed==TRUE) {
1126 map_loc->heading=heading;
1129 g_debug("NotFoundCounter: %d", map_loc->nfcnt);
1130 g_debug("D: %ld %ld", d, (gulong)way_dist_range);
1132 if (check_place==TRUE || d>way_dist_range*4) {
1135 fs=osm_find_nearest_place(NODE_PLACE_SUBURB, lat, lon, &map_loc->secondary);
1136 if (fs==TRUE && map_loc->secondary && map_loc->secondary->isin_p!=0) {
1137 if (osm_place_get(map_loc->secondary->isin_p, lat, lon, &(map_loc->primary))==FALSE) {
1138 if (osm_find_nearest_place(NODE_PLACE_CITY, lat, lon, &map_loc->primary)==TRUE)
1139 g_debug("Near city: %s", map_loc->primary->name);
1140 else if (osm_find_nearest_place(NODE_PLACE_TOWN, lat, lon, &map_loc->primary)==TRUE)
1141 g_debug("Near town: %s", map_loc->primary->name);
1143 g_assert(map_loc->primary!=NULL);
1147 g_debug("In: %s", map_loc->primary ? map_loc->primary->name : "?");
1149 } else if (map_loc->street && map_loc->street->isin_p!=0) {
1150 if (osm_place_get(map_loc->street->isin_p, lat, lon, &map_loc->primary)==FALSE) {
1151 g_debug("Street location not know.");
1153 g_debug("Street is in: %s", map_loc->primary ? map_loc->primary->name : "?");
1156 if (osm_find_nearest_place(NODE_PLACE_CITY, lat, lon, &map_loc->primary)==TRUE)
1157 g_debug("Near city: %s", map_loc->primary->name);
1158 else if (osm_find_nearest_place(NODE_PLACE_TOWN, lat, lon, &map_loc->primary)==TRUE)
1159 g_debug("Near town: %s", map_loc->primary->name);
1161 g_assert(map_loc->primary!=NULL);
1167 return map_loc->street ? TRUE : FALSE;
1174 osm_place_search(gdouble lat, gdouble lon, gchar *text, GtkListStore **store)
1179 gchar tmp1[16], tmp2[16];
1182 g_return_val_if_fail(sql.select_place_search, FALSE);
1183 g_return_val_if_fail(text, FALSE);
1185 ltext=g_strdup_printf("%s%%", text);
1187 if (SQLITE_OK != sqlite3_bind_double(sql.select_place_search, 1, lat) ||
1188 SQLITE_OK != sqlite3_bind_double(sql.select_place_search, 2, lon) ||
1189 SQLITE_OK != sqlite3_bind_text(sql.select_place_search, 3, ltext, -1, SQLITE_TRANSIENT)) {
1190 g_warning("Failed to bind values for sql.select_place_search");
1191 sqlite3_clear_bindings(sql.select_place_search);
1199 *store = gtk_list_store_new(ITEM_NUM_COLUMNS,
1200 G_TYPE_INT, /* ID */
1202 G_TYPE_DOUBLE, /* Latitude */
1203 G_TYPE_DOUBLE, /* Longitude */
1204 G_TYPE_DOUBLE, /* Distance */
1205 G_TYPE_STRING, /* Lat/Lon */
1206 G_TYPE_STRING, /* Label */
1207 G_TYPE_STRING, /* Desc. */
1208 G_TYPE_STRING, /* Category */
1209 G_TYPE_STRING, /* Dummy */
1210 G_TYPE_STRING); /* Dummy */
1212 while (SQLITE_ROW == sqlite3_step(sql.select_place_search)) {
1213 gdouble rlat, rlon, dist;
1215 rlat=sqlite3_column_double(sql.select_place_search, 3);
1216 rlon=sqlite3_column_double(sql.select_place_search, 4);
1217 lat_format(_degformat, rlat, tmp1);
1218 lon_format(_degformat, rlon, tmp2);
1219 dist=calculate_distance(lat, lon, rlat, rlon) * UNITS_CONVERT[_units];
1221 gtk_list_store_append(*store, &iter);
1222 gtk_list_store_set(*store, &iter,
1223 ITEM_ID, sqlite3_column_int(sql.select_place_search, 0),
1227 ITEM_LATLON, g_strdup_printf("%s, %s", tmp1, tmp2),
1228 ITEM_LABEL, sqlite3_column_text(sql.select_place_search, 1),
1233 g_debug("Found: %d places", rows);
1235 sqlite3_reset(sql.select_place_search);
1236 sqlite3_clear_bindings(sql.select_place_search);
1244 * Search for a street(way) starting with given 'text', next given lat/lon
1248 osm_way_search(gdouble lat, gdouble lon, gchar *text, GtkListStore **store)
1253 gchar tmp1[16], tmp2[16];
1256 g_return_val_if_fail(sql.select_way_name_search, FALSE);
1257 g_return_val_if_fail(text, FALSE);
1259 g_debug("Way Search: [%s] around %.6f %.6f", text, lat, lon);
1261 ltext=g_strdup_printf("%s%%", text);
1263 if (SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 1, lat) ||
1264 SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 2, lon) ||
1265 SQLITE_OK != sqlite3_bind_int(sql.select_way_name_search, 3, WAY_ROAD_START) ||
1266 SQLITE_OK != sqlite3_bind_int(sql.select_way_name_search, 4, WAY_ROAD_END) ||
1267 SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 6, range) ||
1268 SQLITE_OK != sqlite3_bind_text(sql.select_way_name_search, 5, ltext, -1, SQLITE_TRANSIENT)) {
1269 g_warning("Failed to bind values for sql.select_way_name_search");
1270 sqlite3_clear_bindings(sql.select_way_name_search);
1278 *store = gtk_list_store_new(ITEM_NUM_COLUMNS,
1279 G_TYPE_INT, /* ID */
1281 G_TYPE_DOUBLE, /* Latitude */
1282 G_TYPE_DOUBLE, /* Longitude */
1283 G_TYPE_DOUBLE, /* Distance */
1284 G_TYPE_STRING, /* Lat/Lon */
1285 G_TYPE_STRING, /* Label */
1286 G_TYPE_STRING, /* Desc. */
1287 G_TYPE_STRING, /* Category */
1288 G_TYPE_STRING, /* Dummy */
1289 G_TYPE_STRING); /* Dummy */
1291 while (SQLITE_ROW == sqlite3_step(sql.select_way_name_search)) {
1292 gdouble rlat, rlon, dist;
1294 rlat=sqlite3_column_double(sql.select_way_name_search, 3);
1295 rlon=sqlite3_column_double(sql.select_way_name_search, 4);
1296 lat_format(_degformat, rlat, tmp1);
1297 lon_format(_degformat, rlon, tmp2);
1298 dist=calculate_distance(lat, lon, rlat, rlon) * UNITS_CONVERT[_units];
1300 gtk_list_store_append(*store, &iter);
1301 gtk_list_store_set(*store, &iter,
1302 ITEM_ID, sqlite3_column_int(sql.select_way_name_search, 0),
1306 ITEM_LATLON, g_strdup_printf("%s, %s", tmp1, tmp2),
1307 ITEM_LABEL, sqlite3_column_text(sql.select_way_name_search, 1),
1312 g_debug("Found: %d items", rows);
1314 sqlite3_reset(sql.select_way_name_search);
1315 sqlite3_clear_bindings(sql.select_way_name_search);