Skip to content

Commit

Permalink
Clip away entire features by bbox. Avoid unnecessary recompression.
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Sep 27, 2023
1 parent f7dc7fa commit e081288
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 97 deletions.
57 changes: 55 additions & 2 deletions clip.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdlib.h>
#include <mapbox/geometry/point.hpp>
#include <mapbox/geometry/multi_polygon.hpp>
#include <mapbox/geometry/wagyu/wagyu.hpp>
Expand Down Expand Up @@ -750,11 +751,20 @@ static std::vector<std::pair<double, double>> clip_poly1(std::vector<std::pair<d
return out;
}

std::atomic<clock_t> decode_time(0);
std::atomic<clock_t> clip_time(0);
std::atomic<clock_t> clean_time(0);
std::atomic<clock_t> encode_time(0);
std::atomic<clock_t> compress_time(0);

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) {
mvt_tile tile, outtile;
bool was_compressed;

clock_t now, then;

now = clock();
try {
if (!tile.decode(s, was_compressed)) {
fprintf(stderr, "Couldn't parse tile %d/%u/%u\n", oz, ox, oy);
Expand All @@ -764,6 +774,8 @@ std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int
fprintf(stderr, "PBF decoding error in tile %d/%u/%u\n", oz, ox, oy);
exit(EXIT_PROTOBUF);
}
then = clock();
decode_time += then - now;

for (auto const &layer : tile.layers) {
mvt_layer outlayer = mvt_layer();
Expand Down Expand Up @@ -811,8 +823,26 @@ std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int
g.y -= ny * outtilesize;
}

now = clock();
// 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) {
Expand All @@ -821,18 +851,23 @@ std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int
} else if (t == VT_POINT) {
geom = clip_point(geom, nz, buffer);
}
then = clock();
clip_time += then - now;

// Scale to output tile extent

to_tile_scale(geom, nz, det);

// Clean geometries

now = clock();
geom = remove_noop(geom, t, 0);
if (t == VT_POLYGON) {
geom = clean_or_clip_poly(geom, 0, 0, false, false);
geom = close_poly(geom);
}
then = clock();
clean_time += then - now;

// Add geometry to output feature

Expand Down Expand Up @@ -865,9 +900,27 @@ std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int
}

if (outtile.layers.size() > 0) {
now = clock();
std::string pbf = outtile.encode();
then = clock();
encode_time += then - now;

std::string compressed;
compress(pbf, compressed, true);
if (do_compress) {
now = clock();
compress(pbf, compressed, true);
then = clock();
compress_time += then - now;
} else {
compressed = pbf;
}

printf("decode %lld, clip %lld, clean %lld, encode %lld, compress %lld\n",
(long long) decode_time,
(long long) clip_time,
(long long) clean_time,
(long long) encode_time,
(long long) compress_time);

return compressed;
} else {
Expand Down
2 changes: 1 addition & 1 deletion geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ int pnpoly(const drawvec &vert, size_t start, size_t nvert, long long testx, lon
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);

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
2 changes: 1 addition & 1 deletion overzoom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading

0 comments on commit e081288

Please sign in to comment.