From 555ababd2e0d792500707846c8028181b1907fcc Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Wed, 20 May 2015 14:57:00 -0700 Subject: [PATCH] Add a flag to dynamically drop a fraction of features from large tiles --- README.md | 1 + tile.cc | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fa6382c4..f7969517 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ Options * -pk: Don't limit tiles to 500K bytes * -po: Don't reorder features to put the same properties in sequence * -pl: Let "dot" simplification apply to lines too + * -pd: Dynamically drop some fraction of features from large tiles to keep them under the 500K size limit. It will probably look ugly at the tile boundaries. Example ------- diff --git a/tile.cc b/tile.cc index b1ffff9c..887f50e0 100644 --- a/tile.cc +++ b/tile.cc @@ -354,6 +354,7 @@ long long write_tile(char **geoms, char *metabase, unsigned *file_bbox, int z, u int line_detail; static bool evaluated = false; double oprogress = 0; + double fraction = 1; char *og = *geoms; @@ -381,6 +382,8 @@ long long write_tile(char **geoms, char *metabase, unsigned *file_bbox, int z, u interval = exp(log(droprate) * (basezoom - z)); } + double fraction_accum = 0; + unsigned long long previndex = 0; double scale = (double) (1LL << (64 - 2 * (z + 8))); double gap = 0; @@ -552,6 +555,12 @@ long long write_tile(char **geoms, char *metabase, unsigned *file_bbox, int z, u } } + fraction_accum += fraction; + if (fraction_accum < 1) { + continue; + } + fraction_accum -= 1; + bool reduced = false; if (t == VT_POLYGON) { geom = reduce_tiny_poly(geom, z, line_detail, &reduced, &accum_area); @@ -611,7 +620,7 @@ long long write_tile(char **geoms, char *metabase, unsigned *file_bbox, int z, u } for (j = 0; j < nlayers; j++) { - if (!prevent['o'] & 0xFF) { + if (!prevent['o' & 0xFF]) { std::sort(features[j].begin(), features[j].end()); } @@ -681,6 +690,11 @@ long long write_tile(char **geoms, char *metabase, unsigned *file_bbox, int z, u evaluate(features[0], metabase, file_keys[0], layername, line_detail, compressed.size()); // XXX layer #endif } + + if (prevent['d' & 0xFF]) { + fraction = fraction * 500000 / compressed.size(); + fprintf(stderr, "Going to try keeping %0.2f%% of the features to make it fit\n", fraction * 100); + } } else { mbtiles_write_tile(outdb, z, tx, ty, compressed.data(), compressed.size()); return count;