From 59746700c71638b91dee765f4f3c5e1f62f21be0 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Sun, 22 Jul 2007 18:08:01 +0300 Subject: [PATCH] Add function to get a place by id. --- src/osm-db.c | 90 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 23 deletions(-) diff --git a/src/osm-db.c b/src/osm-db.c index 3fda99c..b410623 100644 --- a/src/osm-db.c +++ b/src/osm-db.c @@ -20,6 +20,7 @@ struct sql_select_stmt { sqlite3_stmt *select_way_name; sqlite3_stmt *select_way_ref; sqlite3_stmt *select_place; + sqlite3_stmt *select_near_place; }; static struct sql_select_stmt sql; @@ -34,10 +35,19 @@ if (sqlite3_prepare_v2(db, "select name,(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LO " and nodes.nid=places.nid " " and lat between $LAT-$RANGE and $LAT+$RANGE" " 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 */ + +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 " + " nodes.nid=places.nid " + " and places.nid=$NID order by dist limit 1", -1, &sql.select_place, NULL)!=SQLITE_OK) return FALSE; -g_print("A\n"); +return TRUE; /* Ways */ /* Select neareset ways inside lat,lon+-range */ if (sqlite3_prepare_v2(db, "select wid from way,way_seg,nodes," @@ -49,19 +59,16 @@ if (sqlite3_prepare_v2(db, "select wid from way,way_seg,nodes," -1, &sql.select_way_name, NULL)!=SQLITE_OK) return FALSE; -g_print("A\n"); /* Select way nodes */ if (sqlite3_prepare_v2(db, "select num,lat,lon from way_seg,nodes where wsid=? and way_seg.node=nodes.nid order by num", -1, &sql.select_way_nodes, NULL)!=SQLITE_OK) return FALSE; -g_print("C\n"); /* Way name and ref */ if (sqlite3_prepare_v2(db, "select name from way_names where wid=?", -1, &sql.select_way_name, NULL)!=SQLITE_OK) return FALSE; -g_print("A\n"); if (sqlite3_prepare_v2(db, "select ref,int_ref from way_ref where rid=?", -1, &sql.select_way_ref, NULL)!=SQLITE_OK) return FALSE; @@ -75,6 +82,39 @@ osm_init() return TRUE; } +/** + * Get place with given id and distance to current location + */ +gboolean +osm_place_get(guint32 id, gint lat, gint lon, osm_place *n) +{ +sqlite3_clear_bindings(sql.select_place); +sqlite3_reset(sql.select_place); + +if (SQLITE_OK != sqlite3_bind_int(sql.select_place, 1, lat) || + SQLITE_OK != sqlite3_bind_int(sql.select_place, 2, lon) || + SQLITE_OK != sqlite3_bind_int(sql.select_place, 3, id)) { + g_printerr("Failed to bind values\n"); + return FALSE; +} + +if (SQLITE_ROW == sqlite3_step(sql.select_place)) { + const gchar *place; + guint32 dist; + + place=sqlite3_column_text(sql.select_place, 0); + n->name=g_strdup(place); + n->dist=sqlite3_column_int(sql.select_place, 1); + n->dist=sqrt(n->dist); + n->lat=sqlite3_column_int(sql.select_place, 2); + n->lon=sqlite3_column_int(sql.select_place, 3); + n->type=sqlite3_column_int(sql.select_place, 4); + n->isin=sqlite3_column_int(sql.select_place, 5); + return TRUE; +} +return FALSE; +} + /** * Search for the nearest place, type */ @@ -85,7 +125,7 @@ gint range; switch (type) { case NODE_PLACE_SUBURB: - range=75000; + range=95000; break; case NODE_PLACE_CITY: case NODE_PLACE_TOWN: @@ -96,37 +136,41 @@ switch (type) { range=100000; break; default: - range=80000; + range=90000; break; } -g_assert(n); -sqlite3_clear_bindings(sql.select_place); -sqlite3_reset(sql.select_place); +sqlite3_clear_bindings(sql.select_near_place); +sqlite3_reset(sql.select_near_place); -if (SQLITE_OK != sqlite3_bind_int(sql.select_place, 1, lat) || - SQLITE_OK != sqlite3_bind_int(sql.select_place, 2, lon) || - SQLITE_OK != sqlite3_bind_int(sql.select_place, 3, type) || - SQLITE_OK != sqlite3_bind_int(sql.select_place, 4, range)) { +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)) { g_printerr("Failed to bind values\n"); return FALSE; } -n->name=NULL; -if (SQLITE_ROW == sqlite3_step(sql.select_place)) { +if (n->name) { + g_free(n->name); + n->name=NULL; +} +n->isin=n->lat=n->lon=n->dist=0; +if (SQLITE_ROW == sqlite3_step(sql.select_near_place)) { const gchar *place; - gint dist; + guint32 dist; - place=sqlite3_column_text(sql.select_place, 0); + place=sqlite3_column_text(sql.select_near_place, 0); n->name=g_strdup(place); - n->dist=sqlite3_column_int(sql.select_place, 1); - n->lat=sqlite3_column_int(sql.select_place, 2); - n->lon=sqlite3_column_int(sql.select_place, 3); - n->id=sqlite3_column_int(sql.select_place, 4); - n->isin=sqlite3_column_int(sql.select_place, 5); + n->dist=sqlite3_column_int(sql.select_near_place, 1); + n->dist=sqrt((double)n->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=sqlite3_column_int(sql.select_near_place, 5); n->type=type; - g_printf("Place(%d): %s distance: %f\n", type, place, sqrt((float)dist)); + g_printf("Place(%d): %s distance: %f\n", type, place, n->dist); return TRUE; } return FALSE; -- 2.39.5