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 static GHashTable *place_cache;
76 static guint way_dist_range=OSM_RANGE_WAY;
78 osm_way_node *osm_way_get_prev_node(osm_way *w);
79 osm_way_node *osm_way_get_next_node(osm_way *w);
81 /*****************************************************************************/
84 osm_set_way_range(guint sr)
90 osm_set_way_range_from_speed(gfloat speed)
93 way_dist_range=OSM_RANGE_WAY*2;
95 way_dist_range=OSM_RANGE_WAY-lrint((speed/4)*1000);
98 /*****************************************************************************/
101 osm_progress(void *ud)
105 gtk_progress_bar_pulse(dbpw);
106 gtk_main_iteration_do(FALSE);
111 osm_progress_hide(sqlite3 *db)
115 gtk_progress_bar_set_text(dbpw, "");
116 gtk_progress_bar_set_fraction(dbpw, 0.0);
117 sqlite3_progress_handler(db, OSM_DB_PROGRESS_NUM, NULL, NULL);
121 osm_progress_show(sqlite3 *db)
125 gtk_progress_bar_set_text(dbpw, _("Searching..."));
126 sqlite3_progress_handler(db, OSM_DB_PROGRESS_NUM, osm_progress, NULL);
130 osm_progress_set_widget(sqlite3 *db, GtkProgressBar *w)
132 if (dbpw!=NULL && w==NULL)
133 osm_progress_hide(db);
136 osm_progress_show(db);
139 /*****************************************************************************/
142 osm_db_prepare(sqlite3 *db)
145 /* Select nearest place inside lat,lon+-range */
146 if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as dist,"
147 " ilat,ilon,places.nid,isin_p,isin_c "
148 " from places,nodes where type=$TYPE "
149 " and nodes.nid=places.nid "
150 " and ilat between $LAT-$RANGE and $LAT+$RANGE"
151 " and ilon between $LON-$RANGE and $LON+$RANGE order by dist limit 1",
152 -1, &sql.select_near_place, NULL)!=SQLITE_OK)
155 /* Select place name, distance, location, parent-place and type with given ID */
156 if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as dist,"
157 " ilat,ilon,type,isin_p,isin_c "
158 " from places,nodes where "
159 " nodes.nid=places.nid "
160 " and places.nid=$NID order by dist limit 1",
161 -1, &sql.select_place, NULL)!=SQLITE_OK)
165 /* Select nearest ways inside lat,lon+-range */
166 if (sqlite3_prepare_v2(db, "select w.wid,type,nodes,flags,"
167 "(($LAT-n.ilat)*($LAT-n.ilat))+(($LON-n.ilon)*($LON-n.ilon)) as d,wn.f,wn.t,n.ilat,n.ilon "
168 " from way as w,way_n2n as wn,nodes as n "
169 " where w.wid=wn.wid and wn.f=n.nid "
170 " and n.ilat between $LAT-$RANGE and $LAT+$RANGE "
171 " and n.ilon between $LON-$RANGE and $LON+$RANGE "
172 " and w.type between $WTS and $WTY "
174 -1, &sql.select_way2, NULL)!=SQLITE_OK)
177 if (sqlite3_prepare_v2(db, "select w.wid,w.name as name,"
178 "(($LAT-ww.lat)*($LAT-ww.lat))+(($LON-ww.lon)*($LON-ww.lon)) as d,ww.lat,ww.lon "
179 " from way_names as w,way as ww where "
180 " ww.type between $WTS and $WTY and w.wid=ww.wid and w.name like $NAME "
181 " and ww.lat between $LAT-$RANGE and $LAT+$RANGE "
182 " and ww.lon between $LON-$RANGE and $LON+$RANGE "
184 " select w.wid,n.name as name,"
185 "(($LAT-ww.lat)*($LAT-ww.lat))+(($LON-ww.lon)*($LON-ww.lon)) as d,ww.lat,ww.lon "
186 " from way_names as w, way as ww,way_names_nls as n on w.wid=n.wid where "
187 " ww.type between $WTS and $WTY and w.wid=ww.wid and n.name like $NAME "
188 " and ww.lat between $LAT-$RANGE and $LAT+$RANGE "
189 " and ww.lon between $LON-$RANGE and $LON+$RANGE "
190 " order by name limit 100",
191 -1, &sql.select_way_name_search, NULL)!=SQLITE_OK)
194 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",
195 -1, &sql.select_way_next_seg, NULL)!=SQLITE_OK)
198 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",
199 -1, &sql.select_way_prev_seg, NULL)!=SQLITE_OK)
203 if (sqlite3_prepare_v2(db, "select name from way_names where wid=?", -1, &sql.select_way_name, NULL)!=SQLITE_OK)
206 /* Way ref and int_ref */
207 if (sqlite3_prepare_v2(db, "select ref,int_ref from way_ref where rid=?", -1, &sql.select_way_ref, NULL)!=SQLITE_OK)
210 /* Get next, prev node + way information. For routing. */
211 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 "
212 "from way as w, nodes as n, way_n2n as nn where w.wid=nn.wid and nn.f=n.nid and nn.f=?",
213 -1, &sql.select_node_next, NULL)!=SQLITE_OK)
216 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 "
217 "from way as w, nodes as n, way_n2n as nn where w.wid=nn.wid and nn.f=n.nid and nn.t=?",
218 -1, &sql.select_node_prev, NULL)!=SQLITE_OK)
228 sqlite3_finalize(sql.select_way_ref);
229 sqlite3_finalize(sql.select_way_name);
230 sqlite3_finalize(sql.select_way_next_seg);
231 sqlite3_finalize(sql.select_way_prev_seg);
232 sqlite3_finalize(sql.select_way_name_search);
233 sqlite3_finalize(sql.select_way2);
234 sqlite3_finalize(sql.select_place);
235 sqlite3_finalize(sql.select_near_place);
239 g_hash_table_destroy(place_cache);
240 g_timer_destroy(dbt);
244 osm_init(sqlite3 **db)
247 place_cache=g_hash_table_new(g_direct_hash, g_direct_equal);
256 if (osm_db_prepare(osmdb)==FALSE) {
257 g_printerr("Failed to prepare OSM SQL statements:");
258 g_printf("SQLITE: %s\n", sqlite3_errmsg(osmdb));
265 /*****************************************************************************/
268 osm_way_node_new(guint id, gint lat, gint lon, gint flags)
270 osm_way_node *n=g_slice_new(osm_way_node);
280 osm_way_node_free(osm_way_node *n)
283 g_slice_free(osm_way_node, n);
287 * Free way nodes list
290 osm_way_nodes_free(osm_way *w)
297 for (iter=w->nodes; iter!=NULL; iter=iter->next)
298 g_slice_free(osm_way_node, (osm_way_node*)iter->data);
300 g_list_free(w->nodes);
304 * Free a osm_way structure
307 osm_way_free(osm_way *w)
311 osm_way_nodes_free(w);
318 g_slice_free(osm_way, w);
321 /*****************************************************************************/
324 osm_place_free(osm_place *p)
328 g_slice_free(osm_place, p);
332 osm_place_remove(gpointer k, gpointer v, gpointer ud)
334 osm_place_free((osm_place *)v);
341 return g_slice_new0(osm_place);
345 osm_place_cache_lookup(guint32 id)
347 return g_hash_table_lookup(place_cache, GINT_TO_POINTER(id));
351 osm_place_cache_add(osm_place *p)
353 if (osm_place_cache_lookup(p->id)==NULL)
354 g_hash_table_insert(place_cache, GINT_TO_POINTER(p->id), p);
358 osm_place_cache_gc(void)
361 r=g_hash_table_foreach_remove(place_cache, osm_place_remove, NULL);
365 osm_place_update_distance(osm_place *p, gint lat, gint lon)
369 lam=(gdouble)((lat-p->lat)*(lat-p->lat));
370 lom=(gdouble)((lon-p->lon)*(lon-p->lon));
372 p->dist=sqrt(lam+lom);
376 * Get place with given id and distance to current location
379 osm_place_get(guint32 id, gint lat, gint lon, osm_place **nr)
384 n=osm_place_cache_lookup(id);
387 osm_place_update_distance(n, lat, lon);
392 /* XXX: better place for this */
393 if (g_hash_table_size(place_cache)>OSM_PLACE_CACHE_MAX_ITEMS)
394 osm_place_cache_gc();
396 sqlite3_clear_bindings(sql.select_place);
397 sqlite3_reset(sql.select_place);
399 if (SQLITE_OK != sqlite3_bind_int(sql.select_place, 1, lat) ||
400 SQLITE_OK != sqlite3_bind_int(sql.select_place, 2, lon) ||
401 SQLITE_OK != sqlite3_bind_int(sql.select_place, 3, id)) {
402 g_printerr("Failed to bind values for place\n");
406 if (SQLITE_ROW == sqlite3_step(sql.select_place)) {
411 place=sqlite3_column_text(sql.select_place, 0);
412 n->name=g_strdup(place);
413 dist=sqlite3_column_int(sql.select_place, 1);
414 n->dist=sqrt((double)dist);
415 n->lat=sqlite3_column_int(sql.select_place, 2);
416 n->lon=sqlite3_column_int(sql.select_place, 3);
417 n->type=sqlite3_column_int(sql.select_place, 4);
418 n->isin_p=sqlite3_column_int(sql.select_place, 5);
419 /* n->isin_c=sqlite3_column_int(sql.select_place, 6); */
426 * Search for the nearest place with given type
429 osm_find_nearest_place(node_type_t type, gint lat, gint lon, osm_place **nr)
435 case NODE_PLACE_SUBURB:
438 case NODE_PLACE_CITY:
439 case NODE_PLACE_TOWN:
442 case NODE_PLACE_HAMLET:
443 case NODE_PLACE_VILLAGE:
451 sqlite3_clear_bindings(sql.select_near_place);
452 sqlite3_reset(sql.select_near_place);
454 if (SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 1, lat) ||
455 SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 2, lon) ||
456 SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 3, type) ||
457 SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 4, range)) {
458 g_printerr("Failed to bind values for near place\n");
463 n->isin_p=n->lat=n->lon=n->dist=0;
464 if (SQLITE_ROW == sqlite3_step(sql.select_near_place)) {
468 place=sqlite3_column_text(sql.select_near_place, 0);
469 n->name=g_strdup(place);
470 dist=sqlite3_column_int(sql.select_near_place, 1);
471 n->dist=sqrt((double)dist);
472 n->lat=sqlite3_column_int(sql.select_near_place, 2);
473 n->lon=sqlite3_column_int(sql.select_near_place, 3);
474 n->id=sqlite3_column_int(sql.select_near_place, 4);
475 n->isin_p=sqlite3_column_int(sql.select_near_place, 5);
476 /* n->isin_c=sqlite3_column_int(sql.select_near_place, 6); */
479 osm_place_cache_add(n);
493 osm_find_nearest_way_nodes(gint lat, gint lon, guint range)
500 sqlite3_reset(sql.select_way2);
501 sqlite3_clear_bindings(sql.select_way2);
503 if (SQLITE_OK != sqlite3_bind_int(sql.select_way2, 1, lat) ||
504 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 2, lon) ||
505 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 3, range) ||
506 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 4, WAY_ROAD_START) ||
507 SQLITE_OK != sqlite3_bind_int(sql.select_way2, 5, WAY_ROAD_END)) {
508 g_printerr("Failed to bind values for way\n");
512 #ifdef DEBUG_OSM_TIME
516 while (SQLITE_ROW == sqlite3_step(sql.select_way2)) {
521 w=g_slice_new0(osm_way);
522 w->id=sqlite3_column_int(sql.select_way2, 0);
523 w->type=sqlite3_column_int(sql.select_way2, 1);
524 w->nodecnt=sqlite3_column_int(sql.select_way2, 2);
525 w->flags=sqlite3_column_int(sql.select_way2, 3);
526 dist=sqlite3_column_int(sql.select_way2, 4);
527 w->dist=sqrt((gdouble)dist);
528 w->f=sqlite3_column_int(sql.select_way2, 5);
529 w->t=sqlite3_column_int(sql.select_way2, 6);
531 lat=sqlite3_column_int(sql.select_way2, 7);
532 lon=sqlite3_column_int(sql.select_way2, 8);
534 w->node_f=osm_way_node_new(w->f, lat, lon, 0);
536 ways=g_list_prepend(ways, w);
539 #ifdef DEBUG_OSM_TIME
541 g_printf("Query took: %f sec, found: %d ways\n", g_timer_elapsed(dbt, &tms), wc);
547 /*****************************************************************************/
550 osm_get_route_node(guint nid, osm_node_direction d)
555 sqlite3_stmt *psql=NULL;
559 psql=sql.select_node_next;
562 psql=sql.select_node_prev;
565 g_assert_not_reached();
570 sqlite3_clear_bindings(psql);
572 if (SQLITE_OK != sqlite3_bind_int(psql, 1, nid)) {
573 g_printerr("Failed to bind values for route node\n");
577 while (SQLITE_ROW == sqlite3_step(psql)) {
581 w=g_slice_new0(osm_way);
582 w->id=sqlite3_column_int(psql, 0);
583 w->type=sqlite3_column_int(psql, 1);
584 w->flags=sqlite3_column_int(psql, 2);
585 w->speed=sqlite3_column_int(psql, 3);
587 lat=sqlite3_column_double(psql, 5);
588 lon=sqlite3_column_double(psql, 6);
590 w->f=sqlite3_column_int(psql, 7);
591 w->t=sqlite3_column_int(psql, 8);
594 w->node->flags=sqlite3_column_int(psql, 9);
595 w->node->links=sqlite3_column_int(psql, 10);
598 r=g_slist_prepend(r, w);
605 /*****************************************************************************/
607 osm_way_distance(gint lat, gint lon, osm_way_node *f, osm_way_node *t, gdouble *d)
617 return distance_point_to_line((gdouble)lon, (gdouble)lat, (gdouble)f->lon, (gdouble)f->lat, (gdouble)t->lon, (gdouble)t->lat, d);
621 * Search for the nearest way (road)
622 * - First search for ways with nearest node
623 * - If only one is returned then we are done.
624 * - If more than one, go trough the results:
625 * - Load nodes for the way, check prev/next nodes
626 * - Store result if closer than before
627 * - Return closest way
630 #define START_DIST (900000.0)
633 osm_find_nearest_way(gint lat, gint lon)
637 guint range=OSM_RANGE_START;
639 gdouble pdist=START_DIST, dist_n, dist_p;
641 while ((w=osm_find_nearest_way_nodes(lat, lon, range))==NULL && range<=OSM_RANGE_STOP) {
642 range+=OSM_RANGE_STEP;
643 g_printf("Trying with range: %d\n", range);
646 g_printf("Found %d ways withing range %d\n", g_list_length(w), range);
649 if (g_list_length(w)==0)
652 for (iter=w; iter!=NULL; iter=iter->next) {
655 osm_way *way=(osm_way*)iter->data;
658 g_printf("Way: %d (%d) has %d nodes, nearest is %d,%d\n",
659 way->id, way->type, way->nodecnt, way->f, way->t);
664 wnn=osm_way_get_next_node(way);
665 if (osm_way_distance(lat, lon, way->node_f, wnn, &dist_n)==FALSE) {
666 osm_way_node_free(wnn);
668 } else if (dist_n<pdist) {
671 way->distance=dist_n;
673 g_printf("#1 distance: %f (%f)\n", dist_n, pdist);
676 wnp=osm_way_get_prev_node(way);
677 if (osm_way_distance(lat, lon, way->node_f, wnp, &dist_p)==FALSE) {
678 osm_way_node_free(wnp);
680 } else if (dist_p<pdist) {
683 way->distance=dist_n;
685 osm_way_node_free(wnn);
688 g_printf("#2 distance: %f (%f)\n", dist_p, pdist);
692 g_printf("Found close way, distance: %f %f (%f)\n", dist_n, dist_p, pdist);
705 osm_way_get_name(cw);
706 if (cw->type==WAY_MOTORWAY || cw->type==WAY_TRUNK ||
707 cw->type==WAY_PRIMARY || cw->type==WAY_SECONDARY ||
708 cw->type==WAY_TERTIARY) {
713 g_printf("BEST WAY(%d): %s [%s][%s]\n",
714 cw->id, cw->name, cw->ref, cw->int_ref);
715 g_printf("\tType: %d Flags: %d Nodes: %d Dist: %f\n",
716 cw->type, cw->flags, cw->nodecnt, cw->dist);
717 g_printf("\tNF: %d NT: %d Distance: %f\n",
726 /* XXX: These two should be combined to save memory */
728 * Get previous node/segment of given way node
732 osm_way_get_prev_node(osm_way *w)
734 sqlite3_reset(sql.select_way_prev_seg);
735 sqlite3_clear_bindings(sql.select_way_prev_seg);
737 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_prev_seg, 1, w->id) ||
738 SQLITE_OK != sqlite3_bind_int(sql.select_way_prev_seg, 2, w->f) ) {
739 g_printerr("Failed to bind values for prev seg\n");
743 if (SQLITE_ROW == sqlite3_step(sql.select_way_prev_seg)) {
744 return osm_way_node_new(
745 sqlite3_column_int(sql.select_way_prev_seg, 0),
746 sqlite3_column_int(sql.select_way_prev_seg, 1),
747 sqlite3_column_int(sql.select_way_prev_seg, 2),
755 * Get next node/segment of given way node
759 osm_way_get_next_node(osm_way *w)
761 sqlite3_reset(sql.select_way_next_seg);
762 sqlite3_clear_bindings(sql.select_way_next_seg);
764 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_next_seg, 1, w->id) ||
765 SQLITE_OK != sqlite3_bind_int(sql.select_way_next_seg, 2, w->f) ) {
766 g_printerr("Failed to bind values for next seg\n");
770 if (SQLITE_ROW == sqlite3_step(sql.select_way_next_seg)) {
771 return osm_way_node_new(
772 sqlite3_column_int(sql.select_way_next_seg, 0),
773 sqlite3_column_int(sql.select_way_next_seg, 1),
774 sqlite3_column_int(sql.select_way_next_seg, 2),
782 * Get list of nodes for given way
786 osm_way_get_nodes(osm_way *w)
791 sqlite3_reset(sql.select_way_nodes);
792 sqlite3_clear_bindings(sql.select_way_nodes);
794 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_nodes, 1, w->id)) {
795 g_printerr("Failed to bind values way nodes\n");
799 while (SQLITE_ROW == sqlite3_step(sql.select_way_nodes)) {
802 n=g_slice_new(osm_way_node);
803 n->id=sqlite3_column_int(sql.select_way_nodes, 0);
804 n->lat=sqlite3_column_int(sql.select_way_nodes, 1);
805 n->lon=sqlite3_column_int(sql.select_way_nodes, 2);
806 w->nodes=g_list_append(w->nodes, n);
809 return (w->nodes==NULL) ? FALSE : TRUE;
817 osm_way_get_name(osm_way *w)
819 sqlite3_reset(sql.select_way_name);
820 sqlite3_clear_bindings(sql.select_way_name);
822 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_name, 1, w->id)) {
823 g_printerr("Failed to bind values for way name\n");
827 if (SQLITE_ROW == sqlite3_step(sql.select_way_name)) {
829 place=sqlite3_column_text(sql.select_way_name, 0);
830 w->name=g_strdup(place);
837 osm_way_get_name_nls(osm_way *w)
839 sqlite3_reset(sql.select_way_name_nls);
840 sqlite3_clear_bindings(sql.select_way_name_nls);
842 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_name_nls, 1, w->id) ||
843 SQLITE_OK != sqlite3_bind_) {
844 g_printerr("Failed to bind values for way name nls\n");
848 if (SQLITE_ROW == sqlite3_step(sql.select_way_name_nls)) {
850 place=sqlite3_column_text(sql.select_way_name_nls, 0);
851 w->name=g_strdup(place);
859 * Get Way ref and int_ref
863 osm_way_get_ref(osm_way *w)
865 sqlite3_reset(sql.select_way_ref);
866 sqlite3_clear_bindings(sql.select_way_ref);
868 if (SQLITE_OK != sqlite3_bind_int(sql.select_way_ref, 1, w->id)) {
869 g_printerr("Failed to bind values for way ref\n");
873 if (SQLITE_ROW == sqlite3_step(sql.select_way_ref)) {
874 const gchar *ref, *int_ref;
875 ref=sqlite3_column_text(sql.select_way_ref, 0);
876 int_ref=sqlite3_column_text(sql.select_way_ref, 1);
877 w->ref=g_strdup(ref);
878 w->int_ref=g_strdup(int_ref);
883 /******************************************************************************/
886 * Try to figure out where the given lat,lon is. Fills in the given struct,
887 * with street, secondary (suburb) and primary (city,town,village) location.
888 * Will try to minimize the amount of database access by skipping queries
889 * if we haven't moved or if we don't know where we are.
893 osm_get_location_data(gint lat, gint lon, gfloat heading, osm_location *map_loc)
896 gboolean check_place=FALSE;
899 if (map_loc->valid==FALSE) {
905 d=calculate_idistance(lat,lon,map_loc->lat,map_loc->lon);
908 /* Check if we are still near the same way as last time */
909 if (map_loc->street && osm_way_distance(lat, lon, map_loc->street->node_f, map_loc->street->node_t, &dist)==TRUE) {
910 /* We are probably on the same way as last time */
911 if ( (dist>(gdouble)way_dist_range) || (fabs(heading-map_loc->heading)>10.0)) {
912 /* We have moved a large amount, check way again */
913 g_printf("*** dist %f over range, checking again\n", dist);
914 osm_way_free(map_loc->street);
915 map_loc->street=osm_find_nearest_way(lat, lon);
917 map_loc->changed=TRUE;
920 g_printf("*** No change in location: %f %d\n", dist, way_dist_range);
922 /* We are still on the same way as last time */
924 map_loc->changed=FALSE;
929 /* We didn't know our location, so check it, but only if we have moved */
930 if (d>way_dist_range) {
931 g_print("*** Must check location\n");
933 osm_way_free(map_loc->street);
934 map_loc->street=osm_find_nearest_way(lat, lon);
941 if (!map_loc->street) {
942 g_print("*** Street not known\n");
944 map_loc->changed=TRUE;
946 g_print("*** Street known\n");
948 map_loc->changed=TRUE;
953 if (map_loc->changed==TRUE) {
954 map_loc->heading=heading;
958 g_printf("NFC: %d\n", map_loc->nfcnt);
959 g_printf("D: %ld %ld\n", d,(gulong)way_dist_range);
962 if (check_place==TRUE && d>way_dist_range*4) {
965 fs=osm_find_nearest_place(NODE_PLACE_SUBURB, lat, lon, &map_loc->secondary);
966 if (fs==TRUE && map_loc->secondary && map_loc->secondary->isin_p!=0) {
967 if (osm_place_get(map_loc->secondary->isin_p, lat, lon, &(map_loc->primary))==FALSE) {
968 if (osm_find_nearest_place(NODE_PLACE_CITY, lat, lon, &map_loc->primary)==TRUE)
969 g_printf("Near city: %s\n", map_loc->primary->name);
970 else if (osm_find_nearest_place(NODE_PLACE_TOWN, lat, lon, &map_loc->primary)==TRUE)
971 g_printf("Near town: %s\n", map_loc->primary->name);
973 g_printf("Unknown\n");
975 g_printf("In: %s\n", map_loc->primary ? map_loc->primary->name : "?");
977 } else if (map_loc->street && map_loc->street->isin_p!=0) {
978 if (osm_place_get(map_loc->street->isin_p, lat, lon, &map_loc->primary)==FALSE) {
979 g_printf("Street location not know.\n");
981 g_printf("Street is in: %s\n", map_loc->primary ? map_loc->primary->name : "?");
984 if (osm_find_nearest_place(NODE_PLACE_CITY, lat, lon, &map_loc->primary)==TRUE)
985 g_printf("Near city: %s\n", map_loc->primary->name);
986 else if (osm_find_nearest_place(NODE_PLACE_TOWN, lat, lon, &map_loc->primary)==TRUE)
987 g_printf("Near town: %s\n", map_loc->primary->name);
989 g_printf("Unknown\n");
994 return map_loc->street ? TRUE : FALSE;
1000 * Search for a street(way) starting with given 'text', next given lat/lon
1004 osm_way_search(gdouble lat, gdouble lon, gchar *text, GtkListStore **store)
1009 gchar tmp1[16], tmp2[16];
1012 g_printf("Way Search: [%s] around %.6f %.6f\n", text, lat, lon);
1014 ltext=g_strdup_printf("%s%%", text);
1016 if (SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 1, lat) ||
1017 SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 2, lon) ||
1018 SQLITE_OK != sqlite3_bind_int(sql.select_way_name_search, 3, WAY_ROAD_START) ||
1019 SQLITE_OK != sqlite3_bind_int(sql.select_way_name_search, 4, WAY_ROAD_END) ||
1020 SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 6, range) ||
1021 SQLITE_OK != sqlite3_bind_text(sql.select_way_name_search, 5, ltext, -1, SQLITE_TRANSIENT)) {
1022 g_printerr("Failed to bind values for sql.select_way_name_search\n");
1023 sqlite3_clear_bindings(sql.select_way_name_search);
1031 *store = gtk_list_store_new(ITEM_NUM_COLUMNS,
1032 G_TYPE_INT, /* ID */
1034 G_TYPE_DOUBLE, /* Latitude */
1035 G_TYPE_DOUBLE, /* Longitude */
1036 G_TYPE_DOUBLE, /* Distance */
1037 G_TYPE_STRING, /* Lat/Lon */
1038 G_TYPE_STRING, /* Label */
1039 G_TYPE_STRING, /* Desc. */
1040 G_TYPE_STRING, /* Category */
1041 G_TYPE_STRING, /* Dummy */
1042 G_TYPE_STRING); /* Dummy */
1044 while (SQLITE_ROW == sqlite3_step(sql.select_way_name_search)) {
1045 gdouble rlat, rlon, dist;
1047 rlat=sqlite3_column_double(sql.select_way_name_search, 3);
1048 rlon=sqlite3_column_double(sql.select_way_name_search, 4);
1049 lat_format(_degformat, rlat, tmp1);
1050 lon_format(_degformat, rlon, tmp2);
1051 dist=calculate_distance(lat, lon, rlat, rlon) * UNITS_CONVERT[_units];
1053 gtk_list_store_append(*store, &iter);
1054 gtk_list_store_set(*store, &iter,
1055 ITEM_ID, sqlite3_column_int(sql.select_way_name_search, 0),
1059 ITEM_LATLON, g_strdup_printf("%s, %s", tmp1, tmp2),
1060 ITEM_LABEL, sqlite3_column_text(sql.select_way_name_search, 1),
1065 g_printf("Found: %d items\n", rows);
1067 sqlite3_reset(sql.select_way_name_search);
1068 sqlite3_clear_bindings(sql.select_way_name_search);