Compare commits

...
8 Commits
Author SHA1 Message Date
Erica Fischer 59bc5e358f Does it do better cleaning *before* scaling? 2023-10-21 12:00:31 -07:00
Erica Fischer d14ad21ffb Test as affected by this PR 2023-10-20 11:40:44 -07:00
Erica Fischer bad662fd86 Previous output of a test with tiny polygons mixed with continuous 2023-10-20 11:31:56 -07:00
Erica Fischer 67cb8c566f Don't try to revive continous polygons if they collapse away 2023-10-20 08:40:59 -07:00
Erica Fischer a1ce734977 Factor out search for shared nodes 2023-10-20 08:06:42 -07:00
Erica Fischer e1c665b35d Only detect longitude wraparound within each ring. (#150)
* Only detect longitude wraparound within each ring.

Between rings it's OK to jump around from one side of the world
to the other.

* Add test

* Update changelog
2023-10-17 12:04:47 -07:00
Erica Fischer 0ca14086a9 Pull from the back of the list of tiles to overzoom, not the front (#149) 2023-10-04 14:59:19 -07:00
Erica Fischer cc5c1c79df Speed up overzooming in tile-join (#147)
* Clip away entire features by bbox. Avoid unnecessary recompression.

* Move parent tile decoding in tile-join out of overzoom proper

* An ever-growing cache of parent tiles

* Limit the size of the cache

* Remove the current reader *before* checking if we can run the queue

* Clean up

* Add missing #include

* Add comment

* When the tile-join cache fills up, evict the least recently used

* Fix microsecond math

* Factoring out tile-join's cache for testing

* Add unit tests for tile-join cache

* Update changelog and version
2023-10-04 12:14:22 -07:00
17 changed files with 1175 additions and 149 deletions
+12
View File
@@ -1,3 +1,15 @@
# 2.35.0
* Fix a bug in --detect-longitude-wraparound when there are multiple rings
# 2.34.1
* Further improvements to tile-join speed
# 2.34.0
* Improve speed of overzooming in tile-join
# 2.33.0
* Further reduce memory usage of --no-simplification-of-shared-nodes by calculating the list of shared nodes globally using temporary files rather than in memory for each individual tile
+1 -1
View File
@@ -73,7 +73,7 @@ tile-join: tile-join.o projection.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpul
tippecanoe-json-tool: jsontool.o jsonpull/jsonpull.o csv.o text.o geojson-loop.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
unit: unit.o text.o sort.o
unit: unit.o text.o sort.o mvt.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
tippecanoe-overzoom: overzoom.o mvt.o clip.o
+35 -4
View File
@@ -1,6 +1,8 @@
#include <stdlib.h>
#include <mapbox/geometry/point.hpp>
#include <mapbox/geometry/multi_polygon.hpp>
#include <mapbox/geometry/wagyu/wagyu.hpp>
#include <limits.h>
#include "geometry.hpp"
#include "errors.hpp"
#include "compression.hpp"
@@ -751,11 +753,11 @@ static std::vector<std::pair<double, double>> clip_poly1(std::vector<std::pair<d
}
std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep) {
mvt_tile tile, outtile;
bool was_compressed;
int detail, int buffer, std::set<std::string> const &keep, bool do_compress) {
mvt_tile tile;
try {
bool was_compressed;
if (!tile.decode(s, was_compressed)) {
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", oz, ox, oy);
exit(EXIT_MVT);
@@ -765,6 +767,13 @@ std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int
exit(EXIT_PROTOBUF);
}
return overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep, do_compress);
}
std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress) {
mvt_tile outtile;
for (auto const &layer : tile.layers) {
mvt_layer outlayer = mvt_layer();
@@ -813,6 +822,23 @@ std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int
// Clip to output tile
long long xmin = LLONG_MAX;
long long ymin = LLONG_MAX;
long long xmax = LLONG_MIN;
long long ymax = LLONG_MIN;
for (auto const &g : geom) {
xmin = std::min(xmin, g.x);
ymin = std::min(ymin, g.y);
xmax = std::max(xmax, g.x);
ymax = std::max(ymax, g.y);
}
long long b = outtilesize * buffer / 256;
if (xmax < -b || ymax < -b || xmin > outtilesize + b || ymin > outtilesize + b) {
continue;
}
if (t == VT_LINE) {
geom = clip_lines(geom, nz, buffer);
} else if (t == VT_POLYGON) {
@@ -866,8 +892,13 @@ std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int
if (outtile.layers.size() > 0) {
std::string pbf = outtile.encode();
std::string compressed;
compress(pbf, compressed, true);
if (do_compress) {
compress(pbf, compressed, true);
} else {
compressed = pbf;
}
return compressed;
} else {
+1
View File
@@ -1,6 +1,7 @@
#include <string>
#include <vector>
#include <sys/stat.h>
#include "mbtiles.hpp"
#ifndef DIRTILES_HPP
#define DIRTILES_HPP
+50 -17
View File
@@ -21,6 +21,8 @@
#include "errors.hpp"
#include "projection.hpp"
bool is_shared_node(draw d, int z, int tx, int ty, struct node *shared_nodes_map, size_t nodepos);
drawvec decode_geometry(char **meta, int z, unsigned tx, unsigned ty, long long *bbox, unsigned initial_x, unsigned initial_y) {
drawvec out;
@@ -176,7 +178,28 @@ void check_polygon(drawvec &geom) {
}
}
drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *still_needs_simplification, bool *reduced_away, double *accum_area, serial_feature *this_feature, serial_feature *tiny_feature) {
bool can_be_dust(drawvec const &geom, size_t i, size_t j, int z, int tx, int ty, struct node *shared_nodes_map, size_t nodepos) {
if (!prevent[P_SIMPLIFY_SHARED_NODES]) {
return true;
}
size_t count = 0;
for (; i < j; i++) {
if (is_shared_node(geom[i], z, tx, ty, shared_nodes_map, nodepos)) {
count++;
if (count >= 4) {
// the three fixed nodes of this feature,
// plus some intersection with some other feature
return false;
}
}
}
return true;
}
drawvec reduce_tiny_poly(drawvec &geom, int z, int tx, int ty, int detail, bool *still_needs_simplification, bool *reduced_away, double *accum_area, serial_feature *this_feature, serial_feature *tiny_feature, struct node *shared_nodes_map, size_t nodepos) {
drawvec out;
const double pixel = (1LL << (32 - detail - z)) * (double) tiny_polygon_size;
bool includes_real = false;
@@ -213,7 +236,8 @@ drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *still_needs_sim
// OR it is an inner ring and we haven't output an outer ring for it to be
// cut out of, so we are just subtracting its area from the tiny polygon
// rather than trying to deal with it geometrically
if ((area > 0 && area <= pixel * pixel) || (area < 0 && !included_last_outer)) {
if (can_be_dust(geom, i, j, z, tx, ty, shared_nodes_map, nodepos) &&
((area > 0 && area <= pixel * pixel) || (area < 0 && !included_last_outer))) {
*accum_area += area;
*reduced_away = true;
@@ -485,6 +509,26 @@ drawvec impose_tile_boundaries(drawvec &geom, long long extent) {
return out;
}
bool is_shared_node(draw d, int z, int tx, int ty, struct node *shared_nodes_map, size_t nodepos) {
if (nodepos > 0) {
// offset to global
if (z != 0) {
d.x += tx * (1LL << (32 - z));
d.y += ty * (1LL << (32 - z));
}
// to quadkey
struct node n;
n.index = encode_quadkey((unsigned) d.x, (unsigned) d.y);
if (bsearch(&n, shared_nodes_map, nodepos / sizeof(node), sizeof(node), nodecmp) != NULL) {
return true;
}
}
return false;
}
drawvec simplify_lines(drawvec &geom, int z, int tx, int ty, int detail, bool mark_tile_bounds, double simplification, size_t retain, drawvec const &shared_nodes, struct node *shared_nodes_map, size_t nodepos) {
int res = 1 << (32 - detail - z);
long long area = 1LL << (32 - z);
@@ -509,26 +553,15 @@ drawvec simplify_lines(drawvec &geom, int z, int tx, int ty, int detail, bool ma
// To look through the latter, we need to offset and encode the coordinates
// of the feature we are simplifying.
// nodes introduced at the tile edge
auto pt = std::lower_bound(shared_nodes.begin(), shared_nodes.end(), geom[i]);
if (pt != shared_nodes.end() && *pt == geom[i]) {
geom[i].necessary = true;
}
if (nodepos > 0) {
// offset to global
draw d = geom[i];
if (z != 0) {
d.x += tx * (1LL << (32 - z));
d.y += ty * (1LL << (32 - z));
}
// to quadkey
struct node n;
n.index = encode_quadkey((unsigned) d.x, (unsigned) d.y);
if (bsearch(&n, shared_nodes_map, nodepos / sizeof(node), sizeof(node), nodecmp) != NULL) {
geom[i].necessary = true;
}
// global shared nodes
if (is_shared_node(geom[i], z, tx, ty, shared_nodes_map, nodepos)) {
geom[i].necessary = true;
}
}
}
+7 -2
View File
@@ -7,6 +7,7 @@
#include <string>
#include <sqlite3.h>
#include <stdio.h>
#include <mvt.hpp>
#define VT_POINT 1
#define VT_LINE 2
@@ -71,7 +72,7 @@ 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, bool try_scaling);
drawvec close_poly(drawvec &geom);
drawvec reduce_tiny_poly(drawvec &geom, int z, int detail, bool *still_needs_simplification, bool *reduced_away, double *accum_area, serial_feature *this_feature, serial_feature *tiny_feature);
drawvec reduce_tiny_poly(drawvec &geom, int z, int tx, int ty, int detail, bool *still_needs_simplification, bool *reduced_away, double *accum_area, serial_feature *this_feature, serial_feature *tiny_feature, struct node *shared_nodes_map, size_t nodepos);
int clip(long long *x0, long long *y0, long long *x1, long long *y1, long long xmin, long long ymin, long long xmax, long long ymax);
drawvec clip_lines(drawvec &geom, int z, long long buffer);
drawvec stairstep(drawvec &geom, int z, int detail);
@@ -96,8 +97,12 @@ drawvec clip_point(drawvec &geom, long long x1, long long y1, long long x2, long
void visvalingam(drawvec &ls, size_t start, size_t end, double threshold, size_t retain);
int pnpoly(const drawvec &vert, size_t start, size_t nvert, long long testx, long long testy);
double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y);
bool can_be_dust(drawvec const &geom, size_t i, size_t j, int z, int tx, int ty, struct node *shared_nodes_map, size_t nodepos);
std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress);
std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep);
int detail, int buffer, std::set<std::string> const &keep, bool do_compress);
#endif
+1 -1
View File
@@ -91,7 +91,7 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE);
}
std::string out = overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep);
std::string out = overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep, true);
fwrite(out.c_str(), sizeof(char), out.size(), f);
fclose(f);
+16 -11
View File
@@ -325,19 +325,24 @@ static long long scale_geometry(struct serialization_state *sst, long long *bbox
long long y = geom[i].y;
if (additional[A_DETECT_WRAPAROUND]) {
x += offset;
if (has_prev) {
if (x - prev > (1LL << 31)) {
offset -= 1LL << 32;
x -= 1LL << 32;
} else if (prev - x > (1LL << 31)) {
offset += 1LL << 32;
x += 1LL << 32;
if (geom[i].op == VT_LINETO) {
x += offset;
if (has_prev) {
if (x - prev > (1LL << 31)) {
offset -= 1LL << 32;
x -= 1LL << 32;
} else if (prev - x > (1LL << 31)) {
offset += 1LL << 32;
x += 1LL << 32;
}
}
}
has_prev = true;
prev = x;
has_prev = true;
prev = x;
} else {
offset = 0;
prev = x;
}
}
if (x < bbox[0]) {
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+46
View File
@@ -0,0 +1,46 @@
#include <functional>
#include "mvt.hpp"
#include "dirtiles.hpp" // for zxy
struct mvt_tile_seq {
mvt_tile tile;
size_t seq;
};
struct tile_cache {
std::map<zxy, mvt_tile_seq> overzoom_cache;
std::atomic<size_t> seq;
size_t capacity = 1000;
mvt_tile get(zxy parent_tile, std::function<mvt_tile(zxy)> getter) {
mvt_tile source;
auto f = overzoom_cache.find(parent_tile);
if (f == overzoom_cache.end()) {
if (overzoom_cache.size() >= capacity) {
// evict the oldest tile to make room
auto to_erase = overzoom_cache.begin();
for (auto here = overzoom_cache.begin(); here != overzoom_cache.end(); ++here) {
if (here->second.seq < to_erase->second.seq) {
to_erase = here;
}
}
overzoom_cache.erase(to_erase);
}
source = getter(parent_tile);
mvt_tile_seq to_cache;
to_cache.tile = source;
to_cache.seq = seq++;
overzoom_cache.emplace(parent_tile, to_cache);
} else {
f->second.seq = seq++;
source = f->second.tile;
}
return source;
}
};
+46 -17
View File
@@ -33,6 +33,7 @@
#include "csv.hpp"
#include "text.hpp"
#include "tile.hpp"
#include "tile-cache.hpp"
#include <fstream>
#include <sstream>
#include <algorithm>
@@ -379,13 +380,14 @@ double max(double a, double b) {
struct tilecmp {
bool operator()(std::pair<unsigned, unsigned> const &a, std::pair<unsigned, unsigned> const &b) {
// must match behavior of tileset_reader::operator<()
// except backwards, since we are pulling from the end of the list
if (a.first < b.first) {
if (b.first < a.first) {
return true;
}
if (a.first == b.first) {
if (b.first == a.first) {
// Y sorts backwards, in TMS order
if (a.second > b.second) {
if (b.second > a.second) {
return true;
}
}
@@ -435,6 +437,9 @@ struct tileset_reader {
std::vector<std::pair<unsigned, unsigned>> overzoomed_tiles;
bool overzoom_consumed_at_this_zoom = false;
// parent tile cache
tile_cache cache;
// for iterating mbtiles
sqlite3 *db = NULL;
sqlite3_stmt *stmt = NULL;
@@ -551,8 +556,8 @@ struct tileset_reader {
return;
}
auto xy = overzoomed_tiles.front();
overzoomed_tiles.erase(overzoomed_tiles.begin());
auto xy = overzoomed_tiles.back();
overzoomed_tiles.erase(overzoomed_tiles.begin() + overzoomed_tiles.size() - 1);
x = xy.first;
y = xy.second;
@@ -649,7 +654,7 @@ struct tileset_reader {
overzoom_consumed_at_this_zoom = false;
}
std::string get_tile(zxy tile) {
mvt_tile get_tile(zxy tile) {
std::string source;
if (db != NULL) {
@@ -683,7 +688,23 @@ struct tileset_reader {
source = dir_read_tile(dirbase, tile);
}
return source;
mvt_tile content;
if (source.size() == 0) {
return content;
}
try {
bool was_compressed;
if (!content.decode(source, was_compressed)) {
fprintf(stderr, "Couldn't parse tile %lld/%lld/%lld\n", tile.z, tile.x, tile.y);
exit(EXIT_MVT);
}
} catch (std::exception const &e) {
fprintf(stderr, "PBF decoding error in tile %lld/%lld/%lld\n", tile.z, tile.x, tile.y);
exit(EXIT_PROTOBUF);
}
return content;
}
// Sort in z/x/tms_y order, because that is the order of the
@@ -737,14 +758,18 @@ struct tileset_reader {
perror("pthread_mutex_lock");
}
std::string source = get_tile(parent_tile);
std::function<mvt_tile(zxy)> getter = [&](zxy tile) {
return get_tile(tile);
};
mvt_tile source = cache.get(parent_tile, getter);
if (pthread_mutex_unlock(&retrieve_lock) != 0) {
perror("pthread_mutex_unlock");
}
if (source.size() != 0) {
std::string ret = overzoom(source, parent_tile.z, parent_tile.x, parent_tile.y, tile.z, tile.x, tile.y, -1, buffer, std::set<std::string>());
if (source.layers.size() != 0) {
std::string ret = overzoom(source, parent_tile.z, parent_tile.x, parent_tile.y, tile.z, tile.x, tile.y, -1, buffer, std::set<std::string>(), false);
return ret;
}
@@ -1021,13 +1046,7 @@ void decode(struct tileset_reader *readers, std::map<std::string, layermap_entry
f->second.push_back(current.second);
}
if (readers == NULL || readers->zoom != current.first.z || readers->x != current.first.x || readers->y != current.first.y) {
if (tasks.size() > 100 * CPUS) {
dispatch_tasks(tasks, layermaps, outdb, outdir, header, mapping, exclude, include, ifmatched, keep_layers, remove_layers, filter, readers);
tasks.clear();
}
}
// Advance the tileset_reader that we just added as a task.
// The reason this prefetches is so the tileset_reader queue can be
// priority-ordered, so the one with the next relevant tile
// is first in line.
@@ -1039,6 +1058,16 @@ void decode(struct tileset_reader *readers, std::map<std::string, layermap_entry
readers = readers->next;
r->next = NULL;
// Is the next tileset_reader on the tileset_reader queue looking at a different tile?
// Then this tile is done and we can safely run the output queue.
if (readers == NULL || readers->zoom != current.first.z || readers->x != current.first.x || readers->y != current.first.y) {
if (tasks.size() > 100 * CPUS) {
dispatch_tasks(tasks, layermaps, outdb, outdir, header, mapping, exclude, include, ifmatched, keep_layers, remove_layers, filter, readers);
tasks.clear();
}
}
// put the tileset_reader back onto the queue,
// in whatever sequence its next tile calls for
+14 -3
View File
@@ -609,9 +609,17 @@ void *partial_feature_worker(void *v) {
signed char t = (*partials)[i].t;
int z = (*partials)[i].z;
int tx = (*partials)[i].tx;
int ty = (*partials)[i].ty;
int out_detail = (*partials)[i].extra_detail;
drawvec geom = (*partials)[i].geoms[0];
if (t == VT_POLYGON) {
geom = clean_or_clip_poly(geom, 0, 0, false, false);
}
drawvec before_scaling = (*partials)[i].geoms[0];
to_tile_scale(geom, z, out_detail);
if (t == VT_POLYGON) {
@@ -625,8 +633,11 @@ void *partial_feature_worker(void *v) {
check_polygon(geom);
}
if (geom.size() < 3) {
if (area > 0) {
if (geom.size() < 4) {
// Don't try to revive a continuous polygon, because we should be
// able to rely on its three fixed points to keep it alive if it
// actually has any area at this zoom level.
if (area > 0 && can_be_dust(before_scaling, 0, before_scaling.size(), z, tx, ty, a->shared_nodes_map, a->nodepos)) {
// area is in world coordinates, calculated before scaling down
geom = revive_polygon(before, area, z, out_detail);
} else {
@@ -2207,7 +2218,7 @@ long long write_tile(decompressor *geoms, std::atomic<long long> *geompos_in, ch
bool prevent_tiny = prevent[P_TINY_POLYGON_REDUCTION] ||
(prevent[P_TINY_POLYGON_REDUCTION_AT_MAXZOOM] && z == maxzoom);
if (!prevent_tiny && !additional[A_GRID_LOW_ZOOMS]) {
sf.geometry = reduce_tiny_poly(sf.geometry, z, line_detail, &still_need_simplification_after_reduction, &simplified_away_by_reduction, &accum_area, &sf, &tiny_feature);
sf.geometry = reduce_tiny_poly(sf.geometry, z, tx, ty, line_detail, &still_need_simplification_after_reduction, &simplified_away_by_reduction, &accum_area, &sf, &tiny_feature, shared_nodes_map, nodepos);
if (simplified_away_by_reduction) {
strategy->tiny_polygons++;
}
+34
View File
@@ -2,6 +2,7 @@
#include "catch/catch.hpp"
#include "text.hpp"
#include "sort.hpp"
#include "tile-cache.hpp"
#include <unistd.h>
#include <limits.h>
@@ -65,3 +66,36 @@ TEST_CASE("External quicksort", "fqsort") {
fclose(f);
REQUIRE(nread == written);
}
mvt_tile mock_get_tile(zxy tile) {
mvt_layer l;
l.name = std::to_string(tile.z) + "/" + std::to_string(tile.x) + "/" + std::to_string(tile.y);
mvt_tile t;
t.layers.push_back(l);
return t;
}
TEST_CASE("Tile-join cache", "tile cache") {
tile_cache tc;
tc.capacity = 5;
REQUIRE(tc.get(zxy(11, 327, 791), mock_get_tile).layers[0].name == "11/327/791");
REQUIRE(tc.get(zxy(11, 5, 7), mock_get_tile).layers[0].name == "11/5/7");
REQUIRE(tc.get(zxy(11, 5, 8), mock_get_tile).layers[0].name == "11/5/8");
REQUIRE(tc.get(zxy(11, 5, 9), mock_get_tile).layers[0].name == "11/5/9");
REQUIRE(tc.get(zxy(11, 5, 10), mock_get_tile).layers[0].name == "11/5/10");
REQUIRE(tc.get(zxy(11, 327, 791), mock_get_tile).layers[0].name == "11/327/791");
REQUIRE(tc.overzoom_cache.size() == 5);
REQUIRE(tc.overzoom_cache.find(zxy(11, 327, 791)) != tc.overzoom_cache.end());
REQUIRE(tc.overzoom_cache.find(zxy(11, 5, 7)) != tc.overzoom_cache.end());
// verify that additional gets evict the least-recently-used elements
REQUIRE(tc.get(zxy(11, 5, 11), mock_get_tile).layers[0].name == "11/5/11");
REQUIRE(tc.overzoom_cache.size() == 5);
REQUIRE(tc.overzoom_cache.find(zxy(11, 5, 7)) == tc.overzoom_cache.end());
REQUIRE(tc.get(zxy(11, 5, 12), mock_get_tile).layers[0].name == "11/5/12");
REQUIRE(tc.overzoom_cache.size() == 5);
REQUIRE(tc.overzoom_cache.find(zxy(11, 5, 8)) == tc.overzoom_cache.end());
}
+1 -1
View File
@@ -1,6 +1,6 @@
#ifndef VERSION_HPP
#define VERSION_HPP
#define VERSION "v2.33.0"
#define VERSION "v2.35.0"
#endif