Пример #1
0
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;
}
Пример #2
0
static void dump_hierarchy(GList *l, char *prefix)
{
	char *newprefix = g_alloca(sizeof(char) * (strlen(prefix) + 2));
	strcpy(newprefix, prefix);
	strcat(newprefix, " ");
	while (l)
	{
		struct boundary *boundary = l->data;
		fprintf(stderr, "%s:childs:%d:%lld:%s\n", prefix, g_list_length(boundary->children), item_bin_get_relationid(boundary->ib), osm_tag_name(boundary->ib));
		dump_hierarchy(boundary->children, newprefix);
		l = g_list_next(l);
	}
}
Пример #3
0
static GList *
process_boundaries_setup(FILE *boundaries, struct relations *relations)
{
	struct item_bin *ib;
	GList *boundaries_list=NULL;
	struct relations_func *relations_func;

	relations_func=relations_func_new(process_boundaries_member, NULL);
	while ((ib=read_item(boundaries))) {
		char *member=NULL;
		struct boundary *boundary=g_new0(struct boundary, 1);
		char *admin_level=osm_tag_value(ib, "admin_level");
		char *iso=osm_tag_value(ib, "ISO3166-1");
		int has_subrelations=0;
		int has_outer_ways=0;
		
		processed_relations++;
		
		if(!iso)
			iso=osm_tag_value(ib, "iso3166-1:alpha2");
		
		if (admin_level && !strcmp(admin_level, "2")) {
			if(!iso) {
				char *int_name=osm_tag_value(ib,"int_name");
				if(int_name && !strcmp(int_name,"France"))
					iso="FR";
			}
			if (iso) {
				struct country_table *country=country_from_iso2(iso);	
				if (!country) 
					osm_warning("relation",item_bin_get_relationid(ib),0,"Country Boundary contains unknown ISO3166-1 value '%s'\n",iso);
				else {
					boundary->iso2=g_strdup(iso);
					osm_info("relation",item_bin_get_relationid(ib),0,"Country Boundary for '%s'\n",iso);
				}
				boundary->country=country;
			} else 
				osm_warning("relation",item_bin_get_relationid(ib),0,"Country Boundary doesn't contain an ISO3166-1 tag\n");
		}
		while ((member=item_bin_get_attr(ib, attr_osm_member, member))) {
			long long osm_id;
			int read=0;
			enum relation_member_type member_type;
			int member_type_numeric;
			char *rolestr;

			if (sscanf(member,RELATION_MEMBER_PARSE_FORMAT,&member_type_numeric,&osm_id,&read) < 2)
				continue;
				
			member_type=(enum relation_member_type)member_type_numeric;
			rolestr=member+read;
			
			if(member_type==rel_member_node) {
				if(!strcmp(rolestr,"admin_centre") || !strcmp(rolestr,"admin_center"))
					boundary->admin_centre=osm_id;
			}
			if(member_type==rel_member_way) {
				enum geom_poly_segment_type role;
				if (!strcmp(rolestr,"outer") || !strcmp(rolestr,"exclave")) {
					has_outer_ways=1;
					role=geom_poly_segment_type_way_outer;
				}
				else if (!strcmp(rolestr,"inner") || !strcmp(rolestr,"enclave"))
					role=geom_poly_segment_type_way_inner;
				else if (!strcmp(rolestr,""))
					role=geom_poly_segment_type_way_unknown;
				else {
					osm_warning("relation",item_bin_get_relationid(ib),0,"Unknown role %s in member ",rolestr);
					osm_warning("way",osm_id,1,"\n");
					role=geom_poly_segment_type_none;
				}
				relations_add_relation_member_entry(relations, relations_func, boundary, (gpointer)role, rel_member_way, osm_id);
			}
			if(member_type==rel_member_relation) {
				if (!strcmp(rolestr,"outer") || !strcmp(rolestr,"exclave") || !strcmp(rolestr,"inner") || !strcmp(rolestr,"enclave"))
					has_subrelations++;
			}
		}
		if(boundary->iso2 && has_subrelations)
			osm_warning("relation",item_bin_get_relationid(ib),0,"Country boundary '%s' has %d relations as boundary segments. These are not supported yet.\n", boundary->iso2, has_subrelations);
		if(boundary->iso2 && !has_outer_ways) {
			osm_warning("relation",item_bin_get_relationid(ib),0,"Skipping country boundary for '%s' because it has no outer ways.\n", boundary->iso2);
			g_free(boundary->iso2);
			boundary->iso2=NULL;
		}

		boundary->ib=item_bin_dup(ib);
		boundaries_list=g_list_append(boundaries_list, boundary);
	}
	return boundaries_list;
}
Пример #4
0
static GList *
process_boundaries_setup(FILE *boundaries, struct relations *relations)
{
	struct item_bin *ib;
	GList *boundaries_list = NULL;
	struct relations_func *relations_func;
	long long b_counter_1 = 0;
	// long long b_counter_2 = 0;

	//fprintf(stderr,"process_boundaries_setup:001\n");

	relations_func = relations_func_new(process_boundaries_member, NULL);
	while ((ib = read_item(boundaries, 0)))
	{

		//fprintf(stderr,"********DUMP b ***********\n");
		//dump_itembin(ib);
		//fprintf(stderr,"********DUMP b ***********\n");


		char *member = NULL;
		struct boundary *boundary=g_new0(struct boundary, 1);
		char *admin_level = osm_tag_value(ib, "admin_level");
		char *iso = osm_tag_value(ib, "ISO3166-1");

		b_counter_1++;
		if ((b_counter_1 % 500) == 0)
		{
			fprintf(stderr,"boundaries:B:%lld\n", b_counter_1);
		}

		//fprintf(stderr,"process_boundaries_setup:002\n");
		//fprintf(stderr,"== b:%s %s ==\n", iso, admin_level);

		/* disable spain for now since it creates a too large index */
		if (admin_level && !strcmp(admin_level, "2") && (!iso || strcasecmp(iso, "es")))
		{
			if (iso)
			{
				struct country_table *country = country_from_iso2(iso);
				if (!country)
				{
					osm_warning("relation", item_bin_get_relationid(ib), 0, "Country Boundary contains unknown ISO3166-1 value '%s'\n", iso);
				}
				else
				{
					boundary->iso2 = g_strdup(iso);
					osm_info("relation", item_bin_get_relationid(ib), 0, "Country Boundary for '%s'\n", iso);
				}
				boundary->country = country;
			}
			else
			{
				osm_warning("relation", item_bin_get_relationid(ib), 0, "Country Boundary doesn't contain an ISO3166-1 tag\n");
			}
		}

		//b_counter_2 = 0;
		while ((member = item_bin_get_attr(ib, attr_osm_member, member)))
		{
			//fprintf(stderr,"process_boundaries_setup:005\n");
			//fprintf(stderr,"********DUMP b ***********\n");
			//dump_itembin(ib);
			//fprintf(stderr,"********DUMP b ***********\n");

			//b_counter_2++;
			//fprintf(stderr,"boundaries:M:%lld\n", b_counter_2);

			long long wayid;
			int read = 0;
			if (sscanf(member, "2:%Ld:%n", &wayid, &read) >= 1)
			{
				char *rolestr = member + read;
				enum geom_poly_segment_type role;
				if (!strcmp(rolestr, "outer") || !strcmp(rolestr, "exclave"))
					role = geom_poly_segment_type_way_outer;
				else if (!strcmp(rolestr, "inner") || !strcmp(rolestr, "enclave"))
					role = geom_poly_segment_type_way_inner;
				else if (!strcmp(rolestr, ""))
					role = geom_poly_segment_type_way_unknown;
				else
				{
					osm_warning("relation", item_bin_get_relationid(ib), 0, "Unknown role %s in member ", rolestr);
					osm_warning("way", wayid, 1, "\n");
					role = geom_poly_segment_type_none;
				}

				//fprintf(stderr,"process_boundaries_setup:006 %s %Ld\n", rolestr,wayid);

				relations_add_func(relations, relations_func, boundary, (gpointer) role, 2, wayid);
			}
		}

		boundary->ib = item_bin_dup(ib);
		boundaries_list = g_list_append(boundaries_list, boundary);
	}

	return boundaries_list;
}
Пример #5
0
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;
}