Add:mapdriver:search functions for osm maps|ToDo: indexing for town names)
authorplum_ <plum_@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Fri, 22 Feb 2008 10:24:49 +0000 (10:24 +0000)
committerplum_ <plum_@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Fri, 22 Feb 2008 10:24:49 +0000 (10:24 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@901 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/src/data/binfile/binfile.c

index 6309ad1..720f457 100644 (file)
@@ -18,6 +18,8 @@
 
 static int map_id;
 
+static GHashTable *search_results;
+
 struct minmax {
        short min;
        short max;
@@ -60,6 +62,12 @@ struct map_rect_priv {
        struct tile *t;
 };
 
+struct map_search_priv {
+       struct map_rect_priv *mr;
+       struct attr *search;
+       struct map_selection *ms;
+};
+
 
 static void
 map_destroy_binfile(struct map_priv *m)
@@ -357,6 +365,122 @@ map_rect_get_item_byid_binfile(struct map_rect_priv *mr, int id_hi, int id_lo)
        return &mr->item;
 }
 
+static struct map_search_priv *
+binmap_search_new(struct map_priv *map, struct item *item, struct attr *search, int partial)
+{
+       struct map_selection *ms = NULL;
+       if (ms == NULL)
+               ms = g_new(struct map_selection, 1);
+       int i = 0;
+       ms->next = NULL;
+       for (i = 0; i < layer_end; i++)
+       {
+               ms->order[i] = 18;
+       }
+       ms->u.c_rect.lu.x = 0;
+       ms->u.c_rect.lu.y = 10000000;
+       ms->u.c_rect.rl.x = 10000000;
+       ms->u.c_rect.rl.y = 0;
+
+       struct map_rect_priv *map_rec;
+       search_results = g_hash_table_new(g_str_hash, g_str_equal);
+       switch (search->type) {
+               case attr_country_name:
+                       break;
+               case attr_town_name:
+                       map_rec = map_rect_new_binfile(map, ms);
+                       int country = item->id_lo;
+                       struct map_search_priv *msp = g_new(struct map_search_priv, 1);
+                       msp->mr = map_rec;
+                       msp->search = search;
+                       return msp;
+                       break;
+               case attr_town_postal:
+                       break;
+               case attr_street_name:
+                       map_rec = map_rect_new_binfile(map, ms);
+                       struct item *town = map_rect_get_item_byid_binfile(map_rec, item->id_hi, item->id_lo);
+                       if (town) {
+                               struct map_search_priv *msp = g_new(struct map_search_priv, 1);
+                               struct coord *c = g_new(struct coord, 1);
+                               int size = 10000;
+                               switch (town->type) {
+                                       case type_town_label_2e5:
+                                               size = 10000;
+                                               break;
+                                       case type_town_label_2e4:
+                                               size = 2500;
+                                               break;
+                                       case type_town_label_2e3:
+                                               size = 1000;
+                                               break;
+                                       case type_town_label_2e2:
+                                               size = 1000;
+                                               break;
+                                       default:
+                                               break;
+                               }
+                               item_coord_get(town, c, 1);
+                               ms->u.c_rect.lu.x = c->x-size;
+                               ms->u.c_rect.lu.y = c->y+size;
+                               ms->u.c_rect.rl.x = c->x+size;
+                               ms->u.c_rect.rl.y = c->y-size;
+                               
+                               map_rect_destroy_binfile(map_rec);
+                               map_rec = map_rect_new_binfile(map, ms);
+                               msp->mr = map_rec;
+                               msp->search = search;
+                               return msp;
+                       }
+                       map_rect_destroy_binfile(map_rec);
+                       break;
+               default:
+                       break;
+       }
+       return NULL;
+}
+
+struct item *
+binmap_search_get_item(struct map_search_priv *map_search)
+{
+       struct item* it;
+       if (strlen(map_search->search->u.str) <= 2) { return NULL; }
+       while ((it  = map_rect_get_item_binfile(map_search->mr))) {
+               if (map_search->search->type == attr_town_name) {
+                       if ((it->type >= type_town_label) && (it->type <= type_town_label_1e7)) {
+                               struct attr at;
+                               if (binfile_attr_get(it->priv_data, attr_label, &at)) {
+                                       char* tmp = g_strdup(at.u.str);
+                                       if (g_ascii_strncasecmp(tmp, map_search->search->u.str, strlen(map_search->search->u.str)) == 0) {
+                                               return it;
+                                       }
+                               }
+                       }
+               } else if (map_search->search->type == attr_street_name) {
+                       if ((it->type == type_street_3_city) || (it->type == type_street_2_city) || (it->type == type_street_1_city)) {
+                               struct attr at;
+                               if (binfile_attr_get(it->priv_data, attr_label, &at)) {
+                                       char* tmp = g_strdup(at.u.str);
+                                       if (g_ascii_strncasecmp(tmp, map_search->search->u.str, strlen(map_search->search->u.str)) == 0) {
+                                               if (!g_hash_table_lookup(search_results, at.u.str)) {
+                                                       g_hash_table_insert(search_results, tmp, "");
+                                                       return it;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+       return NULL;
+}
+
+static void
+binmap_search_destroy(struct map_search_priv *ms)
+{
+       g_hash_table_destroy(search_results);
+       map_rect_destroy_binfile(ms->mr);
+}
+
 static struct map_methods map_methods_binfile = {
        projection_mg,
        "utf-8",
@@ -365,6 +489,9 @@ static struct map_methods map_methods_binfile = {
        map_rect_destroy_binfile,
        map_rect_get_item_binfile,
        map_rect_get_item_byid_binfile,
+       binmap_search_new,
+       binmap_search_destroy,
+       binmap_search_get_item
 };
 
 static struct map_priv *