import {HttpClient} from '@angular/common/http'; import {Injectable} from '@angular/core'; import {HsUtilsService} from 'hslayers-ng/components/utils/utils.service'; // import attractivity from '../Attractivity.json'; import nuts from '../nuts'; @Injectable({providedIn: 'root'}) export class AdjusterService { nutsCodeRecordRelations = {}; factors: any = [ { name: 'Natural', column: 'N_index', weight: 1, }, { name: 'Social & Human', column: 'S_index', weight: 1, }, { name: 'Anthropic', column: 'A_index', weight: 1, }, { name: 'Economical', column: 'E_index', weight: 1, }, { name: 'Cultural', column: 'C_index', weight: 1, }, { name: 'Institutional', column: 'I_index', weight: 1, }, ]; constructor(public HsUtilsService: HsUtilsService, public $http: HttpClient) { this.$http .get('https://publish.lesprojekt.cz/nodejs/datasets') .toPromise() .then((response: any) => { this.factors = response.data.map((dataset) => { return {factor: dataset.Factor, weight: 1, datasets: []}; }); this.factors = HsUtilsService.removeDuplicates(this.factors, 'factor'); this.factors.forEach((factor) => { factor.datasets = response.data .filter((ds) => ds.Factor === factor.factor) .map((ds) => { return { name: ds.Name, included: true, }; }); }); this.apply(); }); } apply() { this.HsUtilsService.debounce( function () { this.$http .post('https://publish.lesprojekt.cz/nodejs/scores', { factors: this.factors.map((f) => { return { factor: f.factor, weight: f.weight, datasets: f.datasets .filter((ds) => ds.included) .map((ds) => ds.name), }; }), }) .toPromise() .then((response: any) => { this.attractivity = response.data; let max = 0; this.attractivity.forEach((a) => { if (a.aggregate > max) { max = a.aggregate; } }); const normalizer = 1 / max; this.attractivity.forEach((a) => { a.aggregate *= normalizer; }); this.attractivity.forEach((a) => { this.nutsCodeRecordRelations[a.code] = a; }); nuts.nuts3Source.forEachFeature((feature) => { feature.set( 'total', this.nutsCodeRecordRelations[feature.get('NUTS_ID')].aggregate ); feature.set( 'totalForHumans', ( this.nutsCodeRecordRelations[feature.get('NUTS_ID')] .aggregate * 100 ).toFixed(2) ); this.factors.forEach((factor) => { feature.set( factor.factor, ( this.nutsCodeRecordRelations[feature.get('NUTS_ID')][ factor.factor ] * 100 ).toFixed(2) ); }); }); }); }, 300, false, this )(); } }