Skip to content

Commit

Permalink
refactor(Layer): Layer constructor parameter needs Source.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchoqueux committed Feb 5, 2021
1 parent bb581fb commit 11b8645
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 19 deletions.
4 changes: 2 additions & 2 deletions examples/js/plugins/FeatureToolTip.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*
* // Add layers
* var wfsSource = new itowns.WFSSource(...);
* var wfsLayer = new itowns.ColorLayer(wfsSource...);
* var wfsLayer = new itowns.ColorLayer('id_wfs', { source: wfsSource });
* view.addLayer(wfsLayer);
*
* var fileSource = new itowns.FileSource(...);
* var fileLayer = new itowns.GeometryLayer(fileSource...);
* var fileLayer = new itowns.GeometryLayer('id_myFile', { source: fileSource});
* view.addLayer(fileLayer);
*
* FeatureToolTip.addLayer(wfsLayer);
Expand Down
9 changes: 5 additions & 4 deletions examples/source_stream_wfs_25d.html
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,11 @@
},
});

var wfsCartoLayer = new itowns.LabelLayer('wfsCarto', 'EPSG:3946');
wfsCartoLayer.crs = 'EPSG:3946';
wfsCartoLayer.source = wfsCartoSource;
wfsCartoLayer.style = wfsCartoStyle;
var wfsCartoLayer = new itowns.LabelLayer('wfsCarto', {
crs: 'EPSG:3946',
source: wfsCartoSource,
style: wfsCartoStyle,
});

// TODO: enable this again when the stream became available
// Search for "zone_habitat" in this page https://wxs.ign.fr/3ht7xcw6f7nciopo16etuqp2/geoportail/wfs?SERVICE=WFS&REQUEST=GetCapabilities
Expand Down
1 change: 1 addition & 0 deletions src/Core/Prefab/Globe/Atmosphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const mfogDistance = ellipsoidSizes.x * 160.0;

