mirror of
https://github.com/felt/tippecanoe.git
synced 2026-12-05 06:41:41 +01:00
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:
+197
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user