forked from felt/tippecanoe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumerate.cpp
74 lines (58 loc) · 1.69 KB
/
enumerate.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sqlite3.h>
#include "errors.hpp"
void enumerate(char *fname) {
sqlite3 *db;
if (sqlite3_open(fname, &db) != SQLITE_OK) {
fprintf(stderr, "%s: %s\n", fname, sqlite3_errmsg(db));
exit(EXIT_OPEN);
}
char *err = NULL;
if (sqlite3_exec(db, "PRAGMA integrity_check;", NULL, NULL, &err) != SQLITE_OK) {
fprintf(stderr, "%s: integrity_check: %s\n", fname, err);
exit(EXIT_SQLITE);
}
const char *sql = "SELECT zoom_level, tile_column, tile_row from tiles order by zoom_level, tile_column, tile_row;";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) {
fprintf(stderr, "%s: select failed: %s\n", fname, sqlite3_errmsg(db));
exit(EXIT_SQLITE);
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
long long zoom = sqlite3_column_int(stmt, 0);
long long x = sqlite3_column_int(stmt, 1);
long long y = sqlite3_column_int(stmt, 2);
if (zoom < 0 || zoom > 31) {
fprintf(stderr, "Corrupt mbtiles file: impossible zoom level %lld\n", zoom);
exit(EXIT_IMPOSSIBLE);
}
y = (1LL << zoom) - 1 - y;
printf("%s %lld %lld %lld\n", fname, zoom, x, y);
}
sqlite3_finalize(stmt);
if (sqlite3_close(db) != SQLITE_OK) {
fprintf(stderr, "%s: could not close database: %s\n", fname, sqlite3_errmsg(db));
exit(EXIT_CLOSE);
}
}
void usage(char **argv) {
fprintf(stderr, "Usage: %s file.mbtiles ...\n", argv[0]);
exit(EXIT_ARGS);
}
int main(int argc, char **argv) {
extern int optind;
// extern char *optarg;
int i;
while ((i = getopt(argc, argv, "")) != -1) {
usage(argv);
}
if (optind >= argc) {
usage(argv);
}
for (i = optind; i < argc; i++) {
enumerate(argv[i]);
}
return 0;
}