void bbox(struct coord *c, int count, struct rect *r) { if (! count) return; r->l=*c; r->h=*c; while (--count) { c++; bbox_extend(c, r); } }
int item_bin_sort_file(char *in_file, char *out_file, struct rect *r, int *size) { int j,k,count,rc=0; struct coord *c; struct item_bin *ib; FILE *f; unsigned char *p,**idx,*buffer; if (file_get_contents(in_file, &buffer, size)) { ib=(struct item_bin *)buffer; p=buffer; count=0; while (p < buffer+*size) { count++; p+=(*((int *)p)+1)*4; } idx=g_malloc(count*sizeof(void *)); p=buffer; for (j = 0 ; j < count ; j++) { idx[j]=p; p+=(*((int *)p)+1)*4; } qsort(idx, count, sizeof(void *), item_bin_sort_compare); f=fopen(out_file,"wb"); for (j = 0 ; j < count ; j++) { ib=(struct item_bin *)(idx[j]); c=(struct coord *)(ib+1); fwrite(ib, (ib->len+1)*4, 1, f); if (r) { for (k = 0 ; k < ib->clen/2 ; k++) { if (rc) bbox_extend(&c[k], r); else { r->l=c[k]; r->h=c[k]; } rc++; } } } fclose(f); g_free(idx); g_free(buffer); return 1; } return 0; }
void item_bin_write_match(struct item_bin *ib, enum attr_type type, enum attr_type match, int maxdepth, FILE *out) { char *word=item_bin_get_attr(ib, type, NULL); int i,words=0,len; char tilename[32]=""; if (!word) return; if(maxdepth && ib->clen>0) { struct rect r; struct coord *c=(struct coord *)(ib+1); r.l=c[0]; r.h=c[0]; for (i = 1 ; i < ib->clen/2 ; i++) bbox_extend(&c[i], &r); tile(&r,NULL,tilename,maxdepth,overlap,NULL); } /* insert attr_tile_name attribute before the attribute used as alphabetical key (of type type) */ if(maxdepth) { item_bin_add_attr_string(ib, attr_tile_name, tilename); item_bin_add_attr_string(ib, type, word); item_bin_remove_attr(ib,word); word=item_bin_get_attr(ib, type, NULL); } len=ib->len; do { if (linguistics_search(word)) { for (i = 0 ; i < 3 ; i++) { char *str=linguistics_expand_special(word, i); if (str) { ib->len=len; if (i || words) item_bin_add_attr_string(ib, match, str); item_bin_write(ib, out); g_free(str); } } words++; } word=linguistics_next_word(word); } while (word); }
static GList * process_boundaries_finish(GList *boundaries_list) { //fprintf(stderr,"process_boundaries_finish:001\n"); GList *l, *sl, *l2, *ln; GList *ret = NULL; l = boundaries_list; char *f1_name = NULL; char *f2_name = NULL; long long b_counter_1 = 0; long long nodes_counter_ = 0; long long ways_counter_ = 0; while (l) { struct boundary *boundary = l->data; int first = 1; FILE *f = NULL, *fu = NULL; b_counter_1++; if ((b_counter_1 % 500) == 0) { fprintf(stderr,"boundaries_f1:B:%lld\n", b_counter_1); } //fprintf(stderr,"process_boundaries_finish:002\n"); // only lowercase country code if (boundary->iso2) { int i99; for (i99 = 0; boundary->iso2[i99]; i99++) { boundary->iso2[i99] = tolower(boundary->iso2[i99]); } } // only lowercase country code if (boundary->country) { //fprintf(stderr,"process_boundaries_finish:003\n"); char *name = g_strdup_printf("country_%s_poly", boundary->iso2); f1_name = g_strdup_printf("country_%s_poly", boundary->iso2); f = tempfile("", name, 1); g_free(name); } // calc bounding box first = 1; nodes_counter_ = 0; ways_counter_ = 0; sl = boundary->segments; while (sl) { struct geom_poly_segment *gs = sl->data; struct coord *c = gs->first; while (c <= gs->last) { if (first) { boundary->r.l = *c; boundary->r.h = *c; first = 0; } else { bbox_extend(c, &boundary->r); } c++; nodes_counter_++; } sl = g_list_next(sl); ways_counter_++; } //fprintf(stderr, "relid:%lld\n", item_bin_get_relationid(boundary->ib)); //fprintf(stderr, "ways:%lld nodes:%lld\n", ways_counter_, nodes_counter_); boundary->sorted_segments = geom_poly_segments_sort(boundary->segments, geom_poly_segment_type_way_right_side); sl = boundary->sorted_segments; first = 1; while (sl) { //fprintf(stderr,"process_boundaries_finish:004.1\n"); struct geom_poly_segment *gs = sl->data; struct coord *c = gs->first; /* while (c <= gs->last) { if (first) { boundary->r.l = *c; boundary->r.h = *c; first = 0; } else { bbox_extend(c, &boundary->r); } c++; //fprintf(stderr,"process_boundaries_finish:004.2 lx=%d ly=%d hx=%d hy=%d\n",boundary->r.l.x,boundary->r.l.y,boundary->r.h.x,boundary->r.h.y); } */ if (f) { struct item_bin *ib = item_bin_2; item_bin_init(ib, type_selected_line); item_bin_add_coord(ib, gs->first, gs->last - gs->first + 1); item_bin_write(ib, f); } if (boundary->country) { if (!coord_is_equal(*gs->first, *gs->last)) { if (!fu) { char *name = g_strdup_printf("country_%s_broken", boundary->iso2); f2_name = g_strdup_printf("country_%s_broken", boundary->iso2); fprintf(stderr, "*BROKEN* country_%s_broken\n", boundary->iso2); fu = tempfile("", name, 1); g_free(name); } struct item_bin *ib = item_bin_2; item_bin_init(ib, type_selected_point); item_bin_add_coord(ib, gs->first, 1); item_bin_write(ib, fu); item_bin_init(ib, type_selected_point); item_bin_add_coord(ib, gs->last, 1); item_bin_write(ib, fu); } } sl = g_list_next(sl); if (f2_name) { tempfile_unlink("", f2_name); g_free(f2_name); f2_name = NULL; } } ret = process_boundaries_insert(ret, boundary); l = g_list_next(l); if (f) { fclose(f); } if (fu) { if (boundary->country) { //osm_warning("relation", item_bin_get_relationid(boundary->ib), 0, "Broken country polygon '%s'\n", boundary->iso2); fprintf(stderr, "*BROKEN* country polygon '%s' relid=%lld\n", boundary->iso2, item_bin_get_relationid(boundary->ib)); } fclose(fu); } if (f1_name) { tempfile_unlink("", f1_name); g_free(f1_name); f1_name = NULL; } } #if 0 printf("hierarchy\n"); #endif // boundaries_list = g_list_sort(boundaries_list, boundary_bbox_compare); // disable sorting, does not seem to do any good // children stuff totally broken!!! #if 0 b_counter_1 = 0; l = boundaries_list; while (l) { b_counter_1++; if ((b_counter_1 % 500) == 0) { fprintf(stderr,"boundaries_f2:B:%lld\n", b_counter_1); } struct boundary *boundary = l->data; ln = l2 = g_list_next(l); while (l2) { struct boundary *boundary2 = l2->data; if (bbox_contains_bbox(&boundary2->r, &boundary->r)) { boundaries_list = g_list_remove(boundaries_list, boundary); boundary2->children = g_list_append(boundary2->children, boundary); break; } l2 = g_list_next(l2); } l = ln; } #endif // children stuff totally broken!!! // -- DEBUG -- // -- DEBUG -- // -- DEBUG -- // dump_hierarchy(boundaries_list,""); // --> prints huge amounts of data!! be careful // -- DEBUG -- // -- DEBUG -- // -- DEBUG -- return boundaries_list; }
static GList * process_boundaries_finish(GList *boundaries_list) { GList *l,*sl; GList *ret=NULL; l=boundaries_list; while (l) { struct boundary *boundary=l->data; int first=1; FILE *f=NULL,*fu=NULL; if (boundary->country) { char *name=g_strdup_printf("country_%s_poly",boundary->iso2); f=tempfile("",name,1); g_free(name); } boundary->sorted_segments=geom_poly_segments_sort(boundary->segments, geom_poly_segment_type_way_right_side); sl=boundary->sorted_segments; while (sl) { struct geom_poly_segment *gs=sl->data; struct coord *c=gs->first; while (c <= gs->last) { if (first) { boundary->r.l=*c; boundary->r.h=*c; first=0; } else bbox_extend(c, &boundary->r); c++; } if (f) { struct item_bin *ib=tmp_item_bin; item_bin_init(ib, type_selected_line); item_bin_add_coord(ib, gs->first, gs->last-gs->first+1); item_bin_write(ib, f); } if (boundary->country) { if (!coord_is_equal(*gs->first,*gs->last)) { struct item_bin *ib; if (!fu) { char *name=g_strdup_printf("country_%s_broken",boundary->iso2); fu=tempfile("",name,1); g_free(name); } ib=tmp_item_bin; item_bin_init(ib, type_selected_point); item_bin_add_coord(ib, gs->first, 1); item_bin_write(ib, fu); item_bin_init(ib, type_selected_point); item_bin_add_coord(ib, gs->last, 1); item_bin_write(ib, fu); } } sl=g_list_next(sl); } ret=process_boundaries_insert(ret, boundary); l=g_list_next(l); if (f) fclose(f); if (fu) { if (boundary->country) osm_warning("relation",item_bin_get_relationid(boundary->ib),0,"Broken country polygon '%s'\n",boundary->iso2); fclose(fu); } } #if 0 printf("hierarchy\n"); #endif #if 0 boundaries_list=g_list_sort(boundaries_list, boundary_bbox_compare); l=boundaries_list; while (l) { struct boundary *boundary=l->data; GList *l2,*ln; ln=l2=g_list_next(l); while (l2) { struct boundary *boundary2=l2->data; if (bbox_contains_bbox(&boundary2->r, &boundary->r)) { boundaries_list=g_list_remove(boundaries_list, boundary); boundary2->children=g_list_append(boundary2->children, boundary); #if 0 printf("found\n"); #endif break; } l2=g_list_next(l2); } l=ln; } dump_hierarchy(boundaries_list,""); #if 0 printf("hierarchy done\n"); printf("test\n"); test(boundaries_list); #endif #endif return ret; }