Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0.0 alpha - Legend Switcher on WMS #392

Merged
merged 18 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions demo/src/app/geo/layer/layer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,60 @@ export class AppLayerComponent {
})
.subscribe(l => this.map.addLayer(l));

this.layerService
.createAsyncLayer({
title: 'lieu habité',
visible: false,
sourceOptions: {
type: 'wms',
url: 'https://ws.mapserver.transports.gouv.qc.ca/swtq',
optionsFromCapabilities: true,
params: {
layers: 'lieuhabite',
version: '1.3.0'
}
}
})
.subscribe(l => this.map.addLayer(l));

this.layerService
.createAsyncLayer({
title: 'sh_dis_eco',
visible: false,
sourceOptions: {
type: 'wms',
url: 'https://geoegl.msp.gouv.qc.ca/apis/ws/mffpecofor.fcgi',
optionsFromCapabilities: true,
params: {
layers: 'sh_dis_eco',
version: '1.3.0'
}
}
})
.subscribe(l => this.map.addLayer(l));

this.layerService
.createAsyncLayer({
title: 'nurc:Arc_Sample_Parent',
visible: true,
legendOptions: {
// collapsed: false,
display: true,
// url: 'https://v.seloger.com/s/width/1144/visuels/0/m/l/4/0ml42xbt1n3itaboek3qec5dtskdgw6nlscu7j69k.jpg',
stylesAvailable: [{name: 'rain', title: 'Pluie'}, {name: 'raster', title: 'Défaut'}] //
},
sourceOptions: {
type: 'wms',
url: 'https://demo.geo-solutions.it/geoserver/ows',
optionsFromCapabilities: true,
params: {
layers: 'nurc:Arc_Sample', // , test:Linea_costa
version: '1.3.0'
}
}
})
.subscribe(l => this.map.addLayer(l));

this.layerService
.createAsyncLayer({
title: 'Avertissements routier',
Expand Down
Binary file added packages/geo/src/assets/images/loading.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/geo/src/lib/catalog/shared/catalog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export class CatalogService {
const keywordList = layer.KeywordList ? layer.KeywordList : undefined;
const timeFilter = this.capabilitiesService.getTimeFilter(layer);
const timeFilterable = timeFilter && Object.keys(timeFilter).length > 0 ? true : false;
const legendOptions = layer.Style ? this.capabilitiesService.getStyle(layer.Style) : undefined;

const params = Object.assign({}, catalogQueryParams, {
layers: layer.Name,
Expand Down Expand Up @@ -213,6 +214,7 @@ export class CatalogService {
abstract,
keywordList
},
legendOptions,
tooltip: { type: catalogTooltipType } as TooltipContent,
sourceOptions
}
Expand Down
23 changes: 22 additions & 1 deletion packages/geo/src/lib/datasource/shared/capabilities.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ArcGISRestDataSourceOptions,
TileArcGISRestDataSourceOptions
} from './datasources';
import { LegendOptions, ItemStyleOptions } from '../../layer/shared/layers/layer.interface';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -161,6 +162,7 @@ export class CapabilitiesService {
const queryable = layer.queryable;
const timeFilter = this.getTimeFilter(layer);
const timeFilterable = timeFilter && Object.keys(timeFilter).length > 0;
const legendOptions = layer.Style ? this.getStyle(layer.Style) : undefined;

const options: WMSDataSourceOptions = ObjectUtils.removeUndefined({
_layerOptionsFromCapabilities: {
Expand All @@ -174,7 +176,8 @@ export class CapabilitiesService {
extern: metadata ? true : undefined,
abstract,
keywordList
}
},
legendOptions
},
queryable,
timeFilter: timeFilterable ? timeFilter : undefined,
Expand All @@ -189,6 +192,7 @@ export class CapabilitiesService {
capabilities: any
): WMTSDataSourceOptions {
const options = optionsFromCapabilities(capabilities, baseOptions);

return Object.assign(options, baseOptions);
}

Expand Down Expand Up @@ -340,4 +344,21 @@ export class CapabilitiesService {
return timeFilter;
}
}

getStyle(Style): LegendOptions {

const styleOptions: ItemStyleOptions[] = Style
.map((style) => {
return {
name: style.Name,
title: style.Title
};
})
// Handle repeat the style "default" in output (MapServer or OpenLayer)
.filter((item, index, self) => self.findIndex((i: ItemStyleOptions) => i.name === item.name) === index);

const legendOptions: LegendOptions = { stylesAvailable: styleOptions } as LegendOptions;

return legendOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import olFormatEsriJSON from 'ol/format/EsriJSON';
import * as olloadingstrategy from 'ol/loadingstrategy';

import { DataSource } from './datasource';
import { DataSourceLegendOptions } from './datasource.interface';
import { Legend } from './datasource.interface';
import { ArcGISRestDataSourceOptions } from './arcgisrest-datasource.interface';

export class ArcGISRestDataSource extends DataSource {
Expand Down Expand Up @@ -54,12 +54,13 @@ export class ArcGISRestDataSource extends DataSource {
});
}

getLegend(): DataSourceLegendOptions[] {
getLegend(): Legend[] {
const legendInfo = this.options.params.legendInfo;
const legend = super.getLegend();
if (legendInfo === undefined || legend.length > 0) {
return legend;
}

const id = parseInt(this.options.layer, 10);
const lyr = legendInfo.layers[id];
let htmlString = '<table><tr><td>' + lyr.layerName + '</td></tr>';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import olSourceCarto from 'ol/source/CartoDB';

import { DataSource } from './datasource';
import { DataSourceLegendOptions } from './datasource.interface';
import { Legend } from './datasource.interface';
import { CartoDataSourceOptions } from './carto-datasource.interface';
import { QueryHtmlTarget } from '../../../query/shared/query.enums';

Expand Down Expand Up @@ -38,11 +38,12 @@ export class CartoDataSource extends DataSource {
return new olSourceCarto(sourceOptions);
}

getLegend(): DataSourceLegendOptions[] {
getLegend(): Legend[] {
const legend = super.getLegend();
if (legend.length > 0) {
return legend;
}

let htmlString = '<table>';
if (this.options.config.layers[0].legend != null) {
this.options.config.layers[0].legend.items.forEach(f => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface DataSourceOptions {
| 'websocket'
| 'mvt'
| 'cluster';
legend?: DataSourceLegendOptions;
optionsFromCapabilities?: boolean;
// title: string;
// alias?: string;
Expand All @@ -28,18 +27,19 @@ export interface DataSourceOptions {
download?: DownloadOptions;
}

export interface DataSourceLegendOptions {
export interface SourceFieldsOptionsParams {
name: any;
alias?: any;
values?: any;
excludeFromOgcFilters?: boolean;
}

export interface Legend {
collapsed?: boolean;
display?: boolean;
url?: string;
html?: string;
style?: { [key: string]: string | number };
title?: string;
}

export interface SourceFieldsOptionsParams {
name: any;
alias?: any;
values?: any;
excludeFromOgcFilters?: boolean;
currentStyle?: string;
}
20 changes: 17 additions & 3 deletions packages/geo/src/lib/datasource/shared/datasources/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import olSource from 'ol/source/Source';

import {
DataSourceOptions,
DataSourceLegendOptions
Legend
} from './datasource.interface';

import { DataService } from './data.service';
import { generateIdFromSourceOptions } from '../../utils/id-generator';
import { LegendOptions } from '../../../layer';

export abstract class DataSource {

public id: string;
public ol: olSource;
private legend: Legend[];

constructor(
public options: DataSourceOptions = {},
Expand All @@ -28,8 +30,20 @@ export abstract class DataSource {
return generateIdFromSourceOptions(this.options);
}

getLegend(scale?: number): DataSourceLegendOptions[] {
return this.options.legend ? [this.options.legend] : [];
public getLegend(style?: string, scale?: number): Legend[] {
return this.legend ? this.legend : [];
}

public setLegend(options: LegendOptions): Legend[] {
if (options.url) {
this.legend = [{ url: options.url} ];
} else if (options.html) {
this.legend = [{ html: options.html} ];
} else {
this.legend = [];
}

return this.legend;
}

protected abstract onUnwatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import olSourceTileArcGISRest from 'ol/source/TileArcGISRest';
import { uuid } from '@igo2/utils';

import { DataSource } from './datasource';
import { DataSourceLegendOptions } from './datasource.interface';
import { Legend } from './datasource.interface';
import { TileArcGISRestDataSourceOptions } from './tilearcgisrest-datasource.interface';
import { QueryHtmlTarget } from '../../../query/shared/query.enums';

Expand Down Expand Up @@ -31,11 +31,12 @@ export class TileArcGISRestDataSource extends DataSource {
return new olSourceTileArcGISRest(this.options);
}

getLegend(): DataSourceLegendOptions[] {
getLegend(): Legend[] {
const legend = super.getLegend();
if (this.options.legendInfo === undefined || legend.length > 0) {
return legend;
}

const id = parseInt(this.options.layer, 10);
const lyr = this.options.legendInfo.layers[id];
let htmlString = '<table><tr><td>' + lyr.layerName + '</td></tr>';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import olSourceImageWMS from 'ol/source/ImageWMS';
import { DataSourceOptions } from './datasource.interface';
import { WFSDataSourceOptionsParams } from './wfs-datasource.interface';
import { MetadataOptions } from '../../../metadata/shared/metadata.interface';
import { LegendOptions } from '../../../layer/shared/layers/layer.interface';

export interface WMSDataSourceOptions extends DataSourceOptions {
// type?: 'wms';
optionsFromCapabilities?: boolean;
paramsWFS?: WFSDataSourceOptionsParams; // for wms linked with wfs
urlWfs?: string; // if url for linked wfs differ from the url for wms.
url: string;
Expand All @@ -32,4 +32,5 @@ export interface WMSLayerOptionsFromCapabilities {
minResolution?: number;
maxResolution?: string;
metadata?: MetadataOptions;
legendOptions?: LegendOptions;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import olSourceImageWMS from 'ol/source/ImageWMS';

import { DataSource } from './datasource';
import { DataSourceLegendOptions } from './datasource.interface';
import { Legend } from './datasource.interface';
import { WMSDataSourceOptions } from './wms-datasource.interface';
import { WFSService } from './wfs.service';

Expand Down Expand Up @@ -154,9 +154,9 @@ export class WMSDataSource extends DataSource {
return new olSourceImageWMS(this.options);
}

getLegend(scale?: number): DataSourceLegendOptions[] {
getLegend(style?: string, scale?: number): Legend[] {
let legend = super.getLegend();
if (legend.length > 0) {
if (legend.length > 0 && (!style && !scale)) {
return legend;
}

Expand All @@ -175,14 +175,18 @@ export class WMSDataSource extends DataSource {
'SLD_VERSION=1.1.0',
`VERSION=${sourceParams.version || '1.3.0'}`
];
if (style !== undefined) {
params.push(`STYLE=${style}`);
}
if (scale !== undefined) {
params.push(`SCALE=${scale}`);
}

legend = layers.map((layer: string) => {
return {
url: `${baseUrl}?${params.join('&')}&LAYER=${layer}`,
title: layers.length > 1 ? layer : undefined
title: layers.length > 1 ? layer : undefined,
currentStyle: !style ? undefined : style as string
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { DataSourceOptions } from './datasource.interface';

export interface WMTSDataSourceOptions extends DataSourceOptions {
// type?: 'wmts';
optionsFromCapabilities?: boolean;

projection?: string;
layer: string;
style: string;
Expand Down
12 changes: 6 additions & 6 deletions packages/geo/src/lib/layer/layer-item/layer-item.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Layer, TooltipType } from '../shared/layers';
})
export class LayerItemComponent implements OnInit, OnDestroy {

showLegend$: BehaviorSubject<boolean> = new BehaviorSubject(false);
showLegend$: BehaviorSubject<boolean> = new BehaviorSubject(true);

inResolutionRange$: BehaviorSubject<boolean> = new BehaviorSubject(true);

Expand Down Expand Up @@ -53,12 +53,11 @@ export class LayerItemComponent implements OnInit, OnDestroy {
constructor() {}

ngOnInit() {
const legend = this.layer.dataSource.options.legend || {};
let legendCollapsed = legend.collapsed === false ? false : true;
if (this.layer.visible && this.expandLegendIfVisible) {
legendCollapsed = false;
if (this.layer.visible && this.expandLegendIfVisible && (this.layer.firstLoadComponent === true)) {
this.layer.firstLoadComponent = false;
this.layer.legendCollapsed = false;
}
this.toggleLegend(legendCollapsed);
this.toggleLegend(this.layer.legendCollapsed);
this.updateQueryBadge();

const resolution$ = this.layer.map.viewController.resolution$;
Expand All @@ -73,6 +72,7 @@ export class LayerItemComponent implements OnInit, OnDestroy {
}

toggleLegend(collapsed: boolean) {
this.layer.legendCollapsed = collapsed;
this.showLegend$.next(!collapsed);
}

Expand Down
Loading