Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up overzooming in tile-join #147

Merged
merged 14 commits into from
Oct 4, 2023
39 changes: 35 additions & 4 deletions clip.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <sqlite3.h>
#include <stdio.h>
#include <mvt.hpp>

#define VT_POINT 1
#define VT_LINE 2
Expand Down Expand Up @@ -93,7 +94,10 @@ void visvalingam(drawvec &ls, size_t start, size_t end, double threshold, size_t
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);

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
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