Skip to content

Commit

Permalink
Don't let polygons with nonzero area disappear during cleaning
Browse files Browse the repository at this point in the history
If they collapse, turn them into placeholder squares with the
appropriate area so that there aren't visible coverage gaps.
  • Loading branch information
e-n-f committed May 5, 2016
1 parent fa523bf commit 5cc6d97
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.11.4

* Don't let polygons with nonzero area disappear during cleaning

## 1.11.3

* Internal code cleanup
Expand Down
1 change: 1 addition & 0 deletions geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ drawvec reorder_lines(drawvec &geom);
drawvec fix_polygon(drawvec &geom);
std::vector<drawvec> chop_polygon(std::vector<drawvec> &geoms);
void check_polygon(drawvec &geom, drawvec &before);
double get_area(drawvec &geom, size_t i, size_t j);
26 changes: 26 additions & 0 deletions tests/join-population/joined-i.mbtiles.json

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions tests/join-population/joined.mbtiles.json

Large diffs are not rendered by default.

61 changes: 56 additions & 5 deletions tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,47 @@ struct partial_arg {
int tasks;
};

drawvec revive_polygon(drawvec &geom, double area, int z, int detail) {
// From area in world coordinates to area in tile coordinates
long long divisor = 1LL << (32 - detail - z);
area /= divisor * divisor;

if (area == 0) {
return drawvec();
}

int height = ceil(sqrt(area));
int width = round(area / height);
if (width == 0) {
width = 1;
}

long long sx = 0, sy = 0, n = 0;
for (size_t i = 0; i < geom.size(); i++) {
if (geom[i].op == VT_MOVETO || geom[i].op == VT_LINETO) {
sx += geom[i].x;
sy += geom[i].y;
n++;
}
}

if (n > 0) {
sx /= n;
sy /= n;

drawvec out;
out.push_back(draw(VT_MOVETO, sx - (width / 2), sy - (height / 2)));
out.push_back(draw(VT_LINETO, sx - (width / 2) + width, sy - (height / 2)));
out.push_back(draw(VT_LINETO, sx - (width / 2) + width, sy - (height / 2) + height));
out.push_back(draw(VT_LINETO, sx - (width / 2), sy - (height / 2) + height));
out.push_back(draw(VT_LINETO, sx - (width / 2), sy - (height / 2)));

return out;
} else {
return drawvec();
}
}

void *partial_feature_worker(void *v) {
struct partial_arg *a = (struct partial_arg *) v;
std::vector<struct partial> *partials = a->partials;
Expand All @@ -408,13 +449,22 @@ void *partial_feature_worker(void *v) {
int *additional = (*partials)[i].additional;
int maxzoom = (*partials)[i].maxzoom;

double area = 0;
if (t == VT_POLYGON) {
area = get_area(geom, 0, geom.size());
}

if ((t == VT_LINE || t == VT_POLYGON) && !(prevent[P_SIMPLIFY] || (z == maxzoom && prevent[P_SIMPLIFY_LOW]))) {
if (1 /* !reduced */) { // XXX why did this not simplify if reduced?
if (t == VT_LINE) {
geom = remove_noop(geom, t, 32 - z - line_detail);
}

geom = simplify_lines(geom, z, line_detail, !(prevent[P_CLIPPING] || prevent[P_DUPLICATION]));
drawvec ngeom = simplify_lines(geom, z, line_detail, !(prevent[P_CLIPPING] || prevent[P_DUPLICATION]));

if (t != VT_POLYGON || ngeom.size() >= 3) {
geom = ngeom;
}
}
}

Expand All @@ -441,14 +491,15 @@ void *partial_feature_worker(void *v) {
// Scaling may have made the polygon degenerate.
// Give Clipper a chance to try to fix it.
for (size_t g = 0; g < geoms.size(); g++) {
drawvec before;
if (additional[A_DEBUG_POLYGON]) {
before = geoms[g];
}
drawvec before = geoms[g];
geoms[g] = clean_or_clip_poly(geoms[g], 0, 0, 0, false);
if (additional[A_DEBUG_POLYGON]) {
check_polygon(geoms[g], before);
}

if (geoms[g].size() < 3) {
geoms[g] = revive_polygon(before, area / geoms.size(), z, line_detail);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion version.hpp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define VERSION "tippecanoe v1.11.3\n"
#define VERSION "tippecanoe v1.11.4\n"

0 comments on commit 5cc6d97

Please sign in to comment.