63 lines
2.6 KiB
TypeScript
63 lines
2.6 KiB
TypeScript
import { Location } from '@angular/common';
|
|
import { Component, Inject, Injectable, OnInit } from '@angular/core';
|
|
import { Router } from '@angular/router';
|
|
import { FolderService, IItem, IListItem, ItemService, ItemTypeService, commonReducers } from '@farmmaps/common';
|
|
import { Store } from '@ngrx/store';
|
|
import { GeoJSON } from 'ol/format';
|
|
import { getArea } from 'ol/sphere';
|
|
import { Observable, of } from 'rxjs';
|
|
import { combineLatest, switchMap } from 'rxjs/operators';
|
|
import * as mapReducers from '../../reducers/map.reducer';
|
|
import { ForItemType } from '../for-item/for-itemtype.decorator';
|
|
import { AbstractSelectedItemComponent } from '../selected-item/selected-item.component';
|
|
|
|
|
|
@ForItemType("vnd.farmmaps.itemtype.cropfield")
|
|
@Injectable()
|
|
@Component({
|
|
selector: 'fm-map-selected-item-cropfield',
|
|
templateUrl: './selected-item-cropfield.component.html',
|
|
styleUrls: ['./selected-item-cropfield.component.scss']
|
|
})
|
|
export class SelectedItemCropfieldComponent extends AbstractSelectedItemComponent implements OnInit{
|
|
|
|
public items: Observable<IListItem[]>;
|
|
|
|
constructor(store: Store<mapReducers.State | commonReducers.State>, @Inject('itemTypeService') itemTypeService: ItemTypeService, location: Location, router: Router, private itemService$: ItemService, private folderService$: FolderService) {
|
|
super(store, itemTypeService,location,router);
|
|
}
|
|
|
|
areaInHa(item:IItem):number {
|
|
if(!item) return 0;
|
|
// get area from faeture if 0 calculate from polygon
|
|
const a = item.data.area;
|
|
if(a) return a;
|
|
const format = new GeoJSON();
|
|
const polygon = format.readGeometry(item.geometry);
|
|
return getArea(polygon,{projection:"EPSG:4326"}) / 10000;
|
|
}
|
|
|
|
ngOnInit() {
|
|
const childItems = this.folderService$.getItems(this.item.code, 0, 1000);
|
|
const atLocationItems = this.itemService$.getItemList(null,null,null,this.item.code,true);
|
|
this.items = childItems.pipe(
|
|
combineLatest(atLocationItems),
|
|
switchMap(([ci, ali]) => {
|
|
const retVal:IListItem[] = [];
|
|
const codes = {};
|
|
ci.forEach((listItem) => {
|
|
retVal.push(listItem);
|
|
codes[listItem.code]=listItem;
|
|
});
|
|
ali.forEach((atlocationitem) => {
|
|
const listItem = atlocationitem as IListItem;
|
|
const allowedItemTypes = "vnd.farmmaps.itemtype.device.senml"; // this is a hack
|
|
if(!codes[listItem.code] && allowedItemTypes.indexOf(listItem.itemType) >= 0) {
|
|
retVal.push(listItem);
|
|
}
|
|
});
|
|
return of(retVal);
|
|
}));
|
|
}
|
|
}
|