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

enforce TileJSON bounds #4556

Merged
merged 4 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 15 additions & 1 deletion src/source/raster_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ajax = require('../util/ajax');
const Evented = require('../util/evented');
const loadTileJSON = require('./load_tilejson');
const normalizeURL = require('../util/mapbox').normalizeTileURL;
const TileBounds = require('./tile_bounds');

class RasterTileSource extends Evented {

Expand Down Expand Up @@ -32,6 +33,7 @@ class RasterTileSource extends Evented {
return this.fire('error', err);
}
util.extend(this, tileJSON);
this.setBounds(tileJSON.bounds);

// `content` is included here to prevent a race condition where `Style#_updateSources` is called
// before the TileJSON arrives. this makes sure the tiles needed are loaded once TileJSON arrives
Expand All @@ -47,15 +49,27 @@ class RasterTileSource extends Evented {
this.map = map;
}

setBounds(bounds) {
this.bounds = bounds;
if (bounds) {
this.tileBounds = new TileBounds(bounds, this.minzoom, this.maxzoom);
}
}

serialize() {
return {
type: 'raster',
url: this.url,
tileSize: this.tileSize,
tiles: this.tiles
tiles: this.tiles,
bounds: this.bounds,
};
}

hasTile(coord) {
return !this.tileBounds || this.tileBounds.contains(coord);
}

loadTile(tile, callback) {
const url = normalizeURL(tile.coord.url(this.tiles, null, this.scheme), this.url, this.tileSize);

Expand Down
4 changes: 4 additions & 0 deletions src/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ class SourceCache extends Evented {
roundZoom: this._source.roundZoom,
reparseOverscaled: this._source.reparseOverscaled
});

if (this._source.hasTile) {
visibleCoords = visibleCoords.filter((coord) => this._source.hasTile(coord));
}
}

for (i = 0; i < visibleCoords.length; i++) {
Expand Down
33 changes: 33 additions & 0 deletions src/source/tile_bounds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const LngLatBounds = require('../geo/lng_lat_bounds');
const clamp = require('../util/util').clamp;

class TileBounds {
constructor(bounds, minzoom, maxzoom) {
this.bounds = LngLatBounds.convert(bounds);
this.minzoom = minzoom || 0;
this.maxzoom = maxzoom || 24;
}

contains(coord) {
const level = [
[Math.floor(this.lngX(this.bounds.getWest(), coord.z)), Math.floor(this.latY(this.bounds.getNorth(), coord.z))],
[Math.ceil(this.lngX(this.bounds.getEast(), coord.z)), Math.ceil(this.latY(this.bounds.getSouth(), coord.z))]
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: for readability, instead of an array, could this be four individual consts minX, minY, etc.?

const hit = coord.x >= level[0][0] && coord.x < level[1][0] && coord.y >= level[0][1] && coord.y < level[1][1];
return hit;
}

lngX(lng, zoom) {
return (lng + 180) * (Math.pow(2, zoom) / 360);
}

latY(lat, zoom) {
const f = clamp(Math.sin(Math.PI / 180 * lat), -0.9999, 0.9999);
const scale = Math.pow(2, zoom) / (2 * Math.PI);
return Math.pow(2, zoom - 1) + 0.5 * Math.log((1 + f) / (1 - f)) * -scale;
}
}

module.exports = TileBounds;
15 changes: 15 additions & 0 deletions src/source/vector_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Evented = require('../util/evented');
const util = require('../util/util');
const loadTileJSON = require('./load_tilejson');
const normalizeURL = require('../util/mapbox').normalizeTileURL;
const TileBounds = require('./tile_bounds');

class VectorTileSource extends Evented {

Expand Down Expand Up @@ -39,6 +40,8 @@ class VectorTileSource extends Evented {
return;
}
util.extend(this, tileJSON);
this.setBounds(tileJSON.bounds);

// `content` is included here to prevent a race condition where `Style#_updateSources` is called
// before the TileJSON arrives. this makes sure the tiles needed are loaded once TileJSON arrives
// ref: https://github.com/mapbox/mapbox-gl-js/pull/4347#discussion_r104418088
Expand All @@ -48,6 +51,18 @@ class VectorTileSource extends Evented {
});
}

setBounds(bounds) {
this.bounds = bounds;
if (bounds) {
this.tileBounds = new TileBounds(bounds, this.minzoom, this.maxzoom);
}
}

hasTile(coord) {
console.log('has tile');
return !this.tileBounds || this.tileBounds.contains(coord);
}

onAdd(map) {
this.load();
this.map = map;
Expand Down
13 changes: 13 additions & 0 deletions test/unit/source/vector_tile_source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,18 @@ test('VectorTileSource', (t) => {
});
});

t.test('respects TileJSON.bounds', (t)=>{
const source = createSource({
minzoom: 0,
maxzoom: 22,
attribution: "Mapbox",
tiles: ["http://example.com/{z}/{x}/{y}.png"],
});
source.setBounds([[-47, -7], [-45, -5]]);
t.false(source.hasTile({z: 8, x:96, y: 132}), 'returns false for tiles outside bounds');
t.true(source.hasTile({z: 8, x:95, y: 132}), 'returns true for tiles inside bounds');
t.end();
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also include tests:

  • for the case where bounds is included directly on the TileJSON (i.e. where the source is created with {url: ...}
  • equivalent ones for raster source
  • in source_cache.test.js, confirming that SourceCache uses hasTile() to filter which tiles it loads.

t.end();
});