mirror of
https://github.com/felt/tippecanoe.git
synced 2026-12-05 06:41:41 +01:00
With -pn, let neighbors close over polygons that collapse entirely
A polygon that collapses away entirely is replaced by its outline, but with --no-simplification-of-shared-nodes that outline overlaps the neighboring features along every shared border, and those features already cover the space. So leave the shared borders out of the outline, as the spindle strips already do, and if every border is shared, leave the polygon out entirely instead of falling back to a rectangle. For Dane County Census blocks at -D8 -d8 -pt -pn, this reduces the overlapping pixels in tile 12/1031/1503 from 141 to 11 and in 11/515/751 from 915 to 38, and the number of revived rectangles at z8 from 6307 to 1414, at the cost of a few more one-pixel gaps (7 to 10 and 14 to 23) where a block of less than a pixel is left out and its neighbors' borders don't round to the same line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
This commit is contained in:
+21
-1
@@ -421,14 +421,19 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail, double simpl
|
||||
return out;
|
||||
}
|
||||
|
||||
drawvec outline_strips(drawvec const &geom, int z, int detail) {
|
||||
drawvec outline_strips(drawvec const &geom, int z, int detail, double simplification, long long ox, long long oy, bool *all_shared) {
|
||||
drawvec out;
|
||||
*all_shared = false;
|
||||
|
||||
int shift = 32 - detail - z;
|
||||
if (shift <= 0) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// How far a simplified shared border may have strayed from the original shared edges
|
||||
const double tolerance = (1 + std::max(simplification, 0.0)) * (1LL << shift);
|
||||
size_t edges = 0, shared = 0;
|
||||
|
||||
for (size_t i = 0; i < geom.size(); i++) {
|
||||
if (geom[i].op == VT_MOVETO) {
|
||||
size_t j;
|
||||
@@ -441,6 +446,20 @@ drawvec outline_strips(drawvec const &geom, int z, int detail) {
|
||||
if (j - i >= 3) {
|
||||
for (size_t k = i; k < j; k++) {
|
||||
size_t n = (k + 1 < j) ? k + 1 : i; // close the ring if it isn't already
|
||||
if (geom[k].x == geom[n].x && geom[k].y == geom[n].y) {
|
||||
continue;
|
||||
}
|
||||
edges++;
|
||||
|
||||
// Along a border shared with another feature, that feature
|
||||
// already covers the space, so leave it to close over it.
|
||||
if (shared_edges_count > 0 &&
|
||||
(shared_toward(geom[k].x + ox, geom[k].y + oy, geom[n].x + ox, geom[n].y + oy, tolerance) ||
|
||||
shared_toward(geom[n].x + ox, geom[n].y + oy, geom[k].x + ox, geom[k].y + oy, tolerance))) {
|
||||
shared++;
|
||||
continue;
|
||||
}
|
||||
|
||||
add_strip(out, geom[k].x, geom[k].y, geom[n].x, geom[n].y, shift);
|
||||
}
|
||||
}
|
||||
@@ -449,6 +468,7 @@ drawvec outline_strips(drawvec const &geom, int z, int detail) {
|
||||
}
|
||||
}
|
||||
|
||||
*all_shared = edges > 0 && shared == edges;
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
+6
-3
@@ -31,9 +31,12 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail, double simpl
|
||||
double unshared_perimeter(drawvec const &geom, size_t i, size_t j, long long ox, long long oy);
|
||||
|
||||
// Strips (in tile coordinates) one pixel wide along every edge of a polygon
|
||||
// (in world coordinates), which together trace its outline. This is used to
|
||||
// stand in for a polygon that has collapsed away to nothing entirely.
|
||||
drawvec outline_strips(drawvec const &geom, int z, int detail);
|
||||
// (in world coordinates relative to a tile origin at (ox, oy)), which together
|
||||
// trace its outline. This is used to stand in for a polygon that has collapsed
|
||||
// away to nothing entirely. Edges shared with other features (with
|
||||
// --no-simplification-of-shared-nodes) are left out, since those features
|
||||
// already cover the space, and *all_shared is set if every edge was shared.
|
||||
drawvec outline_strips(drawvec const &geom, int z, int detail, double simplification, long long ox, long long oy, bool *all_shared);
|
||||
|
||||
// Union the strips from thin_polygon_strips() with a cleaned polygon
|
||||
// (both in tile coordinates)
|
||||
|
||||
@@ -718,13 +718,23 @@ static void *simplification_worker(void *v) {
|
||||
// which keeps its shape and location. Only if that too is empty,
|
||||
// because the whole polygon is within a pixel or so, fall back to
|
||||
// a rectangle with the polygon's area (but at least a pixel).
|
||||
//
|
||||
// But with --no-simplification-of-shared-nodes, a polygon whose
|
||||
// borders are all shared with other features is left out instead,
|
||||
// so that its neighbors close over the space without overlapping.
|
||||
geom.clear();
|
||||
bool all_shared = false;
|
||||
if (world.size() > 0) {
|
||||
geom = union_polygon_strips(drawvec(), outline_strips(world, z, out_detail));
|
||||
geom = union_polygon_strips(drawvec(), outline_strips(world, z, out_detail, (*features)[i]->simplification,
|
||||
(long long) (*features)[i]->tx << (32 - z), (long long) (*features)[i]->ty << (32 - z), &all_shared));
|
||||
}
|
||||
if (geom.size() < 3) {
|
||||
// area is in world coordinates, calculated before scaling down
|
||||
geom = revive_polygon(before, area, z, out_detail);
|
||||
if (all_shared) {
|
||||
geom.clear();
|
||||
} else {
|
||||
// area is in world coordinates, calculated before scaling down
|
||||
geom = revive_polygon(before, area, z, out_detail);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
geom.clear();
|
||||
|
||||
@@ -674,7 +674,9 @@ TEST_CASE("A polygon that collapses entirely can be replaced by its outline", "[
|
||||
to_tile_scale(scaled, z, detail);
|
||||
REQUIRE(clean_or_clip_poly(scaled, 0, 0, false, true).size() == 0);
|
||||
|
||||
drawvec outline = union_polygon_strips(drawvec(), outline_strips(geom, z, detail));
|
||||
bool all_shared;
|
||||
drawvec outline = union_polygon_strips(drawvec(), outline_strips(geom, z, detail, 1, 0, 0, &all_shared));
|
||||
REQUIRE(!all_shared);
|
||||
double area = get_mp_area(outline);
|
||||
|
||||
// About a pixel wide along both arms, rather than a square
|
||||
|
||||
Reference in New Issue
Block a user