From 36e66041ff2c5ad97c4ae5c80526eb7af70e6aec Mon Sep 17 00:00:00 2001 From: Sidney Gijzen Date: Wed, 28 Aug 2024 16:14:54 +0200 Subject: [PATCH] support custom URL parameters in a GetLegendGraphic request for a layer without a style configured --- CHANGES.md | 1 + .../Ows/WebMapServiceCapabilitiesStratum.ts | 2 +- .../Catalog/Ows/WebMapServiceCatalogItem.ts | 14 ++++++++ .../Ows/WebMapServiceCatalogItemSpec.ts | 34 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 85b5206248..0dd187c041 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ #### next release (8.7.7) +- support URL parameters in a GetLegendGraphic request for a layer without a style configured - [The next improvement] #### 8.7.6 - 2024-08-22 diff --git a/lib/Models/Catalog/Ows/WebMapServiceCapabilitiesStratum.ts b/lib/Models/Catalog/Ows/WebMapServiceCapabilitiesStratum.ts index 3c9069a2e6..9bffe7f89c 100644 --- a/lib/Models/Catalog/Ows/WebMapServiceCapabilitiesStratum.ts +++ b/lib/Models/Catalog/Ows/WebMapServiceCapabilitiesStratum.ts @@ -203,7 +203,7 @@ export default class WebMapServiceCapabilitiesStratum extends LoadableStratum( legendUri = URI( proxyCatalogItemUrl( this.catalogItem, - this.catalogItem.url.split("?")[0] + this.catalogItem.getLegendBaseUrl() ) ); legendUri diff --git a/lib/Models/Catalog/Ows/WebMapServiceCatalogItem.ts b/lib/Models/Catalog/Ows/WebMapServiceCatalogItem.ts index a9c28efbb7..629aa0f494 100644 --- a/lib/Models/Catalog/Ows/WebMapServiceCatalogItem.ts +++ b/lib/Models/Catalog/Ows/WebMapServiceCatalogItem.ts @@ -364,6 +364,20 @@ class WebMapServiceCatalogItem this.setTrait(CommonStrata.user, "isShowingDiff", false); } + getLegendBaseUrl(): string { + // Remove problematic query parameters from URL + const baseUrl = QUERY_PARAMETERS_TO_REMOVE.reduce( + (url, parameter) => + url + .removeQuery(parameter) + .removeQuery(parameter.toUpperCase()) + .removeQuery(parameter.toLowerCase()), + new URI(this.url) + ); + + return baseUrl.toString(); + } + getLegendUrlForStyle( styleId: string, firstDate?: JulianDate, diff --git a/test/Models/Catalog/Ows/WebMapServiceCatalogItemSpec.ts b/test/Models/Catalog/Ows/WebMapServiceCatalogItemSpec.ts index ad896a1f5b..d1e94ddfd6 100644 --- a/test/Models/Catalog/Ows/WebMapServiceCatalogItemSpec.ts +++ b/test/Models/Catalog/Ows/WebMapServiceCatalogItemSpec.ts @@ -971,6 +971,40 @@ describe("WebMapServiceCatalogItem", function () { .catch(done.fail); }); + it("supports parameters in a GetLegendGraphic request for a layer without a style configured", function (done) { + const terria = new Terria(); + const wmsItem = new WebMapServiceCatalogItem("some-layer", terria); + runInAction(() => { + wmsItem.setTrait( + CommonStrata.definition, + "url", + "http://example.com/mapserv?map=%2Fmap%2Fexample.map" + ); + wmsItem.setTrait( + CommonStrata.definition, + "getCapabilitiesUrl", + "test/WMS/styles_and_dimensions.xml" + ); + wmsItem.setTrait(CommonStrata.definition, "layers", "A"); + wmsItem.setTrait( + CommonStrata.definition, + "supportsGetLegendGraphic", + true + ); + }); + + wmsItem + .loadMetadata() + .then(function () { + expect(wmsItem.legends.length).toBe(1); + expect(wmsItem.legends[0].url).toBe( + "http://example.com/mapserv?map=%2Fmap%2Fexample.map&service=WMS&version=1.3.0&request=GetLegendGraphic&format=image%2Fpng&sld_version=1.1.0&layer=A" + ); + }) + .then(done) + .catch(done.fail); + }); + it("fetches legend with colourScaleRange", function (done) { const terria = new Terria(); const wmsItem = new WebMapServiceCatalogItem("some-layer", terria);