| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import {HttpClient} from '@angular/common/http';
- import {Injectable} from '@angular/core';
- import {Subject} from 'rxjs';
- import {catchError} from 'rxjs/operators';
- import {HsConfig} from 'hslayers-ng';
- import {FieldService} from './field.service';
- import {ZonesService} from './zones.service';
- export type Index = 'EVI' | 'RVI4S1';
- @Injectable({providedIn: 'root'})
- export class CalculatorService {
- AVAILABLE_PRODUCTS = ['EVI', 'RVI4S1'] as const;
- BLUR_MIN_VALUE = 0 as const;
- BLUR_MAX_VALUE = 5 as const;
- SERVICE_BASE_URL = 'https://fieldcalc.lesprojekt.cz/' as const;
- availableDates: Array<string>;
- blurValue = 0;
- dateRangeSelects: Subject<{date: string}> = new Subject();
- dateCalendarSelects: Subject<{date: string}> = new Subject();
- lpisLoading = false;
- quantileCount = 4;
- selectedDate;
- //selectedProduct;
- private _datesLoading: boolean;
- private _zonesLoading: boolean;
- constructor(
- private fieldService: FieldService,
- private hsConfig: HsConfig,
- private httpClient: HttpClient,
- private zonesService: ZonesService
- ) {
- this.dateRangeSelects.subscribe(({date}) => {
- this.selectedDate = date;
- });
- /**
- * When new field is selected, clear all other params
- */
- this.fieldService.fieldSelects.subscribe(() => {
- this.availableDates = undefined;
- this.selectedDate = undefined;
- });
- }
- noDates(): boolean {
- return this.availableDates === undefined;
- }
- /**
- * Call 'get_dates' API method
- */
- async getDates({product}: {product: Index}) {
- this.availableDates = undefined;
- this._datesLoading = true;
- try {
- const data = await this.httpClient
- .get<{dates: string[]}>(
- (this.proxyEnabled() ? this.hsConfig.proxyPrefix : '') +
- this.SERVICE_BASE_URL +
- 'get_dates?' +
- 'product=' +
- product +
- '¢roid=' +
- JSON.stringify(this.fieldService.getSelectedFieldCentroid())
- )
- .toPromise();
- this._datesLoading = false;
- console.log('data received!');
- console.log(data);
- this.availableDates = data.dates;
- /* Any previously selected date must be cleaned up
- * so it won't get sent to the API as a wrong param
- */
- this.selectedDate = undefined;
- } catch (err) {
- this._datesLoading = false;
- console.error('Somethin fucked up!');
- console.log(err);
- }
- }
- get datesLoading() {
- return this._datesLoading;
- }
- get zonesLoading() {
- return this._zonesLoading;
- }
- /**
- * Call 'get_zones' API method
- */
- async getZones({product}: {product: Index}) {
- this._zonesLoading = true;
- try {
- const data = await this.httpClient
- .post(
- (this.proxyEnabled() ? this.hsConfig.proxyPrefix : '') +
- this.SERVICE_BASE_URL +
- 'get_zones',
- {
- product,
- number_of_quantiles: this.quantileCount,
- blur: this.blurValue,
- date: this.selectedDate,
- format: 'geojson',
- geojson: this.fieldService.getSelectedFieldGeoJSON(),
- },
- {
- headers: {
- 'Content-Type': 'application/json',
- 'Accept': 'application/json',
- },
- }
- )
- .toPromise();
- console.log('data received!');
- console.log(data);
- this._zonesLoading = false;
- this.zonesService.updateZones(data, {quantileCount: this.quantileCount});
- } catch (err) {
- this._zonesLoading = false;
- console.error('Somethin fucked up!');
- console.log(err);
- }
- }
- private proxyEnabled(): boolean {
- return this.hsConfig.useProxy === undefined || this.hsConfig.useProxy;
- }
- }
|