AW1873 labels
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good

This commit is contained in:
Peter Bastiani 2023-08-21 16:28:32 +02:00
parent 294b9c6dd3
commit e374bb8a73

View File

@ -1,6 +1,6 @@
import { Component, Host, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges, forwardRef, Inject, InjectionToken } from '@angular/core'; import { Component, Host, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges, forwardRef, Inject, InjectionToken, OnDestroy } from '@angular/core';
import { LayerVectorComponent, SourceVectorComponent, MapComponent } from 'ngx-openlayers'; import { LayerVectorComponent, SourceVectorComponent, MapComponent } from 'ngx-openlayers';
import { ItemService, ItemTypeService, IItem, IItemType } from '@farmmaps/common'; import { ItemService, ItemTypeService, IItem, IItemType, FolderService } from '@farmmaps/common';
import { Feature } from 'ol'; import { Feature } from 'ol';
import { Point, Geometry } from 'ol/geom'; import { Point, Geometry } from 'ol/geom';
@ -17,6 +17,8 @@ import { GeoJSON } from 'ol/format';
import { Select } from 'ol/interaction'; import { Select } from 'ol/interaction';
import { IStyles } from '../../../models/style.cache'; import { IStyles } from '../../../models/style.cache';
import { FeatureIconService } from '../../../services/feature-icon.service'; import { FeatureIconService } from '../../../services/feature-icon.service';
import { Subscription } from 'rxjs';
import { getCenter } from 'ol/extent';
@Component({ @Component({
selector: 'fm-map-item-source-vector', selector: 'fm-map-item-source-vector',
@ -25,7 +27,7 @@ import { FeatureIconService } from '../../../services/feature-icon.service';
{ provide: SourceVectorComponent, useExisting: forwardRef(() => ItemVectorSourceComponent) } { provide: SourceVectorComponent, useExisting: forwardRef(() => ItemVectorSourceComponent) }
] ]
}) })
export class ItemVectorSourceComponent extends SourceVectorComponent implements OnInit, OnChanges { export class ItemVectorSourceComponent extends SourceVectorComponent implements OnInit, OnDestroy, OnChanges {
instance: Vector<Geometry>; instance: Vector<Geometry>;
private _format: GeoJSON; private _format: GeoJSON;
private _select: Select; private _select: Select;
@ -38,8 +40,10 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements
@Output() onFeatureSelected: EventEmitter<Feature<Geometry>> = new EventEmitter<Feature<Geometry>>(); @Output() onFeatureSelected: EventEmitter<Feature<Geometry>> = new EventEmitter<Feature<Geometry>>();
@Output() onFeatureHover: EventEmitter<Feature<Geometry>> = new EventEmitter<Feature<Geometry>>(); @Output() onFeatureHover: EventEmitter<Feature<Geometry>> = new EventEmitter<Feature<Geometry>>();
private stylesCache: IStyles = {}; private stylesCache: IStyles = {};
private labelToolSettings = { showFieldName: true, showCropName: true, showArea: false, showCentroid: false };
private sub: Subscription;
constructor(@Host() private layer: LayerVectorComponent, private itemService: ItemService, private map: MapComponent, private itemTypeService: ItemTypeService, private featureIconService$: FeatureIconService) { constructor(@Host() private layer: LayerVectorComponent, private itemService: ItemService, private map: MapComponent, private itemTypeService: ItemTypeService, private featureIconService$: FeatureIconService, private folderService: FolderService) {
super(layer); super(layer);
this._format = new GeoJSON(); this._format = new GeoJSON();
} }
@ -76,6 +80,14 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements
} }
ngOnInit() { ngOnInit() {
this.sub = this.folderService.getFolder('my_settings').subscribe(
userSettingsRoot => {
this.itemService.getChildItemList(userSettingsRoot.code, 'vnd.farmmaps.itemtype.settings.general').subscribe(
items => {
if (items && items.length > 0) this.labelToolSettings = items[0].data.labelToolSettings;
}
)
});
this.strategy = loadingstrategy.bbox; this.strategy = loadingstrategy.bbox;
this.format = new GeoJSON(); this.format = new GeoJSON();
this._select = new Select({ this._select = new Select({
@ -133,7 +145,8 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements
fill: new style.Fill({ fill: new style.Fill({
color: fillColor color: fillColor
}), }),
geometry: (feature: Feature<Geometry>) => this.geometry(feature) geometry: (feature: Feature<Geometry>) => this.geometry(feature),
text: this.getTextForCropField(feature)
}); });
} else { } else {
key = 'file'; key = 'file';
@ -153,6 +166,10 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements
}); });
} }
ngOnDestroy(): void {
if (this.sub) this.sub.unsubscribe();
}
ngOnChanges(changes: SimpleChanges) { ngOnChanges(changes: SimpleChanges) {
if (changes["features"] && this.instance) { if (changes["features"] && this.instance) {
this.instance.clear(true); this.instance.clear(true);
@ -188,4 +205,24 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements
} }
} }
} }
getTextForCropField(feature): style.Text {
if (!this.labelToolSettings) return null;
let displayText = '';
if (this.labelToolSettings.showFieldName) displayText += feature.get('name') + '\n';
if (this.labelToolSettings.showCropName) displayText += feature.get('cropTypeName') + '\n';
if (this.labelToolSettings.showArea) displayText += feature.get('area') + 'ha\n';
if (this.labelToolSettings.showCentroid) displayText += this.getCentroid(feature);
return new style.Text({
font: '13px Calibri,sans-serif',
fill: new style.Fill({ color: '#ffffff' }),
stroke: new style.Stroke({ color: '#000000', width: 2 }),
text: displayText
});
}
private getCentroid(feature: Feature<Geometry>) {
return null;
}
} }