mapbox / tile-cover

Generate the minimum number of tiles to cover a geojson geometry
MIT License
189 stars 40 forks source link

Expected behavior of min_zoom and max_zoom? #64

Closed veltman closed 9 years ago

veltman commented 9 years ago

I'm a little unclear on how a case with multiple zoom levels is supposed to work.

var cover = require("tile-cover");

var geom = {
  "type": "Polygon",
  "coordinates": [
    [
      [
        -74.03549194335938,
        40.66918118282895
      ],
      [
        -74.03549194335938,
        40.84913799774759
      ],
      [
        -73.86245727539062,
        40.84913799774759
      ],
      [
        -73.86245727539062,
        40.66918118282895
      ],
      [
        -74.03549194335938,
        40.66918118282895
      ]
    ]
  ]
};

console.log(cover.tiles(geom,{min_zoom: 14, max_zoom: 14}).length); // 108 tiles
console.log(cover.tiles(geom,{min_zoom: 15, max_zoom: 15}).length); // 368 tiles
console.log(cover.tiles(geom,{min_zoom: 14, max_zoom: 15}).length); // 137 tiles??

I would expect that when min_zoom is less than max_zoom, the array I get back would be the combination of the array for each zoom level in the range. So in the above example, I would assume that the third statement would produce 476 tiles (108 + 368). Is that not what's supposed to happen?

morganherlocker commented 9 years ago

When min and max are equal, the cover is simply a collection of tiles at one zoom level. Setting a lower min_zoom than max_zoom will make the algorithm merge tiles when possible, up to the min_zoom. Here is an example of this in practice:

The idea here is to deliver the absolute minimum number of indexes needed to cover an entire shape. If you are looking for multiple unmerged zoom levels that stack on top of one another, you could do something like:

cover.tiles(geom,{min_zoom: 14, max_zoom: 14}).concat(
cover.tiles(geom,{min_zoom: 15, max_zoom: 15}))
veltman commented 9 years ago

Got it, thanks for the clarification!