-
Notifications
You must be signed in to change notification settings - Fork 53
/
enumerate.c
400 lines (327 loc) · 8.31 KB
/
enumerate.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include "util.h"
#include "graphics.h"
#include "dump.h"
struct file {
FILE *f;
int components;
int zoom;
int bytes;
unsigned char *buf;
int done;
struct file *next;
};
void usage(char **argv) {
fprintf(stderr, "Usage: %s [-ad] [-z max] [-Z min] [-b minlat,minlon,maxlat,maxlon] file\n", argv[0]);
exit(EXIT_FAILURE);
}
int filecmp(const void *v1, const void *v2) {
const struct file *const *f1 = v1;
const struct file *const *f2 = v2;
return memcmp((*f1)->buf, (*f2)->buf, gSortBytes);
}
struct tile {
int xtile;
int ytile;
long long count;
double len;
long long xsum;
long long ysum;
int sibling[2][2];
};
struct bounds {
unsigned int left;
unsigned int top;
unsigned int right;
unsigned int bottom;
};
void handle(long long xx, long long yy, struct tile *tile, char *fname, int minzoom, int maxzoom, int showdist, unsigned int *x, unsigned int *y, struct file **files, int sibling, int verbose, struct bounds *bounds) {
int z;
for (z = minzoom; z <= maxzoom; z++) {
if (tile[z].xtile != xx >> (32 - z) ||
tile[z].ytile != yy >> (32 - z)) {
if (tile[z].count > 0) {
printf("%s %d %d %d",
fname,
z,
tile[z].xtile,
tile[z].ytile);
if (verbose) {
double lat, lon;
tile2latlon(tile[z].xsum / tile[z].count, tile[z].ysum / tile[z].count,
32, &lat, &lon);
printf(" %lld %lf,%lf", tile[z].count, lat, lon);
}
if (showdist) {
printf(" %f", tile[z].len);
}
int qx = tile[z].xtile % 2;
int qy = tile[z].ytile % 2;
tile[z].sibling[qx][qy] = 1;
printf("\n");
}
if (sibling && tile[z].xtile >= 0 && z > 0) {
if (tile[z].xtile / 2 != (xx >> (32 - z)) / 2 ||
tile[z].ytile / 2 != (yy >> (32 - z)) / 2) {
int qx, qy;
for (qx = 0; qx < 2; qx++) {
for (qy = 0; qy < 2; qy++) {
if (tile[z].sibling[qx][qy] == 0) {
printf("%s %d %d %d",
fname, z,
tile[z].xtile / 2 * 2 + qx,
tile[z].ytile / 2 * 2 + qy);
if (verbose) {
double lat, lon;
tile2latlon(tile[z].xtile / 2 * 2 + qx,
tile[z].ytile / 2 * 2 + qy,
z, &lat, &lon);
printf(" 0 %lf,%lf", lat, lon);
}
if (showdist) {
printf(" %f", 0.0);
}
printf("\n");
}
}
}
memset(tile[z].sibling, 0, sizeof(tile[z].sibling));
}
}
tile[z].xtile = xx >> (32 - z);
tile[z].ytile = yy >> (32 - z);
tile[z].count = 0;
tile[z].len = 0;
tile[z].xsum = tile[z].ysum = 0;
}
int include = 0;
if (bounds == NULL) {
include = 1;
} else {
if (xx >= bounds->left && xx <= bounds->right &&
yy >= bounds->top && yy <= bounds->bottom) {
include = 1;
}
}
if (include) {
tile[z].count++;
tile[z].xsum += xx;
tile[z].ysum += yy;
if (showdist && x != NULL) {
int i;
double dist = 0;
double max = 1LL << (32 - z);
for (i = 0; i + 1 < files[0]->components; i++) {
double d1 = (long long) x[i] - x[i + 1];
double d2 = (long long) y[i] - y[i + 1];
double d = sqrt(d1 * d1 + d2 * d2);
#define MAX 6400 /* ~200 feet */
if (d < MAX) {
dist += sqrt(d1 * d1 + d2 * d2) / max;
}
}
tile[z].len += dist;
}
}
}
}
void insert(struct file *m, struct file **head, int bytes) {
while (*head != NULL && memcmp(m->buf, (*head)->buf, bytes) > 0) {
head = &((*head)->next);
}
m->next = *head;
*head = m;
}
int main(int argc, char **argv) {
int i;
extern int optind;
extern char *optarg;
int maxzoom = -1;
int minzoom = 0;
int showdist = 0;
int sibling = 0;
int all = 0;
int verbose = 0;
int usebounds = 0;
struct bounds bounds;
bounds.top = 0;
bounds.left = 0;
bounds.bottom = UINT_MAX;
bounds.right = UINT_MAX;
while ((i = getopt(argc, argv, "z:Z:aDdsvb:")) != -1) {
switch (i) {
case 'z':
maxzoom = atoi(optarg);
break;
case 'Z':
minzoom = atoi(optarg);
break;
case 'd':
showdist = 1;
break;
case 's':
sibling = 1;
break;
case 'a':
all = 1;
break;
case 'D':
all = 2;
break;
case 'b':
{
double minlat, minlon, maxlat, maxlon;
if (sscanf(optarg, "%lf,%lf,%lf,%lf", &minlat, &minlon,
&maxlat, &maxlon) != 4) {
usage(argv);
}
latlon2tile(minlat, minlon, 32, &bounds.left, &bounds.bottom);
latlon2tile(maxlat, maxlon, 32, &bounds.right, &bounds.top);
usebounds = 1;
}
break;
case 'v':
verbose = 1;
break;
default:
usage(argv);
}
}
if (argc - optind != 1) {
usage(argv);
}
char *fname = argv[optind];
char meta[strlen(fname) + 1 + 4 + 1];
sprintf(meta, "%s/meta", fname);
FILE *f = fopen(meta, "r");
if (f == NULL) {
perror(meta);
exit(EXIT_FAILURE);
}
char s[2000] = "";
if (fgets(s, 2000, f) == NULL || strcmp(s, "1\n") != 0) {
fprintf(stderr, "%s: Unknown version %s", meta, s);
exit(EXIT_FAILURE);
}
int mapbits, metabits, maxn;
if (fgets(s, 2000, f) == NULL || sscanf(s, "%d %d %d", &mapbits, &metabits, &maxn) != 3) {
fprintf(stderr, "%s: couldn't find size declaration", meta);
exit(EXIT_FAILURE);
}
fclose(f);
if (maxzoom < 0) {
maxzoom = mapbits / 2 - 8;
}
int bytes = (mapbits + metabits + 7) / 8;
char eof[bytes];
memset(eof, 0xFF, bytes);
gSortBytes = bytes;
int depth;
if (mapbits / 2 + 1 > maxzoom + 9) {
depth = mapbits / 2 + 1;
} else {
depth = maxzoom + 9;
}
struct file *files[depth * maxn];
int nfiles = 0;
struct tile tile[maxzoom + 1];
long long size_total = 0;
long long size_read = 0;
int size_progress = -1;
for (i = 0; i <= maxzoom; i++) {
tile[i].xtile = tile[i].ytile = -1;
tile[i].count = 0;
tile[i].len = 0;
tile[i].xsum = tile[i].ysum = 0;
memset(tile[i].sibling, 0, sizeof(tile[i].sibling));
}
int z_lookup;
for (z_lookup = 0; z_lookup < depth; z_lookup++) {
for (i = 1; i <= maxn; i++) {
if (i == 1 && z_lookup != 0) {
continue;
}
char fn[strlen(fname) + 1 + 5 + 1 + 5 + 1];
sprintf(fn, "%s/%d,%d", fname, i, z_lookup);
FILE *f = fopen(fn, "r");
if (f == NULL) {
perror(fn);
} else {
files[nfiles] = malloc(sizeof(struct file));
files[nfiles]->f = f;
files[nfiles]->components = i;
files[nfiles]->zoom = z_lookup;
files[nfiles]->bytes = bytesfor(mapbits, metabits, i, z_lookup);
files[nfiles]->buf = malloc(files[nfiles]->bytes);
files[nfiles]->done = 0;
struct stat st;
if (stat(fn, &st) == 0) {
size_total += st.st_size;
}
if (fread(files[nfiles]->buf, files[nfiles]->bytes, 1, files[nfiles]->f) != 1) {
memset(files[nfiles]->buf, 0xFF, bytes);
files[nfiles]->done = 1;
} else {
size_read += files[nfiles]->bytes;
}
nfiles++;
}
}
}
if (all) {
dump_begin(all);
}
struct file *head = NULL;
for (i = 0; i < nfiles; i++) {
if (!files[i]->done) {
insert(files[i], &head, bytes);
}
}
while (head != NULL) {
// The problem with this is that only the first component of
// each vector is indexed. We actually want all the tiles that
// the vector intersects, and could queue those by doing
// line drawing, except that the first component might not
// actually have the lowest tile number. How to fix?
unsigned int x[head->components], y[head->components];
unsigned long long meta = 0;
buf2xys(head->buf, mapbits, metabits, head->zoom, head->components, x, y, &meta);
if (all) {
dump_out(all, x, y, head->components, metabits, meta);
} else {
long long xx = x[0], yy = y[0];
handle(xx, yy, tile, fname, minzoom, maxzoom, showdist, x, y, files, sibling, verbose,
usebounds ? &bounds : NULL);
}
if (fread(head->buf, head->bytes, 1, head->f) != 1) {
memset(head->buf, 0xFF, bytes);
head->done = 1;
head = head->next;
} else {
size_read += head->bytes;
struct file *m = head;
head = m->next;
m->next = NULL;
insert(m, &head, bytes);
if (100 * size_read / size_total != size_progress) {
fprintf(stderr, "enumerate: %lld%% \r", 100 * size_read / size_total);
size_progress = 100 * size_read / size_total;
}
}
}
if (all) {
dump_end(all);
} else {
handle(-1, -1, tile, fname, minzoom, maxzoom, showdist, NULL, NULL, files, sibling, verbose,
usebounds ? &bounds : NULL);
}
return 0;
}