Add:maptool:Moved town to country resolution to a separate phase
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 10 Jul 2011 12:22:48 +0000 (12:22 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sun, 10 Jul 2011 12:22:48 +0000 (12:22 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@4616 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/navit/attr_def.h
navit/navit/maptool/itembin.c
navit/navit/maptool/maptool.c
navit/navit/maptool/maptool.h
navit/navit/maptool/osm.c

index 9e7bbdd..600bdd9 100644 (file)
@@ -346,6 +346,7 @@ ATTR(dbus_destination)
 ATTR(dbus_path)
 ATTR(dbus_interface)
 ATTR(dbus_method)
+ATTR(osm_is_in)
 ATTR2(0x0003ffff,type_string_end)
 ATTR2(0x00040000,type_special_begin)
 ATTR(order)
index b7e0c07..35f9070 100644 (file)
@@ -154,6 +154,23 @@ item_bin_add_attr(struct item_bin *ib, struct attr *attr)
 }
 
 void
+item_bin_remove_attr(struct item_bin *ib, void *ptr)
+{
+       unsigned char *s=(unsigned char *)ib;
+       unsigned char *e=s+(ib->len+1)*4;
+       s+=sizeof(struct item_bin)+ib->clen*4;
+       while (s < e) {
+               struct attr_bin *ab=(struct attr_bin *)s;
+               s+=(ab->len+1)*4;
+               if ((void *)(ab+1) == ptr) {
+                       ib->len-=ab->len+1;
+                       memmove(ab,s,e-s);
+                       return;
+               }
+       }
+}
+
+void
 item_bin_add_attr_int(struct item_bin *ib, enum attr_type type, int val)
 {
        struct attr attr;
index a8ff840..859334b 100644 (file)
@@ -364,8 +364,10 @@ osm_collect_data(struct maptool_params *p, char *suffix)
        unlink("coords.tmp");
        if (p->process_ways)
                p->osm.ways=tempfile(suffix,"ways",1);
-       if (p->process_nodes)
+       if (p->process_nodes) {
                p->osm.nodes=tempfile(suffix,"nodes",1);
+               p->osm.towns=tempfile(suffix,"towns",1);
+       }
        if (p->process_ways && p->process_nodes) {
                p->osm.turn_restrictions=tempfile(suffix,"turn_restrictions",1);
                if(doway2poi) {
@@ -408,6 +410,8 @@ osm_collect_data(struct maptool_params *p, char *suffix)
                fclose(p->osm.poly2poi);
        if (p->osm.line2poi)
                fclose(p->osm.line2poi);
+       if (p->osm.towns)
+               fclose(p->osm.towns);
 }
 int debug_ref=0;
 
@@ -806,6 +810,15 @@ int main(int argc, char **argv)
        }
        phase++;
        if (p.start <= phase && p.end >= phase) {
+               FILE *towns=tempfile(suffix,"towns",0);
+               fprintf(stderr,"PROGRESS: Phase %d: assinging towns to countries\n",phase);
+               osm_process_towns(towns);
+               fclose(towns);
+               if(!p.keep_tmpfiles)
+                       tempfile_unlink(suffix,"towns");
+       }
+       phase++;
+       if (p.start <= phase && p.end >= phase) {
                fprintf(stderr,"PROGRESS: Phase %d: sorting countries\n",phase);
                sort_countries(p.keep_tmpfiles);
                p.countries_loaded=1;
index 7c498e9..0886dde 100644 (file)
@@ -181,6 +181,7 @@ struct attr_bin * item_bin_get_attr_bin_last(struct item_bin *ib);
 void item_bin_add_attr_longlong(struct item_bin *ib, enum attr_type type, long long val);
 void item_bin_add_attr_string(struct item_bin *ib, enum attr_type type, char *str);
 void item_bin_add_attr_range(struct item_bin *ib, enum attr_type type, short min, short max);
+void item_bin_remove_attr(struct item_bin *ib, void *ptr);
 void item_bin_write(struct item_bin *ib, FILE *out);
 struct item_bin *item_bin_dup(struct item_bin *ib);
 void item_bin_write_range(struct item_bin *ib, FILE *out, int min, int max);
@@ -241,6 +242,7 @@ struct maptool_osm {
        FILE *ways;
        FILE *line2poi;
        FILE *poly2poi;
+       FILE *towns;
 };
 
 void osm_add_tag(char *k, char *v);
@@ -263,6 +265,7 @@ FILE *resolve_ways_file(FILE *in, char *suffix, char *filename);
 void process_way2poi(FILE *in, FILE *out, int type);
 int map_find_intersections(FILE *in, FILE *out, FILE *out_index, FILE *out_graph, FILE *out_coastline, int final);
 void write_countrydir(struct zip_info *zip_info);
+void osm_process_towns(FILE *in);
 void load_countries(void);
 void remove_countryfiles(void);
 void osm_init(FILE*);
index 1b4a628..31ac7a6 100644 (file)
@@ -1630,10 +1630,9 @@ osm_end_way(struct maptool_osm *osm)
 void
 osm_end_node(struct maptool_osm *osm)
 {
-       int conflict,count,i;
+       int count,i;
        char *postal;
        enum item_type types[10];
-       struct country_table *result=NULL, *lookup;
        struct item_bin *item_bin;
        in_node=0;
 
@@ -1646,7 +1645,6 @@ osm_end_node(struct maptool_osm *osm)
        }
        dbg_assert(count < 10);
        for (i = 0 ; i < count ; i++) {
-               conflict=0;
                if (types[i] == type_none)
                        continue;
                if (ignore_unkown && (types[i] == type_street_unkn || types[i] == type_point_unkn))
@@ -1673,43 +1671,58 @@ osm_end_node(struct maptool_osm *osm)
                        item_bin_add_attr_string(item_bin, item_is_town(*item_bin) ? attr_town_postal : attr_postal, postal);
                }
                item_bin_write(item_bin,osm->nodes);
-               if (item_is_town(*item_bin) && attr_strings[attr_string_label]) {
-                       char *tok,*buf=is_in_buffer;
-                       if (!buf[0] && unknown_country)
-                               strcpy(is_in_buffer, "Unknown");
-                       while ((tok=strtok(buf, ",;"))) {
-                               while (*tok==' ')
-                                       tok++;
-                               lookup=g_hash_table_lookup(country_table_hash,tok);
-                               if (lookup) {
-                                       if (result && result->countryid != lookup->countryid) {
-                                               osm_warning("node",nodeid,0,"conflict for %s %s country %d vs %d\n", attr_strings[attr_string_label], debug_attr_buffer, lookup->countryid, result->countryid);
-                                               conflict=1;
-                                       }
-                                       result=lookup;
+               if (item_is_town(*item_bin) && attr_strings[attr_string_label] && osm->towns) {
+                       item_bin=init_item(item_bin->type);
+                       item_bin_add_coord(item_bin, &ni->c, 1);
+                       item_bin_add_attr_string(item_bin, attr_osm_is_in, is_in_buffer);
+                       item_bin_add_attr_string(item_bin, attr_town_postal, postal);
+                       item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]); 
+                       item_bin_add_attr_string(item_bin, attr_town_name, attr_strings[attr_string_label]);
+                       item_bin_write(item_bin, osm->towns);
+               }
+       }
+       processed_nodes_out++;
+       attr_longest_match_clear();
+}
+
+void
+osm_process_towns(FILE *in)
+{
+       struct item_bin *ib;
+
+       while ((ib=read_item(in)))  {
+               struct country_table *result=NULL, *lookup;
+               char *tok,*is_in=item_bin_get_attr(ib, attr_osm_is_in, NULL), *buf=g_strdup(is_in);
+               int conflict;
+               if (!buf && unknown_country)
+                       buf=g_strdup("Unknown");
+               while ((tok=strtok(buf, ",;"))) {
+                       while (*tok==' ')
+                               tok++;
+                       lookup=g_hash_table_lookup(country_table_hash,tok);
+                       if (lookup) {
+                               if (result && result->countryid != lookup->countryid) {
+                                       osm_warning("node",nodeid,0,"conflict for %s %s country %d vs %d\n", attr_strings[attr_string_label], debug_attr_buffer, lookup->countryid, result->countryid);
+                                       conflict=1;
                                }
-                               buf=NULL;
+                               result=lookup;
                        }
-                       if (result) {
-                               if (!result->file) {
-                                       char *name=g_strdup_printf("country_%d.bin.unsorted", result->countryid);
-                                       result->file=fopen(name,"wb");
-                                       g_free(name);
-                               }
-                               if (result->file) {
-                                       item_bin=init_item(item_bin->type);
-                                       item_bin_add_coord(item_bin, &ni->c, 1);
-                                       item_bin_add_attr_string(item_bin, attr_town_postal, postal);
-                                       item_bin_add_attr_string(item_bin, attr_county_name, attr_strings[attr_string_county_name]); 
-                                       item_bin_add_attr_string(item_bin, attr_town_name, attr_strings[attr_string_label]);
-                                       item_bin_write_match(item_bin, attr_town_name, attr_town_name_match, result->file);
-                               }
-
+                       buf=NULL;
+               }
+               if (result) {
+                       if (!result->file) {
+                               char *name=g_strdup_printf("country_%d.bin.unsorted", result->countryid);
+                               result->file=fopen(name,"wb");
+                               g_free(name);
+                       }
+                       if (result->file) {
+                               if (is_in)
+                                       item_bin_remove_attr(ib, is_in);
+                               item_bin_write_match(ib, attr_town_name, attr_town_name_match, result->file);
                        }
                }
+               g_free(buf);
        }
-       processed_nodes_out++;
-       attr_longest_match_clear();
 }
 
 void