Simplify vector tile code

This commit is contained in:
ahocevar
2018-12-31 00:34:56 +01:00
parent ab797b7160
commit 32696638d2
10 changed files with 396 additions and 462 deletions

View File

@@ -62,9 +62,9 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
class TileClass extends VectorTile {
constructor() {
super(...arguments);
this.setState(TileState.LOADED);
this.setFeatures([feature1, feature2, feature3]);
this.setProjection(getProjection('EPSG:4326'));
this.setState(TileState.LOADED);
tileCallback(this);
}
}
@@ -73,12 +73,14 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
tileClass: TileClass,
tileGrid: createXYZ()
});
source.getSourceTiles = function() {
return [new TileClass([0, 0, 0])];
};
source.getTile = function() {
const tile = VectorTileSource.prototype.getTile.apply(source, arguments);
tile.hasContext = function() {
return true;
};
tile.sourceTilesLoaded = true;
tile.setState(TileState.LOADED);
return tile;
};
@@ -219,13 +221,10 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
});
map.addLayer(layer2);
const spy1 = sinon.spy(VectorTile.prototype, 'getExecutorGroup');
const spy2 = sinon.spy(VectorTile.prototype, 'setExecutorGroup');
map.renderSync();
expect(spy1.callCount).to.be(4);
expect(spy2.callCount).to.be(2);
spy1.restore();
spy2.restore();
const tile = source.getTile(0, 0, 0, 1, getProjection('EPSG:3857'));
expect(Object.keys(tile.executorGroups)[0]).to.be(getUid(layer));
expect(Object.keys(tile.executorGroups)[1]).to.be(getUid(layer2));
});
});
@@ -244,14 +243,13 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
sourceTile.getImage = function() {
return document.createElement('canvas');
};
const tile = new VectorImageTile([0, 0, 0], 1, [0, 0, 0], createXYZ(), {'0,0,0': sourceTile});
const tile = new VectorImageTile([0, 0, 0], 1, [0, 0, 0], createXYZ(),
function() {
return sourceTile;
},
function() {});
tile.transition_ = 0;
tile.tileKeys = ['0,0,0'];
tile.extent = getProjection('EPSG:3857').getExtent();
tile.setState(TileState.LOADED);
tile.getSourceTile = function() {
return sourceTile;
};
layer.getSource().getTile = function() {
return tile;
};
@@ -286,33 +284,37 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
});
describe('#forEachFeatureAtCoordinate', function() {
let layer, renderer, executorGroup;
let layer, renderer, executorGroup, source;
class TileClass extends VectorImageTile {
constructor() {
super(...arguments);
this.extent = [-Infinity, -Infinity, Infinity, Infinity];
this.setState(TileState.LOADED);
const sourceTile = new VectorTile([0, 0, 0]);
sourceTile.setState(TileState.LOADED);
sourceTile.setProjection(getProjection('EPSG:3857'));
sourceTile.getExecutorGroup = function() {
return executorGroup;
};
const key = sourceTile.tileCoord.toString();
this.tileKeys = [key];
this.sourceTiles_ = {};
this.sourceTiles_[key] = sourceTile;
this.wrappedTileCoord = arguments[0];
}
}
beforeEach(function() {
const sourceTile = new VectorTile([0, 0, 0]);
sourceTile.setState(TileState.LOADED);
sourceTile.setProjection(getProjection('EPSG:3857'));
source = new VectorTileSource({
tileClass: TileClass,
tileGrid: createXYZ()
});
source.sourceTiles_ = {
'0/0/0': sourceTile
};
source.sourceTilesByTileKey_ = {
'0/0/0': [sourceTile]
};
executorGroup = {};
source.getTile = function() {
const tile = VectorTileSource.prototype.getTile.apply(source, arguments);
tile.executorGroups[getUid(layer)] = [executorGroup];
return tile;
};
layer = new VectorTileLayer({
source: new VectorTileSource({
tileClass: TileClass,
tileGrid: createXYZ()
})
source: source
});
renderer = new CanvasVectorTileLayerRenderer(layer);
executorGroup.forEachFeatureAtCoordinate = function(coordinate,
@@ -335,7 +337,7 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
rotation: 0
}
};
renderer.renderedTiles = [new TileClass([0, 0, -1], undefined, 1)];
renderer.renderedTiles = [source.getTile(0, 0, 0, 1, getProjection('EPSG:3857'))];
renderer.forEachFeatureAtCoordinate(
coordinate, frameState, 0, spy, undefined);
expect(spy.callCount).to.be(1);

View File

@@ -2,12 +2,15 @@ import Map from '../../../../src/ol/Map.js';
import View from '../../../../src/ol/View.js';
import VectorImageTile from '../../../../src/ol/VectorImageTile.js';
import VectorTile from '../../../../src/ol/VectorTile.js';
import GeoJSON from '../../../../src/ol/format/GeoJSON.js';
import MVT from '../../../../src/ol/format/MVT.js';
import VectorTileLayer from '../../../../src/ol/layer/VectorTile.js';
import {get as getProjection} from '../../../../src/ol/proj.js';
import VectorTileSource from '../../../../src/ol/source/VectorTile.js';
import {createXYZ} from '../../../../src/ol/tilegrid.js';
import TileGrid from '../../../../src/ol/tilegrid/TileGrid.js';
import {listen, unlistenByKey} from '../../../../src/ol/events.js';
import TileState from '../../../../src/ol/TileState.js';
describe('ol.source.VectorTile', function() {
@@ -47,6 +50,24 @@ describe('ol.source.VectorTile', function() {
expect(source.getTile(0, 0, 0, 1, getProjection('EPSG:3857')))
.to.equal(tile);
});
it('loads source tiles', function(done) {
const source = new VectorTileSource({
format: new GeoJSON(),
url: 'spec/ol/data/point.json'
});
const tile = source.getTile(0, 0, 0, 1, source.getProjection());
tile.load();
const key = listen(tile, 'change', function(e) {
if (tile.getState() === TileState.LOADED) {
const sourceTile = tile.load()[0];
expect(sourceTile.getFeatures().length).to.be.greaterThan(0);
unlistenByKey(key);
done();
}
});
});
});
describe('#getTileGridForProjection', function() {

View File

@@ -1,33 +1,17 @@
import TileState from '../../../src/ol/TileState.js';
import {defaultLoadFunction} from '../../../src/ol/VectorImageTile.js';
import {defaultLoadFunction} from '../../../src/ol/source/VectorTile.js';
import VectorTileSource from '../../../src/ol/source/VectorTile.js';
import {listen, listenOnce} from '../../../src/ol/events.js';
import {listen, listenOnce, unlistenByKey} from '../../../src/ol/events.js';
import GeoJSON from '../../../src/ol/format/GeoJSON.js';
import {createXYZ} from '../../../src/ol/tilegrid.js';
import TileGrid from '../../../src/ol/tilegrid/TileGrid.js';
import {getKey} from '../../../src/ol/tilecoord.js';
import EventType from '../../../src/ol/events/EventType.js';
describe('ol.VectorImageTile', function() {
it('configures loader that sets features on the source tile', function(done) {
const source = new VectorTileSource({
format: new GeoJSON(),
url: 'spec/ol/data/point.json'
});
const tile = source.getTile(0, 0, 0, 1, source.getProjection());
tile.load();
const sourceTile = tile.getTile(tile.tileKeys[0]);
const loader = sourceTile.loader_;
expect(typeof loader).to.be('function');
listen(sourceTile, 'change', function(e) {
expect(sourceTile.getFeatures().length).to.be.greaterThan(0);
done();
});
});
it('sets sourceTilesLoaded when previously failed source tiles are loaded', function(done) {
it('triggers "change" when previously failed source tiles are loaded', function(done) {
let sourceTile;
const source = new VectorTileSource({
format: new GeoJSON(),
@@ -44,16 +28,12 @@ describe('ol.VectorImageTile', function() {
listen(tile, 'change', function(e) {
++calls;
if (calls === 1) {
expect(tile.sourceTilesLoaded).to.be(false);
} else if (calls === 2) {
expect(tile.sourceTilesLoaded).to.be(true);
}
if (calls == 2) {
done();
} else {
expect(tile.getState()).to.be(TileState.ERROR);
setTimeout(function() {
sourceTile.setState(TileState.LOADED);
}, 0);
} else if (calls === 2) {
done();
}
});
});
@@ -73,7 +53,7 @@ describe('ol.VectorImageTile', function() {
});
});
it('sets EMPTY state when tile has only empty source tiles', function(done) {
it('sets EMPTY state when tile has only empty source tiles', function() {
const source = new VectorTileSource({
format: new GeoJSON(),
url: ''
@@ -81,14 +61,10 @@ describe('ol.VectorImageTile', function() {
const tile = source.getTile(0, 0, 0, 1, source.getProjection());
tile.load();
listen(tile, 'change', function() {
expect(tile.getState()).to.be(TileState.EMPTY);
done();
});
expect(tile.getState()).to.be(TileState.EMPTY);
});
it('only loads tiles within the source tileGrid\'s extent', function() {
it('only loads tiles within the source tileGrid\'s extent', function(done) {
const url = 'spec/ol/data/point.json';
const source = new VectorTileSource({
projection: 'EPSG:4326',
@@ -106,8 +82,15 @@ describe('ol.VectorImageTile', function() {
const tile = source.getTile(0, 0, 0, 1, source.getProjection());
tile.load();
expect(tile.tileKeys.length).to.be(1);
expect(tile.getTile(tile.tileKeys[0]).tileCoord).to.eql([0, 16, 9]);
const key = listen(tile, EventType.CHANGE, function() {
if (tile.getState() === TileState.LOADED) {
unlistenByKey(key);
const sourceTiles = tile.load();
expect(sourceTiles.length).to.be(1);
expect(sourceTiles[0].tileCoord).to.eql([0, 16, 9]);
done();
}
});
});
it('#dispose() while loading', function() {
@@ -122,13 +105,9 @@ describe('ol.VectorImageTile', function() {
const tile = source.getTile(0, 0, 0, 1, source.getProjection());
tile.load();
expect(tile.loadListenerKeys_.length).to.be(4);
expect(tile.tileKeys.length).to.be(4);
expect(tile.getState()).to.be(TileState.LOADING);
tile.dispose();
expect(tile.loadListenerKeys_.length).to.be(0);
expect(tile.tileKeys.length).to.be(0);
expect(tile.sourceTiles_).to.be(null);
expect(source.sourceTilesByTileKey_[getKey(tile)]).to.be(undefined);
expect(tile.getState()).to.be(TileState.ABORT);
});
@@ -145,14 +124,19 @@ describe('ol.VectorImageTile', function() {
tile.load();
listenOnce(tile, 'change', function() {
expect(tile.getState()).to.be(TileState.LOADING);
expect(tile.sourceTilesLoaded).to.be.ok();
expect(tile.loadListenerKeys_.length).to.be(0);
expect(tile.tileKeys.length).to.be(4);
expect(tile.getState()).to.be(TileState.LOADED);
expect(tile.loadingSourceTiles).to.be(0);
const sourceTiles = tile.load();
expect(sourceTiles.length).to.be(4);
for (let i = 0, ii = sourceTiles.length; i < ii; ++i) {
expect(sourceTiles[i].consumers).to.be(1);
}
tile.dispose();
expect(tile.tileKeys.length).to.be(0);
expect(tile.sourceTiles_).to.be(null);
expect(tile.getState()).to.be(TileState.ABORT);
for (let i = 0, ii = sourceTiles.length; i < ii; ++i) {
expect(sourceTiles[i].consumers).to.be(0);
expect(sourceTiles[i].getState()).to.be(TileState.ABORT);
}
done();
});
});

View File

@@ -1,5 +1,5 @@
import Feature from '../../../src/ol/Feature.js';
import {defaultLoadFunction} from '../../../src/ol/VectorImageTile.js';
import {defaultLoadFunction} from '../../../src/ol/source/VectorTile.js';
import VectorTile from '../../../src/ol/VectorTile.js';
import {listen} from '../../../src/ol/events.js';
import TextFeature from '../../../src/ol/format/TextFeature.js';