Skip to content

Commit

Permalink
feat(map-context): merge configured layers into mapContext and set de…
Browse files Browse the repository at this point in the history
…fault basemap if necessary
  • Loading branch information
tkohr committed Feb 17, 2022
1 parent e7e0254 commit 70dfbdd
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
62 changes: 61 additions & 1 deletion libs/feature/map/src/lib/map-context/map-context.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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,
],
})
})
})
})
34 changes: 33 additions & 1 deletion libs/feature/map/src/lib/map-context/map-context.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
}
}

0 comments on commit 70dfbdd

Please sign in to comment.