getVisibleTiles = function(projection, zoom) {
function getVisibleChildren(x, y, z) {
console.log(x, y, z);
let visible = false;
let stream = projection.stream({
point: () => { visible = true; },
lineStart: () => {},
lineEnd: () => {},
polygonStart: () => {},
polygonEnd: () => {}
});
const extent = mercator.clipExtent();
const size = (extent[1][0] - extent[0][0]) / (2 ** z);
const epsilon = 0.001;
const x0 = x * size + epsilon,
y0 = y * size + epsilon,
x1 = (x + 1) * size - epsilon,
y1 = (y + 1) * size - epsilon;
const step = Math.min(size, 1);
const tile = {
type: "Polygon",
coordinates: [
[]
.concat(d3.range(x0, x1 + step / 2, +step).map(x => [x, y0]))
.concat(d3.range(y0, y1 + step / 2, +step).map(y => [x1, y]))
.concat(d3.range(x1, x0 - step / 2, -step).map(x => [x, y1]))
.concat(d3.range(y1, y0 - step / 2, -step).map(y => [x0, y]))
.map(point => mercator.invert(point))
],
};
d3.geoStream(tile, stream);
if (visible) {
if (z < zoom) {
let visibleChildren = []
.concat(getVisibleChildren(2 * x, 2 * y, z + 1))
.concat(getVisibleChildren(2 * x + 1, 2 * y, z + 1))
.concat(getVisibleChildren(2 * x, 2 * y + 1, z + 1))
.concat(getVisibleChildren(2 * x + 1, 2 * y + 1, z + 1))
return visibleChildren;
} else {
return [tile];
}
} else {
return [];
}
}
return getVisibleChildren(0, 0, 0);
}