From 7243ae76c48161c3b5a20be78ea8f8792ccbbf17 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Fri, 23 Aug 2024 14:10:24 -0700 Subject: [PATCH] Smallest enclosing tile from bbox --- Makefile | 2 +- clip.cpp | 27 +++++++++++++++++++++++++-- geometry.hpp | 3 +++ unit.cpp | 7 +++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 45f3949f..8891141b 100644 --- a/Makefile +++ b/Makefile @@ -74,7 +74,7 @@ tile-join: tile-join.o projection.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpul tippecanoe-json-tool: jsontool.o jsonpull/jsonpull.o csv.o text.o geojson-loop.o $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread -unit: unit.o text.o sort.o mvt.o projection.o +unit: unit.o text.o sort.o mvt.o projection.o clip.o attribute.o jsonpull/jsonpull.o evaluator.o $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread tippecanoe-overzoom: overzoom.o mvt.o clip.o evaluator.o jsonpull/jsonpull.o text.o attribute.o read_json.o projection.o diff --git a/clip.cpp b/clip.cpp index c9db2938..10fc91d9 100644 --- a/clip.cpp +++ b/clip.cpp @@ -11,6 +11,7 @@ #include "evaluator.hpp" #include "serial.hpp" #include "attribute.hpp" +#include "projection.hpp" static std::vector> clip_poly1(std::vector> &geom, long long minx, long long miny, long long maxx, long long maxy, @@ -1184,6 +1185,28 @@ struct index_event { } }; +void get_quadkey_bounds(long long xmin, long long ymin, long long xmax, long long ymax, + unsigned long long *start, unsigned long long *end) { + if (xmin < 0 || ymin < 0 || xmax >= 1LL << 32 || ymax >= 1LL << 32) { + *start = 0; + *end = ULLONG_MAX; + return; + } + + *start = encode_quadkey(xmin, ymin); + *end = encode_quadkey(xmax, ymax); + + for (ssize_t i = 62; i >= 0; i -= 2) { + if ((*start & (3LL << i)) != (*end & (3LL << i))) { + for (; i >= 0; i -= 2) { + *start &= ~(3LL << i); + *end |= 3LL << i; + } + break; + } + } +} + mvt_tile assign_to_bins(mvt_tile const &features, std::vector const &bins, int z, int x, int y, int detail) { std::vector events; @@ -1194,7 +1217,7 @@ mvt_tile assign_to_bins(mvt_tile const &features, std::vector const & unsigned long long start, end; get_bbox(bins[i].features[j].geometry, &xmin, &ymin, &xmax, &ymax, z, x, y, detail); - // get_quadkey_bounds(xmin, xmax, ymin, ymax, &start, &end, z, x, y, detail); + get_quadkey_bounds(xmin, xmax, ymin, ymax, &start, &end); events.emplace_back(start, index_event::ENTER, i, j); events.emplace_back(end, index_event::EXIT, i, j); } @@ -1207,7 +1230,7 @@ mvt_tile assign_to_bins(mvt_tile const &features, std::vector const & unsigned long long start, end; get_bbox(bins[i].features[j].geometry, &xmin, &ymin, &xmax, &ymax, z, x, y, detail); - // get_quadkey_bounds(xmin, xmax, ymin, ymax, &start, &end, z, x, y, detail); + get_quadkey_bounds(xmin, xmax, ymin, ymax, &start, &end); events.emplace_back(start, index_event::CHECK, i, j); } } diff --git a/geometry.hpp b/geometry.hpp index 2af8307f..d82d1d6f 100644 --- a/geometry.hpp +++ b/geometry.hpp @@ -133,4 +133,7 @@ std::string overzoom(std::vector const &tiles, int nz, int nx, int n draw center_of_mass_mp(const drawvec &dv); +void get_quadkey_bounds(long long xmin, long long ymin, long long xmax, long long ymax, + unsigned long long *start, unsigned long long *end); + #endif diff --git a/unit.cpp b/unit.cpp index 8d864808..c8f36491 100644 --- a/unit.cpp +++ b/unit.cpp @@ -5,6 +5,7 @@ #include "tile-cache.hpp" #include "mvt.hpp" #include "projection.hpp" +#include "geometry.hpp" #include #include @@ -144,4 +145,10 @@ TEST_CASE("mvt_geometry bbox") { tile2lonlat(xmax, ymax, 32, &lon, &lat); REQUIRE(std::to_string(lon) == "-122.431641"); REQUIRE(std::to_string(lat) == "37.788081"); + + unsigned long long start, end; + get_quadkey_bounds(xmin, ymin, xmax, ymax, &start, &end); + // 22 bits in common, for z11 + REQUIRE(start == 0x1c84fc0000000000); + REQUIRE(end == 0x1c84ffffffffffff); }