Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tilesToZoom #19

Open
morganherlocker opened this issue Apr 17, 2015 · 0 comments
Open

tilesToZoom #19

morganherlocker opened this issue Apr 17, 2015 · 0 comments

Comments

@morganherlocker
Copy link
Contributor

Convert a set of tiles to a different zoom level.

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

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:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant