Skip to content

Commit

Permalink
feat(catalog): Dig multiples levels in getCapabilities service (#141)
Browse files Browse the repository at this point in the history
* Dig multiples levels in getCapabilities service
Add regex filter on layer's name when added by getCapabilities

* correction to respect npm test
  • Loading branch information
mikesmichael authored and mbarbeau committed May 2, 2018
1 parent 3fb0c52 commit cf7c77d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Directive, Self, OnInit, OnDestroy,
HostListener } from '@angular/core';
import {
Directive, Self, OnInit, OnDestroy,
HostListener
} from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

import { MapService } from '../../map';
Expand Down Expand Up @@ -35,18 +37,18 @@ export class CatalogLayersListBindingDirective implements OnInit, OnDestroy {

this.dataSourceService
.createAsyncDataSource(dataSourceContext as AnyDataSourceContext)
.subscribe(dataSource => {
.subscribe(dataSource => {
const layerInstance = this.layerService.createLayer(dataSource, layerContext);
map.addLayer(layerInstance);
});
}

constructor(@Self() component: CatalogLayersListComponent,
private catalogService: CatalogService,
private mapService: MapService,
private dataSourceService: DataSourceService,
private layerService: LayerService,
private capabilitiesService: CapabilitiesService) {
private catalogService: CatalogService,
private mapService: MapService,
private dataSourceService: DataSourceService,
private layerService: LayerService,
private capabilitiesService: CapabilitiesService) {
this.component = component;
}

Expand All @@ -59,6 +61,66 @@ export class CatalogLayersListBindingDirective implements OnInit, OnDestroy {
this.selectedCatalog$$.unsubscribe();
}

/**
* Dig in the layerList for each layer definition
@param catalog: object of config.json parameter
@param layerList: object of current level of layers
@param groupsLayers: object of group of layers to show in the app
*/
includeRecursiveLayer(catalog, layerList, groupsLayers) {
let currentRegFilter;
let boolRegFilter = true;
let objGroupLayers;
// Dig all levels until last level (layer object are not defined on last level)
for (const group of layerList.Layer) {
if (group.queryable === false && typeof group.Layer !== 'undefined') {
// recursive, check next level
this.includeRecursiveLayer(catalog, group, groupsLayers);
} else {
// Define object of group layer
objGroupLayers = {
title: layerList.Title,
// Add only layers with regFilter condition respected
layers: layerList.Layer.reduce((arrLayer, layer) => {
boolRegFilter = true;
// Check for regex validation on layer's name
if (typeof catalog.regFilters !== 'undefined') {
// Test layer.Name for each regex define in config.json
for (const regFilter of catalog.regFilters) {
boolRegFilter = false;
currentRegFilter = new RegExp(regFilter);
boolRegFilter = currentRegFilter.test(layer.Name);
// If regex is respected, stop the for loop
if (boolRegFilter === true) {
break;
}
}
}
// If layer regex is okay (or not define), add the layer to the group
if (boolRegFilter === true) {
arrLayer.push({
title: layer.Title,
type: 'wms',
url: catalog.url,
params: {
layers: layer.Name
}
});
}
return arrLayer;
}, [])
};
/* If object contain layers (when regFilters is define, the condition
in Layer.map can define group with no layer) */
if (objGroupLayers.layers.length !== 0) {
groupsLayers.push(objGroupLayers);
}
// Break the group (don't add a group of layer for each of their layer!)
break;
}
}
}

handleCatalogChanged(catalog: Catalog) {
if (!catalog || !catalog.url) {
return;
Expand All @@ -78,24 +140,8 @@ export class CatalogLayersListBindingDirective implements OnInit, OnDestroy {
const groupsLayers: GroupLayers[] = [];
this.capabilitiesService.getCapabilities('wms', catalog.url)
.subscribe((capabilities) => {
for (const group of capabilities.Capability.Layer.Layer) {
groupsLayers.push({
title: group.Title,
layers: group.Layer.map((layer) => {
return {
title: layer.Title,
type: 'wms',
url: catalog.url,
params: {
layers: layer.Name
}
};
})
});
}

this.includeRecursiveLayer(catalog, capabilities.Capability.Layer, groupsLayers);
this.component.groupsLayers = groupsLayers;
});
}

}
1 change: 1 addition & 0 deletions src/lib/catalog/shared/catalog.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface Catalog {
title?: string;
url?: string;
type?: string;
regFilters?: Array<string>;
}

export interface CatalogServiceOptions {
Expand Down

0 comments on commit cf7c77d

Please sign in to comment.