@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
89 lines (88 loc) • 3.15 kB
JavaScript
import { appendParams } from 'ol/uri';
class LegendHelper {
}
/**
* Get the WMS legend URL.
* @param url The base url of the wms service.
* @param layerName The name of a wms layer.
* @param options to create the legend url.
* @returns The legend URL or undefined.
*/
Object.defineProperty(LegendHelper, "getWMSLegendURL", {
enumerable: true,
configurable: true,
writable: true,
value: (url, layerName, options) => {
if (!url) {
return undefined;
}
const queryString = {
FORMAT: 'image/png',
TRANSPARENT: 'TRUE',
SERVICE: 'WMS',
VERSION: '1.1.1',
REQUEST: 'GetLegendGraphic',
LAYER: layerName
};
const scale = options?.scale;
const legendRule = options?.legendRule;
const legendWidth = options?.legendWidth;
const legendHeight = options?.legendHeight;
const serverType = options?.serverType;
const dpi = options?.dpi;
const bbox = options?.bbox;
const srs = options?.srs;
const additionalQueryString = options?.additionalQueryString;
if (scale !== undefined) {
queryString.SCALE = scale;
}
if (legendRule !== undefined) {
queryString.RULE = legendRule;
if (legendWidth !== undefined) {
queryString.WIDTH = legendWidth;
}
if (legendHeight !== undefined) {
queryString.HEIGHT = legendHeight;
}
}
if (serverType == 'qgis') {
if (dpi != undefined) {
queryString.DPI = dpi;
}
if (bbox != undefined && srs != undefined && scale != undefined && dpi != undefined && legendRule == undefined) {
queryString.BBOX = bbox.join(',');
queryString.SRS = srs;
queryString.SRCWIDTH = Math.round(((bbox[2] - bbox[0]) / scale) * 39.37 * dpi);
queryString.SRCHEIGHT = Math.round(((bbox[3] - bbox[1]) / scale) * 39.37 * dpi);
delete queryString.SCALE; // QGIS calculate it from the BBOX the SRCWIDTH and the SRCHEIGHT.
}
}
if (additionalQueryString) {
Object.assign(queryString, additionalQueryString);
}
return appendParams(url, queryString);
}
});
/**
* Retrieves the legend URL for a given WMTS tile layer.
* @param {TileLayer<WMTS>} olayer - The OpenLayers tile layer object.
* @returns The legend URL or undefined if not found.
*/
Object.defineProperty(LegendHelper, "getWMTSLegendURL", {
enumerable: true,
configurable: true,
writable: true,
value: (olayer) => {
// BGE case of multiple styles ? case of multiple legendUrl ?
const styles = olayer.get('capabilitiesStyles');
if (!Array.isArray(styles) || styles.length <= 0) {
return;
}
const legendURL = styles[0].LegendURL;
if (!Array.isArray(legendURL) || legendURL.length <= 0) {
return;
}
return legendURL[0].href;
}
});
export default LegendHelper;