Add an option to decode only specified layers

This commit is contained in:
Eric Fischer 2017-02-27 10:10:28 -08:00
parent 30a987d9ba
commit 835fafe30a

View File

@ -6,6 +6,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <map> #include <map>
#include <set>
#include <zlib.h> #include <zlib.h>
#include <math.h> #include <math.h>
#include <fcntl.h> #include <fcntl.h>
@ -49,7 +50,7 @@ struct lonlat {
} }
}; };
void handle(std::string message, int z, unsigned x, unsigned y, int describe) { void handle(std::string message, int z, unsigned x, unsigned y, int describe, std::set<std::string> const &to_decode) {
int within = 0; int within = 0;
mvt_tile tile; mvt_tile tile;
@ -77,12 +78,17 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
printf(", \"features\": [\n"); printf(", \"features\": [\n");
bool first_layer = true;
for (size_t l = 0; l < tile.layers.size(); l++) { for (size_t l = 0; l < tile.layers.size(); l++) {
mvt_layer &layer = tile.layers[l]; mvt_layer &layer = tile.layers[l];
int extent = layer.extent; int extent = layer.extent;
if (to_decode.size() != 0 && !to_decode.count(layer.name)) {
continue;
}
if (describe) { if (describe) {
if (l != 0) { if (!first_layer) {
printf(",\n"); printf(",\n");
} }
@ -93,6 +99,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
printf(" }"); printf(" }");
printf(", \"features\": [\n"); printf(", \"features\": [\n");
first_layer = false;
within = 0; within = 0;
} }
@ -333,7 +340,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
printf("] }\n"); printf("] }\n");
} }
void decode(char *fname, int z, unsigned x, unsigned y) { void decode(char *fname, int z, unsigned x, unsigned y, std::set<std::string> const &to_decode) {
sqlite3 *db; sqlite3 *db;
int oz = z; int oz = z;
unsigned ox = x, oy = y; unsigned ox = x, oy = y;
@ -348,7 +355,7 @@ void decode(char *fname, int z, unsigned x, unsigned y) {
if (strcmp(map, "SQLite format 3") != 0) { if (strcmp(map, "SQLite format 3") != 0) {
if (z >= 0) { if (z >= 0) {
std::string s = std::string(map, st.st_size); std::string s = std::string(map, st.st_size);
handle(s, z, x, y, 1); handle(s, z, x, y, 1, to_decode);
munmap(map, st.st_size); munmap(map, st.st_size);
return; return;
} else { } else {
@ -425,7 +432,7 @@ void decode(char *fname, int z, unsigned x, unsigned y) {
ty = (1LL << tz) - 1 - ty; ty = (1LL << tz) - 1 - ty;
const char *s = (const char *) sqlite3_column_blob(stmt, 0); const char *s = (const char *) sqlite3_column_blob(stmt, 0);
handle(std::string(s, len), tz, tx, ty, 1); handle(std::string(s, len), tz, tx, ty, 1, to_decode);
} }
printf("] }\n"); printf("] }\n");
@ -453,7 +460,7 @@ void decode(char *fname, int z, unsigned x, unsigned y) {
fprintf(stderr, "%s: Warning: using tile %d/%u/%u instead of %d/%u/%u\n", fname, z, x, y, oz, ox, oy); fprintf(stderr, "%s: Warning: using tile %d/%u/%u instead of %d/%u/%u\n", fname, z, x, y, oz, ox, oy);
} }
handle(std::string(s, len), z, x, y, 0); handle(std::string(s, len), z, x, y, 0, to_decode);
handled = 1; handled = 1;
} }
@ -472,7 +479,7 @@ void decode(char *fname, int z, unsigned x, unsigned y) {
} }
void usage(char **argv) { void usage(char **argv) {
fprintf(stderr, "Usage: %s [-t projection] [-Z minzoom] [-z maxzoom] file.mbtiles [zoom x y]\n", argv[0]); fprintf(stderr, "Usage: %s [-t projection] [-Z minzoom] [-z maxzoom] [-l layer ...] file.mbtiles [zoom x y]\n", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -480,8 +487,9 @@ int main(int argc, char **argv) {
extern int optind; extern int optind;
extern char *optarg; extern char *optarg;
int i; int i;
std::set<std::string> to_decode;
while ((i = getopt(argc, argv, "t:Z:z:")) != -1) { while ((i = getopt(argc, argv, "t:Z:z:l:")) != -1) {
switch (i) { switch (i) {
case 't': case 't':
set_projection_or_exit(optarg); set_projection_or_exit(optarg);
@ -495,15 +503,19 @@ int main(int argc, char **argv) {
minzoom = atoi(optarg); minzoom = atoi(optarg);
break; break;
case 'l':
to_decode.insert(optarg);
break;
default: default:
usage(argv); usage(argv);
} }
} }
if (argc == optind + 4) { if (argc == optind + 4) {
decode(argv[optind], atoi(argv[optind + 1]), atoi(argv[optind + 2]), atoi(argv[optind + 3])); decode(argv[optind], atoi(argv[optind + 1]), atoi(argv[optind + 2]), atoi(argv[optind + 3]), to_decode);
} else if (argc == optind + 1) { } else if (argc == optind + 1) {
decode(argv[optind], -1, -1, -1); decode(argv[optind], -1, -1, -1, to_decode);
} else { } else {
usage(argv); usage(argv);
} }