import {HttpClient} from '@angular/common/http'; import {Injectable} from '@angular/core'; import {Subject} from 'rxjs'; import {HsUtilsService} from 'hslayers-ng/components/utils/utils.service'; // import attractivity from '../Attractivity.json'; import {nuts} from '../nuts'; //import {factors} from './factors.js'; @Injectable({providedIn: 'root'}) export class AdjusterService { serviceBaseUrl: string; factors; clusters; clustersLoaded: Subject = new Subject(); method: string; _clusteringInProcess: boolean; constructor( private hsUtilsService: HsUtilsService, private httpClient: HttpClient ) { this.serviceBaseUrl = window.location.hostname === 'localhost' ? 'https://jmacura.ml/ws/' // 'http://localhost:3000/' : 'https://publish.lesprojekt.cz/nodejs/'; this.factors = []; this.clusters = []; this.method = 'haclustwd2'; this._clusteringInProcess = true; this.init(); } /** * Sends a request to polirural-attractiveness-service * and applies the returned values */ apply(): void { const f = () => { this._clusteringInProcess = true; this.httpClient .post(this.serviceBaseUrl + 'clusters', { factors: this.factors.map((f) => { return { factor: f.name, weight: f.weight, datasets: f.datasets .filter((ds) => ds.included) .map((ds) => ds.name), }; }), }) .toPromise() .then((data: any) => { const clusterData = data.response; /*let max = 0; this.clusters.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) => { // Pair each feature with its clustering data const featureData = clusterData.find( (item) => item['nuts_id'] === feature.get('NUTS_ID') ); if (!featureData) { console.error(`No data for feature ${feature.get('NUTS_ID')}`); console.log(feature); return; } Object.keys(featureData).forEach(function (key, index) { if (key !== 'nuts_id') { feature.set(key, featureData[key]); } }); }); const clusters = []; for (const region of clusterData) { if (!clusters.includes(region[this.method])) { clusters.push(region[this.method]); } } this.clusters = clusters; this._clusteringInProcess = false; this.clustersLoaded.next(); }) .catch((error) => { console.warn(`Error obtaining data from ${this.serviceBaseUrl}.`); console.log(error); this._clusteringInProcess = false; }); }; this.hsUtilsService.debounce(f, 300, false, this)(); } init(): void { this.httpClient .get(this.serviceBaseUrl + 'datasets') .toPromise() .then((data: any) => { this.factors = data.map((dataset) => { return {name: dataset.Factor, weight: 1, datasets: []}; }); this.factors = this.hsUtilsService.removeDuplicates( this.factors, 'name' ); this.factors.forEach((factor) => { factor.datasets = data .filter((ds) => ds.Factor === factor.name) .map((ds) => { return { name: ds.Name, desc: ds.Description, included: true, }; }); }); this.apply(); }) .catch((error) => { console.warn(`Web service at ${this.serviceBaseUrl} unavailable!`); console.log(error); this._clusteringInProcess = false; }); } /** * @returns {boolean} true if clustering is in process, false otherwise */ isClusteringInProcess(): boolean { return this._clusteringInProcess; } }