int geom_poly_segment_compatible(struct geom_poly_segment *s1, struct geom_poly_segment *s2, int dir) { int same=0,opposite=0; if (s1->type == geom_poly_segment_type_none || s2->type == geom_poly_segment_type_none) return 0; if (s1->type == s2->type) { same=1; if (s1->type == geom_poly_segment_type_way_inner || s1->type == geom_poly_segment_type_way_outer) { opposite=1; } } if (s1->type == geom_poly_segment_type_way_left_side && s2->type == geom_poly_segment_type_way_right_side) opposite=1; if (s1->type == geom_poly_segment_type_way_right_side && s2->type == geom_poly_segment_type_way_left_side) opposite=1; if (s1->type == geom_poly_segment_type_way_unknown || s2->type == geom_poly_segment_type_way_unknown) { same=1; opposite=1; } if (dir < 0) { if ((opposite && coord_is_equal(*s1->first, *s2->first)) || (same && coord_is_equal(*s1->first, *s2->last))) return 1; } else { if ((opposite && coord_is_equal(*s1->last, *s2->last)) || (same && coord_is_equal(*s1->last, *s2->first))) return 1; } return 0; }
GList * geom_poly_segments_insert(GList *list, struct geom_poly_segment *first, struct geom_poly_segment *second, struct geom_poly_segment *third) { int count; struct geom_poly_segment *ret; struct coord *pos; if (!second) return NULL; ret=g_new(struct geom_poly_segment, 1); ret->type=second->type; count=(second->last-second->first)+1; if (first) count+=(first->last-first->first); if (third) count+=(third->last-third->first); #if 0 fprintf(stderr,"count=%d first=%p second=%p third=%p\n",count,first,second,third); if (first) fprintf(stderr,"first:0x%x,0x%x-0x%x,0x%x (%d)\n",first->first->x,first->first->y,first->last->x,first->last->y, first->last-first->first+1); if (second) fprintf(stderr,"second:0x%x,0x%x-0x%x,0x%x (%d)\n",second->first->x,second->first->y,second->last->x,second->last->y, second->last-second->first+1); if (third) fprintf(stderr,"third:0x%x,0x%x-0x%x,0x%x (%d)\n",third->first->x,third->first->y,third->last->x,third->last->y, third->last-third->first+1); #endif ret->first=g_new(struct coord, count); pos=ret->first; if (first) { count=(first->last-first->first)+1; geom_coord_copy(first->first, pos, count, coord_is_equal(*first->first, *second->first)); pos+=count-1; } count=(second->last-second->first)+1; geom_coord_copy(second->first, pos, count, 0); pos+=count; if (third) { pos--; count=(third->last-third->first)+1; geom_coord_copy(third->first, pos, count, coord_is_equal(*third->last, *second->last)); pos+=count; } ret->last=pos-1; #if 0 fprintf(stderr,"result:0x%x,0x%x-0x%x,0x%x (%d)\n",ret->first->x,ret->first->y,ret->last->x,ret->last->y, ret->last-ret->first+1); #endif list=g_list_prepend(list, ret); #if 0 fprintf(stderr,"List=%p\n",list); #endif return list; }
int geom_poly_segments_point_inside(GList *in, struct coord *c) { int open_matches=0,closed_matches=0; #if 0 fprintf(stderr,"try 0x%x,0x%x:",c->x,c->y); #endif while (in) { struct geom_poly_segment *seg=in->data; if (geom_poly_point_inside(seg->first, seg->last-seg->first+1, c)) { #if 0 fprintf(stderr," inside"); #endif if (coord_is_equal(*seg->first,*seg->last)) closed_matches++; else open_matches++; } else { #if 0 fprintf(stderr," outside"); #endif } in=g_list_next(in); } #if 0 fprintf(stderr,"\n"); fprintf(stderr,"open_matches %d closed_matches %d\n",open_matches,closed_matches); #endif if (closed_matches) { if (closed_matches & 1) return 1; else return 0; } if (open_matches) { if (open_matches & 1) return -1; else return 0; } return 0; }
GList * geom_poly_segments_sort(GList *in, enum geom_poly_segment_type type) { GList *ret=NULL; while (in) { struct geom_poly_segment *seg=in->data; GList *tmp=ret; struct geom_poly_segment *merge_first=NULL,*merge_last=NULL; while (tmp) { struct geom_poly_segment *cseg=tmp->data; if (geom_poly_segment_compatible(seg, cseg, -1)) merge_first=cseg; if (geom_poly_segment_compatible(seg, cseg, 1)) merge_last=cseg; tmp=g_list_next(tmp); } if (merge_first == merge_last) merge_last=NULL; ret=geom_poly_segments_insert(ret, merge_first, seg, merge_last); ret=geom_poly_segments_remove(ret, merge_first); ret=geom_poly_segments_remove(ret, merge_last); in=g_list_next(in); } in=ret; while (in) { struct geom_poly_segment *seg=in->data; if (coord_is_equal(*seg->first, *seg->last)) { long long area=geom_poly_area(seg->first,seg->last-seg->first+1); if (type == geom_poly_segment_type_way_right_side && seg->type == geom_poly_segment_type_way_right_side) { seg->type=area > 0 ? geom_poly_segment_type_way_outer : geom_poly_segment_type_way_inner; } } in=g_list_next(in); } return ret; }
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 void tile_collector_process_tile(char *tile, int *tile_data, struct coastline_tile_data *data) { int poly_start_valid,tile_start_valid,exclude,search=0; struct rect bbox; struct coord cn[2],end,poly_start,tile_start; struct geom_poly_segment *first; struct item_bin *ib=(struct item_bin *)buffer; struct item_bin_sink *out=data->sink->priv_data[1]; int dbgl=1; int edges=0,flags; GList *sorted_segments,*curr; #if 0 if (strncmp(tile,"bcdbdcabddddba",7)) return; #endif #if 0 if (strncmp(tile,"bcdbdcaaaaddba",14)) return; #endif #if 0 fprintf(stderr,"tile %s of size %d\n", tile, *tile_data); #endif tile_bbox(tile, &bbox, 0); sorted_segments=geom_poly_segments_sort(tile_data_to_segments(tile_data), geom_poly_segment_type_way_right_side); #if 0 { GList *sort_segments=sorted_segments; int count=0; while (sort_segments) { struct geom_poly_segment *seg=sort_segments->data; struct item_bin *ib=(struct item_bin *)buffer; char *text=g_strdup_printf("segment %d type %d %p %s area "LONGLONG_FMT,count++,seg->type,sort_segments,coord_is_equal(*seg->first, *seg->last) ? "closed":"open",geom_poly_area(seg->first,seg->last-seg->first+1)); item_bin_init(ib, type_rg_segment); item_bin_add_coord(ib, seg->first, seg->last-seg->first+1); item_bin_add_attr_string(ib, attr_debug, text); // fprintf(stderr,"%s\n",text); g_free(text); // item_bin_dump(ib, stderr); item_bin_write_to_sink(ib, out, NULL); sort_segments=g_list_next(sort_segments); } } #endif flags=0; curr=sorted_segments; while (curr) { struct geom_poly_segment *seg=curr->data; switch (seg->type) { case geom_poly_segment_type_way_inner: flags|=1; break; case geom_poly_segment_type_way_outer: flags|=2; break; default: flags|=4; break; } curr=g_list_next(curr); } if (flags == 1) { item_bin_init(ib, type_poly_water_tiled); item_bin_bbox(ib, &bbox); item_bin_write_to_sink(ib, out, NULL); g_hash_table_insert(data->tile_edges, g_strdup(tile), (void *)15); return; } #if 1 end=bbox.l; tile_start_valid=0; poly_start_valid=0; exclude=0; poly_start.x=0; poly_start.y=0; tile_start.x=0; tile_start.y=0; for (;;) { search++; // item_bin_write_debug_point_to_sink(out, &end, "Search %d",search); dbg(dbgl,"searching next polygon from 0x%x 0x%x\n",end.x,end.y); first=find_next(&bbox, sorted_segments, &end, exclude, cn); exclude=1; if (!first) break; if (!tile_start_valid) { tile_start=cn[0]; tile_start_valid=1; } else { if (cn[0].x == tile_start.x && cn[0].y == tile_start.y) { dbg(dbgl,"end of tile reached\n"); break; } } if (first->type == geom_poly_segment_type_none) { end=cn[0]; continue; } poly_start_valid=0; dbg(dbgl,"start of polygon 0x%x 0x%x\n",cn[0].x,cn[0].y); for (;;) { if (!poly_start_valid) { poly_start=cn[0]; poly_start_valid=1; item_bin_init(ib,type_poly_water_tiled); } else { close_polygon(ib, &end, &cn[0], 1, &bbox, &edges); if (cn[0].x == poly_start.x && cn[0].y == poly_start.y) { dbg(dbgl,"poly end reached\n"); item_bin_write_to_sink(ib, out, NULL); end=cn[0]; break; } } if (first->type == geom_poly_segment_type_none) break; item_bin_add_coord(ib, first->first, first->last-first->first+1); first->type=geom_poly_segment_type_none; end=cn[1]; if (distance_from_ll(&end, &bbox) == -1) { dbg(dbgl,"incomplete\n"); break; } first=find_next(&bbox, sorted_segments, &end, 1, cn); dbg(dbgl,"next segment of polygon 0x%x 0x%x\n",cn[0].x,cn[0].y); } if (search > 55) break; } #endif #if 0 { int *end=tile_data+tile_data[0]; int *curr=tile_data+1; while (curr < end) { struct item_bin *ib=(struct item_bin *)curr; // item_bin_dump(ib); ib->type=type_rg_segment; item_bin_write_to_sink(ib, out, NULL); curr+=ib->len+1; #if 0 { struct coord *c[2]; int i; char *s; c[0]=(struct coord *)(ib+1); c[1]=c[0]+ib->clen/2-1; for (i = 0 ; i < 2 ; i++) { s=coord_to_str(c[i]); item_bin_write_debug_point_to_sink(out, c[i], "%s",s); g_free(s); } } #endif } } #endif g_hash_table_insert(data->tile_edges, g_strdup(tile), (void *)edges); #if 0 item_bin_init(ib, type_border_country); item_bin_bbox(ib, &bbox); item_bin_add_attr_string(ib, attr_debug, tile); item_bin_write_to_sink(ib, out, NULL); #endif #if 0 c.x=(bbox.l.x+bbox.h.x)/2; c.y=(bbox.l.y+bbox.h.y)/2; item_bin_write_debug_point_to_sink(out, &c, "%s %d",tile,edges); #endif }
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; }