#include <math.h>
#include <glib.h>
#include <glib/gstdio.h>
+#include <gtk/gtk.h>
#include <sqlite3.h>
#include "osm.h"
#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;
};
static struct sql_select_stmt sql;
static GTimer *dbt;
+static GtkProgressBar *dbpw=NULL;
/* Cache hash tables */
static GHashTable *_place_cache;
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,"
" 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 "
/* 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;
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
{
n=osm_place_cache_lookup(id);
if (n) {
- g_printf("*** Place cache hit!\n");
osm_place_update_distance(n, lat, lon);
return TRUE;
}
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;
}
{
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;
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;
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;
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);