@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
83 lines (82 loc) • 3.15 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import { appendParams } from 'ol/uri.js';
class LegendHelper {
/**
* Get the WMS legend URL.
* @param serverOgc 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.
*/
static getWMSLegendURL = (serverOgc, layerName, options) => {
if (!serverOgc.url) {
return undefined;
}
const queryString = {
FORMAT: 'image/png',
TRANSPARENT: 'TRUE',
SERVICE: 'WMS',
VERSION: '1.1.1',
REQUEST: 'GetLegendGraphic',
LAYER: layerName
};
if (serverOgc.type === 'qgisserver') {
queryString.LAYERTITLE = 'False';
}
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(serverOgc.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.
*/
static getWMTSLegendURL = (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;