mapbox / mapbox/tilebelt

tilesToZoom

Open
#19 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
288
Forks
42
PR merge metrics
No merged PRs in 30d

Description

Convert a set of tiles to a different zoom level.

Here's a stab at the code (assumes homogenous zoom level for input tiles):

``` js
function tilesToZoom(tiles, zoom) {
var newTiles = zoomTiles(tiles, zoom);
return newTiles;

function zoomTiles(zoomedTiles) {
if(zoomedTiles[0][2] === zoom){
return zoomedTiles;
} else if(zoomedTiles[0][2] < zoom){
var oneIn = [];
zoomedTiles.forEach(function(tile){
oneIn = oneIn.concat(tilebelt.getChildren(tile));
});
return zoomTiles(oneIn);
} else {
var zoomedTiles = zoomedTiles.map(function(tile){
var centroid =
turf.centroid(
turf.bboxPolygon(
tilebelt.tileToBBOX(tile)
)
);
return tilebelt.pointToTile(
centroid.geometry.coordinates[0],
centroid.geometry.coordinates[1], zoom);
});
return zoomedTiles;
}
}
}
```

Initial tests:

``` js
test('tilesToZoom', function(t){
var zoomUp1 = tilesToZoom([[0,0,0]],1);
t.equal(zoomUp1.length, 4);
var zoomUp1HasTiles = true;
[[0,0,1],[1,0,1],[0,1,1],[1,1,1]].forEach(function(tile){
if(!tilebelt.hasTile(zoomUp1, tile)) hasTiles = false;
});
t.true(zoomUp1HasTiles, '0,0,0 zoomUp1 has tiles');

var zoomUp2 = tilesToZoom([[0,0,0]],2);
t.equal(zoomUp2.length, 16, 'zoom in 2x has 16 tiles');

var zoomUp5 = tilesToZoom([[0,0,0]],5);
t.equal(zoomUp5.length, 1024, 'zoom in 5x has 1024 tiles');

var zoomUp8 = tilesToZoom([[0,0,0]],8);
t.equal(zoomUp8.length, 65536, 'zoom in 5x has 65536 tiles');

var zoomDown1 = tilesToZoom([[1,1,1]],0);
t.equal(zoomDown1.length, 1, '[1,1,1] at zoom 0 has 1 tile');
t.equal(zoomDown1.toString(), [0,0,0].toString(), '[1,1,1] zoomed out should be [0,0,0]');

var zoomDown2 = tilesToZoom([[9372,12536,15]],13);
t.equal(zoomDown2.length, 1, '[9372,12536,15] at zoom 13 has 1 tile');
t.equal(zoomDown2.toString(), [2343,3134,13].toString(), 'expected tile');

t.end();
});
```

cc @aaronlidman

Contributor guide

Open the contributing guide

Research direction

Start at the tilesToZoom entry point and review the tilebelt and turf calls in the issue's proposed implementation. Run the supplied tilesToZoom tests, then verify that zooming in and out produces the expected tile counts and coordinates.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.