calculator.service.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {HttpClient} from '@angular/common/http';
  2. import {Injectable} from '@angular/core';
  3. import {Subject} from 'rxjs';
  4. import {catchError} from 'rxjs/operators';
  5. import {HsConfig} from 'hslayers-ng';
  6. import {FieldService} from './field.service';
  7. import {ZonesService} from './zones.service';
  8. export type Index = 'EVI' | 'RVI4S1';
  9. @Injectable({providedIn: 'root'})
  10. export class CalculatorService {
  11. AVAILABLE_PRODUCTS = ['EVI', 'RVI4S1'] as const;
  12. BLUR_MIN_VALUE = 0 as const;
  13. BLUR_MAX_VALUE = 5 as const;
  14. SERVICE_BASE_URL = 'https://fieldcalc.lesprojekt.cz/' as const;
  15. availableDates: Array<string>;
  16. blurValue = 0;
  17. dateRangeSelects: Subject<{date: string}> = new Subject();
  18. dateCalendarSelects: Subject<{date: string}> = new Subject();
  19. lpisLoading = false;
  20. quantileCount = 4;
  21. selectedDate;
  22. //selectedProduct;
  23. private _datesLoading: boolean;
  24. private _zonesLoading: boolean;
  25. constructor(
  26. private fieldService: FieldService,
  27. private hsConfig: HsConfig,
  28. private httpClient: HttpClient,
  29. private zonesService: ZonesService
  30. ) {
  31. this.dateRangeSelects.subscribe(({date}) => {
  32. this.selectedDate = date;
  33. });
  34. /**
  35. * When new field is selected, clear all other params
  36. */
  37. this.fieldService.fieldSelects.subscribe(() => {
  38. this.availableDates = undefined;
  39. this.selectedDate = undefined;
  40. });
  41. }
  42. noDates(): boolean {
  43. return this.availableDates === undefined;
  44. }
  45. /**
  46. * Call 'get_dates' API method
  47. */
  48. async getDates({product}: {product: Index}) {
  49. this.availableDates = undefined;
  50. this._datesLoading = true;
  51. try {
  52. const data = await this.httpClient
  53. .get<{dates: string[]}>(
  54. (this.proxyEnabled() ? this.hsConfig.proxyPrefix : '') +
  55. this.SERVICE_BASE_URL +
  56. 'get_dates?' +
  57. 'product=' +
  58. product +
  59. '&centroid=' +
  60. JSON.stringify(this.fieldService.getSelectedFieldCentroid())
  61. )
  62. .toPromise();
  63. this._datesLoading = false;
  64. console.log('data received!');
  65. console.log(data);
  66. this.availableDates = data.dates;
  67. /* Any previously selected date must be cleaned up
  68. * so it won't get sent to the API as a wrong param
  69. */
  70. this.selectedDate = undefined;
  71. } catch (err) {
  72. this._datesLoading = false;
  73. console.error('Somethin fucked up!');
  74. console.log(err);
  75. }
  76. }
  77. get datesLoading() {
  78. return this._datesLoading;
  79. }
  80. get zonesLoading() {
  81. return this._zonesLoading;
  82. }
  83. /**
  84. * Call 'get_zones' API method
  85. */
  86. async getZones({product}: {product: Index}) {
  87. this._zonesLoading = true;
  88. try {
  89. const data = await this.httpClient
  90. .post(
  91. (this.proxyEnabled() ? this.hsConfig.proxyPrefix : '') +
  92. this.SERVICE_BASE_URL +
  93. 'get_zones',
  94. {
  95. product,
  96. number_of_quantiles: this.quantileCount,
  97. blur: this.blurValue,
  98. date: this.selectedDate,
  99. format: 'geojson',
  100. geojson: this.fieldService.getSelectedFieldGeoJSON(),
  101. },
  102. {
  103. headers: {
  104. 'Content-Type': 'application/json',
  105. 'Accept': 'application/json',
  106. },
  107. }
  108. )
  109. .toPromise();
  110. console.log('data received!');
  111. console.log(data);
  112. this._zonesLoading = false;
  113. this.zonesService.updateZones(data, {quantileCount: this.quantileCount});
  114. } catch (err) {
  115. this._zonesLoading = false;
  116. console.error('Somethin fucked up!');
  117. console.log(err);
  118. }
  119. }
  120. private proxyEnabled(): boolean {
  121. return this.hsConfig.useProxy === undefined || this.hsConfig.useProxy;
  122. }
  123. }