Generalize overzooming functions to multiple tiles

This commit is contained in:
Erica Fischer
2023-12-07 11:25:06 -08:00
parent d7bdbe363b
commit 1e2813ed65
10 changed files with 263 additions and 156 deletions
+4
View File
@@ -1,3 +1,7 @@
# 2.38.0
* `tippecanoe-overzoom` can now merge multiple tiles when overzooming them
# 2.37.1 # 2.37.1
* Reduce maximum memory used for vertex sorting * Reduce maximum memory used for vertex sorting
+6 -1
View File
@@ -984,10 +984,15 @@ to turn those into moderate detail tiles at high zoom levels, for the benefit of
renderers that cannot internally overzoom high-resolution tiles without losing renderers that cannot internally overzoom high-resolution tiles without losing
some of the precision. Running: some of the precision. Running:
tippecanoe-overzoom -o out.mvt.gz inz/inx/iny outz/outx/outy in.mvt.gz tippecanoe-overzoom -o out.mvt.gz in.mvt.gz inz/inx/iny outz/outx/outy
reads tile `inz/inx/iny` of `in.mvt.gz` and produces tile `outz/outx/outy` of `out.mvt.gz`. reads tile `inz/inx/iny` of `in.mvt.gz` and produces tile `outz/outx/outy` of `out.mvt.gz`.
tippecanoe-overzoom -o out.mvt.gz -t outz/outx/outy in.mvt.gz inz/inx/iny in2.mvt.gz in2z/in2x/in2y in3.mvt.gz in3z/in3x/in3y
reads tile `inz/inx/iny` of `in.mvt.gz`, tile `in2z/in2x/in2y` of `in2.mvt.gz`, and tile `in3z/in3x/in3y` of `in3.mvt.gz`,
and produces tile `outz/outx/outy` of `out.mvt.gz` from them.
### Options ### Options
* `-b` *buffer*: Set the tile buffer in the output tile (default 5) * `-b` *buffer*: Set the tile buffer in the output tile (default 5)
+57 -19
View File
@@ -752,41 +752,67 @@ static std::vector<std::pair<double, double>> clip_poly1(std::vector<std::pair<d
return out; return out;
} }
std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int ny, std::string overzoom(std::vector<input_tile> tiles, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress, int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles) { std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles) {
std::vector<source_tile> decoded;
for (auto const &t : tiles) {
mvt_tile tile; mvt_tile tile;
try { try {
bool was_compressed; bool was_compressed;
if (!tile.decode(s, was_compressed)) { if (!tile.decode(t.tile, was_compressed)) {
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", oz, ox, oy); fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", t.z, t.x, t.y);
exit(EXIT_MVT); exit(EXIT_MVT);
} }
} catch (std::exception const &e) { } catch (std::exception const &e) {
fprintf(stderr, "PBF decoding error in tile %d/%u/%u\n", oz, ox, oy); fprintf(stderr, "PBF decoding error in tile %d/%u/%u\n", t.z, t.x, t.y);
exit(EXIT_PROTOBUF); exit(EXIT_PROTOBUF);
} }
return overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep, do_compress, next_overzoomed_tiles); source_tile out;
out.tile = tile;
out.z = t.z;
out.x = t.x;
out.y = t.y;
decoded.push_back(out);
}
return overzoom(decoded, nz, nx, ny, detail, buffer, keep, do_compress, next_overzoomed_tiles);
} }
std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int ny, std::string overzoom(std::vector<source_tile> tiles, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress, int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles) { std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles) {
mvt_tile outtile; mvt_tile outtile;
for (auto const &layer : tile.layers) { for (auto const &tile : tiles) {
mvt_layer outlayer = mvt_layer(); for (auto const &layer : tile.tile.layers) {
mvt_layer *outlayer = NULL;
int det = detail; int det = detail;
if (det <= 0) { if (det <= 0) {
det = std::round(log(layer.extent) / log(2)); det = std::round(log(layer.extent) / log(2));
} }
outlayer.name = layer.name; for (size_t i = 0; i < outtile.layers.size(); i++) {
outlayer.version = layer.version; if (outtile.layers[i].name == layer.name) {
outlayer.extent = 1LL << det; outlayer = &outtile.layers[i];
}
}
if (outlayer == NULL) {
mvt_layer newlayer = mvt_layer();
newlayer.name = layer.name;
newlayer.version = layer.version;
newlayer.extent = 1LL << det;
outtile.layers.push_back(newlayer);
outlayer = &outtile.layers.back();
}
for (auto const &feature : layer.features) { for (auto const &feature : layer.features) {
mvt_feature outfeature; mvt_feature outfeature;
@@ -795,7 +821,7 @@ std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int
// Convert feature geometry to world coordinates // Convert feature geometry to world coordinates
long long tilesize = 1LL << (32 - oz); // source tile size in world coordinates long long tilesize = 1LL << (32 - tile.z); // source tile size in world coordinates
draw ring_closure(0, 0, 0); draw ring_closure(0, 0, 0);
for (auto const &g : feature.geometry) { for (auto const &g : feature.geometry) {
@@ -803,8 +829,8 @@ std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int
geom.push_back(ring_closure); geom.push_back(ring_closure);
} else { } else {
geom.emplace_back(g.op, geom.emplace_back(g.op,
g.x * tilesize / layer.extent + ox * tilesize, g.x * tilesize / layer.extent + tile.x * tilesize,
g.y * tilesize / layer.extent + oy * tilesize); g.y * tilesize / layer.extent + tile.y * tilesize);
if (g.op == mvt_moveto) { if (g.op == mvt_moveto) {
ring_closure = geom.back(); ring_closure = geom.back();
@@ -879,16 +905,19 @@ std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int
for (size_t i = 0; i + 1 < feature.tags.size(); i += 2) { for (size_t i = 0; i + 1 < feature.tags.size(); i += 2) {
if (keep.size() == 0 || keep.find(layer.keys[feature.tags[i]]) != keep.end()) { if (keep.size() == 0 || keep.find(layer.keys[feature.tags[i]]) != keep.end()) {
outlayer.tag(outfeature, layer.keys[feature.tags[i]], layer.values[feature.tags[i + 1]]); outlayer->tag(outfeature, layer.keys[feature.tags[i]], layer.values[feature.tags[i + 1]]);
} }
} }
outlayer.features.push_back(outfeature); outlayer->features.push_back(outfeature);
}
}
} }
} }
if (outlayer.features.size() > 0) { for (ssize_t i = outtile.layers.size() - 1; i >= 0; i--) {
outtile.layers.push_back(outlayer); if (outtile.layers[i].features.size() == 0) {
outtile.layers.erase(outtile.layers.begin() + i);
} }
} }
@@ -903,7 +932,16 @@ std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int
if (outtile.layers.size() > 0) { if (outtile.layers.size() > 0) {
for (size_t x = 0; x < 2; x++) { for (size_t x = 0; x < 2; x++) {
for (size_t y = 0; y < 2; y++) { for (size_t y = 0; y < 2; y++) {
std::string child = overzoom(outtile, nz, nx, ny, source_tile st;
st.tile = outtile;
st.z = nz;
st.x = nx;
st.y = ny;
std::vector<source_tile> sts;
sts.push_back(st);
std::string child = overzoom(sts,
nz + 1, nx * 2 + x, ny * 2 + y, nz + 1, nx * 2 + x, ny * 2 + y,
detail, buffer, keep, false, NULL); detail, buffer, keep, false, NULL);
if (child.size() > 0) { if (child.size() > 0) {
+16 -2
View File
@@ -98,11 +98,25 @@ void visvalingam(drawvec &ls, size_t start, size_t end, double threshold, size_t
int pnpoly(const drawvec &vert, size_t start, size_t nvert, long long testx, long long testy); int pnpoly(const drawvec &vert, size_t start, size_t nvert, long long testx, long long testy);
double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y); double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y);
std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int ny, struct input_tile {
std::string tile;
int z;
int x;
int y;
};
struct source_tile {
mvt_tile tile;
int z;
int x;
int y;
};
std::string overzoom(std::vector<source_tile> tiles, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress, int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles); std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles);
std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int ny, std::string overzoom(std::vector<input_tile> tiles, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress, int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles); std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles);
+10 -1
View File
@@ -1247,11 +1247,20 @@ some of the precision. Running:
.PP .PP
.RS .RS
.nf .nf
tippecanoe\-overzoom \-o out.mvt.gz inz/inx/iny outz/outx/outy in.mvt.gz tippecanoe\-overzoom \-o out.mvt.gz in.mvt.gz inz/inx/iny outz/outx/outy
.fi .fi
.RE .RE
.PP .PP
reads tile \fB\fCinz/inx/iny\fR of \fB\fCin.mvt.gz\fR and produces tile \fB\fCoutz/outx/outy\fR of \fB\fCout.mvt.gz\fR\&. reads tile \fB\fCinz/inx/iny\fR of \fB\fCin.mvt.gz\fR and produces tile \fB\fCoutz/outx/outy\fR of \fB\fCout.mvt.gz\fR\&.
.PP
.RS
.nf
tippecanoe\-overzoom \-o out.mvt.gz \-t outz/outx/outy in.mvt.gz inz/inx/iny in2.mvt.gz in2z/in2x/in2y in3.mvt.gz in3z/in3x/in3y
.fi
.RE
.PP
reads tile \fB\fCinz/inx/iny\fR of \fB\fCin.mvt.gz\fR, tile \fB\fCin2z/in2x/in2y\fR of \fB\fCin2.mvt.gz\fR, and tile \fB\fCin3z/in3x/in3y\fR of \fB\fCin3.mvt.gz\fR,
and produces tile \fB\fCoutz/outx/outy\fR of \fB\fCout.mvt.gz\fR from them.
.SS Options .SS Options
.RS .RS
.IP \(bu 2 .IP \(bu 2
+1 -1
View File
@@ -109,7 +109,7 @@ int compress(std::string const &input, std::string &output, bool gz) {
return 0; return 0;
} }
bool mvt_tile::decode(std::string &message, bool &was_compressed) { bool mvt_tile::decode(const std::string &message, bool &was_compressed) {
layers.clear(); layers.clear();
std::string src; std::string src;
+1 -1
View File
@@ -110,7 +110,7 @@ struct mvt_tile {
std::vector<mvt_layer> layers{}; std::vector<mvt_layer> layers{};
std::string encode(); std::string encode();
bool decode(std::string &message, bool &was_compressed); bool decode(const std::string &message, bool &was_compressed);
}; };
bool is_compressed(std::string const &data); bool is_compressed(std::string const &data);
+31 -3
View File
@@ -18,12 +18,24 @@ std::set<std::string> keep;
void usage(char **argv) { void usage(char **argv) {
fprintf(stderr, "Usage: %s -o newtile.pbf.gz tile.pbf.gz oz/ox/oy nz/nx/ny\n", argv[0]); fprintf(stderr, "Usage: %s -o newtile.pbf.gz tile.pbf.gz oz/ox/oy nz/nx/ny\n", argv[0]);
fprintf(stderr, "to create tile nz/nx/ny from tile oz/ox/oy\n"); fprintf(stderr, "to create tile nz/nx/ny from tile oz/ox/oy\n");
fprintf(stderr, "Usage: %s -o newtile.pbf.gz -t nz/nx/ny tile.pbf.gz oz/ox/oy tile2.pbf.gz oz2/ox2/oy2\n", argv[0]);
fprintf(stderr, "to create tile nz/nx/ny from tiles oz/ox/oy and oz2/ox2/oy2\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
struct source {
std::string file;
int z;
int x;
int y;
};
int main(int argc, char **argv) { int main(int argc, char **argv) {
int i; int i;
const char *outfile = NULL; const char *outfile = NULL;
const char *outtile = NULL;
std::vector<source> sources;
while ((i = getopt(argc, argv, "y:o:d:b:")) != -1) { while ((i = getopt(argc, argv, "y:o:d:b:")) != -1) {
switch (i) { switch (i) {
@@ -43,16 +55,21 @@ int main(int argc, char **argv) {
buffer = atoi(optarg); buffer = atoi(optarg);
break; break;
case 't':
outtile = optarg;
break;
default: default:
usage(argv); usage(argv);
} }
} }
if (argc - optind != 3) { if (outfile == NULL) {
usage(argv); usage(argv);
} }
if (outfile == NULL) { if (outtile == NULL) {
if (argc - optind != 3) {
usage(argv); usage(argv);
} }
@@ -91,9 +108,20 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
std::string out = overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep, true, NULL); input_tile it;
it.tile = tile;
it.z = oz;
it.x = ox;
it.y = oy;
std::vector<input_tile> its;
its.push_back(it);
std::string out = overzoom(its, nz, nx, ny, detail, buffer, keep, true, NULL);
fwrite(out.c_str(), sizeof(char), out.size(), f); fwrite(out.c_str(), sizeof(char), out.size(), f);
fclose(f); fclose(f);
} else {
}
return 0; return 0;
} }
+10 -1
View File
@@ -782,7 +782,16 @@ struct tileset_reader {
} }
if (source.layers.size() != 0) { if (source.layers.size() != 0) {
std::string ret = overzoom(source, parent_tile.z, parent_tile.x, parent_tile.y, tile.z, tile.x, tile.y, -1, buffer, std::set<std::string>(), false, &next_overzoomed_tiles); source_tile st;
st.tile = source;
st.z = parent_tile.z;
st.x = parent_tile.x;
st.y = parent_tile.y;
std::vector<source_tile> sts;
sts.push_back(st);
std::string ret = overzoom(sts, tile.z, tile.x, tile.y, -1, buffer, std::set<std::string>(), false, &next_overzoomed_tiles);
return ret; return ret;
} }
+1 -1
View File
@@ -1,6 +1,6 @@
#ifndef VERSION_HPP #ifndef VERSION_HPP
#define VERSION_HPP #define VERSION_HPP
#define VERSION "v2.37.1" #define VERSION "v2.38.0"
#endif #endif