class Atmosphere extends GeometryLayer {
constructor(id = 'atmosphere', options = {}) {
options.source = false;
super(id, new THREE.Object3D(), options);
this.isAtmosphere = true;

Expand Down
11 changes: 9 additions & 2 deletions src/Layer/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ class Layer extends THREE.EventDispatcher {
* @param {string} id - The id of the layer, that should be unique. It is
* not mandatory, but an error will be emitted if this layer is added a
* {@link View} that already has a layer going by that id.
* @param {Object} [config] - Optional configuration, all elements in it
* @param {Object} config - configuration, all elements in it
* will be merged as is in the layer. For example, if the configuration
* contains three elements `name, protocol, extent`, these elements will be
* contains three elements `name, extent`, these elements will be
* available using `layer.name` or something else depending on the property
* name.
* @param {Source|boolean} config.source - instantiated Source specifies data source to display.
* if config.source is a boolean, it can only be false. if config.source is false,
* the layer doesn't need Source (like debug Layer or procedural layer).
* @param {number} [config.cacheLifeTime=Infinity] - set life time value in cache.
* This value is used for [Cache]{@link Cache} expiration mechanism.
*
Expand Down Expand Up @@ -80,6 +83,10 @@ class Layer extends THREE.EventDispatcher {
console.warn('Layer projection parameter is deprecated, use crs instead.');
config.crs = config.crs || config.projection;
}

if (config.source === undefined || config.source === true) {
throw new Error(`Layer ${id} needs Source`);
}
super();
this.isLayer = true;

Expand Down
1 change: 1 addition & 0 deletions src/Layer/TiledGeometryLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class TiledGeometryLayer extends GeometryLayer {
constructor(id, object3d, schemeTile, builder, config) {
// cacheLifeTime = CACHE_POLICIES.INFINITE because the cache is handled by the builder
config.cacheLifeTime = CACHE_POLICIES.INFINITE;
config.source = false;
super(id, object3d, config);

this.isTiledGeometryLayer = true;
Expand Down
6 changes: 3 additions & 3 deletions test/unit/dataSourceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('Provide in Sources', function () {
};

const planarlayer = new PlanarLayer('globe', globalExtent, new THREE.Group());
const colorlayer = new ColorLayer('color', { crs: 'EPSG:3857' });
const elevationlayer = new ElevationLayer('elevation', { crs: 'EPSG:3857' });
const colorlayer = new ColorLayer('color', { crs: 'EPSG:3857', source: false });
const elevationlayer = new ElevationLayer('elevation', { crs: 'EPSG:3857', source: false });

planarlayer.attach(colorlayer);
planarlayer.attach(elevationlayer);
Expand All @@ -68,7 +68,7 @@ describe('Provide in Sources', function () {
const nodeLayer = material.getLayer(colorlayer.id);
const nodeLayerElevation = material.getLayer(elevationlayer.id);

const featureLayer = new GeometryLayer('geom', new THREE.Group());
const featureLayer = new GeometryLayer('geom', new THREE.Group(), { source: false });
featureLayer.update = FeatureProcessing.update;
featureLayer.crs = 'EPSG:4978';
featureLayer.mergeFeatures = false;
Expand Down
4 changes: 2 additions & 2 deletions test/unit/geometrylayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import GeometryLayer from 'Layer/GeometryLayer';
import ColorLayer from 'Layer/ColorLayer';

describe('GeometryLayer', function () {
const geometry = new GeometryLayer('geometry', new THREE.Group());
const color = new ColorLayer('color');
const geometry = new GeometryLayer('geometry', new THREE.Group(), { source: false });
const color = new ColorLayer('color', { source: false });

it('should attached a color layer', function () {
geometry.attach(color);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('LabelLayer', function () {
let extent;

before('init LabelLayer and a FeatureCollection like', function () {
layer = new LabelLayer('labels');
layer = new LabelLayer('labels', { source: false });
layer.source = {};
layer.style = new Style();
layer.style.zoom = {
Expand Down
14 changes: 9 additions & 5 deletions test/unit/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ColorLayer from 'Layer/ColorLayer';

describe('Layer', function () {
it('should emit an event on property changed', function () {
const layer = new Layer('testId');
const layer = new Layer('testId', { source: false });
layer.defineLayerProperty('test', 0);
layer.addEventListener('test-property-changed', (e) => {
assert.equal(e.type, 'test-property-changed');
Expand All @@ -17,10 +17,10 @@ describe('Layer', function () {

describe('ImageryLayers', function () {
const layers = [
new ColorLayer('l0'),
new ColorLayer('l1'),
new ColorLayer('l2'),
new ColorLayer('l3'),
new ColorLayer('l0', { source: false }),
new ColorLayer('l1', { source: false }),
new ColorLayer('l2', { source: false }),
new ColorLayer('l3', { source: false }),
];

layers[0].sequence = 0;
Expand Down Expand Up @@ -62,4 +62,8 @@ describe('ImageryLayers', function () {
assert.equal(res[2], 'l2');
assert.equal(res[3], 'l0');
});

it('throws error when instance layer without Source', function () {
assert.throws(() => new ColorLayer('id'), /^Error: Layer id needs Source$/);
});
});
1 change: 1 addition & 0 deletions utils/debug/3dTilesDebug.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default function create3dTilesDebugUI(datDebugTool, view, _3dTileslayer)
update: debugIdUpdate,
visible: false,
cacheLifeTime: Infinity,
source: false,
});

View.prototype.addLayer.call(view, obbLayer, _3dTileslayer).then((l) => {
Expand Down
2 changes: 2 additions & 0 deletions utils/debug/TileDebug.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export default function createTileDebugUI(datDebugTool, view, layer, debugInstan
update: debugIdUpdate,
visible: false,
cacheLifeTime: Infinity,
source: false,
});

View.prototype.addLayer.call(view, obbLayer, layer).then((l) => {
Expand All @@ -201,6 +202,7 @@ export default function createTileDebugUI(datDebugTool, view, layer, debugInstan
update: debugIdUpdate,
visible: false,
cacheLifeTime: Infinity,
source: false,
});

View.prototype.addLayer.call(view, sbLayer, layer).then((l) => {
Expand Down

0 comments on commit 11b8645

Please sign in to comment.