From 4e1eacc5b2d78c04513fb1f76cf7862f15018f46 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Thu, 23 Oct 2014 14:04:57 -0700 Subject: [PATCH] Simple enumerate command like the datamaps one --- Makefile | 5 ++++- enumerate.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 enumerate.c diff --git a/Makefile b/Makefile index 736dd5c3..9e53d17f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PREFIX=/usr/local -all: tippecanoe +all: tippecanoe enumerate install: tippecanoe cp tippecanoe $(PREFIX)/bin/tippecanoe @@ -13,6 +13,9 @@ PG= tippecanoe: geojson.o jsonpull.o vector_tile.pb.o tile.o clip.o pool.o mbtiles.o g++ $(PG) -O3 -g -Wall -o $@ $^ -lm -lz -lprotobuf-lite -lsqlite3 +enumerate: enumerate.o + gcc $(PG) -O3 -g -Wall -o $@ $^ -lsqlite3 + libjsonpull.a: jsonpull.o ar rc $@ $^ ranlib $@ diff --git a/enumerate.c b/enumerate.c new file mode 100644 index 00000000..c2c319a4 --- /dev/null +++ b/enumerate.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +void enumerate(char *fname) { + sqlite3 *db; + + if (sqlite3_open(fname, &db) != SQLITE_OK) { + fprintf(stderr, "%s: %s\n", fname, sqlite3_errmsg(db)); + exit(EXIT_FAILURE); + } + + char *sql = "SELECT zoom_level, tile_column, tile_row from tiles;"; + + 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_FAILURE); + } + + 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); + + y = (1LL << zoom) - 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_FAILURE); + } +} + +void usage(char **argv) { + fprintf(stderr, "Usage: %s file.mbtiles ...\n", argv[0]); + exit(EXIT_FAILURE); +} + +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; +}