Skip to content

Commit

Permalink
Cleanup layer id handling
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro committed Apr 1, 2020
1 parent 9ce1b77 commit 3a4581f
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 42 deletions.
4 changes: 2 additions & 2 deletions actions/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ function removeAllLayers() {
};
}

function replacePlaceholderLayer(source, layer) {
function replacePlaceholderLayer(id, layer) {
return {
type: REPLACE_PLACEHOLDER_LAYER,
source,
id,
layer
}
}
Expand Down
4 changes: 2 additions & 2 deletions actions/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ function finishThemeSetup(dispatch, theme, themes, layerConfigs, insertPos, perm
for(let key of Object.keys(externalLayers)) {
let service = key.slice(0, 3);
let serviceUrl = key.slice(4);
ServiceLayerUtils.findLayers(service, serviceUrl, externalLayers[key], (source, layer) => {
dispatch(replacePlaceholderLayer(source, layer));
ServiceLayerUtils.findLayers(service, serviceUrl, externalLayers[key], (id, layer) => {
dispatch(replacePlaceholderLayer(id, layer));
});
}
}
Expand Down
14 changes: 3 additions & 11 deletions components/ImportLayer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,13 @@ class ImportLayerList extends React.PureComponent {
addServiceLayer = (entry) => {
if(entry.resource) {
let params = LayerUtils.splitLayerUrlParam(entry.resource);
ServiceLayerUtils.findLayers(params.type, params.url, [params], (source, layer) => {
ServiceLayerUtils.findLayers(params.type, params.url, [params], (id, layer) => {
if(layer) {
this.props.addLayer(assign({
id: entry.name + Date.now().toString(),
role: LayerRole.USERLAYER
}, layer, {sublayers: null}));
this.props.addLayer(layer);
}
});
} else if(entry.type === "wms" || entry.type === "wfs") {
this.props.addLayer(assign({
id: entry.name + Date.now().toString(),
role: LayerRole.USERLAYER
}, entry, {sublayers: null}));
this.props.addLayer(assign({}, entry, {sublayers: null}));
}
}
}
Expand Down Expand Up @@ -298,10 +292,8 @@ class ImportLayer extends React.Component {
if(!isEmpty(data.features)) {
let features = data.features.map(feature => ({...feature, crs: feature.crs || "EPSG:4326"}));
this.props.addLayerFeatures({
id: filename + Date.now(),
name: filename,
title: filename.replace(/\.[^/.]+$/, ""),
role: LayerRole.USERLAYER,
zoomToExtent: true
}, features, true);
} else {
Expand Down
2 changes: 0 additions & 2 deletions components/widgets/VectorLayerPicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class VectorLayerPicker extends React.Component {
let name = prompt("Enter layer name");
if(name) {
let layer = {
id: uuid.v4(),
title: name,
role: LayerRole.USERLAYER,
type: 'vector'
};
this.props.addLayer(layer);
Expand Down
7 changes: 2 additions & 5 deletions plugins/API.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ class API extends React.Component {
}
addExternalLayer = (resource) => {
let params = LayerUtils.splitLayerUrlParam(resource);
ServiceLayerUtils.findLayers(params.type, params.url, [params], (source, layer) => {
ServiceLayerUtils.findLayers(params.type, params.url, [params], (id, layer) => {
if(layer) {
this.props.addLayer(assign({
id: layer.name + Date.now().toString(),
role: LayerRole.USERLAYER
}, layer, {sublayers: null}));
this.props.addLayer(layer);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/Identify.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Identify extends React.Component {
for(let i = 0; i < queryLayers.length; ++i) {
if(layer.externalLayers && layer.externalLayers[queryLayers[i]]) {
layers.push(layer.externalLayers[queryLayers[i]]);
} else if(layers.length > 0 && layers[layers.length - 1].refid === layer.refid) {
} else if(layers.length > 0 && layers[layers.length - 1].id === layer.id) {
layers[layers.length - 1].queryLayers.push(queryLayers[i]);
} else {
layers.push(assign({}, layer, {queryLayers: [queryLayers[i]]}));
Expand Down
2 changes: 1 addition & 1 deletion plugins/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MapPlugin extends React.Component {
layers.push(assign({}, layer.externalLayers[sublayers[i]], {
opacity: opacities[i]
}));
} else if(layers.length > 0 && layers[layers.length - 1].refid === layer.refid) {
} else if(layers.length > 0 && layers[layers.length - 1].id === layer.id) {
layers[layers.length - 1].params.LAYERS += "," + sublayers[i];
layers[layers.length - 1].params.OPACITIES += "," + opacities[i];
} else {
Expand Down
11 changes: 5 additions & 6 deletions reducers/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ function layers(state = {flat: [], swipe: undefined}, action) {
case ADD_LAYER: {
let newLayers = (state.flat || []).concat();
let newLayer = assign({}, action.layer, {
refid: uuid.v4(),
id: action.layer.id || (action.layer.name + "__" + newLayers.length),
id: action.layer.id || uuid.v4(),
role: action.layer.role || LayerRole.USERLAYER,
queryable: action.layer.queryable || false,
visibility: action.layer.visibility !== undefined ? action.layer.visibility : true,
Expand Down Expand Up @@ -113,7 +112,7 @@ function layers(state = {flat: [], swipe: undefined}, action) {
if(idx === -1 || action.clear) {
let newLayer = assign({}, action.layer, {
type: 'vector',
refid: uuid.v4(),
id: action.layer.id || uuid.v4(),
uuid: uuid.v4(),
features: action.features,
role: action.layer.role || LayerRole.USERLAYER,
Expand Down Expand Up @@ -191,8 +190,8 @@ function layers(state = {flat: [], swipe: undefined}, action) {
let newLayers = state.flat || [];
if(action.layer) {
newLayers = newLayers.map(layer => {
if(layer.type === 'placeholder' && layer.source === action.source) {
let newLayer = {...action.layer, refid: uuid.v4()};
if(layer.type === 'placeholder' && layer.id === action.id) {
let newLayer = {...action.layer};
LayerUtils.addUUIDs(newLayer);
if(newLayer.type === "wms") {
assign(newLayer, LayerUtils.buildWMSLayerParams(newLayer));
Expand All @@ -203,7 +202,7 @@ function layers(state = {flat: [], swipe: undefined}, action) {
}
});
} else {
newLayers = newLayers.filter(layer => !(layer.type === 'placeholder' && layer.source === action.source));
newLayers = newLayers.filter(layer => !(layer.type === 'placeholder' && layer.id === action.id));
}
UrlParams.updateParams({l: LayerUtils.buildWMSLayerUrlParam(newLayers)});
return assign({}, state, {flat: newLayers});
Expand Down
16 changes: 8 additions & 8 deletions utils/LayerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const LayerUtils = {
if(layerConfig.type === 'separator') {
// No point restoring separators
} else if(layerConfig.type !== 'theme') {
external = external.concat(LayerUtils.createExternalLayerPlaceholder(layerConfig, externalLayers));
external = external.concat(LayerUtils.createExternalLayerPlaceholder(layerConfig, externalLayers, layerConfig.id));
}
}
exploded = [...external, ...exploded];
Expand All @@ -55,7 +55,7 @@ const LayerUtils = {
} else if(layerConfig.type === 'separator') {
reordered = reordered.concat(LayerUtils.createSeparatorLayer(layerConfig.name));
} else {
reordered = reordered.concat(LayerUtils.createExternalLayerPlaceholder(layerConfig, externalLayers));
reordered = reordered.concat(LayerUtils.createExternalLayerPlaceholder(layerConfig, externalLayers, layerConfig.id));
}
}
LayerUtils.insertPermalinkLayers(reordered, permalinkLayers);
Expand All @@ -66,25 +66,24 @@ const LayerUtils = {
type: "separator",
title: title,
role: LayerRole.USERLAYER,
refid: uuid.v4(),
uuid: uuid.v4(),
id: uuid.v4()
}]);
},
createExternalLayerPlaceholder: function(layerConfig, externalLayers) {
createExternalLayerPlaceholder: function(layerConfig, externalLayers, id) {
let key = layerConfig.type + ":" + layerConfig.url;
(externalLayers[key] = externalLayers[key] || []).push({
id: id,
name: layerConfig.name,
opacity: layerConfig.opacity,
visibility: layerConfig.visibility
});
return LayerUtils.explodeLayers([{
id: id,
type: "placeholder",
title: layerConfig.name,
role: LayerRole.USERLAYER,
loading: true,
source: layerConfig.type + ':' + layerConfig.url + '#' + layerConfig.name,
refid: uuid.v4(),
uuid: uuid.v4()
}]);
},
Expand Down Expand Up @@ -195,6 +194,7 @@ const LayerUtils = {
},
splitLayerUrlParam(entry) {
const nameOpacityPattern = /([^\[]+)\[(\d+)]/;
let id = uuid.v4();
let type = 'theme';
let url = null;
let opacity = 255;
Expand All @@ -218,7 +218,7 @@ const LayerUtils = {
type = 'separator';
name = name.slice(4);
}
return {type, url, name, opacity, visibility};
return {id, type, url, name, opacity, visibility};
},
pathEqualOrBelow(parent, child) {
return isEqual(child.slice(0, parent.length), parent);
Expand Down Expand Up @@ -392,7 +392,7 @@ const LayerUtils = {
// Attempt to merge with previous if possible
let target = newlayers.length > 0 ? newlayers[newlayers.length - 1] : null;
let source = layer;
if(target && target.sublayers && target.refid === layer.refid) {
if(target && target.sublayers && target.id === layer.id) {
let innertarget = target.sublayers[target.sublayers.length - 1];
let innersource = source.sublayers[0]; // Exploded entries have only one entry per sublayer level
while(innertarget && innertarget.sublayers && innertarget.name === innersource.name) {
Expand Down
6 changes: 3 additions & 3 deletions utils/ServiceLayerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,16 @@ const ServiceLayerUtils = {
let source = type + ':' + serviceUrl + '#' + layerConfig.name;
if(layer) {
layer = assign({}, layer, {
id: layerConfig.id,
opacity: layerConfig.opacity,
visibility: layerConfig.visibility,
id: layerConfig.name + Date.now().toString(),
role: LayerRole.USERLAYER,
sublayers: null
});
callback(source, layer);
callback(layerConfig.id, layer);
} else {
console.warn("Could not find layer " + layerConfig.name);
callback(source, null);
callback(layerConfig.id, null);
}
}
}).catch(err => {
Expand Down
1 change: 0 additions & 1 deletion utils/ThemeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ const ThemeUtils = {
},
createThemeLayer: function(theme, themes, role=LayerRole.THEME) {
let layer = {
id: theme.name + Date.now().toString(),
type: "wms",
url: theme.url,
version: theme.version,
Expand Down

0 comments on commit 3a4581f

Please sign in to comment.