From 3cd6fcfbe0009d04295ebeb3ba2b68676666d280 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Mon, 27 Oct 2014 13:20:17 -0700 Subject: [PATCH] Add option to set buffer size. Pass it through to clipping functions. --- README.md | 1 + geojson.c | 22 +++++++++++++--------- geometry.cc | 32 ++++++++++++++++++-------------- geometry.hh | 4 ++-- tile.cc | 6 +++--- tile.h | 2 +- 6 files changed, 38 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 2e6d20a..14024f9 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Options * -X: Exclude all properties and encode only geometries * -f: Delete the mbtiles file if it already exists instead of giving an error * -r rate: Rate at which dots are dropped at lower zoom levels (default 2.5) + * -b pixels: Buffer size where features are duplicated from adjacent tiles (default 5) Example ------- diff --git a/geojson.c b/geojson.c index 3fd22e7..40371e8 100644 --- a/geojson.c +++ b/geojson.c @@ -172,7 +172,7 @@ struct pool_val *deserialize_string(char **f, struct pool *p, int type) { return ret; } -void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, struct pool *file_keys, unsigned *midx, unsigned *midy, char *layername, int maxzoom, int minzoom, sqlite3 *outdb, double droprate) { +void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, struct pool *file_keys, unsigned *midx, unsigned *midy, char *layername, int maxzoom, int minzoom, sqlite3 *outdb, double droprate, int buffer) { fprintf(stderr, "\n"); long long most = 0; @@ -208,7 +208,7 @@ void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, s fprintf(stderr, " %3.1f%% %d/%u/%u %x %x %lld to %lld \r", (((i - ix) + (j - ix)) / 2.0 / n + (maxzoom - z)) / (maxzoom - minzoom + 1) * 100, z, tx, ty, wx, wy, (long long)(i - ix), (long long)(j - ix)); - long long len = write_tile(i, j, metabase, file_bbox, z, tx, ty, z == maxzoom ? full_detail : low_detail, maxzoom, file_keys, layername, outdb, droprate); + long long len = write_tile(i, j, metabase, file_bbox, z, tx, ty, z == maxzoom ? full_detail : low_detail, maxzoom, file_keys, layername, outdb, droprate, buffer); if (z == maxzoom && len > most) { *midx = tx; @@ -221,7 +221,7 @@ void check(struct index *ix, long long n, char *metabase, unsigned *file_bbox, s fprintf(stderr, "\n"); } -void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom, sqlite3 *outdb, struct pool *exclude, int exclude_all, double droprate) { +void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom, sqlite3 *outdb, struct pool *exclude, int exclude_all, double droprate, int buffer) { char metaname[] = "/tmp/meta.XXXXXXXX"; char indexname[] = "/tmp/index.XXXXXXXX"; @@ -342,7 +342,6 @@ void read_json(FILE *f, char *fname, char *layername, int maxzoom, int minzoom, } int z = maxzoom; - int buffer = 10; unsigned cx = bbox[0] / 2 + bbox[2] / 2; unsigned cy = bbox[1] / 2 + bbox[3] / 2; @@ -470,7 +469,7 @@ next_feature: } qsort(index, indexst.st_size / sizeof(struct index), sizeof(struct index), indexcmp); - check(index, indexst.st_size / sizeof(struct index), meta, file_bbox, &file_keys, &midx, &midy, layername, maxzoom, minzoom, outdb, droprate); + check(index, indexst.st_size / sizeof(struct index), meta, file_bbox, &file_keys, &midx, &midy, layername, maxzoom, minzoom, outdb, droprate, buffer); munmap(index, indexst.st_size); munmap(meta, metast.st_size); @@ -520,12 +519,13 @@ int main(int argc, char **argv) { int minzoom = 0; int force = 0; double droprate = 2.5; + int buffer = 5; struct pool exclude; pool_init(&exclude, 0); int exclude_all = 0; - while ((i = getopt(argc, argv, "l:n:z:Z:d:D:o:x:r:fX")) != -1) { + while ((i = getopt(argc, argv, "l:n:z:Z:d:D:o:x:r:b:fX")) != -1) { switch (i) { case 'n': name = optarg; @@ -567,12 +567,16 @@ int main(int argc, char **argv) { droprate = atof(optarg); break; + case 'b': + buffer = atoi(optarg); + break; + case 'f': force = 1; break; default: - fprintf(stderr, "Usage: %s -o out.mbtiles [-n name] [-l layername] [-z maxzoom] [-Z minzoom] [-d detail] [-D lower-detail] [-x excluded-field ...] [-X] [-r droprate] [file.json]\n", argv[0]); + fprintf(stderr, "Usage: %s -o out.mbtiles [-n name] [-l layername] [-z maxzoom] [-Z minzoom] [-d detail] [-D lower-detail] [-x excluded-field ...] [-X] [-r droprate] [-b buffer] [file.json]\n", argv[0]); exit(EXIT_FAILURE); } } @@ -595,7 +599,7 @@ int main(int argc, char **argv) { if (f == NULL) { fprintf(stderr, "%s: %s: %s\n", argv[0], argv[i], strerror(errno)); } else { - read_json(f, name ? name : argv[i], layer, maxzoom, minzoom, outdb, &exclude, exclude_all, droprate); + read_json(f, name ? name : argv[i], layer, maxzoom, minzoom, outdb, &exclude, exclude_all, droprate, buffer); fclose(f); } } @@ -603,7 +607,7 @@ int main(int argc, char **argv) { fprintf(stderr, "%s: Only accepts one input file\n", argv[0]); exit(EXIT_FAILURE); } else { - read_json(stdin, name ? name : outdir, layer, maxzoom, minzoom, outdb, &exclude, exclude_all, droprate); + read_json(stdin, name ? name : outdir, layer, maxzoom, minzoom, outdb, &exclude, exclude_all, droprate, buffer); } mbtiles_close(outdb, argv); diff --git a/geometry.cc b/geometry.cc index 30bb95b..a06878f 100644 --- a/geometry.cc +++ b/geometry.cc @@ -179,8 +179,8 @@ drawvec shrink_lines(drawvec &geom, int z, int detail, int basezoom, long long * } #endif -static bool inside(draw d, int edge, long long area) { - long long clip_buffer = area / 64; +static bool inside(draw d, int edge, long long area, long long buffer) { + long long clip_buffer = buffer * area / 256; switch (edge) { case 0: // top @@ -214,8 +214,8 @@ static draw get_line_intersection(draw p0, draw p1, draw p2, draw p3) { return draw(VT_LINETO, p0.x + (t * s1_x), p0.y + (t * s1_y)); } -static draw intersect(draw a, draw b, int edge, long long area) { - long long clip_buffer = area / 64; +static draw intersect(draw a, draw b, int edge, long long area, long long buffer) { + long long clip_buffer = buffer * area / 256; switch (edge) { case 0: // top @@ -240,7 +240,7 @@ static draw intersect(draw a, draw b, int edge, long long area) { } // http://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm -static drawvec clip_poly1(drawvec &geom, int z, int detail) { +static drawvec clip_poly1(drawvec &geom, int z, int detail, int buffer) { drawvec out = geom; long long area = 0xFFFFFFFF; @@ -258,13 +258,13 @@ static drawvec clip_poly1(drawvec &geom, int z, int detail) { for (unsigned e = 0; e < in.size(); e++) { draw E = in[e]; - if (inside(E, edge, area)) { - if (!inside(S, edge, area)) { - out.push_back(intersect(S, E, edge, area)); + if (inside(E, edge, area, buffer)) { + if (!inside(S, edge, area, buffer)) { + out.push_back(intersect(S, E, edge, area, buffer)); } out.push_back(E); - } else if (inside(S, edge, area)) { - out.push_back(intersect(S, E, edge, area)); + } else if (inside(S, edge, area, buffer)) { + out.push_back(intersect(S, E, edge, area, buffer)); } S = E; @@ -282,7 +282,7 @@ static drawvec clip_poly1(drawvec &geom, int z, int detail) { return out; } -drawvec clip_poly(drawvec &geom, int z, int detail) { +drawvec clip_poly(drawvec &geom, int z, int detail, int buffer) { if (z == 0) { return geom; } @@ -302,7 +302,7 @@ drawvec clip_poly(drawvec &geom, int z, int detail) { for (unsigned k = i; k < j; k++) { tmp.push_back(geom[k]); } - tmp = clip_poly1(tmp, z, detail); + tmp = clip_poly1(tmp, z, detail, buffer); for (unsigned k = 0; k < tmp.size(); k++) { out.push_back(tmp[k]); } @@ -383,7 +383,7 @@ drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double } -drawvec clip_lines(drawvec &geom, int z, int detail) { +drawvec clip_lines(drawvec &geom, int z, int detail, long long buffer) { drawvec out; unsigned i; @@ -395,12 +395,16 @@ drawvec clip_lines(drawvec &geom, int z, int detail) { double x2 = geom[i - 0].x; double y2 = geom[i - 0].y; + unsigned min = 0; unsigned area = 0xFFFFFFFF; if (z != 0) { area = 1 << (32 - z); + + min -= buffer * area / 256; + area += buffer * area / 256; } - int c = clip(&x1, &y1, &x2, &y2, 0, 0, area, area); + int c = clip(&x1, &y1, &x2, &y2, min, min, area, area); if (c > 1) { // clipped out.push_back(draw(VT_MOVETO, x1, y1)); diff --git a/geometry.hh b/geometry.hh index 9017bc5..b21bb99 100644 --- a/geometry.hh +++ b/geometry.hh @@ -18,8 +18,8 @@ typedef std::vector drawvec; drawvec decode_geometry(char **meta, int z, unsigned tx, unsigned ty, int detail); void to_tile_scale(drawvec &geom, int z, int detail); drawvec remove_noop(drawvec geom, int type); -drawvec clip_poly(drawvec &geom, int z, int detail); +drawvec clip_poly(drawvec &geom, int z, int detail, int buffer); drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *reduced, double *accum_area); -drawvec clip_lines(drawvec &geom, int z, int detail); +drawvec clip_lines(drawvec &geom, int z, int detail, long long buffer); drawvec simplify_lines(drawvec &geom, int z, int detail); drawvec reorder_lines(drawvec &geom); diff --git a/tile.cc b/tile.cc index ae4023e..73176ab 100644 --- a/tile.cc +++ b/tile.cc @@ -340,7 +340,7 @@ void evaluate(std::vector &features, char *metabase, struct pool *file pool_free(&keys); } -long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned tx, unsigned ty, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb, double droprate) { +long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned tx, unsigned ty, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb, double droprate, int buffer) { int line_detail; static bool evaluated = false; @@ -398,11 +398,11 @@ long long write_tile(struct index *start, struct index *end, char *metabase, uns } if (t == VT_LINE) { - geom = clip_lines(geom, z, line_detail); + geom = clip_lines(geom, z, line_detail, buffer); } if (t == VT_POLYGON) { - geom = clip_poly(geom, z, line_detail); + geom = clip_poly(geom, z, line_detail, buffer); } if (t == VT_LINE || t == VT_POLYGON) { diff --git a/tile.h b/tile.h index f1d7d73..5b1ff79 100644 --- a/tile.h +++ b/tile.h @@ -23,4 +23,4 @@ struct index { int maxzoom; }; -long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned x, unsigned y, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb, double droprate); +long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned x, unsigned y, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb, double droprate, int buffer);