From ac34a292e9d5d4dc5d16673e7e05f329e1c80a47 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Thu, 9 Oct 2014 15:11:43 -0700 Subject: [PATCH] Add an flag to remove all properties and use only geometry --- geojson.c | 17 +++++++++++------ tile.cc | 23 +++++++++++++++++++++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/geojson.c b/geojson.c index edba8de..662b32e 100644 --- a/geojson.c +++ b/geojson.c @@ -282,7 +282,7 @@ void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, s fprintf(stderr, "\n"); } -void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom, sqlite3 *outdb, struct pool *exclude, double droprate) { +void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom, sqlite3 *outdb, struct pool *exclude, int exclude_all, double droprate) { char metaname[] = "/tmp/meta.XXXXXXXX"; char indexname[] = "/tmp/index.XXXXXXXX"; @@ -370,7 +370,7 @@ void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom, int i; for (i = 0; i < properties->length; i++) { if (properties->keys[i]->type == JSON_STRING) { - if (is_pooled(exclude, properties->keys[i]->string, VT_STRING)) { + if (exclude_all || is_pooled(exclude, properties->keys[i]->string, VT_STRING)) { continue; } @@ -549,8 +549,9 @@ int main(int argc, char **argv) { struct pool exclude; pool_init(&exclude, 0); + int exclude_all = 0; - while ((i = getopt(argc, argv, "l:n:z:Z:d:D:o:x:r:f")) != -1) { + while ((i = getopt(argc, argv, "l:n:z:Z:d:D:o:x:r:fX")) != -1) { switch (i) { case 'n': name = optarg; @@ -584,6 +585,10 @@ int main(int argc, char **argv) { pool(&exclude, optarg, VT_STRING); break; + case 'X': + exclude_all = 1; + break; + case 'r': droprate = atof(optarg); break; @@ -593,7 +598,7 @@ int main(int argc, char **argv) { break; default: - fprintf(stderr, "Usage: %s -o out.mbtiles [-n name] [-l layername] [-z maxzoom] [-Z minzoom] [-d detail] [-D lower-detail] [-x excluded-field ...] [-r droprate] [file.json]\n", argv[0]); + fprintf(stderr, "Usage: %s -o out.mbtiles [-n name] [-l layername] [-z maxzoom] [-Z minzoom] [-d detail] [-D lower-detail] [-x excluded-field ...] [-X] [-r droprate] [file.json]\n", argv[0]); exit(EXIT_FAILURE); } } @@ -616,7 +621,7 @@ int main(int argc, char **argv) { if (f == NULL) { fprintf(stderr, "%s: %s: %s\n", argv[0], argv[i], strerror(errno)); } else { - read_json(f, name ? name : argv[i], layer, maxzoom, minzoom, outdb, &exclude, droprate); + read_json(f, name ? name : argv[i], layer, maxzoom, minzoom, outdb, &exclude, exclude_all, droprate); fclose(f); } } @@ -624,7 +629,7 @@ int main(int argc, char **argv) { fprintf(stderr, "%s: Only accepts one input file\n", argv[0]); exit(EXIT_FAILURE); } else { - read_json(stdin, name ? name : outdir, layer, maxzoom, minzoom, outdb, &exclude, droprate); + read_json(stdin, name ? name : outdir, layer, maxzoom, minzoom, outdb, &exclude, exclude_all, droprate); } mbtiles_close(outdb, argv); diff --git a/tile.cc b/tile.cc index 403f764..d1bbf7d 100644 --- a/tile.cc +++ b/tile.cc @@ -21,6 +21,7 @@ extern "C" { } #define CMD_BITS 3 +#define MIN_DETAIL 7 // https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp static inline int compress(std::string const& input, std::string& output) { @@ -835,13 +836,31 @@ void evaluate(std::vector &features, char *metabase, struct pool *file fprintf(stderr, "using -x %s would save about %lld, for a tile size of of %lld\n", options[i].name, options[i].val, orig - options[i].val); } } + + struct pool keys, values; + pool_init(&keys, 0); + pool_init(&values, 0); + long long count = 0; + + std::vector empty; + mapnik::vector::tile tile = create_tile(layername, line_detail, features, &count, &keys, &values); + + std::string s; + std::string compressed; + + tile.SerializeToString(&s); + compress(s, compressed); + fprintf(stderr, "geometry alone (-X) would be %lld\n", (long long) compressed.size()); + + pool_free(&values); + pool_free(&keys); } long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned tx, unsigned ty, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb, double droprate) { int line_detail; static bool evaluated = false; - for (line_detail = detail; line_detail >= 7; line_detail--) { + for (line_detail = detail; line_detail >= MIN_DETAIL; line_detail--) { GOOGLE_PROTOBUF_VERIFY_VERSION; struct pool keys, values, dup; @@ -963,7 +982,7 @@ long long write_tile(struct index *start, struct index *end, char *metabase, uns if (compressed.size() > 500000) { fprintf(stderr, "tile %d/%u/%u size is %lld with detail %d, >500000 \n", z, tx, ty, (long long) compressed.size(), line_detail); - if (!evaluated) { + if (line_detail == MIN_DETAIL || !evaluated) { evaluated = true; evaluate(features, metabase, file_keys, layername, line_detail, compressed.size()); }