FarmMapsLib/projects/common-map/src/fm-map/components/aol/item-vector-source/item-vector-source.componen...

192 lines
7.0 KiB
TypeScript
Raw Normal View History

2021-10-05 14:13:25 +00:00
import { Component, Host, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges, forwardRef, Inject, InjectionToken } from '@angular/core';
2020-02-12 19:38:14 +00:00
import { LayerVectorComponent, SourceVectorComponent, MapComponent } from 'ngx-openlayers';
import { ItemService,ItemTypeService,IItem, IItemType } from '@farmmaps/common';
import { Feature } from 'ol';
2021-10-05 11:46:10 +00:00
import { Point,Geometry } from 'ol/geom';
2020-02-12 19:38:14 +00:00
import { MapBrowserEvent } from 'ol';
2021-10-05 14:13:25 +00:00
import { Types } from 'ol/MapBrowserEventType';
2020-02-12 19:38:14 +00:00
import * as style from 'ol/style';
import * as color from 'ol/color';
import * as loadingstrategy from 'ol/loadingstrategy';
import * as condition from 'ol/events/condition';
import * as extent from 'ol/extent';
2021-10-05 11:46:10 +00:00
import {Vector,Cluster,Source} from 'ol/source';
2020-02-12 19:38:14 +00:00
import {Layer} from 'ol/layer';
import {GeoJSON} from 'ol/format';
import {Select} from 'ol/interaction';
2020-02-12 20:33:28 +00:00
import {IStyles} from '../../../models/style.cache';
2020-02-12 19:38:14 +00:00
import {FeatureIconService} from '../../../services/feature-icon.service';
@Component({
selector: 'fm-map-item-source-vector',
template: `<ng-content></ng-content>`,
providers: [
{ provide: SourceVectorComponent , useExisting: forwardRef(() => ItemVectorSourceComponent) }
]
})
export class ItemVectorSourceComponent extends SourceVectorComponent implements OnInit, OnChanges {
2021-10-05 11:46:10 +00:00
instance: Vector<Geometry>;
2020-02-12 19:38:14 +00:00
private _format: GeoJSON;
private _select: Select;
private _hoverSelect: Select;
private _iconScale: number = 0.05;
2021-10-05 11:46:10 +00:00
@Input() features: Array<Feature<Geometry>>;
@Input() selectedFeature: Feature<Geometry>;
2020-02-12 19:38:14 +00:00
@Input() selectedItem: IItem;
2020-02-12 20:33:28 +00:00
@Input() styles:IStyles;
2021-10-05 11:46:10 +00:00
@Output() onFeatureSelected: EventEmitter<Feature<Geometry>> = new EventEmitter<Feature<Geometry>>();
@Output() onFeatureHover: EventEmitter<Feature<Geometry>> = new EventEmitter<Feature<Geometry>>();
2020-02-12 20:33:28 +00:00
private stylesCache:IStyles = {};
2020-02-12 19:38:14 +00:00
2021-01-26 08:12:37 +00:00
constructor(@Host() private layer: LayerVectorComponent, private itemService: ItemService, private map: MapComponent, private itemTypeService: ItemTypeService,private featureIconService$:FeatureIconService) {
2020-02-12 19:38:14 +00:00
super(layer);
this._format = new GeoJSON();
}
2021-10-05 11:46:10 +00:00
geometry(feature: Feature<Geometry>) {
2020-02-12 19:38:14 +00:00
let view = this.map.instance.getView();
let resolution = view.getResolution();
var geometry = feature.getGeometry();
let e = geometry.getExtent();
//var size = Math.max((e[2] - e[0]) / resolution, (e[3] - e[1]) / resolution);
if (resolution > 12) {
geometry = new Point(extent.getCenter(e));
}
return geometry;
}
2021-10-05 11:46:10 +00:00
getSelectedStyle(feature:Feature<Geometry>):style.Style {
let key = feature.get('itemType')+"_selected";
2021-10-05 12:08:55 +00:00
let evaluatedStyle: style.Style =undefined;
2020-02-13 15:51:23 +00:00
var styleEntry = this.stylesCache[key];
if(styleEntry) {
if(typeof styleEntry === 'function') {
evaluatedStyle = styleEntry(feature);
} else {
evaluatedStyle = styleEntry;
}
} else {
2021-10-05 12:08:55 +00:00
evaluatedStyle = this.stylesCache["selected"] as style.Style;
2020-02-13 15:51:23 +00:00
}
2020-04-22 10:16:03 +00:00
if(evaluatedStyle ) {
2021-10-05 12:08:55 +00:00
evaluatedStyle .setGeometry((feature:Feature<Geometry>) => this.geometry(feature));
2020-02-14 09:21:49 +00:00
}
2021-10-05 11:46:10 +00:00
return evaluatedStyle as style.Style
}
2020-02-12 19:38:14 +00:00
ngOnInit() {
this.strategy = loadingstrategy.bbox;
this.format = new GeoJSON();
this._select = new Select({
2020-04-16 11:19:06 +00:00
style:null,
2020-02-12 19:38:14 +00:00
hitTolerance: 10,
2021-10-05 11:46:10 +00:00
layers: [this.layer.instance as Layer<Source>]
2020-02-12 19:38:14 +00:00
});
this._hoverSelect = new Select({
2021-10-05 11:46:10 +00:00
style: (feature:Feature<Geometry>) => {
return this.getSelectedStyle(feature);
2020-02-12 19:38:14 +00:00
},
hitTolerance: 10,
2021-10-05 14:13:25 +00:00
condition: (e: MapBrowserEvent<UIEvent>) => {
2020-02-12 19:38:14 +00:00
return e.type == 'pointermove';
},
2021-10-05 11:46:10 +00:00
layers: [this.layer.instance as Layer<Source>]
2020-02-12 19:38:14 +00:00
});
this.map.instance.addInteraction(this._select);
this.map.instance.addInteraction(this._hoverSelect);
this._select.on('select', (e) => {
if (e.selected.length > 0 && e.selected[0]) {
2020-10-29 18:14:06 +00:00
this.onFeatureSelected.emit(e.selected[0]);
2020-02-12 19:38:14 +00:00
} else {
2020-10-29 18:14:06 +00:00
this.onFeatureSelected.emit(null);
2020-02-12 19:38:14 +00:00
}
});
2020-04-16 11:19:06 +00:00
this._hoverSelect.on('select', (e) => {
if (e.selected.length > 0 && e.selected[0]) {
this.onFeatureHover.emit(e.selected[0]);
} else {
this.onFeatureHover.emit(null);
}
});
2020-02-12 19:38:14 +00:00
this.instance = new Vector(this);
this.host.instance.setSource(this.instance);
this.host.instance.setStyle((feature) => {
2021-08-12 19:02:55 +00:00
var itemType = feature.get('itemType');
var key = itemType + (this.selectedItem?"_I":"");
2020-02-12 20:33:28 +00:00
if (!this.stylesCache[key]) {
2021-08-12 19:02:55 +00:00
if (this.itemTypeService.itemTypes[itemType]) {
let itemTypeEntry = this.itemTypeService.itemTypes[itemType];
2021-08-12 19:40:40 +00:00
let fillColor = color.asArray(itemTypeEntry.iconColor);
2021-01-26 08:12:37 +00:00
fillColor[3] = 0;
2020-02-12 20:33:28 +00:00
this.stylesCache[key] = new style.Style({
2021-08-12 19:02:55 +00:00
image: itemTypeEntry.icon ? new style.Icon({
2020-02-12 19:38:14 +00:00
anchor: [0.5, 1],
scale: 0.05,
2021-08-12 19:02:55 +00:00
src: this.featureIconService$.getIconImageDataUrl(itemTypeEntry.icon)
2020-02-12 19:38:14 +00:00
}):null,
stroke: new style.Stroke({
color: 'red',
width: 1
}),
2021-01-26 08:12:37 +00:00
fill: new style.Fill({
color: fillColor
}),
2021-10-05 11:46:10 +00:00
geometry:(feature:Feature<Geometry>) => this.geometry(feature)
2020-02-12 19:38:14 +00:00
});
} else {
key = 'file';
}
}
2020-02-13 15:14:18 +00:00
let evaluatedStyle =null;
var styleEntry = this.stylesCache[key];
if(typeof styleEntry === 'function') {
2020-02-13 15:14:18 +00:00
evaluatedStyle = styleEntry(feature);
} else {
evaluatedStyle = styleEntry;
}
2020-02-14 09:21:49 +00:00
if(evaluatedStyle && evaluatedStyle.geometry_ == null) {
2020-02-13 15:14:18 +00:00
evaluatedStyle.setGeometry((feature) => this.geometry(feature));
}
return evaluatedStyle;
2020-02-12 19:38:14 +00:00
});
}
ngOnChanges(changes: SimpleChanges) {
if (changes["features"] && this.instance) {
this.instance.clear(true);
this._select.getFeatures().clear();
this.instance.addFeatures(changes["features"].currentValue);
}
if (changes["selectedFeature"] && this.instance) {
2020-04-16 11:19:06 +00:00
var features = this._hoverSelect.getFeatures();
2020-02-12 19:38:14 +00:00
var feature = changes["selectedFeature"].currentValue
//this.instance.clear(false);
//this.instance.addFeatures(features.getArray());
features.clear();
if (feature) {
//this.instance.removeFeature(feature);
features.push(feature)
}
}
if (changes["selectedItem"] && this.instance) {
var item = changes["selectedItem"].currentValue
if (item) {
this.map.instance.removeInteraction(this._hoverSelect);
} else {
this.map.instance.addInteraction(this._hoverSelect);
}
}
2020-02-12 20:33:28 +00:00
if (changes["styles"] && this.instance) {
let styles = changes["styles"].currentValue;
for (const key in styles) {
if (styles.hasOwnProperty(key)) {
2020-02-13 15:14:18 +00:00
this.stylesCache[key]=styles[key];
2020-02-12 20:33:28 +00:00
}
}
}
2020-02-12 19:38:14 +00:00
}
}