Skip to content

Commit

Permalink
Use closepath for each polygon ring to match the vector tile spec
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Oct 28, 2015
1 parent b07a40e commit 358e9ca
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ void handle(std::string message, int z, unsigned x, unsigned y, int describe) {
}

printf("[ %f, %f ]", rings[i][j].lon, rings[i][j].lat);
} else {
if (j != 0) {
printf(", ");
}

printf("[ %f, %f ]", rings[i][0].lon, rings[i][0].lat);
}
}

Expand Down
31 changes: 30 additions & 1 deletion geometry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ drawvec remove_noop(drawvec geom, int type, int shift) {
}

if (geom[i].op == VT_CLOSEPATH) {
fprintf(stderr, "Shouldn't happen\n");
out.push_back(geom[i]);
} else { /* moveto or lineto */
out.push_back(geom[i]);
Expand Down Expand Up @@ -310,6 +309,36 @@ drawvec clean_or_clip_poly(drawvec &geom, int z, int detail, int buffer, bool cl
return out;
}

drawvec close_poly(drawvec &geom) {
drawvec out;

for (unsigned i = 0; i < geom.size(); i++) {
if (geom[i].op == VT_MOVETO) {
unsigned j;
for (j = i + 1; j < geom.size(); j++) {
if (geom[j].op != VT_LINETO) {
break;
}
}

if (j - 1 > i) {
if (geom[j - 1].x != geom[i].x || geom[j - 1].y != geom[i].y) {
fprintf(stderr, "Internal error: polygon not closed\n");
}
}

for (unsigned n = i; n < j - 1; n++) {
out.push_back(geom[n]);
}
out.push_back(draw(VT_CLOSEPATH, 0, 0));

i = j - 1;
}
}

return out;
}

drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area) {
drawvec out;
long long pixel = (1 << (32 - detail - z)) * 2;
Expand Down
1 change: 1 addition & 0 deletions geometry.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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, int detail, long long buffer);
drawvec clean_or_clip_poly(drawvec &geom, int z, int detail, int buffer, bool clip);
drawvec close_poly(drawvec &geom);
drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area);
drawvec clip_lines(drawvec &geom, int z, int detail, long long buffer);
int quick_check(long long *bbox, int z, int detail, long long buffer);
Expand Down
1 change: 1 addition & 0 deletions tile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ long long write_tile(char **geoms, char *metabase, char *stringpool, int z, unsi
// Scaling may have made the polygon degenerate.
// Give Clipper a chance to try to fix it.
geom = clean_or_clip_poly(geom, 0, 0, 0, false);
geom = close_poly(geom);
}

if (t == VT_POINT || to_feature(geom, NULL)) {
Expand Down

0 comments on commit 358e9ca

Please sign in to comment.