1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/**
* Navit, a modular navigation system.
* Copyright (C) 2005-2011 Navit Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include "maptool.h"
struct boundary {
struct item_bin *ib;
GList *segments,*sorted_segments;
GList *children;
struct rect r;
};
struct boundary_member {
long long wayid;
enum geom_poly_segment_type role;
struct boundary *boundary;
};
static guint
boundary_member_hash(gconstpointer key)
{
const struct boundary_member *memb=key;
return (memb->wayid >> 32)^(memb->wayid & 0xffffffff);
}
static gboolean
boundary_member_equal(gconstpointer a, gconstpointer b)
{
const struct boundary_member *memba=a;
const struct boundary_member *membb=b;
return (memba->wayid == membb->wayid);
}
GHashTable *member_hash;
static char *
osm_tag_name(struct item_bin *ib)
{
char *tag=NULL;
while ((tag=item_bin_get_attr(ib, attr_osm_tag, tag))) {
if (!strncmp(tag,"name=",5))
return tag+5;
}
return NULL;
}
static GList *
build_boundaries(FILE *boundaries)
{
struct item_bin *ib;
GList *boundaries_list=NULL;
while ((ib=read_item(boundaries))) {
char *member=NULL;
struct boundary *boundary=g_new0(struct boundary, 1);
while ((member=item_bin_get_attr(ib, attr_osm_member, member))) {
long long wayid;
int read=0;
if (sscanf(member,"2:%Ld:%n",&wayid,&read) >= 1) {
struct boundary_member *memb=g_new(struct boundary_member, 1);
char *role=member+read;
memb->wayid=wayid;
memb->boundary=boundary;
if (!strcmp(role,"outer"))
memb->role=geom_poly_segment_type_way_outer;
else if (!strcmp(role,"inner"))
memb->role=geom_poly_segment_type_way_inner;
else if (!strcmp(role,""))
memb->role=geom_poly_segment_type_way_unknown;
else {
printf("Unknown role %s\n",role);
memb->role=geom_poly_segment_type_none;
}
g_hash_table_insert(member_hash, memb, g_list_append(g_hash_table_lookup(member_hash, memb), memb));
}
}
boundary->ib=item_bin_dup(ib);
boundaries_list=g_list_append(boundaries_list, boundary);
}
return boundaries_list;
}
static void
find_matches(GList *l, struct coord *c)
{
while (l) {
struct boundary *boundary=l->data;
if (bbox_contains_coord(&boundary->r, c)) {
struct item_bin *ib=boundary->ib;
if (geom_poly_segments_point_inside(boundary->sorted_segments,c))
printf("%s,",osm_tag_name(ib));
find_matches(boundary->children, c);
}
l=g_list_next(l);
}
}
static void
test(GList *boundaries_list)
{
struct item_bin *ib;
FILE *f=fopen("country_276.bin.unsorted","r");
printf("start\n");
while ((ib=read_item(f))) {
struct coord *c=(struct coord *)(ib+1);
char *name=item_bin_get_attr(ib, attr_town_name, NULL);
printf("%s:",name);
find_matches(boundaries_list, c);
printf("\n");
}
fclose(f);
printf("end\n");
}
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;
printf("%s:%s\n",prefix,osm_tag_name(boundary->ib));
dump_hierarchy(boundary->children, newprefix);
l=g_list_next(l);
}
}
static gint
boundary_bbox_compare(gconstpointer a, gconstpointer b)
{
const struct boundary *boundarya=a;
const struct boundary *boundaryb=b;
long long areaa=bbox_area(&boundarya->r);
long long areab=bbox_area(&boundaryb->r);
if (areaa > areab)
return 1;
if (areaa < areab)
return -1;
return 0;
}
int
process_boundaries(FILE *boundaries, FILE *ways)
{
struct item_bin *ib;
GList *boundaries_list,*l,*sl,*l2,*ln;
member_hash=g_hash_table_new_full(boundary_member_hash, boundary_member_equal, NULL, NULL);
boundaries_list=build_boundaries(boundaries);
while ((ib=read_item(ways))) {
long long *wayid=item_bin_get_attr(ib, attr_osm_wayid, NULL);
if (wayid) {
GList *l=g_hash_table_lookup(member_hash, wayid);
while (l) {
struct boundary_member *memb=l->data;
memb->boundary->segments=g_list_prepend(memb->boundary->segments,item_bin_to_poly_segment(ib, memb->role));
l=g_list_next(l);
}
}
}
l=boundaries_list;
while (l) {
struct boundary *boundary=l->data;
int first=1;
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++;
}
sl=g_list_next(sl);
}
l=g_list_next(l);
}
printf("hierarchy\n");
boundaries_list=g_list_sort(boundaries_list, boundary_bbox_compare);
l=boundaries_list;
while (l) {
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);
printf("found\n");
break;
}
l2=g_list_next(l2);
}
l=ln;
}
printf("hierarchy done\n");
dump_hierarchy(boundaries_list,"");
printf("test\n");
test(boundaries_list);
return 1;
}
|