Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-clean each ring of polygons to fix the bow tie problem #769

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 58 additions & 31 deletions geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ static void decode_clipped(mapbox::geometry::multi_polygon<long long> &t, drawve
}
}

drawvec clean_or_clip_poly(drawvec &geom, int z, int buffer, bool clip) {
drawvec clean_or_clip_poly(drawvec &geom, int z, int buffer, bool clip, bool even_odd) {
mapbox::geometry::wagyu::wagyu<long long> wagyu;

geom = remove_noop(geom, VT_POLYGON, 0);
Expand Down Expand Up @@ -269,7 +269,7 @@ drawvec clean_or_clip_poly(drawvec &geom, int z, int buffer, bool clip) {

mapbox::geometry::multi_polygon<long long> result;
try {
wagyu.execute(mapbox::geometry::wagyu::clip_type_union, result, mapbox::geometry::wagyu::fill_type_positive, mapbox::geometry::wagyu::fill_type_positive);
wagyu.execute(mapbox::geometry::wagyu::clip_type_union, result, even_odd ? mapbox::geometry::wagyu::fill_type_even_odd : mapbox::geometry::wagyu::fill_type_positive, even_odd ? mapbox::geometry::wagyu::fill_type_even_odd : mapbox::geometry::wagyu::fill_type_positive);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second fill option is only needed to change if you are using "clip" polygon types with add_ring, I believe you are only using subject polygon types so you can probably just always leave the 2nd option as positive?

} catch (std::runtime_error e) {
FILE *f = fopen("/tmp/wagyu.log", "w");
fprintf(f, "%s\n", e.what());
Expand Down Expand Up @@ -925,39 +925,66 @@ drawvec fix_polygon(drawvec &geom) {
ring.push_back(ring[0]);
}

// Reverse ring if winding order doesn't match
// inner/outer expectation

bool reverse_ring = false;
if (prevent[P_USE_SOURCE_POLYGON_WINDING]) {
// GeoJSON winding is reversed from vector winding
reverse_ring = true;
} else if (prevent[P_REVERSE_SOURCE_POLYGON_WINDING]) {
// GeoJSON winding is reversed from vector winding
reverse_ring = false;
} else {
double area = get_area(ring, 0, ring.size());
if ((area > 0) != outer) {
reverse_ring = true;
}
// Don't clean if we need the borders to exactly match between polygons
// or if the user has asked to keep their own winding
if (!additional[A_DETECT_SHARED_BORDERS] && !prevent[P_USE_SOURCE_POLYGON_WINDING] && !prevent[P_REVERSE_SOURCE_POLYGON_WINDING]) {
ring = clean_or_clip_poly(ring, 0, 0, false, true);
}

if (reverse_ring) {
drawvec tmp;
for (int a = ring.size() - 1; a >= 0; a--) {
tmp.push_back(ring[a]);
}
ring = tmp;
}
// This may have produced multiple output rings

// Copy ring into output, fixing the moveto/lineto ops if necessary because of
// reversal or closing
for (size_t ii = 0; ii < ring.size(); ii++) {
if (ring[ii].op == VT_MOVETO) {
// Find the end of the ring
size_t jj;
for (jj = ii + 1; jj < ring.size(); jj++) {
if (ring[jj].op == VT_MOVETO) {
break;
}
}

for (size_t a = 0; a < ring.size(); a++) {
if (a == 0) {
out.push_back(draw(VT_MOVETO, ring[a].x, ring[a].y));
} else {
out.push_back(draw(VT_LINETO, ring[a].x, ring[a].y));
drawvec ring2;
for (size_t rr = ii; rr < jj; rr++) {
ring2.push_back(ring[rr]);
}

// Reverse ring if winding order doesn't match
// inner/outer expectation

bool reverse_ring = false;
if (prevent[P_USE_SOURCE_POLYGON_WINDING]) {
// GeoJSON winding is reversed from vector winding
reverse_ring = true;
} else if (prevent[P_REVERSE_SOURCE_POLYGON_WINDING]) {
// GeoJSON winding is reversed from vector winding
reverse_ring = false;
} else {
double area = get_area(ring2, 0, ring2.size());
if ((area > 0) != outer) {
reverse_ring = true;
}
}

if (reverse_ring) {
drawvec tmp;
for (int a = ring2.size() - 1; a >= 0; a--) {
tmp.push_back(ring2[a]);
}
ring2 = tmp;
}

// Copy ring into output, fixing the moveto/lineto ops if necessary because of
// reversal or closing

for (size_t a = 0; a < ring2.size(); a++) {
if (a == 0) {
out.push_back(draw(VT_MOVETO, ring2[a].x, ring2[a].y));
} else {
out.push_back(draw(VT_LINETO, ring2[a].x, ring2[a].y));
}
}

ii = jj - 1;
}
}

Expand Down
2 changes: 1 addition & 1 deletion geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ drawvec decode_geometry(FILE *meta, std::atomic<long long> *geompos, int z, unsi
void to_tile_scale(drawvec &geom, int z, int detail);
drawvec remove_noop(drawvec geom, int type, int shift);
drawvec clip_point(drawvec &geom, int z, long long buffer);
drawvec clean_or_clip_poly(drawvec &geom, int z, int buffer, bool clip);
drawvec clean_or_clip_poly(drawvec &geom, int z, int buffer, bool clip, bool even_odd);
drawvec simple_clip_poly(drawvec &geom, int z, int buffer);
drawvec close_poly(drawvec &geom);
drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area);
Expand Down
2 changes: 1 addition & 1 deletion plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ std::vector<mvt_layer> parse_layers(int fd, int z, unsigned x, unsigned y, std::
}

if (mb_geometry[t] == VT_POLYGON) {
dv = clean_or_clip_poly(dv, 0, 0, false);
dv = clean_or_clip_poly(dv, 0, 0, false, false);
if (dv.size() < 3) {
dv.clear();
}
Expand Down
Loading