Skip to content

Commit

Permalink
Fix antimeridian adjustment. Z0 can't wait until the end of the tile
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Jan 7, 2025
1 parent 2ad9081 commit 5bcf7d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tests/ne_110m_ocean/join/joined.mbtiles.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ "type": "FeatureCollection", "properties": {
"antimeridian_adjusted_bounds": "-180.000000,-85.051129,180.000000,85.051129",
"antimeridian_adjusted_bounds": "0.000000,-85.051129,359.912109,85.051129",
"bounds": "-180.000000,-85.051129,180.000000,85.051129",
"center": "-45.000000,33.256630,4",
"description": "tests/ne_110m_ocean/join/ocean.mbtiles",
Expand Down
38 changes: 21 additions & 17 deletions tile-join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ void append_tile(std::string message, int z, unsigned x, unsigned y, std::map<st
long long miny = LLONG_MAX;
long long maxx = LLONG_MIN;
long long maxy = LLONG_MIN;

long long minx2 = LLONG_MAX;
long long maxx2 = LLONG_MIN;
bool features_added_to_layer = false;

for (size_t f = 0; f < layer.features.size(); f++) {
Expand Down Expand Up @@ -416,11 +419,26 @@ void append_tile(std::string message, int z, unsigned x, unsigned y, std::map<st
long long gx = std::min((long long) outlayer.extent, std::max(0LL, g.x));
long long gy = std::min((long long) outlayer.extent, std::max(0LL, g.y));

// initially keep bounds in tile coordinates
// to world scale
gx = gx * (1LL << (32 - z)) / outlayer.extent;
gy = gy * (1LL << (32 - z)) / outlayer.extent;

// to world offset
gx += (1LL << (32 - z)) * x;
gy += (1LL << (32 - z)) * y;

minx = std::min(minx, gx);
miny = std::min(miny, gy);
maxx = std::max(maxx, gx);
maxy = std::max(maxy, gy);

// if in the western hemisphere, try shifting to east
if (gx < (1LL << 31)) {
gx += 1LL << 32;
}

minx2 = std::min(minx2, gx);
maxx2 = std::max(maxx2, gx);
}
}

Expand All @@ -446,18 +464,6 @@ void append_tile(std::string message, int z, unsigned x, unsigned y, std::map<st
}

if (features_added_to_layer) {
// to world scale
minx = minx * (1LL << (32 - z)) / outlayer.extent;
miny = miny * (1LL << (32 - z)) / outlayer.extent;
maxx = maxx * (1LL << (32 - z)) / outlayer.extent;
maxy = maxy * (1LL << (32 - z)) / outlayer.extent;

// to world offset
minx += (1LL << (32 - z)) * x;
maxx += (1LL << (32 - z)) * x;
miny += (1LL << (32 - z)) * y;
maxy += (1LL << (32 - z)) * y;

double lat1, lon1;
double lat2, lon2;
tile2lonlat(minx, maxy, 32, &lon1, &lat1);
Expand All @@ -468,10 +474,8 @@ void append_tile(std::string message, int z, unsigned x, unsigned y, std::map<st
a->maxlat = std::max(a->maxlat, std::max(lat1, lat2));
a->maxlon = std::max(a->maxlon, std::max(lon1, lon2));

if (lon1 < 0 || lon2 < 0) {
lon1 += 360;
lon2 += 360;
}
tile2lonlat(minx2, maxy, 32, &lon1, &lat1);
tile2lonlat(maxx2, miny, 32, &lon2, &lat2);

a->minlon2 = std::min(a->minlon2, std::min(lon1, lon2));
a->maxlon2 = std::max(a->maxlon2, std::max(lon1, lon2));
Expand Down

0 comments on commit 5bcf7d9

Please sign in to comment.