From 70dfbddadb76da36487780b940fba9ea6d44ca78 Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Thu, 17 Feb 2022 16:07:28 +0100 Subject: [PATCH] feat(map-context): merge configured layers into mapContext and set default basemap if necessary --- .../map-context/map-context.service.spec.ts | 62 ++++++++++++++++++- .../lib/map-context/map-context.service.ts | 34 +++++++++- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/libs/feature/map/src/lib/map-context/map-context.service.spec.ts b/libs/feature/map/src/lib/map-context/map-context.service.spec.ts index 18512bef42..61ed2ef849 100644 --- a/libs/feature/map/src/lib/map-context/map-context.service.spec.ts +++ b/libs/feature/map/src/lib/map-context/map-context.service.spec.ts @@ -22,7 +22,10 @@ import { MAP_CTX_LAYER_XYZ_FIXTURE, } from './map-context.fixtures' -import { MapContextService } from './map-context.service' +import { + DEFAULT_BASELAYER_CONTEXT, + MapContextService, +} from './map-context.service' const mapStyleServiceMock = { createDefaultStyle: jest.fn(() => new Style()), @@ -191,12 +194,69 @@ describe('MapContextService', () => { const mapContext = MAP_CTX_FIXTURE const mapConfig = MAP_CONFIG_FIXTURE beforeEach(() => { + mapConfig.USE_BASEMAP_FROM_LAYERS = true service.resetMapFromContext(map, mapContext, mapConfig) }) it('set maxZoom', () => { const maxZoom = map.getView().getMaxZoom() expect(maxZoom).toBe(10) }) + it('set first layer as baselayer', () => { + const baselayerUrls = map.getLayers().item(0).getSource().getUrls() + expect(baselayerUrls).toEqual(['https://some-basemap-server']) + }) + it('add one WMS layer from config on top of baselayer', () => { + const layerWMSUrl = map.getLayers().item(1).getSource().getUrls()[0] + expect(layerWMSUrl).toEqual('https://some-wms-server') + }) + it('add one WFS layer from config on top of baselayer', () => { + const layerWFSSource = map.getLayers().item(2).getSource() + expect(layerWFSSource).toBeInstanceOf(VectorSource) + }) + }) + describe('with config, but keeping default basemap', () => { + const map = new Map({}) + const mapContext = MAP_CTX_FIXTURE + const mapConfig = MAP_CONFIG_FIXTURE + beforeEach(() => { + mapConfig.USE_BASEMAP_FROM_LAYERS = false + service.resetMapFromContext(map, mapContext, mapConfig) + }) + it('set first layer as baselayer', () => { + const baselayerUrls = map.getLayers().item(0).getSource().getUrls() + expect(baselayerUrls).toEqual(DEFAULT_BASELAYER_CONTEXT.urls) + }) + }) + }) + describe('#mergeMapConfigWithContext', () => { + const mapContext = MAP_CTX_FIXTURE + const mapConfig = MAP_CONFIG_FIXTURE + beforeEach(() => { + mapConfig.USE_BASEMAP_FROM_LAYERS = true + }) + it('merges mapconfig into existing mapcontext', () => { + const mergedMapContext = service.mergeMapConfigWithContext( + mapContext, + mapConfig + ) + const layersContext = service.getLayersContextFromConfig( + MAP_CONFIG_FIXTURE.LAYERS + ) + + expect(mergedMapContext).toEqual({ + ...MAP_CTX_FIXTURE, + view: { + ...MAP_CTX_FIXTURE.view, + maxZoom: MAP_CONFIG_FIXTURE.MAX_ZOOM, + maxExtent: MAP_CONFIG_FIXTURE.MAX_EXTENT, + }, + layers: [ + layersContext[0], + layersContext[1], + layersContext[2], + ...MAP_CTX_FIXTURE.layers, + ], + }) }) }) }) diff --git a/libs/feature/map/src/lib/map-context/map-context.service.ts b/libs/feature/map/src/lib/map-context/map-context.service.ts index 747332381d..9faa700288 100644 --- a/libs/feature/map/src/lib/map-context/map-context.service.ts +++ b/libs/feature/map/src/lib/map-context/map-context.service.ts @@ -17,7 +17,16 @@ import VectorSource from 'ol/source/Vector' import { MapUtilsService } from '../utils/map-utils.service' import { bbox as bboxStrategy } from 'ol/loadingstrategy' import GeoJSON from 'ol/format/GeoJSON' -import { MapConfig } from '@geonetwork-ui/util/app-config' +import { LayerConfig, MapConfig } from '@geonetwork-ui/util/app-config' + +export const DEFAULT_BASELAYER_CONTEXT: MapContextLayerModel = { + type: MapContextLayerTypeEnum.XYZ, + urls: [ + `https://a.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png`, + `https://b.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png`, + `https://c.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png`, + ], +} @Injectable({ providedIn: 'root', @@ -122,6 +131,29 @@ export class MapContextService { maxExtent: mapConfig.MAX_EXTENT, }), }, + layers: [ + ...(mapConfig.USE_BASEMAP_FROM_LAYERS + ? [] + : [DEFAULT_BASELAYER_CONTEXT]), + ...(mapConfig.LAYERS + ? this.getLayersContextFromConfig(mapConfig.LAYERS) + : []), + ...mapContext.layers, + ], } } + + getLayersContextFromConfig( + layersConfig: LayerConfig[] + ): MapContextLayerModel[] { + const layersModel: MapContextLayerModel[] = [] + layersConfig.forEach((layerConfig) => { + layersModel.push({ + type: MapContextLayerTypeEnum[layerConfig.TYPE.toUpperCase()], + url: layerConfig.URL, + name: layerConfig.NAME, + }) + }) + return layersModel + } }