Skip to content

Commit

Permalink
feat(query, feature): possibility to exclude attributes (#465)
Browse files Browse the repository at this point in the history
* feat(feature):
- mvt, feature, cluster and xyz data sources have excludeAttribute and excludeAttributeOffline in their options.
- Feature has excludeAttribute and excludeAttributeOffline in their meta. those attributes are added in featureFromOL.
- filterFeatureProperties() remove excludeAttribute and excludeAttributeOffline from properties.
- doQueryFeature() use forEachFeatureAtPixel instead of getFeaturesAtPixel because it can provide layers.

* fix featured details

* fix feature.utils.ts

* eat(feature): excludeAttribute is now avalaible for WMS datasource

* Update query.directive.ts

* Update feature.utils.ts

* Update feature-details.component.ts
  • Loading branch information
drekss authored and mbarbeau committed Oct 29, 2019
1 parent 9f70c05 commit 3ecd11e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export interface ClusterDataSourceOptions extends FeatureDataSourceOptions {
source?: FeatureDataSource;
ol?: olSourceVector;
pathOffline?: string;
excludeAttribute?: Array<string>;
excludeAttributeOffline?: Array<string>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface FeatureDataSourceOptions extends DataSourceOptions {
format?: olFormatFeature;
url?: string;
pathOffline?: string;
excludeAttribute?: Array<string>;
excludeAttributeOffline?: Array<string>;

ol?: olSourceVector;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export interface MVTDataSourceOptions extends DataSourceOptions {
ol?: olSourceVectorTile;
url?: string;
pathOffline?: string;
excludeAttribute?: Array<string>;
excludeAttributeOffline?: Array<string>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface WMSDataSourceOptions extends DataSourceOptions {
ol?: olSourceImageWMS;
refreshIntervalSec?: number;
_layerOptionsFromCapabilities?: WMSLayerOptionsFromCapabilities;
excludeAttribute?: Array<string>;
}

export interface WMSDataSourceOptionsParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export interface XYZDataSourceOptions extends DataSourceOptions {
url?: string;
urls?: string[];
pathOffline?: string;
excludeAttribute?: Array<string>;
excludeAttributeOffline?: Array<string>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
ChangeDetectorRef
} from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { NetworkService, ConnectionState } from '@igo2/core';

import { getEntityTitle, getEntityIcon } from '@igo2/common';

import { Feature } from '../shared';
import { SearchSource } from '../../search/shared/sources/source';

@Component({
selector: 'igo-feature-details',
Expand All @@ -17,6 +19,17 @@ import { Feature } from '../shared';
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FeatureDetailsComponent {
private state: ConnectionState;

@Input()
get source(): SearchSource {
return this._source;
}
set source(value: SearchSource ) {
this._source = value;
this.cdRef.detectChanges();
}

@Input()
get feature(): Feature {
return this._feature;
Expand All @@ -25,7 +38,9 @@ export class FeatureDetailsComponent {
this._feature = value;
this.cdRef.detectChanges();
}

private _feature: Feature;
private _source: SearchSource;

/**
* @internal
Expand All @@ -43,8 +58,13 @@ export class FeatureDetailsComponent {

constructor(
private cdRef: ChangeDetectorRef,
private sanitizer: DomSanitizer
) {}
private sanitizer: DomSanitizer,
private networkService: NetworkService
) {
this.networkService.currentState().subscribe((state: ConnectionState) => {
this.state = state;
});
}

htmlSanitizer(value): SafeResourceUrl {
return this.sanitizer.bypassSecurityTrustResourceUrl(value);
Expand All @@ -66,8 +86,8 @@ export class FeatureDetailsComponent {

filterFeatureProperties(feature) {
const allowedFieldsAndAlias = feature.meta ? feature.meta.alias : undefined;

const properties = {};

if (allowedFieldsAndAlias) {
Object.keys(allowedFieldsAndAlias).forEach(field => {
if (feature.properties[field]) {
Expand All @@ -76,7 +96,18 @@ export class FeatureDetailsComponent {
});
return properties;
} else {
return feature.properties;
if (this.state.connection && feature.meta && feature.meta.excludeAttribute) {
const excludeAttribute = feature.meta.excludeAttribute;
excludeAttribute.forEach(attribute => {
delete feature.properties[attribute];
});
} else if (!this.state.connection && feature.meta && feature.meta.excludeAttributeOffline) {
const excludeAttributeOffline = feature.meta.excludeAttributeOffline;
excludeAttributeOffline.forEach(attribute => {
delete feature.properties[attribute];
});
}
return feature.properties;
}
}
}
2 changes: 2 additions & 0 deletions packages/geo/src/lib/feature/shared/feature.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface FeatureMeta {
order?: number;
alias?: {[key: string]: string};
revision?: number;
excludeAttribute?: Array<string>;
excludeAttributeOffline?: Array<string>;
}

export interface FeatureGeometry {
Expand Down
18 changes: 16 additions & 2 deletions packages/geo/src/lib/feature/shared/feature.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as olproj from 'ol/proj';
import * as olstyle from 'ol/style';
import OlFeature from 'ol/Feature';
import OlFormatGeoJSON from 'ol/format/GeoJSON';
import OlLayer from 'ol/layer/Layer';
import { uuid } from '@igo2/utils';

import {
Expand Down Expand Up @@ -78,14 +79,19 @@ export function featureToOl(
* The output object has a reference to the feature id.
* @param olFeature OL Feature
* @param projectionIn OL feature projection
* @param olLayer OL Layer
* @param projectionOut Feature projection
* @returns Feature
*/
export function featureFromOl(
olFeature: OlFeature,
projectionIn: string,
olLayer?: OlLayer,
projectionOut = 'EPSG:4326'
): Feature {
let title;
let exclude;
let excludeOffline;
const olFormat = new OlFormatGeoJSON();

const keys = olFeature.getKeys().filter((key: string) => {
Expand All @@ -101,7 +107,13 @@ export function featureFromOl(
featureProjection: projectionIn
});

const title = olFeature.get('_title');
if (olLayer) {
title = olLayer.get('title');
exclude = olLayer.get('sourceOptions').excludeAttribute;
excludeOffline = olLayer.get('sourceOptions').excludeAttributeOffline;
} else {
title = olFeature.get('_title');
}
const mapTitle = olFeature.get('_mapTitle');
const id = olFeature.getId() ? olFeature.getId() : uuid();

Expand All @@ -113,7 +125,9 @@ export function featureFromOl(
id,
title: title ? title : mapTitle ? mapTitle : id,
mapTitle,
revision: olFeature.getRevision()
revision: olFeature.getRevision(),
excludeAttribute: exclude,
excludeAttributeOffline: excludeOffline
},
properties,
geometry,
Expand Down
27 changes: 21 additions & 6 deletions packages/geo/src/lib/query/shared/query.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,30 @@ export class QueryDirective implements AfterViewInit, OnDestroy {
* @param event OL map browser pointer event
*/
private doQueryFeatures(event: OlMapBrowserPointerEvent): Observable<Feature[]> {
const olFeatures = event.map.getFeaturesAtPixel(event.pixel, {
const clickedFeatures = [];

this.map.ol.forEachFeatureAtPixel(
event.pixel,
(featureOL: OlFeature, layerOL: OlLayer) => {
if (featureOL) {
if (featureOL.get('features')) {
featureOL = featureOL.get('features')[0];
}
const feature = featureFromOl(featureOL, this.map.projection, layerOL);
clickedFeatures.push(feature);

} else {
const feature = featureFromOl(featureOL, this.map.projection, layerOL);
clickedFeatures.push(feature);
}
},
{
hitTolerance: this.queryFeaturesHitTolerance || 0,
layerFilter: this.queryFeaturesCondition ? this.queryFeaturesCondition : olLayerIsQueryable
});
const features = (olFeatures || []).map((olFeature: OlFeature) => {
return featureFromOl(olFeature, this.map.projection);
});

const queryableLayers = this.map.layers.filter(layerIsQueryable);
features.forEach( (feature: Feature) => {
clickedFeatures.forEach( (feature: Feature) => {
queryableLayers.forEach((layer: AnyLayer) => {
if (typeof layer.ol.getSource().hasFeature !== 'undefined') {
if (layer.ol.getSource().hasFeature(feature.ol)) {
Expand All @@ -197,7 +212,7 @@ export class QueryDirective implements AfterViewInit, OnDestroy {
});
});

return of(features);
return of(clickedFeatures);
}

/**
Expand Down
14 changes: 11 additions & 3 deletions packages/geo/src/lib/query/shared/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { Layer } from '../../layer/shared/layers/layer';
import {
WMSDataSource,
CartoDataSource,
TileArcGISRestDataSource
TileArcGISRestDataSource,
WMSDataSourceOptions
} from '../../datasource';

import { QueryFormat, QueryHtmlTarget } from './query.enums';
Expand Down Expand Up @@ -266,8 +267,14 @@ export class QueryService {

return features.map((feature: Feature, index: number) => {
const mapLabel = feature.properties[queryDataSource.mapLabel];
let title = this.getQueryTitle(feature, layer);

let exclude;
if (layer.options.sourceOptions.type === 'wms') {
const sourceOptions = (layer.options.sourceOptions as WMSDataSourceOptions);
exclude = sourceOptions.excludeAttribute;
}

let title = this.getQueryTitle(feature, layer);
if (!title && features.length > 1) {
title = `${layer.title} (${index + 1})`;
} else if (!title) {
Expand All @@ -279,7 +286,8 @@ export class QueryService {
title,
mapTitle: mapLabel,
sourceTitle: layer.title,
order: 1000 - layer.zIndex
order: 1000 - layer.zIndex,
excludeAttribute: exclude
});

return Object.assign(feature, {
Expand Down

0 comments on commit 3ecd11e

Please sign in to comment.