From f17cec5c44a18671e17ba5c650fa68da9cb0d043 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Wed, 3 Feb 2016 15:20:45 -0800 Subject: [PATCH] Factor out gap logic instead of duplicating it --- geojson.c | 29 ++------------------------- tile.cc | 60 ++++++++++++++++++++++++++++++++----------------------- tile.h | 2 ++ 3 files changed, 39 insertions(+), 52 deletions(-) diff --git a/geojson.c b/geojson.c index d4b83fd..85c4916 100644 --- a/geojson.c +++ b/geojson.c @@ -1583,33 +1583,8 @@ int read_json(int argc, char **argv, char *fname, const char *layername, int max tile[z].fullcount++; - // Keep in sync with write_tile() - if (gamma > 0) { - if (tile[z].gap > 0) { - if (map[i].index == tile[z].previndex) { - continue; // Exact duplicate: can't fulfil the gap requirement - } - - if (exp(log((map[i].index - tile[z].previndex) / scale) * gamma) >= tile[z].gap) { - // Dot is further from the previous than the nth root of the gap, - // so produce it, and choose a new gap at the next point. - tile[z].gap = 0; - } else { - continue; - } - } else { - tile[z].gap = (map[i].index - tile[z].previndex) / scale; - - if (tile[z].gap == 0) { - continue; // Exact duplicate: skip - } else if (tile[z].gap < 1) { - continue; // Narrow dot spacing: need to stretch out - } else { - tile[z].gap = 0; // Wider spacing than minimum: so pass through unchanged - } - } - - tile[z].previndex = map[i].index; + if (manage_gap(map[i].index, &tile[z].previndex, scale, gamma, &tile[z].gap)) { + continue; } tile[z].count++; diff --git a/tile.cc b/tile.cc index 3f8a516..c68adeb 100644 --- a/tile.cc +++ b/tile.cc @@ -563,6 +563,38 @@ void *partial_feature_worker(void *v) { return NULL; } +int manage_gap(unsigned long long index, unsigned long long *previndex, double scale, double gamma, double *gap) { + if (gamma > 0) { + if (*gap > 0) { + if (index == *previndex) { + return 1; // Exact duplicate: can't fulfil the gap requirement + } + + if (exp(log((index - *previndex) / scale) * gamma) >= *gap) { + // Dot is further from the previous than the nth root of the gap, + // so produce it, and choose a new gap at the next point. + *gap = 0; + } else { + return 1; + } + } else { + *gap = (index - *previndex) / scale; + + if (*gap == 0) { + return 1; // Exact duplicate: skip + } else if (*gap < 1) { + return 1; // Narrow dot spacing: need to stretch out + } else { + *gap = 0; // Wider spacing than minimum: so pass through unchanged + } + } + + *previndex = index; + } + + return 0; +} + long long write_tile(char **geoms, char *metabase, char *stringpool, int z, unsigned tx, unsigned ty, int detail, int min_detail, int basezoom, struct pool **file_keys, char **layernames, sqlite3 *outdb, double droprate, int buffer, const char *fname, FILE **geomfile, int minzoom, int maxzoom, double todo, char *geomstart, volatile long long *along, double gamma, int nlayers, char *prevent, char *additional, int child_shards, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y, volatile int *running) { int line_detail; double fraction = 1; @@ -767,32 +799,10 @@ long long write_tile(char **geoms, char *metabase, char *stringpool, int z, unsi } if (gamma > 0) { - unsigned long long index = encode(bbox[0] / 2 + bbox[2] / 2, bbox[1] / 2 + bbox[3] / 2); - if (gap > 0) { - if (index == previndex) { - continue; // Exact duplicate: can't fulfil the gap requirement - } - - if (exp(log((index - previndex) / scale) * gamma) >= gap) { - // Dot is further from the previous than the nth root of the gap, - // so produce it, and choose a new gap at the next point. - gap = 0; - } else { - continue; - } - } else { - gap = (index - previndex) / scale; - - if (gap == 0) { - continue; // Exact duplicate: skip - } else if (gap < 1) { - continue; // Narrow dot spacing: need to stretch out - } else { - gap = 0; // Wider spacing than minimum: so pass through unchanged - } + unsigned long long index = index = encode(bbox[0] / 2 + bbox[2] / 2, bbox[1] / 2 + bbox[3] / 2); + if (manage_gap(index, &previndex, scale, gamma, &gap)) { + continue; } - - previndex = index; } } diff --git a/tile.h b/tile.h index f12a2d7..3a4db27 100644 --- a/tile.h +++ b/tile.h @@ -29,6 +29,8 @@ long long write_tile(char **geom, char *metabase, char *stringpool, unsigned *fi int traverse_zooms(int *geomfd, off_t *geom_size, char *metabase, char *stringpool, struct pool **file_keys, unsigned *midx, unsigned *midy, char **layernames, int maxzoom, int minzoom, int basezoom, sqlite3 *outdb, double droprate, int buffer, const char *fname, const char *tmpdir, double gamma, int nlayers, char *prevent, char *additional, int full_detail, int low_detail, int min_detail, long long *meta_off, long long *pool_off, unsigned *initial_x, unsigned *initial_y); +int manage_gap(unsigned long long index, unsigned long long *previndex, double scale, double gamma, double *gap); + extern unsigned initial_x, initial_y; extern int geometry_scale; extern int quiet;