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

feat: enable automatic style creation for wms layers' getLegendGraphic #1578

Merged
merged 3 commits into from
Aug 17, 2022
Merged
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
31 changes: 28 additions & 3 deletions src/layer/wms.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ function createImageSource(options) {
}));
}

function createDefaultStyle(wmsOptions, source, viewer) {
const maxResolution = viewer.getResolutions()[viewer.getResolutions().length - 1];
const styleName = `${wmsOptions.name}_WMSDefault`;
const getLegendString = source.getLegendUrl(maxResolution, wmsOptions.legendParams);
const style = [[{
icon: {
src: `${getLegendString}`
},
extendedLegend: wmsOptions.hasThemeLegend || false
}]];
viewer.addStyle(styleName, style);
return styleName;
}

const wms = function wms(layerOptions, viewer) {
const wmsDefault = {
featureinfoLayer: null
Expand All @@ -54,7 +68,6 @@ const wms = function wms(layerOptions, viewer) {
sourceOptions.projection = viewer.getProjection();
sourceOptions.id = wmsOptions.id;
sourceOptions.format = wmsOptions.format ? wmsOptions.format : sourceOptions.format;

const styleSettings = viewer.getStyle(wmsOptions.styleName);
const wmsStyleObject = styleSettings ? styleSettings[0].find(s => s.wmsStyle) : undefined;
sourceOptions.style = wmsStyleObject ? wmsStyleObject.wmsStyle : '';
Expand All @@ -73,9 +86,21 @@ const wms = function wms(layerOptions, viewer) {
}

if (renderMode === 'image') {
return image(wmsOptions, createImageSource(sourceOptions));
const source = createImageSource(sourceOptions);
if (wmsOptions.styleName === 'default') {
wmsOptions.styleName = createDefaultStyle(wmsOptions, source, viewer);
wmsOptions.style = wmsOptions.styleName;
}
return image(wmsOptions, source);
}

const source = createTileSource(sourceOptions);

if (wmsOptions.styleName === 'default') {
wmsOptions.styleName = createDefaultStyle(wmsOptions, source, viewer);
wmsOptions.style = wmsOptions.styleName;
}
return tile(wmsOptions, createTileSource(sourceOptions));
return tile(wmsOptions, source);
};

export default wms;