#include "latlon.h"
#include "gps.h"
#include "osm-db.h"
+#include "settings.h"
/* #define DEBUG_OSM */
/* #define DEBUG_OSM_TIME */
-1, &sql.select_way2, NULL)!=SQLITE_OK)
return FALSE;
-if (sqlite3_prepare_v2(db, "select w.wid,w.name as name from way_names as w,way as ww where ww.type between $WTS and $WTY and w.wid=ww.wid and w.name like ? "
+if (sqlite3_prepare_v2(db, "select w.wid,w.name as name,"
+ "(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LON-lon)) as d,wn.f,w.lat,wlon "
+ " from way_names as w,way as ww where "
+ " ww.type between $WTS and $WTY and w.wid=ww.wid and w.name like $NAME "
+ " and w.lat between $LAT-$RANGE and $LAT+$RANGE "
+ " and w.lon between $LON-$RANGE and $LON+$RANGE "
" union "
- " select w.wid,n.name as name from way_names as w, way as ww,way_names_nls as n on w.wid=n.wid where "
- " ww.type between $WTS and $WTY and w.wid=ww.wid and n.name like ? order by name",
+ " select w.wid,n.name as name,"
+ "(($LAT-w.lat)*($LAT-w.lat))+(($LON-w.lon)*($LON-w.lon)) as d,wn.f,w.lat,wlon "
+ " from way_names as w, way as ww,way_names_nls as n on w.wid=n.wid where "
+ " ww.type between $WTS and $WTY and w.wid=ww.wid and n.name like $NAME "
+ " and w.lat between $LAT-$RANGE and $LAT+$RANGE "
+ " and w.lon between $LON-$RANGE and $LON+$RANGE "
+ " order by d",
-1, &sql.select_way_name_search, NULL)!=SQLITE_OK)
return FALSE;
void
osm_deinit(void)
{
-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!=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);
+}
osmdb=NULL;
g_hash_table_destroy(place_cache);
g_timer_destroy(dbt);
gboolean
osm_init(sqlite3 **db)
{
-if (!db || !*db)
+place_cache=g_hash_table_new(g_direct_hash, g_direct_equal);
+dbt=g_timer_new();
+
+if (!db || !*db) {
+ osmdb=NULL;
return FALSE;
+}
osmdb=*db;
if (osm_db_prepare(osmdb)==FALSE) {
g_printf("SQLITE: %s\n", sqlite3_errmsg(osmdb));
return FALSE;
}
-place_cache=g_hash_table_new(g_direct_hash, g_direct_equal);
-dbt=g_timer_new();
return TRUE;
}
return map_loc->street ? TRUE : FALSE;
}
+
+gboolean
+osm_way_search(gdouble lat, gdouble lon, gchar *text, GtkListStore **store)
+{
+GtkTreeIter iter;
+gchar *ltext=NULL;
+guint rows=0;
+gchar tmp1[16], tmp2[16];
+gdouble range=1;
+
+g_printf("Way Search: [%s] around %.6f %.6f\n", text, lat, lon);
+
+ltext=g_strdup_printf("%s%%", text);
+
+
+if (SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 1, lat) ||
+ SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 2, lon) ||
+ SQLITE_OK != sqlite3_bind_int(sql.select_way_name_search, 3, WAY_ROAD_START) ||
+ SQLITE_OK != sqlite3_bind_int(sql.select_way_name_search, 4, WAY_ROAD_END) ||
+ SQLITE_OK != sqlite3_bind_double(sql.select_way_name_search, 6, range) ||
+ SQLITE_OK != sqlite3_bind_text(sql.select_way_name_search, 5, ltext, -1, SQLITE_TRANSIENT)) {
+ g_printerr("Failed to bind values for sql.select_way_name_search\n");
+ sqlite3_clear_bindings(sql.select_way_name_search);
+ g_free(ltext);
+ return FALSE;
+}
+
+*store = gtk_list_store_new(ITEM_NUM_COLUMNS,
+ G_TYPE_INT, /* ID */
+ G_TYPE_INT, /* Category ID */
+ 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 Label */
+
+while (SQLITE_ROW == sqlite3_step(sql.select_way_name_search)) {
+ gdouble rlat, rlon, dist;
+
+ rlat=sqlite3_column_double(sql.select_way_name_search, 0);
+ rlon=sqlite3_column_double(sql.select_way_name_search, 1);
+ lat_format(rlat, tmp1);
+ lon_format(rlon, tmp2);
+ dist=0.0;
+
+ gtk_list_store_append(*store, &iter);
+ gtk_list_store_set(*store, &iter,
+ ITEM_ID, sqlite3_column_int(sql.select_way_name_search, 2),
+ ITEM_CATID, sqlite3_column_int(sql.select_way_name_search, 5),
+ 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_way_name_search, 3),
+ ITEM_DESC, sqlite3_column_text(sql.select_way_name_search, 4),
+ ITEM_CATLAB, sqlite3_column_text(sql.select_way_name_search, 6),
+ -1);
+ rows++;
+}
+
+g_printf("Found: %d items\n", rows);
+
+sqlite3_reset(sql.select_way_name_search);
+sqlite3_clear_bindings(sql.select_way_name_search);
+
+if (ltext)
+ g_free(ltext);
+
+return TRUE;
+}
+
+