With -pn, give area covered by more than one polygon to the earliest

Consistent snapping keeps the borders of polygons in a layer from crossing,
but where rounding turned a narrow polygon between two others inside out
(for example, a Census block thinner than a pixel whose two sides cross
when rounded to tile coordinates), the area it covered is covered by both of
its neighbors, and snapping can't decide which of them it belongs to.

So after cleaning the polygons of each layer, find the ones that overlap:
two polygons that share an edge in the same direction both cover the area
beside it (neighbors that tile exactly traverse their shared border in
opposite directions), and where their borders don't share any edges, a
vertex of one is strictly inside the other. Then remove each overlapping
area from all but the polygon with the lowest sequence number, using the
layer's hot pixels so the result stays consistent with the rest.

For Dane County Census blocks at -D8 -d8 -pt -pn, this reduces the
overlapping pixels at z8, z9, and z10 from 41, 61, and 61 to 7, 8, and 16,
without changing the gaps much or taking noticeably longer, and with no
self-intersecting polygons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
This commit is contained in:
Claude
2026-12-01 01:11:29 +00:00
parent b8e8542dfa
commit 082487b594
7 changed files with 278 additions and 7 deletions
+6 -1
View File
@@ -48,7 +48,12 @@
[x - 0.5, x + 0.5) by [y - 0.5, y + 0.5), as in Hobby's snap rounding,
rather than as closed squares, because edges passing diagonally through
pixel corners otherwise zigzagged through the same pixels in different
orders in different polygons, and could cross each other. For Dane County Census blocks at -D8 -d8 -pt -pn, this
orders in different polygons, and could cross each other. Then, where
rounding turned a narrow polygon between two others inside out, so that the
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
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.
+197
View File
@@ -26,6 +26,7 @@
#include <climits>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <tuple>
#include <mapbox/geometry/point.hpp>
#include <mapbox/geometry/multi_polygon.hpp>
@@ -668,3 +669,199 @@ drawvec clean_polygon_with_hot_pixels(drawvec const &geom, hot_pixel_list const
*ok = wagyu_union(geom, NULL, 1, out, false, &hot);
return out;
}
namespace {
struct directed_edge {
long long ax, ay, bx, by;
bool operator==(directed_edge const &o) const {
return ax == o.ax && ay == o.ay && bx == o.bx && by == o.by;
}
};
struct directed_edge_hash {
size_t operator()(directed_edge const &e) const {
size_t h = std::hash<long long>()(e.ax);
h = h * 1000003 ^ std::hash<long long>()(e.ay);
h = h * 1000003 ^ std::hash<long long>()(e.bx);
h = h * 1000003 ^ std::hash<long long>()(e.by);
return h;
}
};
template <typename F>
void for_each_edge(drawvec const &geom, F f) {
for (size_t i = 0; i < geom.size(); i++) {
if (geom[i].op == VT_MOVETO) {
size_t j;
for (j = i + 1; j < geom.size(); j++) {
if (geom[j].op != VT_LINETO) {
break;
}
}
for (size_t k = i; k + 1 < j; k++) {
if (geom[k].x != geom[k + 1].x || geom[k].y != geom[k + 1].y) {
f(directed_edge{geom[k].x, geom[k].y, geom[k + 1].x, geom[k + 1].y});
}
}
i = j - 1;
}
}
}
} // namespace
// Whether the point (px, py) is strictly inside the polygon `geom` by the
// nonzero winding rule, with exact integer arithmetic. A point exactly on the
// polygon's boundary does not count as inside.
static bool strictly_inside(drawvec const &geom, long long px, long long py) {
int wn = 0;
for (size_t i = 0; i < geom.size(); i++) {
if (geom[i].op == VT_MOVETO) {
size_t j;
for (j = i + 1; j < geom.size(); j++) {
if (geom[j].op != VT_LINETO) {
break;
}
}
for (size_t k = i; k + 1 < j; k++) {
long long x1 = geom[k].x, y1 = geom[k].y, x2 = geom[k + 1].x, y2 = geom[k + 1].y;
long long cross = (x2 - x1) * (py - y1) - (px - x1) * (y2 - y1);
if (cross == 0 && std::min(x1, x2) <= px && px <= std::max(x1, x2) && std::min(y1, y2) <= py && py <= std::max(y1, y2)) {
return false; // on the boundary
}
if (y1 <= py) {
if (y2 > py && cross > 0) {
wn++;
}
} else {
if (y2 <= py && cross < 0) {
wn--;
}
}
}
i = j - 1;
}
}
return wn != 0;
}
size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const *hot) {
// Which polygon (the earliest) each directed edge is part of. Cleaned rings
// all have their polygon's interior on the same side, so two polygons that
// tile exactly traverse the border they share in opposite directions, and
// two polygons that share an edge in the same direction both cover the area
// beside it. With consistent snapping, edges of different polygons don't
// cross, so this is where overlaps between them show up.
std::unordered_map<directed_edge, size_t, directed_edge_hash> owner;
std::vector<std::vector<size_t>> overlaps(geoms.size());
for (size_t i = 0; i < geoms.size(); i++) {
for_each_edge(*geoms[i], [&](directed_edge const &e) {
auto found = owner.find(e);
if (found == owner.end()) {
owner.emplace(e, i);
} else if (found->second != i) {
std::vector<size_t> &o = overlaps[i];
if (std::find(o.begin(), o.end(), found->second) == o.end()) {
o.push_back(found->second);
}
}
});
}
// Where the polygons' boundaries don't share any edges where they overlap,
// one of them has a vertex that is strictly inside the other.
{
const long long cell = 16;
std::vector<long long> minx(geoms.size()), miny(geoms.size()), maxx(geoms.size()), maxy(geoms.size());
std::unordered_map<long long, std::vector<size_t>> grid;
auto key = [](long long cx, long long cy) {
return (cx << 32) ^ (cy & 0xFFFFFFFF);
};
for (size_t i = 0; i < geoms.size(); i++) {
minx[i] = miny[i] = LLONG_MAX;
maxx[i] = maxy[i] = LLONG_MIN;
for (auto const &d : *geoms[i]) {
minx[i] = std::min(minx[i], (long long) d.x);
miny[i] = std::min(miny[i], (long long) d.y);
maxx[i] = std::max(maxx[i], (long long) d.x);
maxy[i] = std::max(maxy[i], (long long) d.y);
}
if (geoms[i]->size() == 0) {
continue;
}
for (long long cx = std::floor(minx[i] / (double) cell); cx <= std::floor(maxx[i] / (double) cell); cx++) {
for (long long cy = std::floor(miny[i] / (double) cell); cy <= std::floor(maxy[i] / (double) cell); cy++) {
grid[key(cx, cy)].push_back(i);
}
}
}
for (size_t i = 0; i < geoms.size(); i++) {
for (auto const &d : *geoms[i]) {
auto found = grid.find(key(std::floor(d.x / (double) cell), std::floor(d.y / (double) cell)));
if (found == grid.end()) {
continue;
}
for (size_t j : found->second) {
if (j == i || d.x < minx[j] || d.x > maxx[j] || d.y < miny[j] || d.y > maxy[j]) {
continue;
}
size_t later = std::max(i, j), earlier = std::min(i, j);
std::vector<size_t> &o = overlaps[later];
if (std::find(o.begin(), o.end(), earlier) != o.end()) {
continue;
}
if (strictly_inside(*geoms[j], d.x, d.y)) {
o.push_back(earlier);
}
}
}
}
}
size_t changed = 0;
for (size_t i = 0; i < geoms.size(); i++) {
if (overlaps[i].size() == 0) {
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());
}
// 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);
}
}
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;
}
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);
}
}
}
*geoms[i] = std::move(out);
changed++;
}
return changed;
}
+9
View File
@@ -57,4 +57,13 @@ hot_pixel_list layer_hot_pixels(std::vector<drawvec const *> const &geoms);
// from layer_hot_pixels() as well as its own. Sets *ok to false if that fails.
drawvec clean_polygon_with_hot_pixels(drawvec const &geom, hot_pixel_list const &hot, bool *ok);
// Polygons (in tile coordinates, in order of priority) that have been cleaned
// with consistent snapping should tile without crossing, but where rounding
// turned a narrow polygon between two others inside out, the area it covered
// may be covered by both of its neighbors. Remove each such overlapping area
// from all but the first of the polygons that cover it. Returns how many
// polygons were changed.
// `hot` is the hot pixels of the layer, from layer_hot_pixels(), if any.
size_t remove_overlaps(std::vector<drawvec *> const &geoms, hot_pixel_list const *hot);
#endif
File diff suppressed because one or more lines are too long
+4 -4
View File
@@ -21,7 +21,7 @@
,
{ "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.492188, 39.977120 ], [ -100.019531, 39.977120 ], [ -100.019531, 40.044438 ], [ -99.931641, 40.044438 ], [ -99.755859, 40.044438 ], [ -99.492188, 39.977120 ] ] ] } }
,
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.755859, 40.044438 ], [ -99.492188, 39.977120 ], [ -99.316406, 39.977120 ], [ -99.316406, 39.909736 ], [ -100.019531, 39.909736 ], [ -100.019531, 39.977120 ], [ -99.931641, 40.044438 ], [ -99.755859, 40.044438 ] ] ] } }
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -100.019531, 39.977120 ], [ -99.492188, 39.977120 ], [ -99.316406, 39.977120 ], [ -99.316406, 39.909736 ], [ -100.019531, 39.909736 ], [ -100.019531, 39.977120 ] ] ] } }
] }
] }
,
@@ -49,7 +49,7 @@
,
{ "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.294434, 40.078071 ], [ -99.294434, 40.010787 ], [ -99.492188, 39.977120 ], [ -99.645996, 40.010787 ], [ -99.755859, 39.977120 ], [ -99.821777, 39.993956 ], [ -99.865723, 39.993956 ], [ -99.909668, 40.010787 ], [ -99.931641, 40.010787 ], [ -99.997559, 39.993956 ], [ -99.997559, 40.078071 ], [ -99.294434, 40.078071 ] ] ] } }
,
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.294434, 40.010787 ], [ -99.294434, 39.926588 ], [ -99.997559, 39.926588 ], [ -99.997559, 39.993956 ], [ -99.931641, 40.010787 ], [ -99.909668, 40.010787 ], [ -99.865723, 39.993956 ], [ -99.821777, 39.993956 ], [ -99.645996, 40.010787 ], [ -99.492188, 39.977120 ], [ -99.294434, 40.010787 ] ] ] } }
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.909668, 40.010787 ], [ -99.865723, 39.993956 ], [ -99.821777, 39.993956 ], [ -99.755859, 39.977120 ], [ -99.645996, 40.010787 ], [ -99.492188, 39.977120 ], [ -99.294434, 40.010787 ], [ -99.294434, 39.926588 ], [ -99.997559, 39.926588 ], [ -99.997559, 39.993956 ], [ -99.931641, 40.010787 ], [ -99.909668, 40.010787 ] ] ] } }
] }
] }
,
@@ -95,7 +95,7 @@
,
{ "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.297180, 40.071766 ], [ -99.297180, 40.006580 ], [ -99.319153, 40.017098 ], [ -99.354858, 40.008683 ], [ -99.385071, 40.010787 ], [ -99.420776, 39.993956 ], [ -99.453735, 39.993956 ], [ -99.483948, 39.983434 ], [ -99.519653, 39.993956 ], [ -99.555359, 39.996060 ], [ -99.582825, 40.010787 ], [ -99.618530, 40.008683 ], [ -99.648743, 40.017098 ], [ -99.684448, 40.002372 ], [ -99.717407, 40.000268 ], [ -99.753113, 39.985538 ], [ -99.758606, 39.985538 ], [ -99.783325, 39.989747 ], [ -99.819031, 39.987643 ], [ -99.824524, 39.989747 ], [ -99.846497, 40.002372 ], [ -99.851990, 40.002372 ], [ -99.882202, 40.004476 ], [ -99.917908, 40.017098 ], [ -99.953613, 40.006580 ], [ -99.981079, 40.008683 ], [ -100.000305, 40.000268 ], [ -100.000305, 40.071766 ], [ -99.297180, 40.071766 ] ] ] } }
,
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.912415, 40.017098 ], [ -99.882202, 40.004476 ], [ -99.854736, 40.004476 ], [ -99.851990, 40.002372 ], [ -99.824524, 39.989747 ], [ -99.819031, 39.987643 ], [ -99.813538, 39.985538 ], [ -99.783325, 39.989747 ], [ -99.758606, 39.985538 ], [ -99.753113, 39.985538 ], [ -99.747620, 39.985538 ], [ -99.717407, 40.000268 ], [ -99.684448, 40.002372 ], [ -99.648743, 40.017098 ], [ -99.618530, 40.008683 ], [ -99.582825, 40.010787 ], [ -99.555359, 39.993956 ], [ -99.519653, 39.993956 ], [ -99.483948, 39.983434 ], [ -99.453735, 39.993956 ], [ -99.420776, 39.993956 ], [ -99.385071, 40.010787 ], [ -99.354858, 40.008683 ], [ -99.319153, 40.017098 ], [ -99.297180, 40.006580 ], [ -99.297180, 39.928695 ], [ -100.000305, 39.928695 ], [ -100.000305, 40.000268 ], [ -99.981079, 40.008683 ], [ -99.953613, 40.006580 ], [ -99.923401, 40.017098 ], [ -99.917908, 40.017098 ], [ -99.912415, 40.017098 ] ] ] } }
{ "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.319153, 40.017098 ], [ -99.297180, 40.006580 ], [ -99.297180, 39.928695 ], [ -100.000305, 39.928695 ], [ -100.000305, 40.000268 ], [ -99.981079, 40.008683 ], [ -99.953613, 40.006580 ], [ -99.917908, 40.017098 ], [ -99.882202, 40.004476 ], [ -99.851990, 40.002372 ], [ -99.824524, 39.989747 ], [ -99.819031, 39.987643 ], [ -99.813538, 39.985538 ], [ -99.783325, 39.989747 ], [ -99.758606, 39.985538 ], [ -99.753113, 39.985538 ], [ -99.747620, 39.985538 ], [ -99.717407, 40.000268 ], [ -99.684448, 40.002372 ], [ -99.648743, 40.017098 ], [ -99.618530, 40.008683 ], [ -99.582825, 40.010787 ], [ -99.555359, 39.993956 ], [ -99.519653, 39.993956 ], [ -99.483948, 39.983434 ], [ -99.453735, 39.993956 ], [ -99.420776, 39.993956 ], [ -99.385071, 40.010787 ], [ -99.354858, 40.008683 ], [ -99.319153, 40.017098 ] ] ] } }
] }
] }
,
@@ -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.923401, 40.017098 ], [ -99.919281, 40.017098 ], [ -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.853363, 40.004476 ], [ -99.849243, 40.002372 ], [ -99.847870, 40.002372 ], [ -99.842377, 39.999216 ], [ -99.824524, 39.988695 ], [ -99.820404, 39.987643 ], [ -99.812164, 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.923401, 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.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 ] ] ] } }
] }
] }
,
+23
View File
@@ -2629,6 +2629,29 @@ long long write_tile(decompressor *geoms, std::atomic<long long> *geompos_in, ch
}
}
// After consistent snapping, give any area that is covered by more than one
// polygon in a layer (because rounding turned a narrow polygon between them
// inside out) to the one that comes first, as with other tiebreaks.
if (consistent_snapping) {
std::map<long long, std::vector<std::pair<long long, drawvec *>>> layer_polygons;
for (auto const &f : features) {
if (f->t == VT_POLYGON && f->geometry.size() > 0) {
layer_polygons[f->layer].emplace_back(f->seq, &f->geometry);
}
}
for (auto &lp : layer_polygons) {
std::stable_sort(lp.second.begin(), lp.second.end(), [](std::pair<long long, drawvec *> const &a, std::pair<long long, drawvec *> const &b) {
return a.first < b.first;
});
std::vector<drawvec *> layer_geoms;
for (auto const &g : lp.second) {
layer_geoms.push_back(g.second);
}
auto hot = hot_pixels_by_layer.find(lp.first);
remove_overlaps(layer_geoms, hot != hot_pixels_by_layer.end() ? &hot->second : NULL);
}
}
for (size_t i = 0; i < features.size(); i++) {
signed char t = features[i]->t;
+37
View File
@@ -826,3 +826,40 @@ TEST_CASE("Consistent snapping doesn't make neighboring polygons' edges cross",
}
REQUIRE(crossings == 0);
}
TEST_CASE("Overlaps between polygons go to the earlier polygon", "[wagyu]") {
auto ring = [](std::vector<std::pair<long long, long long>> pts) {
drawvec g;
for (size_t i = 0; i < pts.size(); i++) {
g.push_back(draw(i == 0 ? VT_MOVETO : VT_LINETO, pts[i].first, pts[i].second));
}
g.push_back(draw(VT_LINETO, pts[0].first, pts[0].second));
if (get_area(g, 0, g.size()) < 0) {
std::reverse(g.begin(), g.end());
g[0].op = VT_MOVETO;
g[g.size() - 1].op = VT_LINETO;
}
return g;
};
// Two squares that overlap in a 2x10 strip, without sharing any edges
drawvec first = ring({{0, 0}, {10, 0}, {10, 10}, {0, 10}});
drawvec second = ring({{8, 0}, {20, 0}, {20, 10}, {8, 10}});
// and one that shares a border with the first in the same direction,
// so that it covers the same triangle beside it
drawvec third = ring({{0, 10}, {10, 10}, {5, 5}});
// Clean them consistently first, as tippecanoe does, which splits
// each square's edges where the other's corners touch them
hot_pixel_list hot = layer_hot_pixels({&first, &second, &third});
bool ok;
first = clean_polygon_with_hot_pixels(first, hot, &ok);
second = clean_polygon_with_hot_pixels(second, hot, &ok);
third = clean_polygon_with_hot_pixels(third, hot, &ok);
std::vector<drawvec *> geoms = {&first, &second, &third};
REQUIRE(remove_overlaps(geoms, &hot) == 2);
REQUIRE(get_mp_area(first) == 100);
REQUIRE(get_mp_area(second) == 100);
REQUIRE(get_mp_area(third) == 0);
}