From be82d9ff85eb5dc41f8a869fa08bcd6a8df98307 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Thu, 9 Aug 2007 19:23:37 +0300 Subject: [PATCH] Ask db for location as little as possible. --- src/osm-db.c | 118 +++++++++++++++++++++++++++++++++++---------------- src/osm-db.h | 1 + 2 files changed, 83 insertions(+), 36 deletions(-) diff --git a/src/osm-db.c b/src/osm-db.c index bfc0e2c..861c520 100644 --- a/src/osm-db.c +++ b/src/osm-db.c @@ -41,7 +41,7 @@ static GtkProgressBar *dbpw=NULL; /* Cache hash tables */ static GHashTable *_place_cache; -static gint way_dist_range=OSM_RANGE_WAY; +static guint way_dist_range=OSM_RANGE_WAY; osm_way_node *osm_way_get_prev_node(osm_way *w); osm_way_node *osm_way_get_next_node(osm_way *w); @@ -49,7 +49,7 @@ osm_way_node *osm_way_get_next_node(osm_way *w); /*****************************************************************************/ void -osm_set_way_range(gint sr) +osm_set_way_range(guint sr) { way_dist_range=sr; } @@ -57,10 +57,10 @@ way_dist_range=sr; void osm_set_way_range_from_speed(gfloat speed) { -if (speed>54) - way_dist_range=9000; +if (speed>54.0) + way_dist_range=OSM_RANGE_WAY*2; else - way_dist_range=OSM_RANGE_WAY-lrint((speed/3)*1000); + way_dist_range=OSM_RANGE_WAY-lrint((speed/4)*1000); } static int @@ -294,6 +294,7 @@ 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; } @@ -496,7 +497,7 @@ return ways; /*****************************************************************************/ -inline gdouble +static inline gdouble magnitude(gdouble x1, gdouble y1, gdouble x2, gdouble y2) { gdouble x,y; @@ -775,59 +776,104 @@ return FALSE; /******************************************************************************/ +/** + * Try to figure out where the given lat,lon is. Fills in the given struct, + * with street, secondary (suburb) and primary (city,town,village) location. + * Will try to minimize the amount of database access by skipping queries + * if we haven't moved or if we don't know where we are. + * + */ gboolean osm_get_location_data(gint lat, gint lon, osm_location *map_loc) { gdouble dist; -gboolean fs, check_place=FALSE; -static gboolean inp=FALSE; +gboolean check_place=FALSE; +gulong d; + +if (map_loc->valid==FALSE) { + map_loc->lat=lat; + map_loc->lon=lon; + map_loc->valid=TRUE; + d=way_dist_range*5; +} else { + d=calculate_idistance(lat,lon,map_loc->lat,map_loc->lon); +} /* 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) { - if (dist>way_dist_range) { + /* We are probably on the same way as last time */ + if (dist>(gdouble)way_dist_range) { + /* We have moved a large amount, check way again */ + g_printf("*** dist %f over range, checking again\n", dist); osm_way_free(map_loc->street); map_loc->street=osm_find_nearest_way(lat, lon); - check_place=FALSE; + check_place=TRUE; + map_loc->changed=TRUE; } else { + g_printf("*** No change in location: %f %d\n", dist, way_dist_range); + /* We are still on the same way as last time */ check_place=FALSE; + map_loc->changed=FALSE; } + map_loc->lat=lat; + map_loc->lon=lon; } else { - osm_way_free(map_loc->street); - map_loc->street=osm_find_nearest_way(lat, lon); - check_place=TRUE; + /* We didn't know our location, so check it, but only if we have moved */ + if (d>way_dist_range) { + g_print("*** Must check location\n"); + osm_way_free(map_loc->street); + map_loc->street=osm_find_nearest_way(lat, lon); +#if 0 + map_loc->lat=lat; + map_loc->lon=lon; +#endif + } + + if (!map_loc->street) { + g_print("*** Street not known\n"); + map_loc->changed=TRUE; + } else { + g_print("*** Street known\n"); + map_loc->changed=TRUE; + check_place=TRUE; + } } -if (check_place==TRUE) { -fs=osm_find_nearest_place(NODE_PLACE_SUBURB, lat, lon, &map_loc->secondary); -if (fs==TRUE && map_loc->secondary && map_loc->secondary->isin!=0) { - if (osm_place_get(map_loc->secondary->isin, lat, lon, &(map_loc->primary))==FALSE) { +g_printf("D: %ld %ld\n", d,(gulong)way_dist_range); + +if (check_place==TRUE && d>way_dist_range*4) { + gboolean fs; + + fs=osm_find_nearest_place(NODE_PLACE_SUBURB, lat, lon, &map_loc->secondary); + if (fs==TRUE && map_loc->secondary && map_loc->secondary->isin!=0) { + if (osm_place_get(map_loc->secondary->isin, lat, lon, &(map_loc->primary))==FALSE) { + if (osm_find_nearest_place(NODE_PLACE_CITY, lat, lon, &map_loc->primary)==TRUE) + g_printf("Near city: %s\n", map_loc->primary->name); + else if (osm_find_nearest_place(NODE_PLACE_TOWN, lat, lon, &map_loc->primary)==TRUE) + g_printf("Near town: %s\n", map_loc->primary->name); + else + g_printf("Unknown\n"); + } else { + g_printf("In: %s\n", map_loc->primary ? map_loc->primary->name : "?"); + } + } else if (map_loc->street && map_loc->street->isin!=0) { + if (osm_place_get(map_loc->street->isin, lat, lon, &map_loc->primary)==FALSE) { + g_printf("Street location not know.\n"); + } else { + g_printf("Street is in: %s\n", map_loc->primary ? map_loc->primary->name : "?"); + } + } else { if (osm_find_nearest_place(NODE_PLACE_CITY, lat, lon, &map_loc->primary)==TRUE) g_printf("Near city: %s\n", map_loc->primary->name); else if (osm_find_nearest_place(NODE_PLACE_TOWN, lat, lon, &map_loc->primary)==TRUE) g_printf("Near town: %s\n", map_loc->primary->name); else g_printf("Unknown\n"); - } else { - g_printf("In: %s\n", map_loc->primary ? map_loc->primary->name : "?"); - } -} else if (map_loc->street && map_loc->street->isin!=0) { - if (osm_place_get(map_loc->street->isin, lat, lon, &map_loc->primary)==FALSE) { - g_printf("Street location not know.\n"); - } else { - g_printf("Street is in: %s\n", map_loc->primary ? map_loc->primary->name : "?"); - } -} else { - if (osm_find_nearest_place(NODE_PLACE_CITY, lat, lon, &map_loc->primary)==TRUE) - g_printf("Near city: %s\n", map_loc->primary->name); - else if (osm_find_nearest_place(NODE_PLACE_TOWN, lat, lon, &map_loc->primary)==TRUE) - g_printf("Near town: %s\n", map_loc->primary->name); - else - g_printf("Unknown\n"); -} + } } +g_print("------\n\n"); + return map_loc->street ? TRUE : FALSE; } - - diff --git a/src/osm-db.h b/src/osm-db.h index 6403e0e..819981c 100644 --- a/src/osm-db.h +++ b/src/osm-db.h @@ -10,6 +10,7 @@ gboolean osm_init(void); void osm_deinit(void); void osm_set_way_range_from_speed(gfloat speed); +void osm_set_way_range(guint sr); void osm_progress_set_widget(sqlite3 *db, GtkProgressBar *w); -- 2.39.5