Skip to content

Commit

Permalink
feat: util to set visiblity for a list of layers
Browse files Browse the repository at this point in the history
  • Loading branch information
ahennr committed May 15, 2023
1 parent 309706d commit f250e56
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/MapUtil/MapUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,4 +778,56 @@ describe('MapUtil', () => {
expect(inScaleRange).toBe(false);
});
});

describe('#setVisibilityForLayers', () => {
it('is defined', () => {
expect(MapUtil.setVisibilityForLayers).toBeDefined();
});

it('sets visibility for named layer correctly ', () => {
const testName = 'testVisiblityWms';
const layer = new OlLayerImage({
source: new OlSourceImageWMS({
url: 'https://ows.terrestris.de/osm-gray/service',
params: {LAYERS: 'OSM-WMS', TILED: true},
serverType: 'geoserver'
}),
properties: {
name: testName
}
});

map.addLayer(layer);

MapUtil.setVisibilityForLayers(map, [testName], true);
expect(MapUtil.getLayerByName(map, testName)?.getVisible()).toBe(true);


MapUtil.setVisibilityForLayers(map, [testName], false);
expect(MapUtil.getLayerByName(map, testName)?.getVisible()).toBe(false);
});
});

it('sets visibility for layer correctly which is accessed by feature type name', () => {
const testFeatureType = 'TEST:MY_FEATURE_TYPE_NAME';
const layer = new OlLayerImage({
source: new OlSourceImageWMS({
url: 'https://my-test-wms.de/service',
params: {LAYERS: testFeatureType}
}),
properties: {
name: 'This layer is just a dummy test layer'
}
});

map.addLayer(layer);

MapUtil.setVisibilityForLayers(map, [testFeatureType], true);
expect(MapUtil.getLayerByNameParam(map, testFeatureType)?.getVisible()).toBe(true);


MapUtil.setVisibilityForLayers(map, [testFeatureType], false);
expect(MapUtil.getLayerByNameParam(map, testFeatureType)?.getVisible()).toBe(false);
});

});
24 changes: 24 additions & 0 deletions src/MapUtil/MapUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import UrlUtil from '@terrestris/base-util/dist/UrlUtil/UrlUtil';
import { isNil } from 'lodash';
import findIndex from 'lodash/findIndex';
import _isFinite from 'lodash/isFinite';
import _isNil from 'lodash/isNil';
import _isString from 'lodash/isString';
import OlFeature from 'ol/Feature';
import OlGeometry from 'ol/geom/Geometry';
import OlGeomGeometryCollection from 'ol/geom/GeometryCollection';
import OlBaseLayer from 'ol/layer/Base';
import OlLayerGroup from 'ol/layer/Group';
import OlLayerImage from 'ol/layer/Image';
import OlLayer from 'ol/layer/Layer';
import OlLayerTile from 'ol/layer/Tile';
import OlMap from 'ol/Map';
import { toLonLat } from 'ol/proj';
Expand Down Expand Up @@ -485,6 +487,28 @@ export class MapUtil {
};
}

/**
* Set visibility for layer having names (if in map)
* @param {OlMap} olMap The OpenLayers map.
* @param {string[]} layerNames An array of layer names (feature type names can also be used)
* @param {boolean} visible if layer should be visible or not
*/
static setVisibilityForLayers(olMap: OlMap, layerNames: string[], visible: boolean) {
if (_isNil(olMap)) {
return;
}
if (_isNil(layerNames) || layerNames.length === 0) {
return;
}
layerNames?.forEach(layerName => {
let layer = MapUtil.getLayerByName(olMap, layerName) as OlLayer;
if (_isNil(layer)) {
layer = MapUtil.getLayerByNameParam(olMap, layerName) as OlLayer;
}
layer?.setVisible(visible);
});
}

}

export default MapUtil;

0 comments on commit f250e56

Please sign in to comment.