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

Remove redundant vector layer feature initialization #1206

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
52 changes: 28 additions & 24 deletions web/client/components/map/openlayers/Layer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const OpenlayersLayer = React.createClass({
onInvalid: () => {}
};
},
getInitialState: function() {
return {layer: null};
},
componentDidMount() {
this.valid = true;
this.tilestoload = 0;
Expand All @@ -46,25 +49,25 @@ const OpenlayersLayer = React.createClass({
const newOpacity = (newProps.options && newProps.options.opacity !== undefined) ? newProps.options.opacity : 1.0;
this.setLayerOpacity(newOpacity);

if (newProps.position !== this.props.position && this.layer.setZIndex) {
this.layer.setZIndex(newProps.position);
if (newProps.position !== this.props.position && this.state.layer.setZIndex) {
this.state.layer.setZIndex(newProps.position);
}
if (this.props.options) {
this.updateLayer(newProps, this.props);
}
},
componentWillUnmount() {
if (this.layer && this.props.map) {
if (this.layer.detached) {
this.layer.remove();
if (this.state.layer && this.props.map) {
if (this.state.layer.detached) {
this.state.layer.remove();
} else {
this.props.map.removeLayer(this.layer);
this.props.map.removeLayer(this.state.layer);
}
}
},
render() {
if (this.props.children) {
const layer = this.layer;
const layer = this.state.layer;
const children = layer ? React.Children.map(this.props.children, child => {
return child ? React.cloneElement(child, {container: layer, styleName: this.props.options && this.props.options.styleName}) : null;
}) : null;
Expand All @@ -75,18 +78,18 @@ const OpenlayersLayer = React.createClass({
);
}

return Layers.renderLayer(this.props.type, this.props.options, this.props.map, this.props.mapId, this.layer);
return Layers.renderLayer(this.props.type, this.props.options, this.props.map, this.props.mapId, this.state.layer);
},
setLayerVisibility(visibility) {
var oldVisibility = this.props.options && this.props.options.visibility !== false;
if (visibility !== oldVisibility && this.layer && this.isValid()) {
this.layer.setVisible(visibility);
if (visibility !== oldVisibility && this.state.layer && this.isValid(this.state.layer)) {
this.state.layer.setVisible(visibility);
}
},
setLayerOpacity(opacity) {
var oldOpacity = (this.props.options && this.props.options.opacity !== undefined) ? this.props.options.opacity : 1.0;
if (opacity !== oldOpacity && this.layer) {
this.layer.setOpacity(opacity);
if (opacity !== oldOpacity && this.state.layer) {
this.state.layer.setOpacity(opacity);
}
},
generateOpts(options, position, srs) {
Expand All @@ -99,10 +102,11 @@ const OpenlayersLayer = React.createClass({
createLayer(type, options, position) {
if (type) {
const layerOptions = this.generateOpts(options, position, CoordinatesUtils.normalizeSRS(this.props.srs));
this.layer = Layers.createLayer(type, layerOptions, this.props.map, this.props.mapId);
if (this.layer && !this.layer.detached) {
this.addLayer(options);
let layer = Layers.createLayer(type, layerOptions, this.props.map, this.props.mapId);
if (layer && !layer.detached) {
this.addLayer(layer, options);
}
this.setState({layer: layer});
}
},
updateLayer(newProps, oldProps) {
Expand All @@ -116,30 +120,30 @@ const OpenlayersLayer = React.createClass({
}
Layers.updateLayer(
this.props.type,
this.layer,
this.state.layer,
this.generateOpts(newProps.options, newProps.position, newProps.srs),
this.generateOpts(oldProps.options, oldProps.position, oldProps.srs),
this.props.map,
this.props.mapId);
},
addLayer(options) {
if (this.isValid()) {
this.props.map.addLayer(this.layer);
this.layer.getSource().on('tileloadstart', () => {
addLayer(layer, options) {
if (this.isValid(layer)) {
this.props.map.addLayer(layer);
layer.getSource().on('tileloadstart', () => {
if (this.tilestoload === 0) {
this.props.onLayerLoading(options.id);
this.tilestoload++;
} else {
this.tilestoload++;
}
});
this.layer.getSource().on('tileloadend', () => {
layer.getSource().on('tileloadend', () => {
this.tilestoload--;
if (this.tilestoload === 0) {
this.props.onLayerLoad(options.id);
}
});
this.layer.getSource().on('tileloaderror', (event) => {
layer.getSource().on('tileloaderror', (event) => {
this.tilestoload--;
this.props.onLayerError(options.id);
if (this.tilestoload === 0) {
Expand All @@ -148,8 +152,8 @@ const OpenlayersLayer = React.createClass({
});
}
},
isValid() {
const valid = Layers.isValid(this.props.type, this.layer);
isValid(layer) {
const valid = Layers.isValid(this.props.type, layer);
if (this.valid && !valid) {
this.props.onInvalid(this.props.type, this.props.options);
}
Expand Down
30 changes: 15 additions & 15 deletions web/client/components/map/openlayers/__tests__/Layer-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ describe('Openlayers layer', () => {


expect(layer).toExist();
expect(layer.layer).toExist();
expect(layer.state.layer).toExist();

expect(layer.layer.detached).toBe(true);
expect(layer.state.layer.detached).toBe(true);

layer.layer.remove();
layer.state.layer.remove();
});

it('creates a google layer for openlayers map', () => {
Expand Down Expand Up @@ -544,10 +544,10 @@ describe('Openlayers layer', () => {
<OpenlayersLayer type="bing" options={options} map={map}/>, document.getElementById("container"));

expect(layer).toExist();
expect(layer.layer).toExist();
expect(layer.state.layer).toExist();
// count layers
expect(map.getLayers().getLength()).toBe(1);
expect(layer.layer.getVisible()).toBe(true);
expect(layer.state.layer.getVisible()).toBe(true);
layer.setProps({options: assign({}, {
"type": "bing",
"title": "Bing Aerial",
Expand All @@ -557,7 +557,7 @@ describe('Openlayers layer', () => {
"visibility": true
})});
expect(map.getLayers().getLength()).toBe(1);
expect(layer.layer.getVisible()).toBe(true);
expect(layer.state.layer.getVisible()).toBe(true);
layer.setProps({options: assign({}, {
"type": "bing",
"title": "Bing Aerial",
Expand All @@ -567,7 +567,7 @@ describe('Openlayers layer', () => {
"visibility": false
})});
expect(map.getLayers().getLength()).toBe(1);
expect(layer.layer.getVisible()).toBe(false);
expect(layer.state.layer.getVisible()).toBe(false);

});

Expand Down Expand Up @@ -607,10 +607,10 @@ describe('Openlayers layer', () => {
// count layers
expect(map.getLayers().getLength()).toBe(1);

expect(layer.layer.getOpacity()).toBe(1.0);
expect(layer.state.layer.getOpacity()).toBe(1.0);

layer.setProps({options: {opacity: 0.5}, position: 0});
expect(layer.layer.getOpacity()).toBe(0.5);
expect(layer.state.layer.getOpacity()).toBe(0.5);
});

it('respects layer ordering', () => {
Expand All @@ -632,10 +632,10 @@ describe('Openlayers layer', () => {
// count layers
expect(map.getLayers().getLength()).toBe(1);

expect(layer.layer.getZIndex()).toBe(10);
expect(layer.state.layer.getZIndex()).toBe(10);

layer.setProps({position: 2});
expect(layer.layer.getZIndex()).toBe(2);
expect(layer.state.layer.getZIndex()).toBe(2);
});

it('changes wms params', () => {
Expand All @@ -660,11 +660,11 @@ describe('Openlayers layer', () => {
// count layers
expect(map.getLayers().getLength()).toBe(1);

expect(layer.layer.getSource()).toExist();
expect(layer.layer.getSource().getParams()).toExist();
expect(layer.layer.getSource().getParams().cql_filter).toBe("INCLUDE");
expect(layer.state.layer.getSource()).toExist();
expect(layer.state.layer.getSource().getParams()).toExist();
expect(layer.state.layer.getSource().getParams().cql_filter).toBe("INCLUDE");

layer.setProps({options: {params: {cql_filter: "EXCLUDE"}}});
expect(layer.layer.getSource().getParams().cql_filter).toBe("EXCLUDE");
expect(layer.state.layer.getSource().getParams().cql_filter).toBe("EXCLUDE");
});
});
18 changes: 1 addition & 17 deletions web/client/components/map/openlayers/plugins/VectorLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,7 @@ var styleFunction = function(feature) {

Layers.registerType('vector', {
create: (options) => {
let features;
let featuresCrs = options.featuresCrs || 'EPSG:4326';
let layerCrs = options.crs || 'EPSG:3857';
if (options.features) {
let featureCollection = options.features;
if (Array.isArray(options.features)) {
featureCollection = { "type": "FeatureCollection", features: featureCollection};
}
features = (new ol.format.GeoJSON()).readFeatures(featureCollection);
if (featuresCrs !== layerCrs) {
features.forEach((f) => f.getGeometry().transform(featuresCrs, layerCrs));
}
}

const source = new ol.source.Vector({
features: features
});
const source = new ol.source.Vector();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is needed to allow configuring the layer via JSON instead of using Feature.jsx components

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uhm, there needs to be a way to suppress this though, since when adding a vector layer to the layers state, Feature.jsx objects are created automatically. This leads to those Feature.jsx objects being correctly managed whereas those added directly are unmanaged, meaning they are not removed anymore when changing the layer properties (i.e. changing the features field) without removing and re-adding the layer. I don't know the JSON use-case, do you see a solution?

Copy link
Contributor

Choose a reason for hiding this comment

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

I need to do some testing, I will let you know...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mbarto Did you manage to have a look at this?

Copy link
Member

Choose a reason for hiding this comment

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

It should be fixed by this commit, using forceUpdate.
d33e1f8#diff-b2a987f6d21da3daede77b61016d2380R129

Can you confirm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately I still end up with unmanaged features using latest master.


let style = options.nativeStyle;
if (!style && options.style) {
Expand Down