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 /* #define DEBUG_OSM */
39 /* #define DEBUG_OSM_TIME */
40 #define OSM_PLACE_CACHE_MAX_ITEMS (64)
42 #define OSM_DB_PROGRESS_NUM (20000)
44 /* Node search ranges */
45 #define OSM_RANGE_START (16384)
46 #define OSM_RANGE_STEP (8192)
47 #define OSM_RANGE_STOP (65535)
49 static sqlite3 *osmdb;
50 static gboolean osm_db_ok;
52 struct sql_select_stmt {
53 sqlite3_stmt *select_way;
54 sqlite3_stmt *select_way2;
55 sqlite3_stmt *select_way_next_seg;
56 sqlite3_stmt *select_way_prev_seg;
58 sqlite3_stmt *select_way_nodes;
59 sqlite3_stmt *select_way_name;
60 sqlite3_stmt *select_way_name_nls;
61 sqlite3_stmt *select_way_name_search;
62 sqlite3_stmt *select_way_ref;
63 sqlite3_stmt *select_place;
64 sqlite3_stmt *select_near_place;
66 sqlite3_stmt *select_node_next;
67 sqlite3_stmt *select_node_prev;
69 static struct sql_select_stmt sql;
71 static GtkProgressBar *dbpw=NULL;
73 /* Cache hash tables */
74 struct osm_place_cache {
79 static struct osm_place_cache pcache;
81 static guint way_dist_range=OSM_RANGE_WAY;
83 osm_way_node *osm_way_get_prev_node(osm_way *w);
84 osm_way_node *osm_way_get_next_node(osm_way *w);
86 /*****************************************************************************/
89 osm_set_way_range(guint sr)
95 osm_set_way_range_from_speed(gfloat speed)
98 way_dist_range=OSM_RANGE_WAY*2;
100 way_dist_range=OSM_RANGE_WAY-lrint((speed/4)*1000);
103 /*****************************************************************************/
106 osm_progress(void *ud)
110 gtk_progress_bar_pulse(dbpw);
111 gtk_main_iteration_do(FALSE);
116 osm_progress_hide(sqlite3 *db)
120 gtk_progress_bar_set_text(dbpw, "");
121 gtk_progress_bar_set_fraction(dbpw, 0.0);
122 sqlite3_progress_handler(db, OSM_DB_PROGRESS_NUM, NULL, NULL);
126 osm_progress_show(sqlite3 *db)
130 gtk_progress_bar_set_text(dbpw, _("Searching..."));
131 sqlite3_progress_handler(db, OSM_DB_PROGRESS_NUM, osm_progress, NULL);
135 osm_progress_set_widget(sqlite3 *db, GtkProgressBar *w)
137 if (dbpw!=NULL && w==NULL)
138 osm_progress_hide(db);
141 osm_progress_show(db);
144 /*****************************************************************************/
147 osm_db_prepare(sqlite3 *db)
149 /* Select nearest place inside lat,lon+-range */
150 if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as dist,"
151 " ilat,ilon,places.nid,isin_p,isin_c "
152 " from places,nodes where type=$TYPE "
153 " and nodes.nid=places.nid "
154 " and ilat between $LAT-$RANGE and $LAT+$RANGE"
155 " and ilon between $LON-$RANGE and $LON+$RANGE order by dist limit 1",
156 -1, &sql.select_near_place, NULL)!=SQLITE_OK)
159 /* Select place name, distance, location, parent-place and type with given ID */
160 if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as dist,"
161 " ilat,ilon,type,isin_p,isin_c "
162 " from places,nodes where "
163 " nodes.nid=places.nid "
164 " and places.nid=$NID order by dist limit 1",
165 -1, &sql.select_place, NULL)!=SQLITE_OK)
169 /* Select nearest ways inside lat,lon+-range */
170 if (sqlite3_prepare_v2(db, "select w.wid,type,nodes,flags,"
171 "(($LAT-n.ilat)*($LAT-n.ilat))+(($LON-n.ilon)*($LON-n.ilon)) as d,wn.f,wn.t,n.ilat,n.ilon "
172 " from way as w,way_n2n as wn,nodes as n "
173 " where w.wid=wn.wid and wn.f=n.nid "
174 " and n.ilat between $LAT-$RANGE and $LAT+$RANGE "
175 " and n.ilon between $LON-$RANGE and $LON+$RANGE "
176 " and w.type between $WTS and $WTY "
178 -1, &sql.select_way2, NULL)!=SQLITE_OK)
181 if (sqlite3_prepare_v2(db, "select w.wid,w.name as name,"
182 "(($LAT-ww.lat)*($LAT-ww.lat))+(($LON-ww.lon)*($LON-ww.lon)) as d,ww.lat,ww.lon "
183 " from way_names as w,way as ww where "
184 " ww.type between $WTS and $WTY and w.wid=ww.wid and w.name like $NAME "
185 " and ww.lat between $LAT-$RANGE and $LAT+$RANGE "
186 " and ww.lon between $LON-$RANGE and $LON+$RANGE "
188 " select w.wid,n.name as name,"
189 "(($LAT-ww.lat)*($LAT-ww.lat))+(($LON-ww.lon)*($LON-ww.lon)) as d,ww.lat,ww.lon "
190 " from way_names as w, way as ww,way_names_nls as n on w.wid=n.wid where "
191 " ww.type between $WTS and $WTY and w.wid=ww.wid and n.name like $NAME "
192 " and ww.lat between $LAT-$RANGE and $LAT+$RANGE "
193 " and ww.lon between $LON-$RANGE and $LON+$RANGE "
194 " order by name limit 100",
195 -1, &sql.select_way_name_search, NULL)!=SQLITE_OK)
198 if (sqlite3_prepare_v2(db, "select wn.t,ilat,ilon from way_n2n as wn,nodes where wid=? and wn.f=? and wn.t=nodes.nid limit 1",
199 -1, &sql.select_way_next_seg, NULL)!=SQLITE_OK)
202 if (sqlite3_prepare_v2(db, "select wn.f,ilat,ilon from way_n2n as wn,nodes where wid=? and wn.t=? and wn.f=nodes.nid limit 1",
203 -1, &sql.select_way_prev_seg, NULL)!=SQLITE_OK)
207 if (sqlite3_prepare_v2(db, "select name from way_names where wid=?", -1, &sql.select_way_name, NULL)!=SQLITE_OK)
210 /* Way ref and int_ref */
211 if (sqlite3_prepare_v2(db, "select ref,int_ref from way_ref where rid=?", -1, &sql.select_way_ref, NULL)!=SQLITE_OK)
214 /* Get next, prev node + way information. For routing. */
215 if (sqlite3_prepare_v2(db, "select w.wid,w.type,w.flags,w.speed,n.nid,n.rlat,n.rlon,nn.f,nn.t,n.f,n.l "
216 "from way as w, nodes as n, way_n2n as nn where w.wid=nn.wid and nn.f=n.nid and nn.f=?",
217 -1, &sql.select_node_next, NULL)!=SQLITE_OK)
220 if (sqlite3_prepare_v2(db, "select w.wid,w.type,w.flags,w.speed,n.nid,n.rlat,n.rlon,nn.f,nn.t,n.f,n.l "
221 "from way as w, nodes as n, way_n2n as nn where w.wid=nn.wid and nn.f=n.nid and nn.t=?",
222 -1, &sql.select_node_prev, NULL)!=SQLITE_OK)
232 if (sql.select_way_ref)
233 sqlite3_finalize(sql.select_way_ref);
234 if (sql.select_way_name)
235 sqlite3_finalize(sql.select_way_name);
236 if (sql.select_way_next_seg)
237 sqlite3_finalize(sql.select_way_next_seg);
238 if (sql.select_way_prev_seg)
239 sqlite3_finalize(sql.select_way_prev_seg);
240 if (sql.select_way_name_search)
241 sqlite3_finalize(sql.select_way_name_search);
243 sqlite3_finalize(sql.select_way2);
244 if (sql.select_place)
245 sqlite3_finalize(sql.select_place);
246 if (sql.select_near_place)
247 sqlite3_finalize(sql.select_near_place);
248 if (sql.select_node_next)
249 sqlite3_finalize(sql.select_node_next);
250 if (sql.select_node_prev)
251 sqlite3_finalize(sql.select_node_prev);
255 memset(&sql, 0, sizeof(sql));
256 g_hash_table_destroy(pcache.cache);
259 g_timer_destroy(dbt);
263 osm_init(sqlite3 **db)
266 pcache.cache=g_hash_table_new(g_direct_hash, g_direct_equal);
275 memset(&sql, 0, sizeof(sql));
276 if (osm_db_prepare(osmdb)==FALSE) {
277 g_printerr("Failed to prepare OSM SQL statements:");
278 g_printf("SQLITE: %s\n", sqlite3_errmsg(osmdb));
285 /*****************************************************************************/
288 osm_way_node_new(guint id, gint lat, gint lon, gint flags)
290 osm_way_node *n=g_slice_new(osm_way_node);
300 osm_way_node_free(osm_way_node *n)
303 g_slice_free(osm_way_node, n);
307 * Free way nodes list
310 osm_way_nodes_free(osm_way *w)
317 for (iter=w->nodes; iter!=NULL; iter=iter->next)
318 g_slice_free(osm_way_node, (osm_way_node*)iter->data);
320 g_list_free(w->nodes);
324 * Free a osm_way structure
327 osm_way_free(osm_way *w)
331 osm_way_nodes_free(w);
338 g_slice_free(osm_way, w);
341 /*****************************************************************************/
344 osm_place_free(osm_place *p)
348 g_slice_free(osm_place, p);
352 osm_place_remove(gpointer k, gpointer v, gpointer ud)
354 osm_place_free((osm_place *)v);
361 return g_slice_new0(osm_place);
365 osm_place_cache_lookup(guint32 id)
368 r=g_hash_table_lookup(pcache.cache, GINT_TO_POINTER(id));
374 g_debug("OSM: Cache %d/%d", pcache.hit, pcache.miss);
379 osm_place_cache_add(osm_place *p)
381 if (osm_place_cache_lookup(p->id)==NULL)
382 g_hash_table_insert(pcache.cache, GINT_TO_POINTER(p->id), p);
386 osm_place_cache_gc(void)
390 r=g_hash_table_foreach_remove(pcache.cache, osm_place_remove, NULL);
391 g_debug("OSM: Cache cleared (%d)", r);
397 osm_place_update_distance(osm_place *p, gint lat, gint lon)
401 lam=(gdouble)((lat-p->lat)*(lat-p->lat));
402 lom=(gdouble)((lon-p->lon)*(lon-p->lon));
404 p->dist=sqrt(lam+lom);
408 * Get place with given id and distance to current location
411 osm_place_get(guint32 id, gint lat, gint lon, osm_place **nr)
416 n=osm_place_cache_lookup(id);
418 osm_place_update_distance(n, lat, lon);
423 /* XXX: better place for this */
424 if (g_hash_table_size(pcache.cache)>OSM_PLACE_CACHE_MAX_ITEMS)
425 osm_place_cache_gc();
427 sqlite3_clear_bindings(sql.select_place);
428 sqlite3_reset(sql.select_place);
430 if (SQLITE_OK != sqlite3_bind_int(sql.select_place, 1, lat) ||
431 SQLITE_OK != sqlite3_bind_int(sql.select_place, 2, lon) ||
432 SQLITE_OK != sqlite3_bind_int(sql.select_place, 3, id)) {
433 g_printerr("Failed to bind values for place\n");
437 if (SQLITE_ROW == sqlite3_step(sql.select_place)) {
442 place=sqlite3_column_text(sql.select_place, 0);
443 n->name=g_strdup(place);
444 dist=sqlite3_column_int(sql.select_place, 1);
445 n->dist=sqrt((double)dist);
446 n->lat=sqlite3_column_int(sql.select_place, 2);
447 n->lon=sqlite3_column_int(sql.select_place, 3);
448 n->type=sqlite3_column_int(sql.select_place, 4);
449 n->isin_p=sqlite3_column_int(sql.select_place, 5);
450 /* n->isin_c=sqlite3_column_int(sql.select_place, 6); */
457 * Search for the nearest place with given type
460 osm_find_nearest_place(node_type_t type, gint lat, gint lon, osm_place **nr)
466 case NODE_PLACE_SUBURB:
469 case NODE_PLACE_CITY:
470 case NODE_PLACE_TOWN:
473 case NODE_PLACE_HAMLET:
474 case NODE_PLACE_VILLAGE:
482 sqlite3_clear_bindings(sql.select_near_place);
483 sqlite3_reset(sql.select_near_place);
485 if (SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 1, lat) ||
486 SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 2, lon) ||
487 SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 3, type) ||
488 SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 4, range)) {
489 g_printerr("Failed to bind values for near place\n");
494 n->isin_p=n->lat=n->lon=n->dist=0;
495 if (SQLITE_ROW == sqlite3_step(sql.select_near_place)) {
499 place=sqlite3_column_text(sql.select_near_place, 0);
500 n->name=g_strdup(place);
501 dist=sqlite3_column_int(sql.select_near_place, 1);
502 n->dist=sqrt((double)dist);
503 n->lat=sqlite3_column_int(sql.select_near_place, 2);
504 n->lon=sqlite3_column_int(sql.select_near_place, 3);
505 n->id=sqlite3_column_int(sql.select_near_place, 4);
506 n->isin_p=sqlite3_column_int(sql.select_near_place, 5);
507 /* n->isin_c=sqlite3_column_int(sql.select_near_place, 6); */
510 osm_place_cache_add(n);
524 osm_find_nearest_way_nodes(gint lat, gint lon, guint range)
531 sqlite3_reset(sql.select_way2);
532 sqlite3_clear_bindings(sql.select_way2);
534 if (SQLITE_OK != sqlite3_bind_int(sql.select_way2, 1, lat) ||
535 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 2, lon) ||
536 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 3, range) ||
537 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 4, WAY_ROAD_START) ||
538 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 5, WAY_ROAD_END)) {
539 g_printerr("Failed to bind values for way\n");
543 #ifdef DEBUG_OSM_TIME
547 while (SQLITE_ROW == sqlite3_step(sql.select_way2)) {
552 w=g_slice_new0(osm_way);
553 w->id=sqlite3_column_int(sql.select_way2, 0);
554 w->type=sqlite3_column_int(sql.select_way2, 1);
555 w->nodecnt=sqlite3_column_int(sql.select_way2, 2);
556 w->flags=sqlite3_column_int(sql.select_way2, 3);
557 dist=sqlite3_column_int(sql.select_way2, 4);
558 w->dist=sqrt((gdouble)dist);
559 w->f=sqlite3_column_int(sql.select_way2, 5);
560 w->t=sqlite3_column_int(sql.select_way2, 6);
562 lat=sqlite3_column_int(sql.select_way2, 7);
563 lon=sqlite3_column_int(sql.select_way2, 8);
565 w->node_f=osm_way_node_new(w->f, lat, lon, 0);
567 ways=g_list_prepend(ways, w);
570 #ifdef DEBUG_OSM_TIME
572 g_printf("Query took: %f sec, found: %d ways\n", g_timer_elapsed(dbt, &tms), wc);
578 /*****************************************************************************/
581 osm_get_route_node(guint nid, osm_node_direction d)
586 sqlite3_stmt *psql=NULL;
590 psql=sql.select_node_next;
593 psql=sql.select_node_prev;
596 g_assert_not_reached();
601 sqlite3_clear_bindings(psql);
603 if (SQLITE_OK != sqlite3_bind_int(psql, 1, nid)) {
604 g_printerr("Failed to bind values for route node\n");
608 while (SQLITE_ROW == sqlite3_step(psql)) {
612 w=g_slice_new0(osm_way);
613 w->id=sqlite3_column_int(psql, 0);
614 w->type=sqlite3_column_int(psql, 1);
615 w->flags=sqlite3_column_int(psql, 2);
616 w->speed=sqlite3_column_int(psql, 3);
618 lat=sqlite3_column_double(psql, 5);
619 lon=sqlite3_column_double(psql, 6);
621 w->f=sqlite3_column_int(psql, 7);
622 w->t=sqlite3_column_int(psql, 8);
625 w->node->flags=sqlite3_column_int(psql, 9);
626 w->node->links=sqlite3_column_int(psql, 10);
629 r=g_slist_prepend(r, w);
636 /*****************************************************************************/
638 osm_way_distance(gint lat, gint lon, osm_way_node *f, osm_way_node *t, gdouble *d)
648 return distance_point_to_line((gdouble)lon, (gdouble)lat, (gdouble)f->lon, (gdouble)f->lat, (gdouble)t->lon, (gdouble)t->lat, d);
652 * Search for the nearest way (road)
653 * - First search for ways with nearest node
654 * - If only one is returned then we are done.
655 * - If more than one, go trough the results:
656 * - Load nodes for the way, check prev/next nodes
657 * - Store result if closer than before
658 * - Return closest way
661 #define START_DIST (900000.0)
664 osm_find_nearest_way(gint lat, gint lon)
668 guint range=OSM_RANGE_START;
670 gdouble pdist=START_DIST, dist_n, dist_p;
672 while ((w=osm_find_nearest_way_nodes(lat, lon, range))==NULL && range<=OSM_RANGE_STOP) {
673 range+=OSM_RANGE_STEP;
674 g_printf("Trying with range: %d\n", range);
677 g_printf("Found %d ways withing range %d\n", g_list_length(w), range);
680 if (g_list_length(w)==0)
683 for (iter=w; iter!=NULL; iter=iter->next) {
686 osm_way *way=(osm_way*)iter->data;
689 g_printf("Way: %d (%d) has %d nodes, nearest is %d,%d\n",
690 way->id, way->type, way->nodecnt, way->f, way->t);
695 wnn=osm_way_get_next_node(way);
696 if (osm_way_distance(lat, lon, way->node_f, wnn, &dist_n)==FALSE) {
697 osm_way_node_free(wnn);
699 } else if (dist_n<pdist) {
702 way->distance=dist_n;
704 g_printf("#1 distance: %f (%f)\n", dist_n, pdist);
707 wnp=osm_way_get_prev_node(way);
708 if (osm_way_distance(lat, lon, way->node_f, wnp, &dist_p)==FALSE) {
709 osm_way_node_free(wnp);
711 } else if (dist_p<pdist) {
714 way->distance=dist_n;
716 osm_way_node_free(wnn);
719 g_printf("#2 distance: %f (%f)\n", dist_p, pdist);
723 g_printf("Found close way, distance: %f %f (%f)\n", dist_n, dist_p, pdist);
736 osm_way_get_name(cw);
737 if (cw->type==WAY_MOTORWAY || cw->type==WAY_TRUNK ||
738 cw->type==WAY_PRIMARY || cw->type==WAY_SECONDARY ||
739 cw->type==WAY_TERTIARY) {
744 g_printf("BEST WAY(%d): %s [%s][%s]\n",
745 cw->id, cw->name, cw->ref, cw->int_ref);
746 g_printf("\tType: %d Flags: %d Nodes: %d Dist: %f\n",
747 cw->type, cw->flags, cw->nodecnt, cw->dist);
748 g_printf("\tNF: %d NT: %d Distance: %f\n",
757 /* XXX: These two should be combined to save memory */
759 * Get previous node/segment of given way node
763 osm_way_get_prev_node(osm_way *w)
765 sqlite3_reset(sql.select_way_prev_seg);
766 sqlite3_clear_bindings(sql.select_way_prev_seg);
768 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_prev_seg, 1, w->id) ||
769 SQLITE_OK != sqlite3_bind_int(sql.select_way_prev_seg, 2, w->f) ) {
770 g_printerr("Failed to bind values for prev seg\n");
774 if (SQLITE_ROW == sqlite3_step(sql.select_way_prev_seg)) {
775 return osm_way_node_new(
776 sqlite3_column_int(sql.select_way_prev_seg, 0),
777 sqlite3_column_int(sql.select_way_prev_seg, 1),
778 sqlite3_column_int(sql.select_way_prev_seg, 2),
786 * Get next node/segment of given way node
790 osm_way_get_next_node(osm_way *w)
792 sqlite3_reset(sql.select_way_next_seg);
793 sqlite3_clear_bindings(sql.select_way_next_seg);
795 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_next_seg, 1, w->id) ||
796 SQLITE_OK != sqlite3_bind_int(sql.select_way_next_seg, 2, w->f) ) {
797 g_printerr("Failed to bind values for next seg\n");
801 if (SQLITE_ROW == sqlite3_step(sql.select_way_next_seg)) {
802 return osm_way_node_new(
803 sqlite3_column_int(sql.select_way_next_seg, 0),
804 sqlite3_column_int(sql.select_way_next_seg, 1),
805 sqlite3_column_int(sql.select_way_next_seg, 2),
813 * Get list of nodes for given way
817 osm_way_get_nodes(osm_way *w)
822 sqlite3_reset(sql.select_way_nodes);
823 sqlite3_clear_bindings(sql.select_way_nodes);
825 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_nodes, 1, w->id)) {
826 g_printerr("Failed to bind values way nodes\n");
830 while (SQLITE_ROW == sqlite3_step(sql.select_way_nodes)) {
833 n=g_slice_new(osm_way_node);
834 n->id=sqlite3_column_int(sql.select_way_nodes, 0);
835 n->lat=sqlite3_column_int(sql.select_way_nodes, 1);
836 n->lon=sqlite3_column_int(sql.select_way_nodes, 2);
837 w->nodes=g_list_append(w->nodes, n);
840 return (w->nodes==NULL) ? FALSE : TRUE;
848 osm_way_get_name(osm_way *w)
850 sqlite3_reset(sql.select_way_name);
851 sqlite3_clear_bindings(sql.select_way_name);
853 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_name, 1, w->id)) {
854 g_printerr("Failed to bind values for way name\n");
858 if (SQLITE_ROW == sqlite3_step(sql.select_way_name)) {
860 place=sqlite3_column_text(sql.select_way_name, 0);
861 w->name=g_strdup(place);
868 osm_way_get_name_nls(osm_way *w)
870 sqlite3_reset(sql.select_way_name_nls);
871 sqlite3_clear_bindings(sql.select_way_name_nls);
873 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_name_nls, 1, w->id) ||
874 SQLITE_OK != sqlite3_bind_) {
875 g_printerr("Failed to bind values for way name nls\n");
879 if (SQLITE_ROW == sqlite3_step(sql.select_way_name_nls)) {
881 place=sqlite3_column_text(sql.select_way_name_nls, 0);
882 w->name=g_strdup(place);
890 * Get Way ref and int_ref
894 osm_way_get_ref(osm_way *w)
896 sqlite3_reset(sql.select_way_ref);
897 sqlite3_clear_bindings(sql.select_way_ref);
899 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_ref, 1, w->id)) {
900 g_printerr("Failed to bind values for way ref\n");
904 if (SQLITE_ROW == sqlite3_step(sql.select_way_ref)) {
905 const gchar *ref, *int_ref;
906 ref=sqlite3_column_text(sql.select_way_ref, 0);
907 int_ref=sqlite3_column_text(sql.select_way_ref, 1);
908 w->ref=g_strdup(ref);
909 w->int_ref=g_strdup(int_ref);
914 /******************************************************************************/
917 * Try to figure out where the given lat,lon is. Fills in the given struct,
918 * with street, secondary (suburb) and primary (city,town,village) location.
919 * Will try to minimize the amount of database access by skipping queries
920 * if we haven't moved or if we don't know where we are.
924 osm_get_location_data(gint lat, gint lon, gfloat heading, osm_location *map_loc)
927 gboolean check_place=FALSE;
930 if (map_loc->valid==FALSE) {
936 d=calculate_idistance(lat,lon,map_loc->lat,map_loc->lon);
939 /* Check if we are still near the same way as last time */
940 if (map_loc->street && osm_way_distance(lat, lon, map_loc->street->node_f, map_loc->street->node_t, &dist)==TRUE) {
941 /* We are probably on the same way as last time */
942 if ( (dist>(gdouble)way_dist_range) || (fabs(heading-map_loc->heading)>10.0)) {
943 /* We have moved a large amount, check way again */
944 g_printf("*** dist %f over range, checking again\n", dist);
945 osm_way_free(map_loc->street);
946 map_loc->street=osm_find_nearest_way(lat, lon);
948 map_loc->changed=TRUE;
951 g_printf("*** No change in location: %f %d\n", dist, way_dist_range);
953 /* We are still on the same way as last time */
955 map_loc->changed=FALSE;
960 /* We didn't know our location, so check it, but only if we have moved */
961 if (d>way_dist_range) {
962 g_print("*** Must check location\n");
964 osm_way_free(map_loc->street);
965 map_loc->street=osm_find_nearest_way(lat, lon);
972 if (!map_loc->street) {
973 g_print("*** Street not known\n");
975 map_loc->changed=TRUE;
977 g_print("*** Street known\n");
979 map_loc->changed=TRUE;
984 if (map_loc->changed==TRUE) {
985 map_loc->heading=heading;
989 g_printf("NFC: %d\n", map_loc->nfcnt);
990 g_printf("D: %ld %ld\n", d,(gulong)way_dist_range);
993 if (check_place==TRUE && d>way_dist_range*4) {
996 fs=osm_find_nearest_place(NODE_PLACE_SUBURB, lat, lon, &map_loc->secondary);
997 if (fs==TRUE && map_loc->secondary && map_loc->secondary->isin_p!=0) {
998 if (osm_place_get(map_loc->secondary->isin_p, lat, lon, &(map_loc->primary))==FALSE) {
999 if (osm_find_nearest_place(NODE_PLACE_CITY, lat, lon, &map_loc->primary)==TRUE)
1000 g_printf("Near city: %s\n", map_loc->primary->name);
1001 else if (osm_find_nearest_place(NODE_PLACE_TOWN, lat, lon, &map_loc->primary)==TRUE)
1002 g_printf("Near town: %s\n", map_loc->primary->name);
1004 g_printf("Unknown\n");
1006 g_printf("In: %s\n", map_loc->primary ? map_loc->primary->name : "?");
1008 } else if (map_loc->street && map_loc->street->isin_p!=0) {
1009 if (osm_place_get(map_loc->street->isin_p, lat, lon, &map_loc->primary)==FALSE) {
1010 g_printf("Street location not know.\n");
1012 g_printf("Street is in: %s\n", map_loc->primary ? map_loc->primary->name : "?");
1015 if (osm_find_nearest_place(NODE_PLACE_CITY, lat, lon, &map_loc->primary)==TRUE)
1016 g_printf("Near city: %s\n", map_loc->primary->name);
1017 else if (osm_find_nearest_place(NODE_PLACE_TOWN, lat, lon, &map_loc->primary)==TRUE)
1018 g_printf("Near town: %s\n", map_loc->primary->name);
1020 g_printf("Unknown\n");
1025 return map_loc->street ? TRUE : FALSE;
1031 * Search for a street(way) starting with given 'text', next given lat/lon
1035 osm_way_search(gdouble lat, gdouble lon, gchar *text, GtkListStore **store)
1040 gchar tmp1[16], tmp2[16];
1043 g_printf("Way Search: [%s] around %.6f %.6f\n", text, lat, lon);
1045 ltext=g_strdup_printf("%s%%", text);
1047 if (SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 1, lat) ||
1048 SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 2, lon) ||
1049 SQLITE_OK != sqlite3_bind_int(sql.select_way_name_search, 3, WAY_ROAD_START) ||
1050 SQLITE_OK != sqlite3_bind_int(sql.select_way_name_search, 4, WAY_ROAD_END) ||
1051 SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 6, range) ||
1052 SQLITE_OK != sqlite3_bind_text(sql.select_way_name_search, 5, ltext, -1, SQLITE_TRANSIENT)) {
1053 g_printerr("Failed to bind values for sql.select_way_name_search\n");
1054 sqlite3_clear_bindings(sql.select_way_name_search);
1062 *store = gtk_list_store_new(ITEM_NUM_COLUMNS,
1063 G_TYPE_INT, /* ID */
1065 G_TYPE_DOUBLE, /* Latitude */
1066 G_TYPE_DOUBLE, /* Longitude */
1067 G_TYPE_DOUBLE, /* Distance */
1068 G_TYPE_STRING, /* Lat/Lon */
1069 G_TYPE_STRING, /* Label */
1070 G_TYPE_STRING, /* Desc. */
1071 G_TYPE_STRING, /* Category */
1072 G_TYPE_STRING, /* Dummy */
1073 G_TYPE_STRING); /* Dummy */
1075 while (SQLITE_ROW == sqlite3_step(sql.select_way_name_search)) {
1076 gdouble rlat, rlon, dist;
1078 rlat=sqlite3_column_double(sql.select_way_name_search, 3);
1079 rlon=sqlite3_column_double(sql.select_way_name_search, 4);
1080 lat_format(_degformat, rlat, tmp1);
1081 lon_format(_degformat, rlon, tmp2);
1082 dist=calculate_distance(lat, lon, rlat, rlon) * UNITS_CONVERT[_units];
1084 gtk_list_store_append(*store, &iter);
1085 gtk_list_store_set(*store, &iter,
1086 ITEM_ID, sqlite3_column_int(sql.select_way_name_search, 0),
1090 ITEM_LATLON, g_strdup_printf("%s, %s", tmp1, tmp2),
1091 ITEM_LABEL, sqlite3_column_text(sql.select_way_name_search, 1),
1096 g_printf("Found: %d items\n", rows);
1098 sqlite3_reset(sql.select_way_name_search);
1099 sqlite3_clear_bindings(sql.select_way_name_search);