Skip to content

Commit

Permalink
return null on invalid/non-existing tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Aug 14, 2015
1 parent f9d9c8b commit 27aea1c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ npm run build-min # minified production build

### Changelog

##### 2.1.3 (Aug 14, 2015)
##### 2.1.4 (Aug 14, 2015)

- Improved `getTile` to always return `null` on non-existing or invalid tiles.

##### 2.1.3 (Aug 13, 2015)

- Added `solidChildren` option that includes children of solid filled square tiles in the index (off by default).
- Added back solid tile heuristics (not tiling solid filled square tiles further).
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ GeoJSONVT.prototype.getTile = function (z, x, y) {
parent = this.tiles[toID(z0, x0, y0)];
}

if (!parent) return null;

if (debug > 1) console.log('found parent tile z%d-%d-%d', z0, x0, y0);

// if we found a parent tile containing the original geometry, we can drill down from it
Expand All @@ -186,11 +188,13 @@ GeoJSONVT.prototype.getTile = function (z, x, y) {
if (debug > 1) console.timeEnd('drilling down');
}

if (!this.tiles[id]) return null;

return transformTile(this.tiles[id], extent);
};

function transformTile(tile, extent) {
if (!tile || tile.transformed) return tile;
if (tile.transformed) return tile;

var z2 = tile.z2,
tx = tile.x,
Expand Down
14 changes: 7 additions & 7 deletions test/test-get-tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ test('getTile: us-states.json', function (t) {
console.log = function () {};
var index = geojsonvt(getJSON('us-states.json'), {debug: 2});

var tile1 = index.getTile(7, 37, 48).features,
tile2 = index.getTile(9, 148, 192).features,
tile3 = index.getTile(11, 592, 768).features;

console.log = log;

t.same(tile1, getJSON('us-states-z7-37-48.json'), 'z7-37-48');
t.same(index.getTile(7, 37, 48).features, getJSON('us-states-z7-37-48.json'), 'z7-37-48');

t.same(index.getTile(9, 148, 192).features, square, 'z9-148-192 (clipped square)');
t.same(index.getTile(11, 592, 768).features, square, 'z11-592-768 (clipped square)');

t.equal(index.getTile(11, 800, 400), null, 'non-existing tile');
t.equal(index.getTile(-5, 123.25, 400.25), null, 'invalid tile');

t.same(tile2, square, 'z9-148-192 (clipped square)');
t.same(tile3, square, 'z11-592-768 (clipped square)');
t.end();
});

Expand Down

0 comments on commit 27aea1c

Please sign in to comment.