forked from mapbox/tile-count
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.cpp
333 lines (275 loc) · 6.82 KB
/
create.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <limits.h>
#include "tippecanoe/projection.hpp"
#include "header.hpp"
#include "serial.hpp"
#include "merge.hpp"
extern "C" {
#include "jsonpull/jsonpull.h"
}
bool quiet = false;
void usage(char **argv) {
fprintf(stderr, "Usage: %s -o out.count [-s binsize] [in.csv ...]\n", argv[0]);
}
void write_point(FILE *out, long long &seq, double lon, double lat, unsigned long long count) {
if (seq % 100000 == 0) {
if (!quiet) {
fprintf(stderr, "Read %.1f million records\r", seq / 1000000.0);
}
}
seq++;
long long x, y;
projection->project(lon, lat, 32, &x, &y);
if (x >= 0 && y >= 0 && x < (1LL << 32) && y < (1LL << 32)) {
unsigned long long index = encode(x, y);
while (count > MAX_COUNT) {
write64(out, index);
write32(out, MAX_COUNT);
count -= MAX_COUNT;
}
write64(out, index);
write32(out, count);
}
}
void read_json(FILE *out, FILE *in, const char *fname, long long &seq) {
json_pull *jp = json_begin_file(in);
while (1) {
json_object *j = json_read(jp);
if (j == NULL) {
if (jp->error != NULL) {
fprintf(stderr, "%s:%d: %s\n", fname, jp->line, jp->error);
}
json_free(jp->root);
break;
}
if (j->type == JSON_HASH) {
json_free(j);
} else if (j->type == JSON_ARRAY) {
if (j->length >= 2) {
if (j->array[0]->type == JSON_NUMBER && j->array[1]->type == JSON_NUMBER) {
write_point(out, seq, j->array[0]->number, j->array[1]->number, 1);
}
}
json_free(j);
}
}
json_end(jp);
}
void read_into(FILE *out, FILE *in, const char *fname, long long &seq) {
int c = getc(in);
if (c != EOF) {
ungetc(c, in);
}
if (c == '{') {
read_json(out, in, fname, seq);
return;
}
size_t line = 0;
char s[2000];
while (fgets(s, 2000, in)) {
double lon, lat;
long long count;
line++;
size_t n = sscanf(s, "%lf,%lf,%lld", &lon, &lat, &count);
if (n == 2) {
count = 1;
} else if (n != 3) {
fprintf(stderr, "%s:%zu: Can't understand %s", fname, line, s);
continue;
}
if (count < 0) {
fprintf(stderr, "%s:%zu: Count is negative in %s\n", fname, line, s);
exit(EXIT_FAILURE);
}
if (count > (long long) MAX_COUNT) {
fprintf(stderr, "%s:%zu: Count is too large in %s\n", fname, line, s);
exit(EXIT_FAILURE);
}
write_point(out, seq, lon, lat, count);
}
}
int indexcmp(const void *p1, const void *p2) {
return memcmp(p1, p2, INDEX_BYTES);
}
void *run_sort(void *p) {
struct merge *m = (struct merge *) p;
void *map = mmap(NULL, m->end - m->start, PROT_READ | PROT_WRITE, MAP_PRIVATE, m->fd, m->start);
if (map == MAP_FAILED) {
perror("mmap (sort)");
exit(EXIT_FAILURE);
}
qsort(map, (m->end - m->start) / RECORD_BYTES, RECORD_BYTES, indexcmp);
// Sorting and then copying avoids the need to
// write out intermediate stages of the sort.
void *map2 = mmap(NULL, m->end - m->start, PROT_READ | PROT_WRITE, MAP_SHARED, m->fd, m->start);
if (map2 == MAP_FAILED) {
perror("mmap (write)");
exit(EXIT_FAILURE);
}
memcpy(map2, map, m->end - m->start);
munmap(map, m->end - m->start);
munmap(map2, m->end - m->start);
return NULL;
}
void sort_and_merge(int fd, int out, int zoom, size_t cpus) {
struct stat st;
if (fstat(fd, &st) < 0) {
perror("stat");
exit(EXIT_FAILURE);
}
if (st.st_size % RECORD_BYTES != 0) {
fprintf(stderr, "File size not a multiple of record length\n");
exit(EXIT_FAILURE);
}
long long to_sort = st.st_size;
int bytes = RECORD_BYTES;
int page = sysconf(_SC_PAGESIZE);
long long unit = (50 * 1024 * 1024 / bytes) * bytes;
while (unit % page != 0) {
unit += bytes;
}
size_t nmerges = (to_sort + unit - 1) / unit;
struct merge merges[nmerges];
long long start;
for (start = 0; start < to_sort; start += unit) {
long long end = start + unit;
if (end > to_sort) {
end = to_sort;
}
merges[start / unit].start = start;
merges[start / unit].end = end;
merges[start / unit].fd = fd;
}
for (size_t i = 0; i < nmerges; i += cpus) {
if (!quiet) {
fprintf(stderr, "Sorting part %zu of %zu \r", i + 1, nmerges);
}
pthread_t pthreads[cpus];
for (size_t j = 0; j < cpus && i + j < nmerges; j++) {
if (pthread_create(&pthreads[j], NULL, run_sort, &merges[i + j]) != 0) {
perror("pthread_create (sort)");
exit(EXIT_FAILURE);
}
}
for (size_t j = 0; j < cpus && i + j < nmerges; j++) {
void *retval;
if (pthread_join(pthreads[j], &retval) != 0) {
perror("pthread_join (sort)");
exit(EXIT_FAILURE);
}
}
}
if (write(out, header_text, HEADER_LEN) != HEADER_LEN) {
perror("write header");
exit(EXIT_FAILURE);
}
if (to_sort > 0) {
void *map = mmap(NULL, to_sort, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
perror("mmap (for merge)");
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < nmerges; i++) {
merges[i].map = (unsigned char *) map;
}
do_merge(merges, nmerges, out, bytes, to_sort / bytes, zoom, quiet, cpus, 0, 0);
munmap(map, st.st_size);
}
}
int main(int argc, char **argv) {
extern int optind;
extern char *optarg;
char *outfile = NULL;
int zoom = 32;
size_t cpus = sysconf(_SC_NPROCESSORS_ONLN);
int i;
while ((i = getopt(argc, argv, "fs:o:p:q")) != -1) {
switch (i) {
case 's':
zoom = atoi(optarg);
break;
case 'o':
outfile = optarg;
break;
case 'p':
cpus = atoi(optarg);
break;
case 'q':
quiet = true;
break;
default:
usage(argv);
exit(EXIT_FAILURE);
}
}
if (outfile == NULL) {
usage(argv);
exit(EXIT_FAILURE);
}
if (zoom < 0 || zoom > 32) {
fprintf(stderr, "%s: zoom (-s) must be in the range 0 to 32, not %d\n", argv[0], zoom);
exit(EXIT_FAILURE);
}
int fd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, 0777);
if (fd < 0) {
perror(outfile);
exit(EXIT_FAILURE);
}
int fd2 = dup(fd);
if (fd2 < 0) {
perror("dup output file");
exit(EXIT_FAILURE);
}
FILE *fp = fdopen(fd2, "wb");
if (fp == NULL) {
perror("fdopen output file");
exit(EXIT_FAILURE);
}
if (unlink(outfile) != 0) {
perror("unlink output file");
exit(EXIT_FAILURE);
}
long long seq = 0;
if (optind == argc) {
read_into(fp, stdin, "standard input", seq);
} else {
for (; optind < argc; optind++) {
FILE *in = fopen(argv[optind], "r");
if (in == NULL) {
perror(argv[optind]);
exit(EXIT_FAILURE);
} else {
read_into(fp, in, argv[optind], seq);
fclose(in);
}
}
}
if (!quiet) {
fprintf(stderr, "Total of %lld\n", seq);
}
if (fflush(fp) != 0) {
perror("flush output file");
exit(EXIT_FAILURE);
}
if (fclose(fp) != 0) {
perror("close output file");
exit(EXIT_FAILURE);
}
int f = open(outfile, O_CREAT | O_TRUNC | O_RDWR, 0777);
if (f < 0) {
perror(outfile);
exit(EXIT_FAILURE);
}
sort_and_merge(fd, f, zoom, cpus);
if (close(f) != 0) {
perror("close");
}
return 0;
}