Skip to content

Commit

Permalink
Merge pull request #17 from KaiVolland/backport-getlegend-nontiled
Browse files Browse the repository at this point in the history
Add support for OlSourceImageWMS to getLegendGraphicUrl method (for ol5)
  • Loading branch information
KaiVolland committed Sep 4, 2018
2 parents a25979d + ad29a6f commit d915b89
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
22 changes: 16 additions & 6 deletions src/MapUtil/MapUtil.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import OlMap from 'ol/Map';
import OlSourceTileWMS from 'ol/source/TileWMS';
import OlLayerTile from 'ol/layer/Tile';
import OlSourceImageWMS from 'ol/source/ImageWMS';
import OlLayerGroup from 'ol/layer/Group';
import OlLayerBase from 'ol/layer/Base';
import { METERS_PER_UNIT } from 'ol/proj/Units';
Expand Down Expand Up @@ -300,15 +300,25 @@ export class MapUtil {
* @return {String|undefined} The getLegendGraphicUrl.
*/
static getLegendGraphicUrl(layer, extraParams) {
if (!(layer instanceof OlLayerBase)) {
Logger.error('Invalid input parameter for MapUtil.getLegendGraphicUrl.');
if (!layer) {
Logger.error('No layer passed to MapUtil.getLegendGraphicUrl.');
return;
}

if (layer instanceof OlLayerTile
&& layer.getSource() instanceof OlSourceTileWMS) {
const source = layer.getSource();
if (!(layer instanceof OlLayerBase) || !source) {
Logger.error('Invalid layer passed to MapUtil.getLegendGraphicUrl.');
return;
}

const isTiledWMS = source instanceof OlSourceTileWMS;
const isImageWMS = source instanceof OlSourceImageWMS;

if (isTiledWMS || isImageWMS) {
const source = layer.getSource();
const url = source.getUrls() ? source.getUrls()[0] : '';
const url = isTiledWMS ?
source.getUrls() ? source.getUrls()[0] : ''
: source.getUrl();
const params = {
LAYER: source.getParams().LAYERS,
VERSION: '1.3.0',
Expand Down
65 changes: 47 additions & 18 deletions src/MapUtil/MapUtil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import OlInteractionDragRotateAndZoom from 'ol/interaction/DragRotateAndZoom';
import OlInteractionDraw from 'ol/interaction/Draw';
import OlLayerTile from 'ol/layer/Tile';
import OlLayerImage from 'ol/layer/Image';
import OlSourceTileWMS from 'ol/source/TileWMS';
import OlSourceImageWMS from 'ol/source/ImageWMS';
import OlSourceTileJson from 'ol/source/TileJSON';
import OlFeature from 'ol/Feature';
import OlGeomPoint from 'ol/geom/Point';
Expand Down Expand Up @@ -466,6 +468,7 @@ describe('MapUtil', () => {

let layer1;
let layer2;
let layer3;

beforeEach(() => {
layer1 = new OlLayerTile({
Expand All @@ -476,7 +479,15 @@ describe('MapUtil', () => {
serverType: 'geoserver'
})
});
layer2 = new OlLayerTile({
layer2 = new OlLayerImage({
name: 'OSM-WMS',
source: new OlSourceImageWMS({
url: 'https://ows.terrestris.de/osm-gray/service',
params: {'LAYERS': 'OSM-WMS', 'TILED': true},
serverType: 'geoserver'
})
});
layer3 = new OlLayerTile({
name: 'Food insecurity',
source: new OlSourceTileJson({
url: 'https://api.tiles.mapbox.com/v3/mapbox.20110804-hoa-foodinsecurity-3month.json?secure',
Expand All @@ -490,36 +501,54 @@ describe('MapUtil', () => {
const legendUrl = MapUtil.getLegendGraphicUrl();
expect(legendUrl).toBeUndefined();
expect(logSpy).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalledWith('Invalid input parameter for MapUtil.getLegendGraphicUrl.');
expect(logSpy).toHaveBeenCalledWith('No layer passed to MapUtil.getLegendGraphicUrl.');

logSpy.mockReset();
logSpy.mockRestore();
});

it('logs a warning if called with an unsupported layersource', () => {
const logSpy = jest.spyOn(Logger, 'warn');
const legendUrl = MapUtil.getLegendGraphicUrl(layer2);
const legendUrl = MapUtil.getLegendGraphicUrl(layer3);
expect(legendUrl).toBeUndefined();
expect(logSpy).toHaveBeenCalledWith('Source of "Food insecurity" is currently not supported by MapUtil.getLegendGraphicUrl.');
logSpy.mockReset();
logSpy.mockRestore();
});

it('returns a getLegendGraphicUrl from a given layer', () => {
const legendUrl = MapUtil.getLegendGraphicUrl(layer1);
const url = 'https://ows.terrestris.de/osm-gray/service?';
const layerParam = 'LAYER=OSM-WMS';
const versionParam = 'VERSION=1.3.0';
const serviceParam = 'SERVICE=WMS';
const requestParam = 'REQUEST=getLegendGraphic';
const formatParam = 'FORMAT=image%2Fpng';

expect(legendUrl).toContain(url);
expect(legendUrl).toContain(layerParam);
expect(legendUrl).toContain(versionParam);
expect(legendUrl).toContain(serviceParam);
expect(legendUrl).toContain(requestParam);
expect(legendUrl).toContain(formatParam);
describe('returns a getLegendGraphicUrl from a given layer', () => {
it('… for a tiled Layer', () => {
const legendUrl = MapUtil.getLegendGraphicUrl(layer1);
const url = 'https://ows.terrestris.de/osm-gray/service?';
const layerParam = 'LAYER=OSM-WMS';
const versionParam = 'VERSION=1.3.0';
const serviceParam = 'SERVICE=WMS';
const requestParam = 'REQUEST=getLegendGraphic';
const formatParam = 'FORMAT=image%2Fpng';

expect(legendUrl).toContain(url);
expect(legendUrl).toContain(layerParam);
expect(legendUrl).toContain(versionParam);
expect(legendUrl).toContain(serviceParam);
expect(legendUrl).toContain(requestParam);
expect(legendUrl).toContain(formatParam);
});
it('… for an image Layer', () => {
const legendUrl = MapUtil.getLegendGraphicUrl(layer2);
const url = 'https://ows.terrestris.de/osm-gray/service?';
const layerParam = 'LAYER=OSM-WMS';
const versionParam = 'VERSION=1.3.0';
const serviceParam = 'SERVICE=WMS';
const requestParam = 'REQUEST=getLegendGraphic';
const formatParam = 'FORMAT=image%2Fpng';

expect(legendUrl).toContain(url);
expect(legendUrl).toContain(layerParam);
expect(legendUrl).toContain(versionParam);
expect(legendUrl).toContain(serviceParam);
expect(legendUrl).toContain(requestParam);
expect(legendUrl).toContain(formatParam);
});
});

it('accepts extraParams for the request', () => {
Expand Down

0 comments on commit d915b89

Please sign in to comment.