]> err.no Git - mapper/commitdiff
Start to make it do something. Place query works.
authorKaj-Michael Lang <milang@angel.tal.org>
Sat, 21 Jul 2007 17:52:42 +0000 (20:52 +0300)
committerKaj-Michael Lang <milang@angel.tal.org>
Sat, 21 Jul 2007 17:52:42 +0000 (20:52 +0300)
src/osm-db.c

index 71ef84a7273e78d68812343cc01a59d39a690000..62c07ae99dfbed5582b89f84ff5c7a1a9cc9c2e6 100644 (file)
@@ -24,32 +24,47 @@ struct sql_select_stmt {
 static struct sql_select_stmt sql;
 
 gboolean
-osm_init_sql_selects(void)
+osm_db_prepare(sqlite3 *db)
 {
 /* Place */
 /* Select nearest place inside lat,lon+-range */
-sqlite3_prepare(db, "select name from place where type=? "
+if (sqlite3_prepare_v2(db, "select name,(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LON-lon)) as dist,"
+                                       " lat,lon,places.nid "
+                                       " from places,nodes where type=$TYPE "
+                                       " and nodes.nid=places.nid "
                                        " and lat between $LAT-$RANGE and $LAT+$RANGE"
-                                       " and lon between $LON-$RANGE and $LON+$RANGE",
-                   -1, &sql.select_place, NULL);
+                                       " and lon between $LON-$RANGE and $LON+$RANGE order by dist limit 1",
+                   -1, &sql.select_place, NULL)!=SQLITE_OK)
+       return FALSE;
 
+g_print("A\n");
 /* Ways */
 /* Select neareset ways inside lat,lon+-range */
-sqlite3_prepare(db, "select wid from way,way_seg,nodes where wid=wsid and way_seg.node=nodes.nid "
+if (sqlite3_prepare_v2(db, "select wid from way,way_seg,nodes,"
+                                       "(($LAT-lat)*($LAT-lat))+(($LON-lon)*($LON-lon)) as dist"
+                                       " where wid=wsid and way_seg.node=nodes.nid "
                                        " and nodes.lat between $LAT-$RANGE and $LAT+$RANGE "
                                        " and nodes.lon between $LON-$RANGE and $LON+$RANGE "
-                                       " order by (($LAT-lat)*($LAT-lan))+(($LON-lon)*($LON-lon))",
-                   -1, &sql.select_way_name, NULL);
+                                       " order by dist",
+                   -1, &sql.select_way_name, NULL)!=SQLITE_OK)
+       return FALSE;
 
+g_print("A\n");
 /* Select way nodes */
-sqlite3_prepare(db, "select wsid,lat,lon from way_seg,nodes where wsid=? and way_seg.node=nodes.nid",
-                   -1, &sql.select_way_nodes, NULL);
+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 */
-sqlite3_prepare(db, "select name from way_names where wid=?",
-                   -1, &sql.select_way_name, NULL);
-sqlite3_prepare(db, "select ref,int_ref from way_ref where rid=?",
-                   -1, &sql.select_way_ref, NULL);
+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;
 
 return TRUE;
 }
@@ -63,17 +78,77 @@ return TRUE;
 /**
  * Search for the nearest place, type
  */
-osm_place *
-osm_find_nearest_place(gint type, gint lat, gint lon)
+gboolean
+osm_find_nearest_place(node_type_t type, gint lat, gint lon, osm_place *n)
 {
+gint range;
+
+switch (type) {
+       case NODE_PLACE_SUBURB:
+               range=70000;
+       break;
+       default:
+               range=100000;
+       break;
+}
+
+g_assert(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, type) ||
+    SQLITE_OK != sqlite3_bind_int(sql.select_place, 4, range)) {
+       g_printerr("Failed to bind values\n");
+       return FALSE;
+}
+
+n->name=NULL;
+if (SQLITE_ROW == sqlite3_step(sql.select_place)) {
+       const gchar *place;
+       gint dist;
+
+       place=sqlite3_column_text(sql.select_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->type=type;
+
+       g_printf("Place(%d): %s distance: %f\n", type, place, sqrt((float)dist));
+       return TRUE;
+}
+return FALSE;
 }
 
 /**
  * Search for the nearest way (road)
+ * - First search for ways with nearest node
+ * - If only one is returned then we are done.
+ * - If more than one, go trough the results:
+ *   - Load nodes for the way, check prev/next nodes
+ *   - Store result if closer than before
+ * - Return closest way
  */
 osm_way *
 osm_find_nearest_way(gint lat, gint lon)
 {
 
 }
+
+/**
+ * Get list of nodes for given way
+ */
+gboolean
+osm_way_get_nodes(osm_way *w)
+{
+if (w->nodes!=NULL)
+       return TRUE;
+}
+
+void
+osm_way_free(osm_way *w)
+{
+}