From bdfb06cd6ab73e37319069b3a7b1e1d4e83c53d6 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Thu, 5 Dec 2024 12:05:50 -0800 Subject: [PATCH] Make tippecanoe-overzoom accept filters from a file (#307) * Make tippecanoe-overzoom accept filters from a file * Accept clip polygons from a file too * Add test of clipping by polygon from file * Add a test of reading an overzoom filter from a file * Update version and changelog --- CHANGELOG.md | 4 ++++ Makefile | 10 ++++++++++ overzoom.cpp | 32 +++++++++++++++++++++++++++++++ tests/pbf/clip-poly.json | 1 + tests/pbf/scalerank-0-filter.json | 1 + version.hpp | 2 +- 6 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/pbf/clip-poly.json create mode 100644 tests/pbf/scalerank-0-filter.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 20edf8fa..81ef86c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.72.0 + +* Add --clip-polygon-file and --feature-filter-file options to tippecanoe-overzoom + # 2.71.0 * Add --clip-bounding-box and --clip-polygon options to tippecanoe-overzoom diff --git a/Makefile b/Makefile index 7f521ea3..d01eb42b 100644 --- a/Makefile +++ b/Makefile @@ -326,6 +326,11 @@ overzoom-test: tippecanoe-overzoom ./tippecanoe-decode tests/pbf/0-0-0-pop-expr.pbf 0 0 0 > tests/pbf/0-0-0-pop-expr.pbf.json.check cmp tests/pbf/0-0-0-pop-expr.pbf.json.check tests/pbf/0-0-0-pop-expr.pbf.json rm tests/pbf/0-0-0-pop-expr.pbf tests/pbf/0-0-0-pop-expr.pbf.json.check + # Same filter test, but reading the filter from a file + ./tippecanoe-overzoom -y NAME -J tests/pbf/scalerank-0-filter.json -o tests/pbf/0-0-0-pop-expr.pbf tests/pbf/0-0-0-pop.pbf 0/0/0 0/0/0 + ./tippecanoe-decode tests/pbf/0-0-0-pop-expr.pbf 0 0 0 > tests/pbf/0-0-0-pop-expr.pbf.json.check + cmp tests/pbf/0-0-0-pop-expr.pbf.json.check tests/pbf/0-0-0-pop-expr.pbf.json + rm tests/pbf/0-0-0-pop-expr.pbf tests/pbf/0-0-0-pop-expr.pbf.json.check # Filtering with multiplier # 243 features in the source tile tests/pbf/0-0-0-pop.pbf # 8 features survive into the output, from 9 clusters of 30 @@ -382,6 +387,11 @@ overzoom-test: tippecanoe-overzoom ./tippecanoe-decode tests/pbf/bin-11-327-791-ids-clip.pbf.out 11 327 791 > tests/pbf/bin-11-327-791-ids-clip.pbf.out.json.check cmp tests/pbf/bin-11-327-791-ids-clip.pbf.out.json.check tests/pbf/bin-11-327-791-ids-clip.pbf.out.json rm tests/pbf/bin-11-327-791-ids-clip.pbf.out.json.check tests/pbf/bin-11-327-791-ids-clip.pbf.out + # Binning by id, clipping by polygon from file + ./tippecanoe-overzoom -o tests/pbf/bin-11-327-791-ids-clip.pbf.out --clip-polygon-file=tests/pbf/clip-poly.json --assign-to-bins tests/pbf/sf-zips.json --bin-by-id-list bin-ids tests/pbf/yearbuilt.pbf 11/327/791 11/327/791 + ./tippecanoe-decode tests/pbf/bin-11-327-791-ids-clip.pbf.out 11 327 791 > tests/pbf/bin-11-327-791-ids-clip.pbf.out.json.check + cmp tests/pbf/bin-11-327-791-ids-clip.pbf.out.json.check tests/pbf/bin-11-327-791-ids-clip.pbf.out.json + rm tests/pbf/bin-11-327-791-ids-clip.pbf.out.json.check tests/pbf/bin-11-327-791-ids-clip.pbf.out # Binning by id, attribute stripping # Note that it still works even if we exclude the ID that we are binning by ./tippecanoe-overzoom -yZCTA5CE10 -ytippecanoe:count -o tests/pbf/bin-11-327-791-ids-zip.pbf.out --assign-to-bins tests/pbf/sf-zips.json --bin-by-id-list bin-ids tests/pbf/yearbuilt.pbf 11/327/791 11/327/791 diff --git a/overzoom.cpp b/overzoom.cpp index b6fa1b56..112f0187 100644 --- a/overzoom.cpp +++ b/overzoom.cpp @@ -40,6 +40,26 @@ void usage(char **argv) { exit(EXIT_FAILURE); } +std::string read_json_file(const char *fname) { + std::string out; + + FILE *f = fopen(fname, "r"); + if (f == NULL) { + perror(optarg); + exit(EXIT_OPEN); + } + + char buf[2000]; + size_t nread; + while ((nread = fread(buf, sizeof(char), 2000, f)) != 0) { + out += std::string(buf, nread); + } + + fclose(f); + + return out; +} + int main(int argc, char **argv) { int i; const char *outtile = NULL; @@ -60,6 +80,7 @@ int main(int argc, char **argv) { {"output", required_argument, 0, 'o'}, {"filter-points-multiplier", no_argument, 0, 'm'}, {"feature-filter", required_argument, 0, 'j'}, + {"feature-filter-file", required_argument, 0, 'J'}, {"preserve-input-order", no_argument, 0, 'o' & 0x1F}, {"accumulate-attribute", required_argument, 0, 'E'}, {"unidecode-data", required_argument, 0, 'u' & 0x1F}, @@ -72,6 +93,7 @@ int main(int argc, char **argv) { {"no-tile-compression", no_argument, 0, 'd' & 0x1F}, {"clip-bounding-box", required_argument, 0, 'k' & 0x1F}, {"clip-polygon", required_argument, 0, 'l' & 0x1F}, + {"clip-polygon-file", required_argument, 0, 'm' & 0x1F}, {0, 0, 0, 0}, }; @@ -122,6 +144,10 @@ int main(int argc, char **argv) { filter = optarg; break; + case 'J': + filter = read_json_file(optarg); + break; + case 'o' & 0x1F: preserve_input_order = true; break; @@ -181,6 +207,12 @@ int main(int argc, char **argv) { break; } + case 'm' & 0x1F: { + clipbbox clip = parse_clip_poly(read_json_file(optarg)); + clipbboxes.push_back(clip); + break; + } + default: fprintf(stderr, "Unrecognized flag -%c\n", i); usage(argv); diff --git a/tests/pbf/clip-poly.json b/tests/pbf/clip-poly.json new file mode 100644 index 00000000..26fa7355 --- /dev/null +++ b/tests/pbf/clip-poly.json @@ -0,0 +1 @@ +{"coordinates":[[[-122.4527379,37.8128815],[-122.4598853,37.7834743],[-122.4280914,37.7959397],[-122.4527379,37.8128815]]],"type":"Polygon"} diff --git a/tests/pbf/scalerank-0-filter.json b/tests/pbf/scalerank-0-filter.json new file mode 100644 index 00000000..c2b996d4 --- /dev/null +++ b/tests/pbf/scalerank-0-filter.json @@ -0,0 +1 @@ +{"*":["SCALERANK","eq",0]} diff --git a/version.hpp b/version.hpp index a67e7576..bf022696 100644 --- a/version.hpp +++ b/version.hpp @@ -1,6 +1,6 @@ #ifndef VERSION_HPP #define VERSION_HPP -#define VERSION "v2.71.0" +#define VERSION "v2.72.0" #endif