Simplify vector tile projection handling

This commit is contained in:
ahocevar
2019-03-10 09:34:40 +01:00
parent 7002053678
commit b2722542fe
10 changed files with 148 additions and 181 deletions

View File

@@ -1,5 +1,4 @@
import Feature from '../../../../src/ol/Feature.js';
import {getWidth} from '../../../../src/ol/extent.js';
import MVT from '../../../../src/ol/format/MVT.js';
import Point from '../../../../src/ol/geom/Point.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
@@ -22,15 +21,20 @@ where('ArrayBuffer.isView').describe('ol.format.MVT', function() {
describe('#readFeatures', function() {
const options = {
featureProjection: 'EPSG:3857',
extent: [1824704.739223726, 6141868.096770482, 1827150.7241288517, 6144314.081675608]
};
it('uses ol.render.Feature as feature class by default', function() {
const format = new MVT({layers: ['water']});
const features = format.readFeatures(data);
const features = format.readFeatures(data, options);
expect(features[0]).to.be.a(RenderFeature);
});
it('parses only specified layers', function() {
const format = new MVT({layers: ['water']});
const features = format.readFeatures(data);
const features = format.readFeatures(data, options);
expect(features.length).to.be(10);
});
@@ -64,29 +68,27 @@ where('ArrayBuffer.isView').describe('ol.format.MVT', function() {
featureClass: Feature,
layers: ['building']
});
let features = format.readFeatures(data);
let features = format.readFeatures(data, options);
expect(features[0].getId()).to.be(2);
// ol.render.Feature
format = new MVT({
layers: ['building']
});
features = format.readFeatures(data);
features = format.readFeatures(data, options);
expect(features[0].getId()).to.be(2);
});
it('sets the extent of the last readFeatures call', function() {
const format = new MVT();
format.readFeatures(data);
const extent = format.getLastExtent();
expect(getWidth(extent)).to.be(4096);
});
});
});
describe('ol.format.MVT', function() {
const options = {
featureProjection: 'EPSG:3857',
extent: [1824704.739223726, 6141868.096770482, 1827150.7241288517, 6144314.081675608]
};
describe('#createFeature_', function() {
it('accepts a geometryName', function() {
const format = new MVT({
@@ -176,7 +178,9 @@ describe('ol.format.MVT', function() {
ends.push(10, 20);
createdEnds = ends;
};
const feature = format.createFeature_({}, rawFeature);
format.dataProjection.setExtent([0, 0, 4096, 4096]);
format.dataProjection.setWorldExtent(options.extent);
const feature = format.createFeature_({}, rawFeature, format.adaptOptions(options));
expect(feature).to.be.a(RenderFeature);
expect(feature.getType()).to.be('Polygon');
expect(feature.getFlatCoordinates()).to.equal(createdFlatCoordinates);

View File

@@ -9,8 +9,7 @@ import {getCenter} from '../../../../../src/ol/extent.js';
import MVT from '../../../../../src/ol/format/MVT.js';
import Point from '../../../../../src/ol/geom/Point.js';
import VectorTileLayer from '../../../../../src/ol/layer/VectorTile.js';
import {get as getProjection, fromLonLat} from '../../../../../src/ol/proj.js';
import Projection from '../../../../../src/ol/proj/Projection.js';
import {get as getProjection} from '../../../../../src/ol/proj.js';
import {checkedFonts} from '../../../../../src/ol/render/canvas.js';
import RenderFeature from '../../../../../src/ol/render/Feature.js';
import CanvasVectorTileLayerRenderer from '../../../../../src/ol/renderer/canvas/VectorTileLayer.js';
@@ -64,7 +63,6 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
constructor() {
super(...arguments);
this.setFeatures([feature1, feature2, feature3]);
this.setProjection(getProjection('EPSG:4326'));
this.setState(TileState.LOADED);
tileCallback(this);
}
@@ -188,30 +186,6 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
}, 1600);
});
it('transforms geometries when tile and view projection are different', function() {
let tile;
tileCallback = function(t) {
tile = t;
};
map.renderSync();
expect(tile.getProjection()).to.equal(getProjection('EPSG:3857'));
expect(feature1.getGeometry().getCoordinates()).to.eql(fromLonLat([1, -1]));
});
it('Geometries are transformed from tile-pixels', function() {
const proj = new Projection({code: 'EPSG:3857', units: 'tile-pixels'});
let tile;
tileCallback = function(t) {
t.setProjection(proj);
t.setExtent([0, 0, 4096, 4096]);
tile = t;
};
map.renderSync();
expect(tile.getProjection()).to.equal(getProjection('EPSG:3857'));
expect(feature1.getGeometry().getCoordinates()).to.eql([-20027724.40316874, 20047292.282409746]);
expect(feature3.flatCoordinates_).to.eql([-20027724.40316874, 20047292.282409746]);
});
it('works for multiple layers that use the same source', function() {
const layer2 = new VectorTileLayer({
source: source,
@@ -240,7 +214,6 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
})
});
const sourceTile = new VectorTile([0, 0, 0], 2);
sourceTile.setProjection(getProjection('EPSG:3857'));
sourceTile.features_ = [];
sourceTile.getImage = function() {
return document.createElement('canvas');
@@ -299,7 +272,6 @@ describe('ol.renderer.canvas.VectorTileLayer', function() {
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()

View File

@@ -1,38 +1,43 @@
import Feature from '../../../src/ol/Feature.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';
import GeoJSON from '../../../src/ol/format/GeoJSON.js';
import MVT from '../../../src/ol/format/MVT.js';
import {get as getProjection} from '../../../src/ol/proj.js';
import Projection from '../../../src/ol/proj/Projection.js';
import {createXYZ} from '../../../src/ol/tilegrid.js';
describe('ol.VectorTile', function() {
it('loader sets features on the tile and updates proj units', function(done) {
// mock format that return a tile-pixels feature
const format = new TextFeature();
format.readProjection = function(source) {
return new Projection({
code: '',
units: 'tile-pixels'
});
};
format.readFeatures = function(source, options) {
return [new Feature()];
};
it('loader reprojects GeoJSON features', function(done) {
const format = new GeoJSON();
const tile = new VectorTile([0, 0, 0], null, null, format);
const url = 'spec/ol/data/point.json';
defaultLoadFunction(tile, url);
const loader = tile.loader_;
listen(tile, 'change', function(e) {
expect(tile.getFeatures().length).to.be.greaterThan(0);
expect(tile.getProjection().getUnits()).to.be('tile-pixels');
expect(tile.getFeatures()[0].getGeometry().getFlatCoordinates()).to.eql([-9724792.346778862, 4164041.638405114]);
done();
});
loader.call(tile, [], 1, getProjection('EPSG:3857'));
});
it('loader reprojects MVT features', function(done) {
const format = new MVT();
const tileGrid = createXYZ({
tileSize: 512
});
const tile = new VectorTile([14, 8938, 5680], null, null, format);
const url = 'spec/ol/data/14-8938-5680.vector.pbf';
defaultLoadFunction(tile, url);
const loader = tile.loader_;
listen(tile, 'change', function(e) {
expect(tile.getFeatures()[1246].getGeometry().getFlatCoordinates()).to.eql([1827804.0218549764, 6144812.116688028]);
done();
});
const extent = tileGrid.getTileCoordExtent(tile.tileCoord);
loader.call(tile, extent, 1, getProjection('EPSG:3857'));
});
});