2020-02-20 20:20:50 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-11-25 14:31:42 +00:00
|
|
|
|
|
|
|
import { Store, Action } from '@ngrx/store';
|
|
|
|
import { Effect, Actions,ofType } from '@ngrx/effects';
|
|
|
|
|
|
|
|
import { Observable , of } from 'rxjs';
|
2020-02-20 20:20:50 +00:00
|
|
|
import { withLatestFrom, switchMap, map, catchError, mergeMap } from 'rxjs/operators';
|
2019-11-25 14:31:42 +00:00
|
|
|
|
2019-12-11 09:29:47 +00:00
|
|
|
import {GeoJSON,WKT} from 'ol/format';
|
2019-11-25 14:31:42 +00:00
|
|
|
import {Feature} from 'ol';
|
2020-02-20 20:20:50 +00:00
|
|
|
import { getCenter } from 'ol/extent';
|
2019-11-25 14:31:42 +00:00
|
|
|
import {Point} 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';
|
2019-11-25 14:31:42 +00:00
|
|
|
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';
|
|
|
|
|
|
|
|
|
2019-11-25 14:31:42 +00:00
|
|
|
@Injectable()
|
|
|
|
export class MapEffects {
|
2019-12-11 09:29:47 +00:00
|
|
|
private _geojsonFormat: GeoJSON;
|
|
|
|
private _wktFormat: WKT;
|
|
|
|
|
|
|
|
private toPointFeature(updateEvent:commonActions.DeviceUpdateEvent): Feature {
|
|
|
|
var f = this._wktFormat.readFeature(updateEvent.attributes["geometry"],{
|
|
|
|
dataProjection: 'EPSG:4326',
|
|
|
|
featureProjection: 'EPSG:3857'
|
|
|
|
});
|
|
|
|
f.setId(updateEvent.itemCode);
|
2019-11-25 14:31:42 +00:00
|
|
|
var centroid = getCenter(f.getGeometry().getExtent());
|
|
|
|
f.setGeometry(new Point(centroid));
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
init$: Observable<Action> = 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=[];
|
2019-11-25 14:31:42 +00:00
|
|
|
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));
|
2019-11-25 14:31:42 +00:00
|
|
|
}
|
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,
|
|
|
|
src: this.featureIconService$.getIconImageDataUrl("fa fa-file-o")
|
|
|
|
}),
|
|
|
|
stroke: new style.Stroke({
|
|
|
|
color: 'red',
|
|
|
|
width: 1
|
|
|
|
}),
|
|
|
|
fill: new style.Fill({
|
|
|
|
color: 'rgba(0, 0, 255, 0.1)'
|
|
|
|
})
|
|
|
|
})));
|
|
|
|
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
|
|
|
|
}),
|
|
|
|
fill: new style.Fill({
|
|
|
|
color: 'rgba(0, 0, 255, 0.1)'
|
|
|
|
})
|
|
|
|
})));
|
|
|
|
|
|
|
|
return actions;
|
2019-11-25 14:31:42 +00:00
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
initBaseLayers$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(mapActions.INIT),
|
|
|
|
withLatestFrom(this.store$.select(mapReducers.selectGetProjection)),
|
|
|
|
map(([action, projection]) => new mapActions.LoadBaseLayers(projection)));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
loadBaseLayers$: Observable<Action> = 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))));
|
|
|
|
}));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
initRootItems$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(commonActions.INITROOTSUCCESS),
|
|
|
|
map((action) => new mapActions.Init()
|
|
|
|
));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
startSearch$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(mapActions.STARTSEARCH),
|
|
|
|
switchMap((action: mapActions.StartSearch) => {
|
|
|
|
console.debug("Start search");
|
|
|
|
var startDate = action.queryState.startDate;
|
|
|
|
var endDate = action.queryState.endDate;
|
|
|
|
var newAction:Observable<Action>;
|
|
|
|
if (action.queryState.itemCode || action.queryState.parentCode || action.queryState.itemType || action.queryState.query || action.queryState.tags) {
|
|
|
|
newAction= this.itemService$.getFeatures(action.queryState.bbox, "EPSG:3857", action.queryState.query, action.queryState.tags, startDate, endDate, action.queryState.itemType, action.queryState.parentCode).pipe(
|
|
|
|
switchMap((features: any) => {
|
|
|
|
for (let f of features.features) {
|
|
|
|
if (f.properties && f.properties["code"]) {
|
|
|
|
f.id = f.properties["code"];
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 09:29:47 +00:00
|
|
|
return of(new mapActions.StartSearchSuccess(this._geojsonFormat.readFeatures(features), action.queryState));
|
2019-11-25 14:31:42 +00:00
|
|
|
}
|
|
|
|
),
|
|
|
|
catchError(error => of(new commonActions.Fail(error))));
|
|
|
|
} else {
|
|
|
|
newAction= of(new commonActions.Escape(true,false));
|
|
|
|
}
|
|
|
|
return newAction;
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
startSearchSucces$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(mapActions.STARTSEARCHSUCCESS),
|
|
|
|
mergeMap((action: mapActions.StartSearchSuccess) => {
|
2020-01-07 21:27:46 +00:00
|
|
|
return [new commonActions.SetMenuVisible(false)];
|
2019-11-25 14:31:42 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
selectItem$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(mapActions.SELECTITEM),
|
|
|
|
withLatestFrom(this.store$.select(mapReducers.selectGetSelectedItem)),
|
|
|
|
switchMap(([action, selectedItem]) => {
|
|
|
|
let a = action as mapActions.SelectItem;
|
|
|
|
let itemCode = selectedItem ? selectedItem.code : "";
|
|
|
|
if (a.itemCode != itemCode) {
|
|
|
|
return this.itemService$.getItem(a.itemCode).pipe(
|
|
|
|
map((item: IItem) => new mapActions.SelectItemSuccess(item)),
|
|
|
|
catchError(error => of(new commonActions.Fail(error))))
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
selectItemSuccess$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(mapActions.SELECTITEMSUCCESS),
|
|
|
|
switchMap((action:mapActions.SelectItemSuccess) => {
|
|
|
|
return this.itemService$.getFeature(action.item.code, "EPSG:3857").pipe(
|
2019-12-11 09:29:47 +00:00
|
|
|
map((feature: any) => {
|
|
|
|
let f = this._geojsonFormat.readFeature(feature);
|
|
|
|
f.setId(action.item.code);
|
|
|
|
return new mapActions.AddFeatureSuccess(f );
|
|
|
|
}),
|
2019-11-25 14:31:42 +00:00
|
|
|
catchError(error => of(new commonActions.Fail(error))));
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2020-02-27 16:59:00 +00:00
|
|
|
@Effect()
|
|
|
|
selectItemSuccessTemporal$: Observable<Action> = this.actions$.pipe(
|
|
|
|
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-02-27 20:41:52 +00:00
|
|
|
map(items => items.sort((a, b) => b.dataDate.getTime() - a.dataDate.getTime())),
|
2020-02-27 16:59:00 +00:00
|
|
|
map((items) => new mapActions.SelectTemporalItemsSuccess(items )),
|
|
|
|
catchError(error => of(new commonActions.Fail(error))));
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2019-11-25 14:31:42 +00:00
|
|
|
@Effect()
|
|
|
|
uploadedItemClick$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(commonActions.UPLOADEDFILECLICK),
|
|
|
|
switchMap((action: commonActions.UploadedFileClick) => of(new mapActions.DoQuery(tassign(mapReducers.initialState.query, {itemCode:action.itemCode})))
|
|
|
|
));
|
|
|
|
|
|
|
|
//@Effect()
|
|
|
|
//itemAdded$: Observable<Action> = this.actions$.pipe(
|
|
|
|
// ofType(commonActions.ITEMADDEDEVENT),
|
|
|
|
// withLatestFrom(this.store$.select(mapReducers.selectGetParentCode)),
|
|
|
|
// mergeMap(([action, parentCode]) => {
|
|
|
|
// let itemAddedAction = action as commonActions.ItemAddedEvent;
|
|
|
|
// if (parentCode && itemAddedAction.attributes["parentCode"] == parentCode) {
|
|
|
|
// return this.itemService$.getFeature(itemAddedAction.itemCode,"EPSG:3857").pipe(
|
|
|
|
// map((feature: Feature) => new mapActions.AddFeatureSuccess(this.toPointFeature(feature))),
|
|
|
|
// catchError(error => of(new commonActions.Fail(error))))
|
|
|
|
// } else
|
|
|
|
// return [
|
|
|
|
// ];
|
|
|
|
// }));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
featureUpdate$: Observable<Action> = this.actions$.pipe(
|
2019-12-11 09:29:47 +00:00
|
|
|
ofType(commonActions.DEVICEUPDATEEVENT),
|
2019-11-25 14:31:42 +00:00
|
|
|
withLatestFrom(this.store$.select(mapReducers.selectGetFeatures)),
|
|
|
|
mergeMap(([action, features]) => {
|
2019-12-11 09:29:47 +00:00
|
|
|
let deviceUpdateEventAction = action as commonActions.DeviceUpdateEvent;
|
2019-11-25 14:31:42 +00:00
|
|
|
var feature: Feature = null;
|
|
|
|
for (let f of features) {
|
2019-12-11 09:29:47 +00:00
|
|
|
if (f.getId() == deviceUpdateEventAction.itemCode) {
|
2019-11-25 14:31:42 +00:00
|
|
|
feature = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (feature) {
|
2019-12-11 09:29:47 +00:00
|
|
|
return of(new mapActions.UpdateFeatureSuccess(this.toPointFeature(deviceUpdateEventAction)));
|
2019-11-25 14:31:42 +00:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
itemUpdate$: Observable<Action> = 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(
|
|
|
|
map((item: IItem) => new mapActions.SelectItemSuccess(item)),
|
|
|
|
catchError(error => of(new commonActions.Fail(error))));
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
setQueryState$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(mapActions.SETQUERYSTATE),
|
|
|
|
switchMap((action: mapActions.SetQueryState) => {
|
|
|
|
var newAction:Action;
|
|
|
|
if (action.queryState.itemCode && action.queryState.itemCode != "") {
|
|
|
|
newAction= new mapActions.SelectItem(action.queryState.itemCode);
|
|
|
|
} else {
|
|
|
|
newAction= new mapActions.StartSearch(action.queryState);
|
|
|
|
}
|
|
|
|
return of(newAction);
|
|
|
|
}));
|
|
|
|
|
|
|
|
@Effect()
|
|
|
|
setState$: Observable<Action> = this.actions$.pipe(
|
|
|
|
ofType(mapActions.SETSTATE),
|
|
|
|
switchMap((action: mapActions.SetState) => {
|
|
|
|
var newAction:Action;
|
|
|
|
if (action.queryState.itemCode && action.queryState.itemCode != "") {
|
|
|
|
newAction= new mapActions.SelectItem(action.queryState.itemCode);
|
|
|
|
} else {
|
|
|
|
newAction= new mapActions.StartSearch(action.queryState);
|
|
|
|
}
|
|
|
|
return of(newAction);
|
|
|
|
}));
|
|
|
|
|
2020-02-12 19:38:14 +00:00
|
|
|
constructor(private actions$: Actions, private store$: Store<mapReducers.State>, private folderService$: FolderService, private itemService$: ItemService,private featureIconService$:FeatureIconService) {
|
2019-12-11 09:29:47 +00:00
|
|
|
this._geojsonFormat = new GeoJSON();
|
|
|
|
this._wktFormat = new WKT();
|
2019-11-25 14:31:42 +00:00
|
|
|
}
|
|
|
|
}
|