Skip to content

Commit 1e8f8ba

Browse files
committed
Fix: Display legend for external WMS layers
External WMS layers were showing empty legend icons because QGIS Server returns empty icon fields in JSON GetLegendGraphic responses. - Added getLegendGraphicPNG() method to WMS class - Modified updateLayerTreeLayersSymbology() to detect external WMS layers - External WMS layers now fetch PNG legend and convert to base64 - Normal layers continue using JSON format (no breaking changes)
1 parent 86bec30 commit 1e8f8ba

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

assets/src/modules/WMS.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,29 @@ export default class WMS {
8686
body: params,
8787
});
8888
}
89+
90+
/**
91+
* Get legend graphic as PNG image URL from WMS
92+
* @param {object} options - optional parameters which can override this._defaultGetLegendGraphicsParameters
93+
* @returns {string} PNG image URL
94+
* @memberof WMS
95+
*/
96+
getLegendGraphicPNG(options) {
97+
const layers = options['LAYERS'] ?? options['LAYER'];
98+
// Check if layer is specified
99+
if (!layers) {
100+
throw new RequestError(
101+
'LAYERS or LAYER parameter is required for getLegendGraphic request',
102+
options,
103+
);
104+
}
105+
106+
const params = new URLSearchParams({
107+
...this._defaultGetLegendGraphicParameters,
108+
...options,
109+
FORMAT: 'image/png' // Force PNG format for external WMS layers
110+
});
111+
112+
return `${globalThis['lizUrls'].wms}?${params}`;
113+
}
89114
}

assets/src/modules/action/Symbology.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,54 @@ export async function updateLayerTreeLayersSymbology(treeLayers, method=HttpRequ
3232

3333
if (method.toUpperCase() == HttpRequestMethods.GET) {
3434
for (const treeLayer of treeLayers) {
35+
// Check if this is an external WMS layer
36+
const isExternalWMS = treeLayer.itemState?.externalWmsToggle === true;
37+
3538
const wmsParams = {
3639
LAYER: treeLayer.wmsName,
3740
STYLES: treeLayer.wmsSelectedStyleName,
3841
};
3942

40-
await wms.getLegendGraphic(wmsParams).then((response) => {
41-
for (const node of response.nodes) {
42-
// If the layer has no symbology, there is no type property
43-
if (node.hasOwnProperty('type')) {
44-
treeLayer.symbology = node;
45-
}
43+
if (isExternalWMS) {
44+
// For external WMS layers, get PNG legend directly
45+
try {
46+
const pngUrl = wms.getLegendGraphicPNG(wmsParams);
47+
// Fetch the PNG and convert to base64
48+
const response = await fetch(pngUrl);
49+
const blob = await response.blob();
50+
const reader = new FileReader();
51+
52+
await new Promise((resolve, reject) => {
53+
reader.onloadend = () => {
54+
const base64data = reader.result.split(',')[1]; // Remove data:image/png;base64, prefix
55+
treeLayer.symbology = {
56+
type: 'layer',
57+
name: treeLayer.wmsName,
58+
title: treeLayer.name,
59+
icon: base64data
60+
};
61+
resolve();
62+
};
63+
reader.onerror = reject;
64+
reader.readAsDataURL(blob);
65+
});
66+
} catch (error) {
67+
console.error('Error loading external WMS legend:', error);
68+
// Fallback to default icon will be handled by symbology state
4669
}
47-
}).catch((error) => {
48-
console.error(error);
49-
});
70+
} else {
71+
// For normal layers, use JSON format
72+
await wms.getLegendGraphic(wmsParams).then((response) => {
73+
for (const node of response.nodes) {
74+
// If the layer has no symbology, there is no type property
75+
if (node.hasOwnProperty('type')) {
76+
treeLayer.symbology = node;
77+
}
78+
}
79+
}).catch((error) => {
80+
console.error(error);
81+
});
82+
}
5083
}
5184
return treeLayers;
5285
}

0 commit comments

Comments
 (0)