From 33f44e5b2d38571f32cba5b3472f60a75273333b Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Tue, 7 Aug 2007 16:32:34 +0300 Subject: [PATCH] Add support for a progress bar in case query takes much time. --- src/osm-db.c | 72 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/src/osm-db.c b/src/osm-db.c index 909b5b4..c09592e 100644 --- a/src/osm-db.c +++ b/src/osm-db.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "osm.h" @@ -15,7 +16,9 @@ #include "osm-db.h" /* #define DEBUG_OSM */ -#define OSM_PLACE_CACHE_MAX_ITEMS 40 +#define OSM_PLACE_CACHE_MAX_ITEMS (40) +#define OSM_DB_PROGRESS_NUM (3000) +#define OSM_START_RANGE (8192) struct sql_select_stmt { sqlite3_stmt *select_way; @@ -31,6 +34,7 @@ struct sql_select_stmt { }; static struct sql_select_stmt sql; static GTimer *dbt; +static GtkProgressBar *dbpw=NULL; /* Cache hash tables */ static GHashTable *_place_cache; @@ -43,17 +47,44 @@ osm_way_node *osm_way_get_next_node(osm_way *w); static int osm_progress(void *ud) { -g_print("."); +if (!dbpw) + return 0; +gtk_progress_bar_pulse(dbpw); +gtk_main_iteration_do(FALSE); return 0; } +static void +osm_progress_hide(sqlite3 *db) +{ +if (!dbpw) + return; +gtk_widget_hide(GTK_WIDGET(dbpw)); +sqlite3_progress_handler(db, OSM_DB_PROGRESS_NUM, NULL, NULL); +} + +static void +osm_progress_show(sqlite3 *db) +{ +if (!dbpw) + return; +gtk_widget_show(GTK_WIDGET(dbpw)); +sqlite3_progress_handler(db, OSM_DB_PROGRESS_NUM, osm_progress, NULL); +} + +void +osm_progress_set_widget(sqlite3 *db, GtkProgressBar *w) +{ +if (dbpw!=NULL && w==NULL) + osm_progress_hide(db); +dbpw=w; +if (w!=NULL) + osm_progress_show(db); +} + gboolean osm_db_prepare(sqlite3 *db) { -#ifdef DEBUG_OSM -sqlite3_progress_handler(db, 1000, osm_progress, NULL); -#endif - /* Place */ /* Select nearest place inside lat,lon+-range */ if (sqlite3_prepare_v2(db, "select name,(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LON-lon)) as dist," @@ -64,8 +95,8 @@ if (sqlite3_prepare_v2(db, "select name,(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LO " and lon between $LON-$RANGE and $LON+$RANGE order by dist limit 1", -1, &sql.select_near_place, NULL)!=SQLITE_OK) return FALSE; -/* Select place name, distance, location, parent-place and type with given ID */ +/* Select place name, distance, location, parent-place and type with given ID */ if (sqlite3_prepare_v2(db, "select name,(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LON-lon)) as dist," " lat,lon,type,isin " " from places,nodes where " @@ -77,13 +108,13 @@ if (sqlite3_prepare_v2(db, "select name,(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LO /* Ways */ /* Select neareset ways inside lat,lon+-range */ if (sqlite3_prepare_v2(db, "select wid,type,nodes,flags," - "(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LON-lon)) as dist,f,t,lat,lon " - " from way,way_s2s,nodes" - " where wid=wsid and way_s2s.f=nodes.nid " - " and lat between $LAT-$RANGE and $LAT+$RANGE " - " and lon between $LON-$RANGE and $LON+$RANGE " - " and type between $WTS and $WTY " - " order by dist", + "(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LON-lon)) as d,f,t,lat,lon " + " from way as w,way_s2s as ws,nodes as n " + " where w.wid=ws.wsid and ws.f=n.nid " + " and n.lat between $LAT-$RANGE and $LAT+$RANGE " + " and n.lon between $LON-$RANGE and $LON+$RANGE " + " and w.type between $WTS and $WTY " + " order by d", -1, &sql.select_way2, NULL)!=SQLITE_OK) return FALSE; @@ -219,9 +250,7 @@ static void osm_place_cache_gc(void) { gint r; -g_printf("*** Clearing place cache\n"); r=g_hash_table_foreach_remove(_place_cache, osm_place_remove, NULL); -g_printf("*** Removed %d items\n", r); } static void @@ -243,7 +272,6 @@ osm_place_get(guint32 id, gint lat, gint lon, osm_place *n) { n=osm_place_cache_lookup(id); if (n) { - g_printf("*** Place cache hit!\n"); osm_place_update_distance(n, lat, lon); return TRUE; } @@ -484,12 +512,10 @@ gboolean osm_way_distance(gint lat, gint lon, osm_way_node *f, osm_way_node *t, gdouble *d) { if (!f) { - g_printerr("## From point missing\n"); return FALSE; } if (!t) { - g_printerr("## To point missing\n"); return FALSE; } @@ -513,7 +539,7 @@ osm_find_nearest_way(gint lat, gint lon) { GList *iter; GList *w=NULL; -guint range=4096; +guint range=OSM_START_RANGE; osm_way *cw=NULL; gdouble pdist=START_DIST, dist_n, dist_p; @@ -522,7 +548,9 @@ while ((w=osm_find_nearest_way_nodes(lat, lon, range))==NULL && range<=65536) { g_printf("Trying with range: %d\n", range); } +#ifdef DEBUG_OSM g_printf("Found ways: %d\n", g_list_length(w)); +#endif if (g_list_length(w)==0) return NULL; @@ -532,8 +560,10 @@ for (iter=w; iter!=NULL; iter=iter->next) { osm_way_node *wnp; osm_way *way=(osm_way*)iter->data; +#ifdef DEBUG_OSM g_printf("WAY %d (%d) HAS %d NODES, nearest is %d\n", way->id, way->type, way->nodecnt, way->f); +#endif way->node_t=NULL; @@ -564,7 +594,9 @@ for (iter=w; iter!=NULL; iter=iter->next) { g_printf("#2 distance: %f (%f)\n", dist_p, pdist); } +#ifdef DEBUG_OSM g_printf("Found close way, distance: %f %f (%f)\n", dist_n, dist_p, pdist); +#endif if (!cw) { osm_way_free(way); -- 2.39.5