FarmMapsLib/projects/common-map/src/fm-map/effects/map.effects.ts

410 lines
16 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
2021-07-27 14:26:57 +00:00
import { Store, Action,createFeatureSelector } from '@ngrx/store';
import { ROUTER_NAVIGATED, RouterReducerState } from '@ngrx/router-store';
import * as fromRouter from '@ngrx/router-store';
import { createEffect, Actions,ofType } from '@ngrx/effects';
2021-07-27 14:26:57 +00:00
import { EMPTY, Observable , of} from 'rxjs';
import { withLatestFrom, switchMap, map, catchError, mergeMap } from 'rxjs/operators';
2019-12-11 09:29:47 +00:00
import {GeoJSON,WKT} from 'ol/format';
import {Feature} from 'ol';
2020-04-21 11:22:54 +00:00
import { getCenter,createEmpty,extend } from 'ol/extent';
2021-10-05 11:46:10 +00:00
import {Point,Geometry} from 'ol/geom'
import * as mapActions from '../actions/map.actions';
import * as mapReducers from '../reducers/map.reducer';
import {commonReducers} from '@farmmaps/common';
import {commonActions} from '@farmmaps/common';
2020-02-12 19:38:14 +00:00
import { IItem } from '@farmmaps/common';
import { FolderService, ItemService } from '@farmmaps/common';
import { tassign } from 'tassign';
2020-02-12 19:38:14 +00:00
import {FeatureIconService} from '../services/feature-icon.service';
import * as style from 'ol/style';
2020-04-22 10:34:07 +00:00
import { ItemTypeService,IQueryState } from '@farmmaps/common';
2021-03-06 13:06:31 +00:00
import { TemporalItemLayer } from '../models/item.layer'
2020-03-22 19:08:53 +00:00
2021-07-27 14:26:57 +00:00
export const getRouterState = createFeatureSelector<RouterReducerState>('router');
export const {
selectCurrentRoute, // select the current route
selectQueryParams, // select the current route query params
selectQueryParam, // factory function to select a query param
selectRouteParams, // select the current route params
selectRouteParam, // factory function to select a route param
selectRouteData, // select the current route data
selectUrl, // select the current url
} = fromRouter.getSelectors(getRouterState);
2020-02-12 19:38:14 +00:00
@Injectable()
export class MapEffects {
2019-12-11 09:29:47 +00:00
private _geojsonFormat: GeoJSON;
private _wktFormat: WKT;
2021-07-28 15:12:54 +00:00
private overrideSelectedItemLayer: boolean = false;
2019-12-11 09:29:47 +00:00
2021-10-05 11:46:10 +00:00
private updateFeatureGeometry(feature:Feature<Geometry>, updateEvent:commonActions.DeviceUpdateEvent): Feature<Geometry> {
2021-08-12 15:13:40 +00:00
let newFeature = feature.clone();
2019-12-11 09:29:47 +00:00
var f = this._wktFormat.readFeature(updateEvent.attributes["geometry"],{
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
var centroid = getCenter(f.getGeometry().getExtent());
2021-08-12 15:13:40 +00:00
newFeature.setId(feature.getId());
newFeature.setGeometry(new Point(centroid));
return newFeature;
}
2021-07-27 14:26:57 +00:00
init$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.INIT),
withLatestFrom(this.store$.select(commonReducers.selectGetRootItems)),
switchMap(([action, rootItems]) => {
2020-02-12 19:38:14 +00:00
let actions=[];
for (let rootItem of rootItems) {
2020-02-12 19:38:14 +00:00
if (rootItem.itemType == "UPLOADS_FOLDER") actions.push(new mapActions.SetParent(rootItem.code));
}
2020-02-12 19:38:14 +00:00
// initialize default feature styles
actions.push(new mapActions.SetStyle('file',new style.Style({
image: new style.Icon({
anchor: [0.5, 1],
scale: 0.05,
2021-01-24 08:09:43 +00:00
src: this.featureIconService$.getIconImageDataUrl("fal fa-file")
}),
2020-02-12 19:38:14 +00:00
stroke: new style.Stroke({
color: 'red',
width: 1
2021-01-26 08:12:37 +00:00
}),
fill: new style.Fill({
color: 'rgba(0, 0, 0,0)'
2020-02-12 19:38:14 +00:00
})
})));
actions.push(new mapActions.SetStyle('selected',new style.Style({
image: new style.Icon({
anchor: [0.5, 1],
scale: 0.08,
src: this.featureIconService$.getIconImageDataUrl(null)
}),
stroke: new style.Stroke({
color: 'red',
width: 3
2021-01-26 08:12:37 +00:00
}),
fill: new style.Fill({
color: 'rgba(0, 0, 0, 0)'
2020-02-12 19:38:14 +00:00
})
})));
2020-02-12 19:38:14 +00:00
return actions;
}
2021-07-27 14:26:57 +00:00
)));
2021-07-27 14:26:57 +00:00
initBaseLayers$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.INIT),
withLatestFrom(this.store$.select(mapReducers.selectGetProjection)),
2021-07-27 14:26:57 +00:00
map(([action, projection]) => new mapActions.LoadBaseLayers(projection)))
);
2021-07-27 14:26:57 +00:00
loadBaseLayers$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.LOADBASELAYERS),
switchMap((action: mapActions.LoadBaseLayers) => {
return this.itemService$.getItemList("vnd.farmmaps.itemtype.layer", { "isBaseLayer": true }).pipe(
map((items: IItem[]) => new mapActions.LoadBaseLayersSuccess(items)),
catchError(error => of(new commonActions.Fail(error))));
2021-07-27 14:26:57 +00:00
})));
2021-07-27 14:26:57 +00:00
startSearch$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.STARTSEARCH),
2020-10-28 12:31:12 +00:00
switchMap((action) => {
2020-04-22 06:59:04 +00:00
let a = action as mapActions.StartSearch;
var startDate = a.queryState.startDate;
var endDate = a.queryState.endDate;
var newAction:Observable<Action>;
2020-04-22 06:59:04 +00:00
if (a.queryState.itemCode || a.queryState.parentCode || a.queryState.itemType || a.queryState.query || a.queryState.tags) {
newAction= this.itemService$.getFeatures(a.queryState.bbox, "EPSG:3857", a.queryState.query, a.queryState.tags, startDate, endDate, a.queryState.itemType, a.queryState.parentCode, a.queryState.dataFilter, a.queryState.level).pipe(
switchMap((features: any) => {
for (let f of features.features) {
if (f.properties && f.properties["code"]) {
f.id = f.properties["code"];
}
}
2020-10-28 12:31:12 +00:00
return of(new mapActions.StartSearchSuccess(this._geojsonFormat.readFeatures(features), a.queryState));
}
),
catchError(error => of(new commonActions.Fail(error))));
} else {
return [];
}
return newAction;
2021-07-27 14:26:57 +00:00
})));
2021-07-27 14:26:57 +00:00
zoomToExtent$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.STARTSEARCHSUCCESS),
mergeMap((action: mapActions.StartSearchSuccess) => {
2020-04-21 11:22:54 +00:00
let actions =[];
actions.push(new commonActions.SetMenuVisible(false));
let extent = createEmpty();
2021-07-27 07:31:14 +00:00
if (!action.query.bboxFilter) {
2020-04-21 11:22:54 +00:00
if (extent) {
for (let f of action.features) {
2021-10-05 11:46:10 +00:00
extend(extent, (f as Feature<Geometry>).getGeometry().getExtent());
2020-04-21 11:22:54 +00:00
}
2020-09-29 12:56:23 +00:00
if(action.features && action.features.length >0) {
actions.push(new mapActions.SetExtent(extent));
}
}
2020-04-21 11:22:54 +00:00
}
return actions;
2021-07-27 14:26:57 +00:00
})));
2020-04-21 11:22:54 +00:00
2021-07-27 14:26:57 +00:00
zoomToExtent2$ = createEffect(() => this.actions$.pipe(
2020-12-11 12:35:51 +00:00
ofType(mapActions.SETFEATURES),
2021-04-19 06:43:20 +00:00
switchMap((action: mapActions.SetFeatures) => {
2020-12-11 12:35:51 +00:00
let extent = createEmpty();
2021-04-19 06:43:20 +00:00
if (extent) {
for (let f of action.features) {
2021-10-05 11:46:10 +00:00
extend(extent, (f as Feature<Geometry>).getGeometry().getExtent());
2021-04-19 06:43:20 +00:00
}
if(action.features.length>0) return of(new mapActions.SetExtent(extent));
}
2021-04-19 06:43:20 +00:00
return EMPTY;
2021-07-27 14:26:57 +00:00
})));
2020-12-11 12:35:51 +00:00
2021-07-27 14:26:57 +00:00
hideMenu$ = createEffect(() => this.actions$.pipe(
2020-04-21 11:22:54 +00:00
ofType(mapActions.STARTSEARCHSUCCESS),
mergeMap((action: mapActions.StartSearchSuccess) => {
return of(new commonActions.SetMenuVisible(false));
2021-07-27 14:26:57 +00:00
})));
2021-07-27 14:26:57 +00:00
selectItem$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SELECTITEM),
withLatestFrom(this.store$.select(mapReducers.selectGetSelectedItem)),
2020-10-28 12:31:12 +00:00
switchMap(([action, selectedItem]) => {
let a = action as mapActions.SelectItem;
let itemCode = selectedItem ? selectedItem.code : "";
2020-10-28 12:31:12 +00:00
if (a.itemCode != itemCode) {
return this.itemService$.getItem(a.itemCode).pipe(
switchMap(child => {
return this.itemService$.getItem(child.parentCode)
.pipe(map(parent => {
return {child, parent};
}),catchError(() => { let parent:IItem = null;return of({child,parent})})
);
}),
map(data => new mapActions.SelectItemSuccess(data.child, data.parent)),
catchError(error => of(new commonActions.Fail(error))))
} else {
return [];
}
}
2021-07-27 14:26:57 +00:00
)));
2021-07-27 14:26:57 +00:00
selectItemSuccessSetLayer$ = createEffect(() => this.actions$.pipe(
2020-12-16 17:04:39 +00:00
ofType(mapActions.SELECTITEMSUCCESS),
map((action:mapActions.SelectItemSuccess) =>
2020-12-16 17:04:39 +00:00
new mapActions.SetSelectedItemLayer(action.item)
)
2021-07-27 14:26:57 +00:00
));
2020-12-16 17:04:39 +00:00
2021-07-27 14:26:57 +00:00
selectItemSuccess$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SELECTITEMSUCCESS),
switchMap((action:mapActions.SelectItemSuccess) => {
2021-07-27 14:26:57 +00:00
if(!this.overrideSelectedItemLayer) {
2021-07-27 07:16:58 +00:00
return this.itemService$.getFeature(action.item.code, "EPSG:3857").pipe(
map((feature: any) => {
let f = this._geojsonFormat.readFeature(feature);
f.setId(action.item.code);
return new mapActions.AddFeatureSuccess(f );
}),
catchError(error => of(new commonActions.Fail(error))));
2021-07-27 07:16:58 +00:00
} else {
return EMPTY;
}
}
2021-07-27 14:26:57 +00:00
)));
2021-07-27 14:26:57 +00:00
selectItemSuccessTemporal$ = createEffect(() => this.actions$.pipe(
2020-02-27 16:59:00 +00:00
ofType(mapActions.SELECTITEMSUCCESS),
switchMap((action:mapActions.SelectItemSuccess) => {
if(action.item.itemType == "vnd.farmmaps.itemtype.temporal") {
return this.itemService$.getChildItemList(action.item.code,null).pipe(
2020-03-03 09:21:24 +00:00
map(items => new mapActions.SelectTemporalItemsSuccess(
items.sort((a, b) =>
2020-07-07 07:24:45 +00:00
-(Date.parse(b.dataDate) - Date.parse(a.dataDate))
2020-03-03 09:21:24 +00:00
)
)),
2020-02-27 16:59:00 +00:00
catchError(error => of(new commonActions.Fail(error))));
} else {
return [];
}
2020-02-27 16:59:00 +00:00
}
2021-07-27 14:26:57 +00:00
)));
2020-02-27 16:59:00 +00:00
2021-07-27 14:26:57 +00:00
uploadedItemClick$ = createEffect(() => this.actions$.pipe(
ofType(commonActions.UPLOADEDFILECLICK),
2021-07-29 10:52:26 +00:00
switchMap((action: commonActions.UploadedFileClick) => of(new mapActions.DoQuery(tassign(mapReducers.initialState.query.querystate, {itemCode:action.itemCode})))
2021-07-27 14:26:57 +00:00
)));
2021-07-27 14:26:57 +00:00
featureUpdate$ = createEffect(() => this.actions$.pipe(
2019-12-11 09:29:47 +00:00
ofType(commonActions.DEVICEUPDATEEVENT),
withLatestFrom(this.store$.select(mapReducers.selectGetFeatures)),
mergeMap(([action, features]) => {
2019-12-11 09:29:47 +00:00
let deviceUpdateEventAction = action as commonActions.DeviceUpdateEvent;
2021-10-05 11:46:10 +00:00
var feature: Feature<Geometry> = null;
for (let f of features) {
2019-12-11 09:29:47 +00:00
if (f.getId() == deviceUpdateEventAction.itemCode) {
feature = f;
break;
}
}
if (feature) {
2021-08-12 15:13:40 +00:00
return of(new mapActions.UpdateFeatureSuccess(this.updateFeatureGeometry(feature,deviceUpdateEventAction)));
} else {
return [];
}
2021-07-27 14:26:57 +00:00
})));
2021-07-27 14:26:57 +00:00
itemUpdate$ = createEffect(() => this.actions$.pipe(
ofType(commonActions.ITEMCHANGEDEVENT),
withLatestFrom(this.store$.select(mapReducers.selectGetSelectedItem)),
mergeMap(([action, selectedItem]) => {
let itemChangedAction = action as commonActions.ItemChangedEvent;
if (selectedItem && selectedItem.code == itemChangedAction.itemCode) {
return this.itemService$.getItem(itemChangedAction.itemCode).pipe(
switchMap(child => {
return this.itemService$.getItem(child.parentCode)
.pipe(map(parent => {
return {child, parent};
})
);
}),
map(data => new mapActions.SelectItemSuccess(data.child, data.parent)),
catchError(error => of(new commonActions.Fail(error))));
} else {
return [];
}
2021-07-27 14:26:57 +00:00
})));
2020-04-21 10:31:20 +00:00
getActionFromQueryState(queryState:IQueryState, inSearch:boolean):Observable<Action>|[] {
2020-04-22 07:25:10 +00:00
if(!inSearch && (queryState.itemType || queryState.parentCode || queryState.itemCode || queryState.query || queryState.tags)) {
var newAction:Action;
if (queryState.itemCode && queryState.itemCode != "") {
newAction= new mapActions.SelectItem(queryState.itemCode);
2020-04-21 10:31:20 +00:00
} else {
newAction= new mapActions.StartSearch(queryState);
}
2020-04-21 10:31:20 +00:00
} else {
2020-10-28 12:31:12 +00:00
newAction = new mapActions.Clear();
2020-04-21 10:31:20 +00:00
}
2020-10-28 12:31:12 +00:00
return of(newAction);
}
2020-04-21 10:31:20 +00:00
2021-07-27 14:26:57 +00:00
getLayerValue$ = createEffect(() => this.actions$.pipe(
2021-03-05 16:19:30 +00:00
ofType(mapActions.GETLAYERVALUE),
2021-03-05 17:41:09 +00:00
mergeMap((action:mapActions.GetLayerValue) => {
2021-03-05 16:19:30 +00:00
var l = action.itemLayer.item.data["layers"][action.itemLayer.layerIndex];
var scale = l.scale?l.scale:1;
return this.itemService$.getLayerValue(action.itemLayer.item.code,action.itemLayer.layerIndex,action.x,action.y).pipe(
2021-03-05 17:41:09 +00:00
mergeMap((v: number) => {
2021-03-05 16:19:30 +00:00
let a=[];
2021-03-05 18:03:50 +00:00
if(v !== null) {
2021-03-05 17:41:09 +00:00
if(l.renderer && l.renderer.colorMap && l.renderer.colorMap.colormapType == "manual") {
l.renderer.colorMap.entries.forEach((e) => {
2021-08-19 13:25:22 +00:00
if(e.value == v && e.label) {
2021-03-05 17:41:09 +00:00
v=e.label;
return;
}
});
a.push(new mapActions.GetLayerValueSuccess({date:action.itemLayer.item.dataDate,value:v,layerName:l.name,quantity:"",unit:l.unit}));
2021-03-05 17:41:09 +00:00
} else {
a.push(new mapActions.GetLayerValueSuccess({date:action.itemLayer.item.dataDate,value:v*scale,layerName:l.name,quantity:l.quantity,unit:l.unit}));
2021-03-05 17:41:09 +00:00
}
}
2021-03-05 16:19:30 +00:00
return a;
}))
}
2021-07-27 14:26:57 +00:00
)));
2021-03-05 16:19:30 +00:00
2021-07-27 14:26:57 +00:00
updateLayerValuesOnLayerAddedOrRemoved$ = createEffect(() => this.actions$.pipe(
2021-07-30 12:06:31 +00:00
ofType(mapActions.ADDLAYER,mapActions.REMOVELAYER,mapActions.SELECTITEMSUCCESS,mapActions.SELECTTEMPORALITEMSSUCCESS, mapActions.NEXTTEMPORAL,mapActions.PREVIOUSTEMPORAL,mapActions.TOGGLELAYERVALUESENABLED,mapActions.SETLAYERINDEX,mapActions.SETSELECTEDITEMLAYER),
2021-03-05 16:19:30 +00:00
withLatestFrom(this.store$.select(mapReducers.selectGetLayerValuesX)),
withLatestFrom(this.store$.select(mapReducers.selectGetLayerValuesY)),
map(([[action,x],y]) => new mapActions.SetLayerValuesLocation(x,y))
2021-07-27 14:26:57 +00:00
));
2021-03-05 16:19:30 +00:00
2021-07-27 14:26:57 +00:00
getLayerValues$ = createEffect(() => this.actions$.pipe(
2021-03-05 16:19:30 +00:00
ofType(mapActions.SETLAYERVALUESLOCATION),
withLatestFrom(this.store$.select(mapReducers.selectGetSelectedItemLayer)),
withLatestFrom(this.store$.select(mapReducers.selectGetLayerValuesEnabled)),
withLatestFrom(this.store$.select(mapReducers.selectGetOverlayLayers)),
2021-03-05 17:41:09 +00:00
mergeMap(([[[action, selected], enabled],overlayLayers]) => {
let layers = [];
2021-03-06 13:06:31 +00:00
if(selected) {
2021-03-06 13:29:01 +00:00
if(selected && (selected as TemporalItemLayer).selectedItemLayer ) {
2021-03-06 13:06:31 +00:00
selected=(selected as TemporalItemLayer).selectedItemLayer;
}
layers.push(selected);
}
2021-03-05 16:19:30 +00:00
overlayLayers.forEach((ol) => {
if(ol!=selected) layers.push(ol);
});
let a = action as mapActions.SetLayerValuesLocation;
let actions = [];
if(enabled) {
2021-03-05 16:19:30 +00:00
layers.forEach((ol) => {
if("vnd.farmmaps.itemtype.shape.processed,vnd.farmmaps.itemtype.geotiff.processed".indexOf(ol.item.itemType)>=0) {
actions.push(new mapActions.GetLayerValue(ol,a.x,a.y));
}
});
}
2021-03-05 16:19:30 +00:00
return actions;
2021-07-27 14:26:57 +00:00
})));
2021-03-05 16:19:30 +00:00
2021-07-27 14:26:57 +00:00
setState$ = createEffect(() => this.actions$.pipe(
ofType(mapActions.SETSTATE),
withLatestFrom(this.store$.select(mapReducers.selectGetInSearch)),
switchMap(([action,inSearch]) => {
2020-04-21 10:31:20 +00:00
let a = action as mapActions.SetState;
return this.getActionFromQueryState(a.queryState,inSearch);
2021-07-27 14:26:57 +00:00
})));
2021-07-27 14:26:57 +00:00
escape$ = createEffect(() => this.actions$.pipe(
2020-10-28 12:31:12 +00:00
ofType(commonActions.ESCAPE),
switchMap((action) => {
let a = action as commonActions.Escape;
if(a.escapeKey) {
return of(new mapActions.Clear());
} else {
return EMPTY;
}
2021-07-27 14:26:57 +00:00
})));
setOverride$ = createEffect(() => this.actions$.pipe(
ofType(ROUTER_NAVIGATED),
switchMap(() => this.store$.select(selectRouteData as any)),
switchMap((data: any) => {
if(data && data["fm-map-map"]) {
let params = data["fm-map-map"];
this.overrideSelectedItemLayer = params["overrideSelectedItemlayer"] ? params["overrideSelectedItemlayer"] : false;
2021-07-28 15:12:54 +00:00
} else {
this.overrideSelectedItemLayer = false;
2021-07-27 14:26:57 +00:00
}
return [];
})
));
2020-10-28 12:31:12 +00:00
2021-07-27 14:26:57 +00:00
constructor(private actions$: Actions, private store$: Store<mapReducers.State>, private folderService$: FolderService, private itemService$: ItemService,private featureIconService$:FeatureIconService,private itemTypeService$:ItemTypeService) {
2019-12-11 09:29:47 +00:00
this._geojsonFormat = new GeoJSON();
this._wktFormat = new WKT();
}
}