Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Add d3.geo.tile.overflow for wrapping. #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions geo/tile/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Zoomable Tiles

* Panning & Zooming: <http://bl.ocks.org/mbostock/4132797>
* Panning & Zooming (Wrapped): <http://bl.ocks.org/jasondavies/0051a06829e72b423ba9>
* Clipping: <http://bl.ocks.org/mbostock/4150951>
* Vector Tiles: <http://bl.ocks.org/mbostock/5593150>
* Raster Tiles & Vector Overlay: <http://bl.ocks.org/mbostock/5342063>
Expand Down
29 changes: 22 additions & 7 deletions geo/tile/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ d3.geo.tile = function() {
var size = [960, 500],
scale = 256,
translate = [size[0] / 2, size[1] / 2],
zoomDelta = 0;
zoomDelta = 0,
X = clamp,
Y = clamp;

function tile() {
var z = Math.max(Math.log(scale) / Math.LN2 - 8, 0),
z0 = Math.round(z + zoomDelta),
k = Math.pow(2, z - z0 + 8),
origin = [(translate[0] - scale / 2) / k, (translate[1] - scale / 2) / k],
tiles = [],
cols = d3.range(Math.max(0, Math.floor(-origin[0])), Math.max(0, Math.ceil(size[0] / k - origin[0]))),
rows = d3.range(Math.max(0, Math.floor(-origin[1])), Math.max(0, Math.ceil(size[1] / k - origin[1])));
w = 1 << z0,
x0 = X(Math.floor(-origin[0]), w),
y0 = Y(Math.floor(-origin[1]), w),
x1 = X(Math.ceil(size[0] / k - origin[0]), w),
y1 = Y(Math.ceil(size[1] / k - origin[1]), w);

rows.forEach(function(y) {
cols.forEach(function(x) {
for (var y = y0; y < y1; ++y) {
for (var x = x0; x < x1; ++x) {
tiles.push([x, y, z0]);
});
});
}
}

tiles.translate = origin;
tiles.scale = k;
Expand Down Expand Up @@ -49,5 +54,15 @@ d3.geo.tile = function() {
return tile;
};

tile.overflow = function(_) {
if (!arguments.length) return [X === identity, Y === identity];
X = _[0] ? identity : clamp;
Y = _[1] ? identity : clamp;
return tile;
};

return tile;

function identity(x) { return x; }
function clamp(x, max) { return Math.max(0, Math.min(max, x)); }
};