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

This commit is contained in:
Peter Bastiani 2023-08-22 13:16:37 +02:00
parent 20dff38cf7
commit 9facbe76c8

View File

@ -1,4 +1,4 @@
import { Component, Host, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges, forwardRef, Inject, InjectionToken, OnDestroy } from '@angular/core'; import { Component, Host, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges, forwardRef, Inject, InjectionToken, OnDestroy, LOCALE_ID } from '@angular/core';
import { LayerVectorComponent, SourceVectorComponent, MapComponent } from 'ngx-openlayers'; import { LayerVectorComponent, SourceVectorComponent, MapComponent } from 'ngx-openlayers';
import { ItemService, ItemTypeService, IItem, IItemType, FolderService } from '@farmmaps/common'; import { ItemService, ItemTypeService, IItem, IItemType, FolderService } from '@farmmaps/common';
@ -19,6 +19,7 @@ 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 { Subscription } from 'rxjs';
import { getCenter } from 'ol/extent'; import { getCenter } from 'ol/extent';
import { formatNumber } from '@angular/common';
@Component({ @Component({
selector: 'fm-map-item-source-vector', selector: 'fm-map-item-source-vector',
@ -42,8 +43,9 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements
private stylesCache: IStyles = {}; private stylesCache: IStyles = {};
private labelToolSettings = { showFieldName: true, showCropName: true, showArea: false, showCentroid: false }; private labelToolSettings = { showFieldName: true, showCropName: true, showArea: false, showCentroid: false };
private sub: Subscription; private sub: Subscription;
private propertiesToShow: string[] = [];
constructor(@Host() private layer: LayerVectorComponent, private itemService: ItemService, private map: MapComponent, private itemTypeService: ItemTypeService, private featureIconService$: FeatureIconService, private folderService: FolderService) { constructor(@Host() private layer: LayerVectorComponent, private itemService: ItemService, private map: MapComponent, private itemTypeService: ItemTypeService, private featureIconService$: FeatureIconService, private folderService: FolderService, @Inject(LOCALE_ID) private locale: string) {
super(layer); super(layer);
this._format = new GeoJSON(); this._format = new GeoJSON();
} }
@ -84,7 +86,13 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements
userSettingsRoot => { userSettingsRoot => {
this.itemService.getChildItemList(userSettingsRoot.code, 'vnd.farmmaps.itemtype.settings.general').subscribe( this.itemService.getChildItemList(userSettingsRoot.code, 'vnd.farmmaps.itemtype.settings.general').subscribe(
items => { items => {
if (items && items.length > 0) this.labelToolSettings = items[0].data.labelToolSettings; if (items && items.length > 0) {
this.propertiesToShow = [];
if (items[0].data.labelToolSettings.showFieldName) this.propertiesToShow.push('name');
if (items[0].data.labelToolSettings.showCropName) this.propertiesToShow.push('cropTypeName');
if (items[0].data.labelToolSettings.showArea) this.propertiesToShow.push('area');
if (items[0].data.labelToolSettings.showCentroid) this.propertiesToShow.push('centroid');
}
} }
) )
}); });
@ -146,7 +154,7 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements
color: fillColor color: fillColor
}), }),
geometry: (feature: Feature<Geometry>) => this.geometry(feature), geometry: (feature: Feature<Geometry>) => this.geometry(feature),
text: this.getTextForCropField(feature) text: this.getDisplayTextForFeature(feature, this.propertiesToShow)
}); });
} else { } else {
key = 'file'; key = 'file';
@ -206,23 +214,34 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements
} }
} }
getTextForCropField(feature): style.Text { getDisplayTextForFeature(feature: Feature<Geometry>, propertiesToShow: string[], overrule: style.Text = null, zoom: { min: number, current: number } = null): style.Text {
if (!this.labelToolSettings) return null; if (!propertiesToShow) return null;
let displayText = ''; if (propertiesToShow.length <= 0) return null;
if (this.labelToolSettings.showFieldName) displayText += feature.get('name') + '\n'; if (zoom && zoom.current > zoom.min) return null;
if (this.labelToolSettings.showCropName && feature.get('cropTypeName')) displayText += feature.get('cropTypeName') + '\n';
if (this.labelToolSettings.showArea && feature.get('area')) displayText += feature.get('area') + 'ha\n';
if (this.labelToolSettings.showCentroid && this.getCentroid(feature)) displayText += this.getCentroid(feature);
return new style.Text({ let displayText = '';
for (let i = 0; i < propertiesToShow.length; i++) {
let value = feature.get(propertiesToShow[i]);
switch (propertiesToShow[i]) {
case "area": value = formatNumber(value, this.locale, '0.1-2') + 'ha'; break;
case "centroid": value = feature.getGeometry() ? getCenter(feature.getGeometry().getExtent()) : null; break;
}
displayText += value + (i < propertiesToShow.length ? '\n': '');
}
const styleText = new style.Text({
font: '13px Calibri,sans-serif', font: '13px Calibri,sans-serif',
fill: new style.Fill({ color: '#ffffff' }), fill: new style.Fill({ color: '#ffffff' }),
stroke: new style.Stroke({ color: '#000000', width: 2 }), stroke: new style.Stroke({ color: '#000000', width: 2 }),
text: displayText text: displayText
}); });
if (overrule) {
if (overrule.getFont()) styleText.setFont(overrule.getFont());
if (overrule.getFill()) styleText.setFill(overrule.getFill());
if (overrule.getStroke()) styleText.setStroke(overrule.getStroke());
} }
private getCentroid(feature: Feature<Geometry>) { return styleText;
return null;
} }
} }