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
+146 -108
View File
@@ -752,143 +752,172 @@ static std::vector<std::pair<double, double>> clip_poly1(std::vector<std::pair<d
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,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles) {
mvt_tile tile;
std::vector<source_tile> decoded;
try {
bool was_compressed;
if (!tile.decode(s, was_compressed)) {
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", oz, ox, oy);
exit(EXIT_MVT);
for (auto const &t : tiles) {
mvt_tile tile;
try {
bool was_compressed;
if (!tile.decode(t.tile, was_compressed)) {
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", t.z, t.x, t.y);
exit(EXIT_MVT);
}
} catch (std::exception const &e) {
fprintf(stderr, "PBF decoding error in tile %d/%u/%u\n", t.z, t.x, t.y);
exit(EXIT_PROTOBUF);
}
} catch (std::exception const &e) {
fprintf(stderr, "PBF decoding error in tile %d/%u/%u\n", oz, ox, oy);
exit(EXIT_PROTOBUF);
source_tile out;
out.tile = tile;
out.z = t.z;
out.x = t.x;
out.y = t.y;
decoded.push_back(out);
}
return overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep, do_compress, next_overzoomed_tiles);
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,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles) {
mvt_tile outtile;
for (auto const &layer : tile.layers) {
mvt_layer outlayer = mvt_layer();
for (auto const &tile : tiles) {
for (auto const &layer : tile.tile.layers) {
mvt_layer *outlayer = NULL;
int det = detail;
if (det <= 0) {
det = std::round(log(layer.extent) / log(2));
}
int det = detail;
if (det <= 0) {
det = std::round(log(layer.extent) / log(2));
}
outlayer.name = layer.name;
outlayer.version = layer.version;
outlayer.extent = 1LL << det;
for (auto const &feature : layer.features) {
mvt_feature outfeature;
drawvec geom;
int t = feature.type;
// Convert feature geometry to world coordinates
long long tilesize = 1LL << (32 - oz); // source tile size in world coordinates
draw ring_closure(0, 0, 0);
for (auto const &g : feature.geometry) {
if (g.op == mvt_closepath) {
geom.push_back(ring_closure);
} else {
geom.emplace_back(g.op,
g.x * tilesize / layer.extent + ox * tilesize,
g.y * tilesize / layer.extent + oy * tilesize);
if (g.op == mvt_moveto) {
ring_closure = geom.back();
ring_closure.op = mvt_lineto;
}
for (size_t i = 0; i < outtile.layers.size(); i++) {
if (outtile.layers[i].name == layer.name) {
outlayer = &outtile.layers[i];
}
}
// Now offset from world coordinates to output tile coordinates,
// but retain world scale, because that is what tippecanoe clipping expects
if (outlayer == NULL) {
mvt_layer newlayer = mvt_layer();
long long outtilesize = 1LL << (32 - nz); // destination tile size in world coordinates
for (auto &g : geom) {
g.x -= nx * outtilesize;
g.y -= ny * outtilesize;
newlayer.name = layer.name;
newlayer.version = layer.version;
newlayer.extent = 1LL << det;
outtile.layers.push_back(newlayer);
outlayer = &outtile.layers.back();
}
// Clip to output tile
for (auto const &feature : layer.features) {
mvt_feature outfeature;
drawvec geom;
int t = feature.type;
long long xmin = LLONG_MAX;
long long ymin = LLONG_MAX;
long long xmax = LLONG_MIN;
long long ymax = LLONG_MIN;
// Convert feature geometry to world coordinates
for (auto const &g : geom) {
xmin = std::min(xmin, g.x);
ymin = std::min(ymin, g.y);
xmax = std::max(xmax, g.x);
ymax = std::max(ymax, g.y);
}
long long tilesize = 1LL << (32 - tile.z); // source tile size in world coordinates
draw ring_closure(0, 0, 0);
long long b = outtilesize * buffer / 256;
if (xmax < -b || ymax < -b || xmin > outtilesize + b || ymin > outtilesize + b) {
continue;
}
for (auto const &g : feature.geometry) {
if (g.op == mvt_closepath) {
geom.push_back(ring_closure);
} else {
geom.emplace_back(g.op,
g.x * tilesize / layer.extent + tile.x * tilesize,
g.y * tilesize / layer.extent + tile.y * tilesize);
if (t == VT_LINE) {
geom = clip_lines(geom, nz, buffer);
} else if (t == VT_POLYGON) {
drawvec dv;
geom = simple_clip_poly(geom, nz, buffer, dv, false);
} else if (t == VT_POINT) {
geom = clip_point(geom, nz, buffer);
}
// Scale to output tile extent
to_tile_scale(geom, nz, det);
// Clean geometries
geom = remove_noop(geom, t, 0);
if (t == VT_POLYGON) {
geom = clean_or_clip_poly(geom, 0, 0, false, false);
geom = close_poly(geom);
}
// Add geometry to output feature
outfeature.type = t;
for (auto const &g : geom) {
outfeature.geometry.emplace_back(g.op, g.x, g.y);
}
// ID and attributes, if it didn't get clipped away
if (outfeature.geometry.size() > 0) {
if (feature.has_id) {
outfeature.has_id = true;
outfeature.id = feature.id;
}
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()) {
outlayer.tag(outfeature, layer.keys[feature.tags[i]], layer.values[feature.tags[i + 1]]);
if (g.op == mvt_moveto) {
ring_closure = geom.back();
ring_closure.op = mvt_lineto;
}
}
}
outlayer.features.push_back(outfeature);
// Now offset from world coordinates to output tile coordinates,
// but retain world scale, because that is what tippecanoe clipping expects
long long outtilesize = 1LL << (32 - nz); // destination tile size in world coordinates
for (auto &g : geom) {
g.x -= nx * outtilesize;
g.y -= ny * outtilesize;
}
// Clip to output tile
long long xmin = LLONG_MAX;
long long ymin = LLONG_MAX;
long long xmax = LLONG_MIN;
long long ymax = LLONG_MIN;
for (auto const &g : geom) {
xmin = std::min(xmin, g.x);
ymin = std::min(ymin, g.y);
xmax = std::max(xmax, g.x);
ymax = std::max(ymax, g.y);
}
long long b = outtilesize * buffer / 256;
if (xmax < -b || ymax < -b || xmin > outtilesize + b || ymin > outtilesize + b) {
continue;
}
if (t == VT_LINE) {
geom = clip_lines(geom, nz, buffer);
} else if (t == VT_POLYGON) {
drawvec dv;
geom = simple_clip_poly(geom, nz, buffer, dv, false);
} else if (t == VT_POINT) {
geom = clip_point(geom, nz, buffer);
}
// Scale to output tile extent
to_tile_scale(geom, nz, det);
// Clean geometries
geom = remove_noop(geom, t, 0);
if (t == VT_POLYGON) {
geom = clean_or_clip_poly(geom, 0, 0, false, false);
geom = close_poly(geom);
}
// Add geometry to output feature
outfeature.type = t;
for (auto const &g : geom) {
outfeature.geometry.emplace_back(g.op, g.x, g.y);
}
// ID and attributes, if it didn't get clipped away
if (outfeature.geometry.size() > 0) {
if (feature.has_id) {
outfeature.has_id = true;
outfeature.id = feature.id;
}
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()) {
outlayer->tag(outfeature, layer.keys[feature.tags[i]], layer.values[feature.tags[i + 1]]);
}
}
outlayer->features.push_back(outfeature);
}
}
}
}
if (outlayer.features.size() > 0) {
outtile.layers.push_back(outlayer);
for (ssize_t i = outtile.layers.size() - 1; i >= 0; i--) {
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) {
for (size_t x = 0; x < 2; x++) {
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,
detail, buffer, keep, false, NULL);
if (child.size() > 0) {