]> err.no Git - mapper/blobdiff - src/osm-db.c
Fixes to gstreamer element and caps handlings.
[mapper] / src / osm-db.c
index 566b25d6e934ea989bc1ee58e9ab4414c639b263..25f7d49c6ca853623e79389efda8ff908e01475b 100644 (file)
 #include <math.h>
 #include <glib.h>
 #include <glib/gstdio.h>
+#include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <sqlite3.h>
 
 #include "osm.h"
 #include "latlon.h"
-#include "gps.h"
 #include "osm-db.h"
 #include "settings.h"
 
@@ -39,7 +39,7 @@
 /* #define DEBUG_OSM_TIME */
 #define OSM_PLACE_CACHE_MAX_ITEMS (64)
 
-#define OSM_DB_PROGRESS_NUM (20000)
+#define OSM_DB_PROGRESS_NUM (15000)
 
 /* Node search ranges */
 #define OSM_RANGE_START (16384)
@@ -61,7 +61,8 @@ struct sql_select_stmt {
        sqlite3_stmt *select_way_name_search;
        sqlite3_stmt *select_way_ref;
        sqlite3_stmt *select_place;
-       sqlite3_stmt *select_near_place;
+       sqlite3_stmt *select_place_near;
+       sqlite3_stmt *select_place_search;
 
        sqlite3_stmt *select_node_next;
        sqlite3_stmt *select_node_prev;
@@ -71,9 +72,15 @@ static GTimer *dbt;
 static GtkProgressBar *dbpw=NULL;
 
 /* Cache hash tables */
-static GHashTable *place_cache;
+struct osm_place_cache {
+       GHashTable *cache;
+       guint hit;
+       guint miss;
+};
+static struct osm_place_cache pcache;
 
 static guint way_dist_range=OSM_RANGE_WAY;
+static gint sid=0;
 
 osm_way_node *osm_way_get_prev_node(osm_way *w);
 osm_way_node *osm_way_get_next_node(osm_way *w);
@@ -90,19 +97,25 @@ void
 osm_set_way_range_from_speed(gfloat speed)
 {
 if (speed>54.0)
-       way_dist_range=OSM_RANGE_WAY*2;
+       way_dist_range=OSM_RANGE_WAY;
 else
        way_dist_range=OSM_RANGE_WAY-lrint((speed/4)*1000);
 }
 
 /*****************************************************************************/
 
-static int
-osm_progress(void *ud)
+static gboolean
+osm_progress_pulse(void)
 {
 if (!dbpw)
-       return 0;
+       return FALSE;
 gtk_progress_bar_pulse(dbpw);
+return TRUE;
+}
+
+static int
+osm_progress(void *ud)
+{
 gtk_main_iteration_do(FALSE);
 return 0;
 }
@@ -123,17 +136,27 @@ osm_progress_show(sqlite3 *db)
 if (!dbpw)
        return;
 gtk_progress_bar_set_text(dbpw, _("Searching..."));
+gtk_progress_bar_pulse(dbpw);
+gtk_main_iteration_do(FALSE);
 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)
+if (dbpw!=NULL && w==NULL) {
        osm_progress_hide(db);
+       if (sid!=0)
+               g_source_remove(sid);
+       sid=0;
+       dbpw=NULL;
+       return;
+}
 dbpw=w;
-if (w!=NULL)
+if (w!=NULL) {
        osm_progress_show(db);
+       sid=g_timeout_add(200, osm_progress_pulse, NULL);
+}
 }
 
 /*****************************************************************************/
@@ -141,26 +164,34 @@ if (w!=NULL)
 gboolean
 osm_db_prepare(sqlite3 *db)
 {
-/* Place */
 /* Select nearest place inside lat,lon+-range */
-if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as dist,"
+if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
                                        " ilat,ilon,places.nid,isin_p,isin_c "
                                        " from places,nodes where type=$TYPE "
                                        " and nodes.nid=places.nid "
                                        " and ilat between $LAT-$RANGE and $LAT+$RANGE"
-                                       " and ilon between $LON-$RANGE and $LON+$RANGE order by dist limit 1",
-                   -1, &sql.select_near_place, NULL)!=SQLITE_OK)
+                                       " and ilon between $LON-$RANGE and $LON+$RANGE order by d limit 1",
+                   -1, &sql.select_place_near, NULL)!=SQLITE_OK)
        return FALSE;
 
 /* Select place name, distance, location, parent-place and type with given ID */
