mirror of
https://github.com/felt/tippecanoe.git
synced 2026-12-05 06:41:41 +01:00
Don't snap polygons a second time when removing overlaps
Removing the area that a polygon shares with an earlier polygon ran wagyu on geometry that had already been snapped to tile coordinates, and snapping it again could change it. In Dane County tile 8/64/93, consistent snapping bent one block's edge into a 45-degree diagonal that left it a half-pixel triangle, and when that block's overlap with another block was removed, the diagonal passed through the corner of the hot pixel of the vertex beside it, so it was snapped again and the triangle collapsed, leaving a hole between it and its neighbor. Elsewhere in the same tile, two half-pixel overlaps were left behind the same way. The polygons' edges no longer cross after consistent snapping, so the difference needs no new vertices. So compute it at 16 times the tile resolution, where the vertices are far from each other's hot pixels, and use the result if all of its vertices are the original ones. Only if they aren't, fall back to snapping at tile resolution to the layer's hot pixels. For Dane County at -D8 -d8 -pt -pn -zg, this reduces the overlapping pixels at z8, z9, and z10 from 7, 8, and 16 to 5, 3, and 3, and removes the hole and both overlaps in 8/64/93 at -z8. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
This commit is contained in:
+4
-1
@@ -53,7 +53,10 @@
|
||||
area it covered is covered by both of its neighbors, that area is removed
|
||||
from all but the earliest of them. These overlaps are found from edges that
|
||||
two polygons share in the same direction, which neighbors that tile exactly
|
||||
never do, and from vertices of one polygon strictly inside another. For Dane County Census blocks at -D8 -d8 -pt -pn, this
|
||||
never do, and from vertices of one polygon strictly inside another. The
|
||||
overlapping area is removed at a higher resolution than the tile, so that
|
||||
the polygon isn't snapped to tile coordinates a second time, which could
|
||||
collapse parts of it that the first snapping had left, and leave holes. For Dane County Census blocks at -D8 -d8 -pt -pn, this
|
||||
reduces the gaps between blocks at z9 and z10 from 55 and 155 pixels to 13
|
||||
and 23, and the overlaps from 113 and 175 to 56 and 59, and takes about 7%
|
||||
longer for Natural Earth countries at -z8 -pn.
|
||||
|
||||
+50
-29
@@ -826,41 +826,62 @@ size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const
|
||||
continue;
|
||||
}
|
||||
|
||||
mapbox::geometry::multi_polygon<long long> result;
|
||||
try {
|
||||
mapbox::geometry::wagyu::wagyu<long long> wagyu;
|
||||
add_rings(wagyu, *geoms[i], mapbox::geometry::wagyu::polygon_type_subject, 1);
|
||||
drawvec all = *geoms[i];
|
||||
for (size_t earlier : overlaps[i]) {
|
||||
add_rings(wagyu, *geoms[earlier], mapbox::geometry::wagyu::polygon_type_clip, 1);
|
||||
all.insert(all.end(), geoms[earlier]->begin(), geoms[earlier]->end());
|
||||
// The polygons have already been snapped, and their edges don't cross,
|
||||
// so the difference doesn't need any new vertices. Snapping them again at
|
||||
// tile resolution could still bend their edges (for example, a 45-degree
|
||||
// edge through the corner of the hot pixel of the vertex beside it) and
|
||||
// collapse parts of them, so do it at a higher resolution, where the
|
||||
// vertices are far from each other's hot pixels, and use the result if
|
||||
// it does only have the original vertices. Otherwise, snap at tile
|
||||
// resolution to the layer's hot pixels, consistently with the rest.
|
||||
drawvec out;
|
||||
bool done = false;
|
||||
for (long long scale : {16LL, 1LL}) {
|
||||
mapbox::geometry::multi_polygon<long long> result;
|
||||
try {
|
||||
mapbox::geometry::wagyu::wagyu<long long> wagyu;
|
||||
add_rings(wagyu, *geoms[i], mapbox::geometry::wagyu::polygon_type_subject, scale);
|
||||
drawvec all = *geoms[i];
|
||||
for (size_t earlier : overlaps[i]) {
|
||||
add_rings(wagyu, *geoms[earlier], mapbox::geometry::wagyu::polygon_type_clip, scale);
|
||||
all.insert(all.end(), geoms[earlier]->begin(), geoms[earlier]->end());
|
||||
}
|
||||
if (scale == 1 && hot != NULL) {
|
||||
std::vector<mapbox::geometry::point<long long>> near = hot_pixels_near(all, *hot);
|
||||
if (near.size() > 0) {
|
||||
wagyu.add_hot_pixels(near);
|
||||
}
|
||||
}
|
||||
wagyu.execute(mapbox::geometry::wagyu::clip_type_difference, result, mapbox::geometry::wagyu::fill_type_positive, mapbox::geometry::wagyu::fill_type_positive);
|
||||
} catch (std::runtime_error &e) {
|
||||
continue;
|
||||
}
|
||||
// Snap the same way as the polygons were cleaned, so the result
|
||||
// stays consistent with them.
|
||||
if (hot != NULL) {
|
||||
std::vector<mapbox::geometry::point<long long>> near = hot_pixels_near(all, *hot);
|
||||
if (near.size() > 0) {
|
||||
wagyu.add_hot_pixels(near);
|
||||
|
||||
out.clear();
|
||||
bool exact = true;
|
||||
for (auto const &poly : result) {
|
||||
for (auto const &ring : poly) {
|
||||
for (size_t k = 0; k < ring.size(); k++) {
|
||||
if (ring[k].x % scale != 0 || ring[k].y % scale != 0) {
|
||||
exact = false;
|
||||
}
|
||||
out.emplace_back(k == 0 ? VT_MOVETO : VT_LINETO, ring[k].x / scale, ring[k].y / scale);
|
||||
}
|
||||
if (ring.size() > 0 && (ring.front().x != ring.back().x || ring.front().y != ring.back().y)) {
|
||||
out.emplace_back(VT_LINETO, ring.front().x / scale, ring.front().y / scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
wagyu.execute(mapbox::geometry::wagyu::clip_type_difference, result, mapbox::geometry::wagyu::fill_type_positive, mapbox::geometry::wagyu::fill_type_positive);
|
||||
} catch (std::runtime_error &e) {
|
||||
continue;
|
||||
if (exact) {
|
||||
done = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
drawvec out;
|
||||
for (auto const &poly : result) {
|
||||
for (auto const &ring : poly) {
|
||||
for (size_t k = 0; k < ring.size(); k++) {
|
||||
out.emplace_back(k == 0 ? VT_MOVETO : VT_LINETO, ring[k].x, ring[k].y);
|
||||
}
|
||||
if (ring.size() > 0 && (ring.front().x != ring.back().x || ring.front().y != ring.back().y)) {
|
||||
out.emplace_back(VT_LINETO, ring.front().x, ring.front().y);
|
||||
}
|
||||
}
|
||||
if (done) {
|
||||
*geoms[i] = std::move(out);
|
||||
changed++;
|
||||
}
|
||||
*geoms[i] = std::move(out);
|
||||
changed++;
|
||||
}
|
||||
|
||||
return changed;
|
||||
|
||||
@@ -111,7 +111,7 @@
|
||||
,
|
||||
{ "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.295807, 40.071766 ], [ -99.295807, 40.006580 ], [ -99.308167, 40.012891 ], [ -99.320526, 40.017098 ], [ -99.331512, 40.014994 ], [ -99.354858, 40.008683 ], [ -99.383698, 40.010787 ], [ -99.396057, 40.006580 ], [ -99.419403, 39.993956 ], [ -99.455109, 39.992904 ], [ -99.483948, 39.982382 ], [ -99.496307, 39.984486 ], [ -99.519653, 39.993956 ], [ -99.542999, 39.992904 ], [ -99.553986, 39.995008 ], [ -99.584198, 40.010787 ], [ -99.595184, 40.011839 ], [ -99.618530, 40.008683 ], [ -99.648743, 40.017098 ], [ -99.659729, 40.013943 ], [ -99.683075, 40.002372 ], [ -99.718781, 40.000268 ], [ -99.742126, 39.987643 ], [ -99.744873, 39.986591 ], [ -99.747620, 39.985538 ], [ -99.750366, 39.985538 ], [ -99.753113, 39.984486 ], [ -99.754486, 39.984486 ], [ -99.783325, 39.990799 ], [ -99.806671, 39.986591 ], [ -99.812164, 39.986591 ], [ -99.817657, 39.986591 ], [ -99.820404, 39.987643 ], [ -99.842377, 39.999216 ], [ -99.847870, 40.002372 ], [ -99.849243, 40.002372 ], [ -99.857483, 40.004476 ], [ -99.858856, 40.004476 ], [ -99.882202, 40.004476 ], [ -99.905548, 40.013943 ], [ -99.909668, 40.014994 ], [ -99.912415, 40.016046 ], [ -99.915161, 40.016046 ], [ -99.917908, 40.017098 ], [ -99.919281, 40.017098 ], [ -99.953613, 40.007632 ], [ -99.982452, 40.008683 ], [ -100.000305, 40.000268 ], [ -100.000305, 40.071766 ], [ -99.295807, 40.071766 ] ] ] } }
|
||||
,
|
||||
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.917908, 40.017098 ], [ -99.915161, 40.016046 ], [ -99.912415, 40.016046 ], [ -99.909668, 40.014994 ], [ -99.905548, 40.013943 ], [ -99.882202, 40.003424 ], [ -99.858856, 40.004476 ], [ -99.857483, 40.004476 ], [ -99.849243, 40.002372 ], [ -99.847870, 40.002372 ], [ -99.842377, 39.999216 ], [ -99.824524, 39.988695 ], [ -99.820404, 39.987643 ], [ -99.817657, 39.986591 ], [ -99.812164, 39.986591 ], [ -99.806671, 39.986591 ], [ -99.783325, 39.990799 ], [ -99.759979, 39.984486 ], [ -99.754486, 39.984486 ], [ -99.753113, 39.984486 ], [ -99.750366, 39.985538 ], [ -99.747620, 39.985538 ], [ -99.744873, 39.986591 ], [ -99.742126, 39.987643 ], [ -99.718781, 40.000268 ], [ -99.683075, 40.002372 ], [ -99.659729, 40.013943 ], [ -99.648743, 40.016046 ], [ -99.618530, 40.008683 ], [ -99.595184, 40.011839 ], [ -99.584198, 40.010787 ], [ -99.553986, 39.995008 ], [ -99.542999, 39.992904 ], [ -99.519653, 39.993956 ], [ -99.496307, 39.984486 ], [ -99.483948, 39.982382 ], [ -99.455109, 39.992904 ], [ -99.419403, 39.992904 ], [ -99.396057, 40.006580 ], [ -99.383698, 40.010787 ], [ -99.354858, 40.008683 ], [ -99.331512, 40.014994 ], [ -99.320526, 40.016046 ], [ -99.308167, 40.012891 ], [ -99.295807, 40.005528 ], [ -99.295807, 39.927642 ], [ -100.000305, 39.927642 ], [ -100.000305, 40.000268 ], [ -99.982452, 40.008683 ], [ -99.953613, 40.007632 ], [ -99.919281, 40.017098 ], [ -99.917908, 40.017098 ] ] ] } }
|
||||
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.917908, 40.017098 ], [ -99.915161, 40.016046 ], [ -99.912415, 40.016046 ], [ -99.909668, 40.014994 ], [ -99.905548, 40.013943 ], [ -99.882202, 40.003424 ], [ -99.858856, 40.004476 ], [ -99.857483, 40.004476 ], [ -99.849243, 40.002372 ], [ -99.847870, 40.002372 ], [ -99.842377, 39.999216 ], [ -99.824524, 39.988695 ], [ -99.820404, 39.987643 ], [ -99.817657, 39.986591 ], [ -99.812164, 39.986591 ], [ -99.806671, 39.986591 ], [ -99.783325, 39.990799 ], [ -99.759979, 39.984486 ], [ -99.754486, 39.984486 ], [ -99.753113, 39.984486 ], [ -99.750366, 39.985538 ], [ -99.747620, 39.985538 ], [ -99.742126, 39.987643 ], [ -99.718781, 40.000268 ], [ -99.683075, 40.002372 ], [ -99.659729, 40.013943 ], [ -99.648743, 40.016046 ], [ -99.618530, 40.008683 ], [ -99.595184, 40.011839 ], [ -99.584198, 40.010787 ], [ -99.553986, 39.995008 ], [ -99.542999, 39.992904 ], [ -99.519653, 39.993956 ], [ -99.496307, 39.984486 ], [ -99.483948, 39.982382 ], [ -99.455109, 39.992904 ], [ -99.419403, 39.992904 ], [ -99.396057, 40.006580 ], [ -99.383698, 40.010787 ], [ -99.354858, 40.008683 ], [ -99.331512, 40.014994 ], [ -99.320526, 40.016046 ], [ -99.308167, 40.012891 ], [ -99.295807, 40.005528 ], [ -99.295807, 39.927642 ], [ -100.000305, 39.927642 ], [ -100.000305, 40.000268 ], [ -99.982452, 40.008683 ], [ -99.953613, 40.007632 ], [ -99.919281, 40.017098 ], [ -99.917908, 40.017098 ] ] ] } }
|
||||
] }
|
||||
] }
|
||||
,
|
||||
|
||||
@@ -863,3 +863,116 @@ TEST_CASE("Overlaps between polygons go to the earlier polygon", "[wagyu]") {
|
||||
REQUIRE(get_mp_area(second) == 100);
|
||||
REQUIRE(get_mp_area(third) == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Removing overlaps doesn't snap the polygons again", "[wagyu]") {
|
||||
// Census blocks around a half-pixel triangle (in Dane County tile 8/64/93),
|
||||
// as rounded to tile coordinates, in order of sequence number. Consistent
|
||||
// snapping leaves the triangle in one of them, whose diagonal edge then
|
||||
// passes through the corner of the hot pixel of the vertex beside it.
|
||||
// Removing that block's overlap with another must not snap it again,
|
||||
// which collapsed the triangle and left a hole.
|
||||
auto parse = [](const char *s) {
|
||||
drawvec g;
|
||||
std::istringstream in(s);
|
||||
std::string tok;
|
||||
while (in >> tok) {
|
||||
bool m = tok[0] == 'M';
|
||||
if (m) {
|
||||
tok = tok.substr(1);
|
||||
}
|
||||
long long x, y;
|
||||
sscanf(tok.c_str(), "%lld,%lld", &x, &y);
|
||||
g.push_back(draw(m ? VT_MOVETO : VT_LINETO, x, y));
|
||||
}
|
||||
g.push_back(draw(VT_LINETO, (long long) g[0].x, (long long) g[0].y));
|
||||
return g;
|
||||
};
|
||||
std::vector<drawvec> in = {
|
||||
parse("M65,245 65,244 66,244 66,244 66,245 65,245"),
|
||||
parse("M66,244 66,244 65,244 65,244 66,244"),
|
||||
parse("M67,244 67,244 67,244 67,244 67,243 67,243 67,243 67,243 67,243 67,244"),
|
||||
parse("M66,244 66,244 66,244 67,245 67,244 67,245 67,245 66,245 66,245 66,245 66,244"),
|
||||
parse("M66,244 66,244 66,244 66,244 67,244 67,244 67,244 67,244 66,244 66,244"),
|
||||
parse("M68,242 67,243 67,243 66,243 66,242 67,242 68,242 67,243 68,243"),
|
||||
parse("M65,244 65,245 65,245 65,245 65,245 65,244"),
|
||||
parse("M64,244 65,245 64,245 64,244 64,244"),
|
||||
parse("M65,244 65,244 65,244 64,244 64,244 65,244"),
|
||||
parse("M65,244 65,244 66,244 66,244 66,244 66,244 65,244"),
|
||||
parse("M66,244 67,244 67,244 67,244 67,244 67,244 66,244 66,244 66,244"),
|
||||
parse("M69,240 71,238 73,237 73,241 69,240"),
|
||||
parse("M66,243 66,243 66,243 66,243 66,244 66,244 65,244 65,244 66,243"),
|
||||
parse("M67,244 67,243 68,243 68,243 68,243 68,244 67,244"),
|
||||
parse("M67,243 67,243 67,243 68,243 68,243 65,244"),
|
||||
parse("M61,244 63,239 66,235 69,234 69,236 67,236 68,241 68,242 67,242 67,241 65,242 65,243 65,243 65,244 64,244 64,243 62,244 62,244 61,244"),
|
||||
parse("M65,245 65,245 65,244 65,244 65,244 65,244 65,244 65,244 65,244 65,244 65,244 65,243 65,243 65,243 65,244 65,244 65,244 65,244 65,245"),
|
||||
parse("M67,244 67,244 68,244 68,244 68,244 68,244 68,244 68,244 73,245 73,245 73,249 72,249 71,249 71,249 69,249 66,247 66,247 66,247 66,245 67,245 67,245 67,245 67,244 67,244"),
|
||||
parse("M64,244 64,245 64,245 64,244 64,244"),
|
||||
parse("M66,244 67,244 67,245 67,244 67,245 66,244"),
|
||||
parse("M69,243 68,244 68,244 68,243 68,243 68,243 68,243 69,243"),
|
||||
parse("M68,241 67,236 69,236 69,240 68,241"),
|
||||
parse("M73,241 73,243 73,244 68,244 68,244 68,244 69,243 68,243 67,243 68,242 68,241 69,240 73,241"),
|
||||
parse("M67,243 67,244 67,244 67,243 67,243"),
|
||||
parse("M65,244 65,243 66,243 66,242 66,243 65,244"),
|
||||
parse("M66,242 66,242 66,242 66,242 66,243 66,242"),
|
||||
parse("M63,244 64,244 64,245 63,245 63,244"),
|
||||
parse("M64,245 64,244 64,245 64,245 64,245"),
|
||||
parse("M65,244 65,244 65,244 65,244 65,245 65,244 65,244"),
|
||||
parse("M66,244 66,243 66,243 66,243 66,243 67,242 66,242 66,243 67,243 67,243 67,243 67,244 66,244 66,244 66,244 66,244"),
|
||||
parse("M67,244 67,244 66,244 66,244 67,244"),
|
||||
parse("M67,245 67,244 67,245 67,245"),
|
||||
parse("M73,245 68,244 68,244 73,244 73,245"),
|
||||
parse("M64,244 64,244 64,244 64,244 64,244"),
|
||||
parse("M65,243 65,243 65,242 67,241 67,242 66,243 66,242 66,242 66,242 66,242 66,243 65,243"),
|
||||
parse("M69,236 71,238 69,240 69,236"),
|
||||
parse("M64,245 64,245 64,244 64,244 64,245"),
|
||||
parse("M66,244 66,244 66,244 66,244 66,244 66,245 66,245 66,245 66,245 66,245 65,245 66,245 66,244 66,244 63,244 63,244 64,244"),
|
||||
parse("M65,244 65,245 64,244 64,244 63,244 63,244 63,245 63,244 63,244"),
|
||||
parse("M65,245 67,243 67,243")
|
||||
};
|
||||
|
||||
std::vector<drawvec const *> ptrs;
|
||||
for (auto const &g : in) {
|
||||
ptrs.push_back(&g);
|
||||
}
|
||||
hot_pixel_list hot = layer_hot_pixels(ptrs);
|
||||
|
||||
std::vector<drawvec> out;
|
||||
for (auto const &g : in) {
|
||||
bool ok;
|
||||
out.push_back(clean_polygon_with_hot_pixels(g, hot, &ok));
|
||||
}
|
||||
std::vector<drawvec *> geoms;
|
||||
for (auto &g : out) {
|
||||
geoms.push_back(&g);
|
||||
}
|
||||
remove_overlaps(geoms, &hot);
|
||||
|
||||
// Sample the area (with coordinates doubled, so the samples are at
|
||||
// quarter-pixel offsets that are never on a boundary)
|
||||
for (auto &g : out) {
|
||||
for (auto &d : g) {
|
||||
d.x *= 4;
|
||||
d.y *= 4;
|
||||
}
|
||||
}
|
||||
size_t overlaps = 0;
|
||||
for (long long y = 238 * 4 + 1; y < 245 * 4; y += 2) {
|
||||
for (long long x = 62 * 4 + 1; x < 70 * 4; x += 2) {
|
||||
size_t n = 0;
|
||||
for (auto const &g : out) {
|
||||
n += pnpoly_mp(g, x, y);
|
||||
}
|
||||
if (n > 1) {
|
||||
overlaps++;
|
||||
}
|
||||
}
|
||||
}
|
||||
REQUIRE(overlaps == 0);
|
||||
|
||||
// The triangle between (67, 241), (67, 242), and (66, 242) is covered once
|
||||
size_t n = 0;
|
||||
for (auto const &g : out) {
|
||||
n += pnpoly_mp(g, 4 * 67 - 1, 4 * 242 - 1);
|
||||
}
|
||||
REQUIRE(n == 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user