Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/geo/tileRanger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,11 @@ export class TileRanger {
if (verbose) {
console.log("find minimal zoom where the the area can be converted by area the size of single tile to skip levels that can't have full hashes");
}
const dx = boundingRange.maxX - boundingRange.minX;
const dy = boundingRange.maxY - boundingRange.minY;
const minXZoom = Math.max(Math.floor(Math.log2(1 << (zoom + 1)) / dx) - 1, 0);
const minYZoom = Math.max(Math.floor(Math.log2(1 << zoom) / dy), 0);

const xTilesCount = boundingRange.maxX - boundingRange.minX + 1;
const yTilesCount = boundingRange.maxY - boundingRange.minY + 1;
const minXZoom = Math.max(Math.floor((zoom + 1) / xTilesCount) - 1, 0);
const minYZoom = Math.max(Math.floor(zoom / yTilesCount), 0);
const minZoom = Math.min(minXZoom, minYZoom);

if (verbose) {
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/geo/tileRanger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ describe('TileRanger', () => {
];
expect(tileRanges).toEqual(expectedRanges);
});

it('encodes non-bbox polygon whose bounding range is a single tile', async () => {
// Triangle fully within tile {x: 0, y: 1, zoom: 1}, which covers lon [-180, -90] lat [0, 90].
// This exercises the single-tile edge case where xTilesCount=1, yTilesCount=1.
const poly = polygon([
[
[-170, 10],
[-100, 10],
[-135, 80],
[-170, 10],
],
]);

const tileRanges = [];
const gen = ranger.encodeFootprint(poly, 1);
for await (const range of gen) {
tileRanges.push(range);
}

const expectedRanges = [{ minX: 0, maxX: 0, minY: 1, maxY: 1, zoom: 1 }];
expect(tileRanges).toEqual(expectedRanges);
});
});

describe('generateTiles', () => {
Expand Down
Loading