Skip to content

Commit

Permalink
feat: enable automatic style creation for wms layers' getLegendGraphic (
Browse files Browse the repository at this point in the history
#1578)

* feat: enable automatic style creation for wms layers' getLegendGraphic

* fix: call createImageSource() once

* fix: set style property as well as styleName like a manually given style would
  • Loading branch information
Grammostola authored Aug 17, 2022
1 parent 64a93c6 commit 2e45974
Showing 1 changed file with 28 additions and 3 deletions.
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;

0 comments on commit 2e45974

Please sign in to comment.