Skip to content

Commit 0fb9d7f

Browse files
authored
Merge pull request #434 from chrismayer/fix-wms-legend-sld-version
Ensure "SLD_VERSION" is set in "GetLegendGraphic" for WMS 1.3.0
2 parents 4dc854f + 337cc87 commit 0fb9d7f

4 files changed

Lines changed: 67 additions & 4 deletions

File tree

src/util/LayerLegend.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import TileWmsSource from 'ol/source/TileWMS';
66
import ImageWmsSource from 'ol/source/ImageWMS';
7+
import ObjectUtil from './Object';
78

89
const CustomLegend = {
910
/**
@@ -45,6 +46,13 @@ const WMSSourceLegend = {
4546
* @returns {String} The legend URL.
4647
*/
4748
getUrl (source, resolution, options) {
49+
const wmsParams = source.getParams();
50+
const wmsVersion = ObjectUtil.getValueIgnoreCase(wmsParams, 'VERSION') || '1.3.0';
51+
// apply mandatory SLD_VERSION param for WMS 1.3.0 GetLegendGraphic request
52+
if (wmsVersion === '1.3.0' && !ObjectUtil.getValueIgnoreCase(options, 'SLD_VERSION')) {
53+
options.SLD_VERSION = '1.1.0';
54+
}
55+
4856
return source.getLegendUrl(resolution, options);
4957
}
5058
}

src/util/Object.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,34 @@ const ObjectUtil = {
5555
result.push(path);
5656
}
5757
return result;
58+
},
59+
60+
/**
61+
* Returns the value from an object using a case-insensitive key.
62+
*
63+
* @param {Object} obj The object to search in
64+
* @param {String} key The key to retrieve value for
65+
* @returns {*} The value for the given key or undefined, if not found
66+
*/
67+
getValueIgnoreCase (obj, key) {
68+
if (typeof obj !== 'object' || obj === null) {
69+
console.warn('getValueIgnoreCase: First argument must be a valid object');
70+
return;
71+
}
72+
if (typeof key !== 'string') {
73+
console.warn('getValueIgnoreCase: Second argument must be a string');
74+
return;
75+
}
76+
77+
const lowerCaseKey = key.toLowerCase();
78+
79+
for (const objKey in obj) {
80+
if (Object.prototype.hasOwnProperty.call(obj, objKey) && objKey.toLowerCase() === lowerCaseKey) {
81+
return obj[objKey];
82+
}
83+
}
84+
85+
return undefined; // key not found
5886
}
5987
}
6088

tests/unit/specs/components/layerlist/LayerLegendImage.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,27 +144,27 @@ describe('layerlist/LayerLegendImage.vue', () => {
144144
await comp.setProps({ layer: wmsLayer });
145145
expect(vm.legendURL).to.equal('https://ahocevar.com/geoserver/wms?' +
146146
'SERVICE=WMS&VERSION=1.3.0&REQUEST=GetLegendGraphic&FORMAT=image%2Fpng&LAYER=topp%3Astates&' +
147-
'SCALE=139770566.00717944&language=en');
147+
'SCALE=139770566.00717944&language=en&SLD_VERSION=1.1.0');
148148
});
149149

150150
it('legendURL supports localization and scale', async () => {
151151
await comp.setProps({ layer: wmsLayer });
152152

153153
expect(vm.legendURL).to.equal('https://ahocevar.com/geoserver/wms?' +
154154
'SERVICE=WMS&VERSION=1.3.0&REQUEST=GetLegendGraphic&FORMAT=image%2Fpng&LAYER=topp%3Astates&' +
155-
'SCALE=139770566.00717944&language=en');
155+
'SCALE=139770566.00717944&language=en&SLD_VERSION=1.1.0');
156156

157157
vm.$i18n.locale = 'de';
158158

159159
expect(vm.legendURL).to.equal('https://ahocevar.com/geoserver/wms?' +
160160
'SERVICE=WMS&VERSION=1.3.0&REQUEST=GetLegendGraphic&FORMAT=image%2Fpng&LAYER=topp%3Astates&' +
161-
'SCALE=139770566.00717944&language=de');
161+
'SCALE=139770566.00717944&language=de&SLD_VERSION=1.1.0');
162162

163163
view.setResolution(1000.0);
164164

165165
expect(vm.legendURL).to.equal('https://ahocevar.com/geoserver/wms?' +
166166
'SERVICE=WMS&VERSION=1.3.0&REQUEST=GetLegendGraphic&FORMAT=image%2Fpng&LAYER=topp%3Astates&' +
167-
'SCALE=3571428.571428572&language=de');
167+
'SCALE=3571428.571428572&language=de&SLD_VERSION=1.1.0');
168168
});
169169

170170
afterEach(() => {

tests/unit/specs/util/Object.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,31 @@ describe('ObjectUtil', () => {
7575
'.array[1].prop'
7676
]);
7777
});
78+
79+
it('getValueIgnoreCase returns correct results', () => {
80+
const object = {
81+
foo: 1,
82+
BAR: 2,
83+
KaLLe: 3
84+
};
85+
86+
let result1 = ObjectUtil.getValueIgnoreCase(object, 'foo');
87+
expect(result1).to.equal(1);
88+
result1 = ObjectUtil.getValueIgnoreCase(object, 'FOO');
89+
expect(result1).to.equal(1);
90+
result1 = ObjectUtil.getValueIgnoreCase(object, 'fOO');
91+
expect(result1).to.equal(1);
92+
let result2 = ObjectUtil.getValueIgnoreCase(object, 'bar');
93+
expect(result2).to.equal(2);
94+
result2 = ObjectUtil.getValueIgnoreCase(object, 'BAR');
95+
expect(result2).to.equal(2);
96+
result2 = ObjectUtil.getValueIgnoreCase(object, 'bAr');
97+
expect(result2).to.equal(2);
98+
let result3 = ObjectUtil.getValueIgnoreCase(object, 'kalle');
99+
expect(result3).to.equal(3);
100+
result3 = ObjectUtil.getValueIgnoreCase(object, 'KALLE');
101+
expect(result3).to.equal(3);
102+
result3 = ObjectUtil.getValueIgnoreCase(object, 'kAlle');
103+
expect(result3).to.equal(3);
104+
});
78105
});

0 commit comments

Comments
 (0)