-if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as dist,"
+if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
                                        " ilat,ilon,type,isin_p,isin_c "
                                        " from places,nodes where "
                                        " nodes.nid=places.nid "
-                                       " and places.nid=$NID order by dist limit 1",
+                                       " and places.nid=$NID order by d limit 1",
                    -1, &sql.select_place, NULL)!=SQLITE_OK)
        return FALSE;
 
+/* Search */
+if (sqlite3_prepare_v2(db, "select places.nid,name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as d,"
+                                       " rlat,rlon,type,isin_p,isin_c "
+                                       " from places,nodes where "
+                                       " nodes.nid=places.nid "
+                                       " and name like $NAME order by d limit 200",
+                   -1, &sql.select_place_search, NULL)!=SQLITE_OK)
+       return FALSE;
+
 /* Ways */
 /* Select nearest ways inside lat,lon+-range */
 if (sqlite3_prepare_v2(db, "select w.wid,type,nodes,flags,"
@@ -187,7 +218,7 @@ if (sqlite3_prepare_v2(db, "select w.wid,w.name as name,"
                                        " ww.type between $WTS and $WTY and w.wid=ww.wid and n.name like $NAME "
                                        " and ww.lat between $LAT-$RANGE and $LAT+$RANGE "
                                        " and ww.lon between $LON-$RANGE and $LON+$RANGE "
-                                       " order by name limit 100",
+                                       " order by name limit 200",
                        -1, &sql.select_way_name_search, NULL)!=SQLITE_OK)
        return FALSE;
 
@@ -224,19 +255,34 @@ return TRUE;
 void
 osm_deinit(void)
 {
-if (osmdb!=NULL) {
-       sqlite3_finalize(sql.select_way_ref);
-       sqlite3_finalize(sql.select_way_name);
-       sqlite3_finalize(sql.select_way_next_seg);
-       sqlite3_finalize(sql.select_way_prev_seg);
-       sqlite3_finalize(sql.select_way_name_search);
-       sqlite3_finalize(sql.select_way2);
-       sqlite3_finalize(sql.select_place);
-       sqlite3_finalize(sql.select_near_place);
+if (osmdb) {
+       if (sql.select_way_ref)
+               sqlite3_finalize(sql.select_way_ref);
+       if (sql.select_way_name)
+               sqlite3_finalize(sql.select_way_name);
+       if (sql.select_way_next_seg)
+               sqlite3_finalize(sql.select_way_next_seg);
+       if (sql.select_way_prev_seg)
+               sqlite3_finalize(sql.select_way_prev_seg);
+       if (sql.select_way_name_search)
+               sqlite3_finalize(sql.select_way_name_search);
+       if (sql.select_way2)
+               sqlite3_finalize(sql.select_way2);
+       if (sql.select_place)
+               sqlite3_finalize(sql.select_place);
+       if (sql.select_place_near)
+               sqlite3_finalize(sql.select_place_near);
+       if (sql.select_node_next)
+               sqlite3_finalize(sql.select_node_next);
+       if (sql.select_node_prev)
+               sqlite3_finalize(sql.select_node_prev);
 }
 osmdb=NULL;
 osm_db_ok=FALSE;
-g_hash_table_destroy(place_cache);
+memset(&sql, 0, sizeof(sql));
+g_hash_table_destroy(pcache.cache);
+pcache.hit=0;
+pcache.miss=0;
 g_timer_destroy(dbt);
 }
 
@@ -244,7 +290,7 @@ gboolean
 osm_init(sqlite3 **db)
 {
 osm_db_ok=FALSE;
-place_cache=g_hash_table_new(g_direct_hash, g_direct_equal);
+pcache.cache=g_hash_table_new(g_direct_hash, g_direct_equal);
 dbt=g_timer_new();
 
 if (!db || !*db) {
@@ -253,6 +299,7 @@ if (!db || !*db) {
 }
 
 osmdb=*db;
+memset(&sql, 0, sizeof(sql));
 if (osm_db_prepare(osmdb)==FALSE) {
        g_printerr("Failed to prepare OSM SQL statements:");
        g_printf("SQLITE: %s\n", sqlite3_errmsg(osmdb));
@@ -344,21 +391,33 @@ return g_slice_new0(osm_place);
 static osm_place *
 osm_place_cache_lookup(guint32 id)
 {
-return g_hash_table_lookup(place_cache, GINT_TO_POINTER(id));
+osm_place *r;
+r=g_hash_table_lookup(pcache.cache, GINT_TO_POINTER(id));
+if (r) 
+       pcache.hit++; 
+else 
+       pcache.miss++;
+
+g_debug("OSM: Cache %d/%d", pcache.hit, pcache.miss);
+return r;
 }
 
 static void
 osm_place_cache_add(osm_place *p)
 {
 if (osm_place_cache_lookup(p->id)==NULL)
-       g_hash_table_insert(place_cache, GINT_TO_POINTER(p->id), p);
+       g_hash_table_insert(pcache.cache, GINT_TO_POINTER(p->id), p);
 }
 
 static void
 osm_place_cache_gc(void)
 {
-gint r;
-r=g_hash_table_foreach_remove(place_cache, osm_place_remove, NULL);
+guint r;
+
+r=g_hash_table_foreach_remove(pcache.cache, osm_place_remove, NULL);
+g_debug("OSM: Cache cleared (%d)", r);
+pcache.hit=0;
+pcache.miss=0;
 }
 
 static void
@@ -383,14 +442,13 @@ osm_place *n;
 n=*nr;
 n=osm_place_cache_lookup(id);
 if (n) {
-       g_print("*P!\n");
        osm_place_update_distance(n, lat, lon);
        return TRUE;
 }
 n=NULL;
 
 /* XXX: better place for this */
-if (g_hash_table_size(place_cache)>OSM_PLACE_CACHE_MAX_ITEMS)
+if (g_hash_table_size(pcache.cache)>OSM_PLACE_CACHE_MAX_ITEMS)
        osm_place_cache_gc();
 
 sqlite3_clear_bindings(sql.select_place);
@@ -448,32 +506,32 @@ switch (type) {
        break;
 }
 
-sqlite3_clear_bindings(sql.select_near_place);
-sqlite3_reset(sql.select_near_place);
+sqlite3_clear_bindings(sql.select_place_near);
+sqlite3_reset(sql.select_place_near);
 
-if (SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 1, lat) ||
-    SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 2, lon) ||
-    SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 3, type) ||
-    SQLITE_OK != sqlite3_bind_int(sql.select_near_place, 4, range)) {
+if (SQLITE_OK != sqlite3_bind_int(sql.select_place_near, 1, lat) ||
+    SQLITE_OK != sqlite3_bind_int(sql.select_place_near, 2, lon) ||
+    SQLITE_OK != sqlite3_bind_int(sql.select_place_near, 3, type) ||
+    SQLITE_OK != sqlite3_bind_int(sql.select_place_near, 4, range)) {
        g_printerr("Failed to bind values for near place\n");
        return FALSE;
 }
 
 n=osm_place_new();
 n->isin_p=n->lat=n->lon=n->dist=0;
-if (SQLITE_ROW == sqlite3_step(sql.select_near_place)) {
+if (SQLITE_ROW == sqlite3_step(sql.select_place_near)) {
        const gchar *place;
        guint32 dist;
 
-       place=sqlite3_column_text(sql.select_near_place, 0);
+       place=sqlite3_column_text(sql.select_place_near, 0);
        n->name=g_strdup(place);
-       dist=sqlite3_column_int(sql.select_near_place, 1);
+       dist=sqlite3_column_int(sql.select_place_near, 1);
        n->dist=sqrt((double)dist);
-       n->lat=sqlite3_column_int(sql.select_near_place, 2);
-       n->lon=sqlite3_column_int(sql.select_near_place, 3);
-       n->id=sqlite3_column_int(sql.select_near_place, 4);
-       n->isin_p=sqlite3_column_int(sql.select_near_place, 5);
-/*     n->isin_c=sqlite3_column_int(sql.select_near_place, 6); */
+       n->lat=sqlite3_column_int(sql.select_place_near, 2);
+       n->lon=sqlite3_column_int(sql.select_place_near, 3);
+       n->id=sqlite3_column_int(sql.select_place_near, 4);
+       n->isin_p=sqlite3_column_int(sql.select_place_near, 5);
+/*     n->isin_c=sqlite3_column_int(sql.select_place_near, 6); */
        n->type=type;
 
        osm_place_cache_add(n);
@@ -654,10 +712,8 @@ 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,%d\n", 
+       g_debug("Way: %d (%d) has %d nodes, nearest is %d,%d",
                way->id, way->type, way->nodecnt, way->f, way->t);
-#endif
 
        way->node_t=NULL;
 
@@ -670,7 +726,7 @@ for (iter=w; iter!=NULL; iter=iter->next) {
                cw=way;
                way->distance=dist_n;
                way->node_t=wnn;
-               g_printf("#1 distance: %f (%f)\n", dist_n, pdist);
+               g_debug("#1 distance: %f (%f)", dist_n, pdist);
        }
 
        wnp=osm_way_get_prev_node(way);
@@ -685,12 +741,10 @@ for (iter=w; iter!=NULL; iter=iter->next) {
                        osm_way_node_free(wnn);
                }
                way->node_t=wnp;
-               g_printf("#2 distance: %f (%f)\n", dist_p, pdist);
+               g_debug("#2 distance: %f (%f)", dist_p, pdist);
        }
 
-#ifdef DEBUG_OSM
-       g_printf("Found close way, distance: %f %f (%f)\n", dist_n, dist_p, pdist);
-#endif
+       g_debug("Found close way, distance: %f %f (%f)", dist_n, dist_p, pdist);
 
        if (!cw) {
                osm_way_free(way);
@@ -709,16 +763,9 @@ if (cw->type==WAY_MOTORWAY || cw->type==WAY_TRUNK ||
                osm_way_get_ref(cw);
 }
 
-#ifdef DEBUG_OSM
-g_printf("BEST WAY(%d): %s [%s][%s]\n", 
-       cw->id, cw->name, cw->ref, cw->int_ref);
-g_printf("\tType: %d Flags: %d Nodes: %d Dist: %f\n", 
-       cw->type, cw->flags, cw->nodecnt, cw->dist);
-g_printf("\tNF: %d NT: %d Distance: %f\n", 
-       cw->f, 
-       cw->t, 
-       cw->distance);
-#endif
+g_debug("Found way: (ID: %d): [%s]:[%s][%s]", cw->id, cw->name, cw->ref, cw->int_ref);
+g_debug("T: %d F: %d N#: %d D: %f", cw->type, cw->flags, cw->nodecnt, cw->dist);
+g_debug("\tNF#: %d NT#: %d (D: %f)", cw->f, cw->t, cw->distance);
 
 return cw;
 }
@@ -890,7 +937,7 @@ return FALSE;
  *
  */
 gboolean 
-osm_get_location_data(gint lat, gint lon, osm_location *map_loc)
+osm_get_location_data(gint lat, gint lon, gfloat heading, osm_location *map_loc)
 {
 gdouble dist;
 gboolean check_place=FALSE;
@@ -908,9 +955,10 @@ if (map_loc->valid==FALSE) {
 /* Check if we are still near the same way as last time */
 if (map_loc->street && osm_way_distance(lat, lon, map_loc->street->node_f, map_loc->street->node_t, &dist)==TRUE) {
        /* We are probably on the same way as last time */
-       if ( (dist>(gdouble)way_dist_range) || (fabs(_gps->heading-map_loc->heading)>10.0)) {
+       if ( (dist>(gdouble)way_dist_range) || (fabs(heading-map_loc->heading)>10.0)) {
                /* We have moved a large amount, check way again */
-               g_printf("*** dist %f over range, checking again\n", dist);
+               g_debug("Distance %f > %f range or angle %f > 10.0, checking location", 
+                       dist, way_dist_range, fabs(heading-map_loc->heading));
                osm_way_free(map_loc->street);
                map_loc->street=osm_find_nearest_way(lat, lon);
                check_place=TRUE;
@@ -951,8 +999,7 @@ if (map_loc->street && osm_way_distance(lat, lon, map_loc->street->node_f, map_l
 }
 
 if (map_loc->changed==TRUE) {
-       map_loc->heading=_gps->heading;
-       map_loc->speed=_gps->speed;
+       map_loc->heading=heading;
 }
 
 #if 0
@@ -995,6 +1042,74 @@ if (check_place==TRUE && d>way_dist_range*4) {
 return map_loc->street ? TRUE : FALSE;
 }
 
+/**
+ * osm_place_search
+ */
+gboolean
+osm_place_search(gdouble lat, gdouble lon, gchar *text, GtkListStore **store)
+{
+GtkTreeIter iter;
+gchar *ltext=NULL;
+guint rows=0;
+gchar tmp1[16], tmp2[16];
+gdouble range=6;
+
+ltext=g_strdup_printf("%s%%", text);
+
+if (SQLITE_OK != sqlite3_bind_double(sql.select_place_search, 1, lat) ||
+    SQLITE_OK != sqlite3_bind_double(sql.select_place_search, 2, lon) ||
+       SQLITE_OK != sqlite3_bind_text(sql.select_place_search,   3, ltext, -1, SQLITE_TRANSIENT)) {
+               g_printerr("Failed to bind values for sql.select_place_search\n");
+               sqlite3_clear_bindings(sql.select_place_search);
+               g_free(ltext);
+               return FALSE;
+}
+
+if (ltext)
+       g_free(ltext);
+
+*store = gtk_list_store_new(ITEM_NUM_COLUMNS, 
+                               G_TYPE_INT,             /* ID */
+                               G_TYPE_INT,             /*  */
+                               G_TYPE_DOUBLE,  /* Latitude */
+                               G_TYPE_DOUBLE,  /* Longitude */
+                               G_TYPE_DOUBLE,  /* Distance */
+                               G_TYPE_STRING,  /* Lat/Lon */
+                               G_TYPE_STRING,  /* Label */
+                               G_TYPE_STRING,  /* Desc. */
+                               G_TYPE_STRING,  /* Category */
+                               G_TYPE_STRING,  /* Dummy */
+                               G_TYPE_STRING); /* Dummy */
+
+while (SQLITE_ROW == sqlite3_step(sql.select_place_search)) {
+       gdouble rlat, rlon, dist;
+
+       rlat=sqlite3_column_double(sql.select_place_search, 3);
+       rlon=sqlite3_column_double(sql.select_place_search, 4);
+       lat_format(_degformat, rlat, tmp1);
+       lon_format(_degformat, rlon, tmp2);
+       dist=calculate_distance(lat, lon, rlat, rlon) * UNITS_CONVERT[_units];
+
+       gtk_list_store_append(*store, &iter);
+       gtk_list_store_set(*store, &iter,
+               ITEM_ID, sqlite3_column_int(sql.select_place_search, 0),
+               ITEM_LAT, rlat,
+               ITEM_LON, rlon,
+               ITEM_DIST, dist,
+               ITEM_LATLON, g_strdup_printf("%s, %s", tmp1, tmp2),
+               ITEM_LABEL, sqlite3_column_text(sql.select_place_search, 1),
+               -1);
+       rows++;
+}
+
+g_printf("Found: %d places\n", rows);
+
+sqlite3_reset(sql.select_place_search);
+sqlite3_clear_bindings(sql.select_place_search);
+
+return TRUE;
+}
+
 /**
  * osm_way_search
  *
@@ -1070,5 +1185,3 @@ sqlite3_clear_bindings(sql.select_way_name_search);
 
 return TRUE;
 }
-
-