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

feat(geo.layer.vector) use sourcefield's alias on query #459

Merged
merged 4 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions demo/src/app/geo/query/query.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ export class AppQueryComponent {
this.dataSourceService
.createAsyncDataSource({
type: 'vector',
queryable: true
queryable: true,
sourceFields:[
{ name:"name", alias:"Alias name" },
{ name:"description", alias:"Alias description" }
]
} as QueryableDataSourceOptions )
.subscribe(dataSource => {
this.map.addLayer(
Expand All @@ -124,14 +128,17 @@ export class AppQueryComponent {
olproj.transform([-72, 47.8], 'EPSG:4326', 'EPSG:3857'),
olproj.transform([-73.5, 47.4], 'EPSG:4326', 'EPSG:3857'),
olproj.transform([-72.4, 48.6], 'EPSG:4326', 'EPSG:3857')
])
]),
description: 'feature1 - description'
});

const feature2 = new olFeature({
name: 'feature2',
geometry: new olPoint(
olproj.transform([-73, 46.6], 'EPSG:4326', 'EPSG:3857')
)
),
description: 'feature2 - description'

});

const feature3 = new olFeature({
Expand All @@ -142,7 +149,8 @@ export class AppQueryComponent {
olproj.transform([-73, 47], 'EPSG:4326', 'EPSG:3857'),
olproj.transform([-71.2, 46.6], 'EPSG:4326', 'EPSG:3857')
]
])
]),
description: 'feature3 - description'
});

dataSource.ol.addFeatures([feature1, feature2, feature3]);
Expand Down
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 @@ -7,6 +7,7 @@ import { EntityKey, EntityStoreOptions } from '@igo2/common';
import { VectorLayer } from '../../layer';
import { IgoMap } from '../../map';
import { FeatureMotion } from './feature.enums';
import OlFeature from 'ol/Feature';

export interface Feature<P = {[key: string]: any}> {
type: string;
Expand All @@ -15,6 +16,7 @@ export interface Feature<P = {[key: string]: any}> {
properties: P;
extent?: [number, number, number, number];
meta?: FeatureMeta;
ol?: OlFeature;
}

export interface FeatureMeta {
Expand Down
3 changes: 2 additions & 1 deletion packages/geo/src/lib/feature/shared/feature.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export function featureFromOl(
revision: olFeature.getRevision()
},
properties,
geometry
geometry,
ol: olFeature
};
}

Expand Down
11 changes: 11 additions & 0 deletions packages/geo/src/lib/query/shared/query.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ export class QueryDirective implements AfterViewInit, OnDestroy {
const features = (olFeatures || []).map((olFeature: OlFeature) => {
return featureFromOl(olFeature, this.map.projection);
});

this.map.layers.forEach(layer=>{
features.forEach(feature=>{
if(typeof layer.ol.getSource().hasFeature != 'undefined'){
if(layer.ol.getSource().hasFeature(feature.ol)){
feature.meta.alias = this.queryService.getAllowedFieldsAndAlias(layer);
}
}
})
})

return of(features);
}

Expand Down
43 changes: 29 additions & 14 deletions packages/geo/src/lib/query/shared/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,7 @@ export class QueryService {
): Feature[] {
const queryDataSource = layer.dataSource as QueryableDataSource;

let allowedFieldsAndAlias;
if (
layer.options &&
layer.options.sourceOptions &&
layer.options.sourceOptions.sourceFields &&
layer.options.sourceOptions.sourceFields.length >= 1
) {
allowedFieldsAndAlias = {};
layer.options.sourceOptions.sourceFields.forEach(sourceField => {
const alias = sourceField.alias ? sourceField.alias : sourceField.name;
allowedFieldsAndAlias[sourceField.name] = alias;
});
}
let allowedFieldsAndAlias = this.getAllowedFieldsAndAlias(layer);
let features = [];
switch (queryDataSource.options.queryFormat) {
case QueryFormat.GML3:
Expand Down Expand Up @@ -449,7 +437,7 @@ export class QueryService {
return result;
}

private featureToResult(
public featureToResult(
featureOL: olFeature,
zIndex: number,
allowedFieldsAndAlias?
Expand Down Expand Up @@ -617,4 +605,31 @@ export class QueryService {

return mime;
}

getAllowedFieldsAndAlias(layer: any){
let allowedFieldsAndAlias;
if (
layer.options &&
layer.options.sourceOptions &&
layer.options.sourceOptions.sourceFields &&
layer.options.sourceOptions.sourceFields.length >= 1
) {
allowedFieldsAndAlias = {};
layer.options.sourceOptions.sourceFields.forEach(sourceField => {
const alias = sourceField.alias ? sourceField.alias : sourceField.name;
allowedFieldsAndAlias[sourceField.name] = alias;
});
} else if (layer.options &&
layer.options.source &&
layer.options.source.options &&
layer.options.source.options.sourceFields &&
layer.options.source.options.sourceFields.length >= 1){
allowedFieldsAndAlias = {};
layer.options.source.options.sourceFields.forEach(sourceField => {
const alias = sourceField.alias ? sourceField.alias : sourceField.name;
allowedFieldsAndAlias[sourceField.name] = alias;
});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be replace.
i was searching where "sourceOptions" is set ? and why not to reference to "source.options" ?
vector layer to not have this properties and "sourceOptions" seems to be a duplicate of "source.options"
@mbarbeau

return allowedFieldsAndAlias;
}
}