| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- import {HttpClient} from '@angular/common/http';
- import {Injectable} from '@angular/core';
- import {Vector as VectorLayer} from 'ol/layer';
- import {HsDialogContainerService} from 'hslayers-ng/components/layout/dialogs/dialog-container.service';
- import {HsUtilsService} from 'hslayers-ng/components/utils/utils.service';
- import attractivenessConfig from '../attractiveness.config.json';
- import clusteringMethods from '../data/clustering_methods.json';
- import {AdjusterEventService} from './adjuster-event.service';
- import {AdjusterLegendService} from './adjuster-legend.service';
- import {AdjusterLoaderComponent} from './adjuster-loader.component';
- import {AttractivenessClustersService} from './attractiveness-clusters.service';
- import {AttractivenessIndexService} from './attractiveness-index.service';
- import {nuts} from '../nuts';
- @Injectable({providedIn: 'root'})
- export class AdjusterService {
- /** To be read from a config file */
- allowedClusteringMethods: Array<string>;
- /** To be read from a config file */
- initialWeights: Record<string, number>;
- /** To be read from a config file */
- serviceBaseUrl: string;
- /** Used in the UI as a selector */
- allowClusters = true;
- /** Used in the UI as a selector */
- allowIndex = true;
- factors = [];
- clusters = [];
- numberOfClusters: number;
- method: string;
- methods: Array<MethodDescription>;
- private _clusteringInProcess: boolean;
- private _clustersLoaded: boolean;
- private _loadInProcess: boolean;
- private _raiInProcess: boolean;
- constructor(
- public adjusterEventService: AdjusterEventService,
- public adjusterLegendService: AdjusterLegendService,
- public attractivenessIndexService: AttractivenessIndexService,
- public attractivenessClustersService: AttractivenessClustersService,
- public hsDialogContainerService: HsDialogContainerService,
- public hsUtilsService: HsUtilsService,
- public httpClient: HttpClient
- ) {
- // First safely set configurable properties
- this.allowedClusteringMethods =
- attractivenessConfig?.allowedClusteringMethods ?? [];
- this.initialWeights = attractivenessConfig?.initialWeights ?? {};
- this.serviceBaseUrl =
- attractivenessConfig?.serviceBaseUrl ??
- 'https://publish.lesprojekt.cz/nodejs/';
- // 'https://jmacura.ml/ws/' // 'http://localhost:3000/'
- this.methods = clusteringMethods.filter((m) =>
- this.allowedClusteringMethods.includes(m.codename)
- );
- this.method = 'haclustwd2'; //TODO: set in config/or not use at all?
- this.numberOfClusters = 12;
- }
- /**
- * Sends a request to polirural-attractiveness-service
- * and applies the returned values
- */
- apply(): void {
- if (this.allowIndex) {
- this.calculateIndex();
- }
- if (this.allowClusters) {
- this.calculateClusters();
- }
- }
- async calculateIndex(): Promise<void> {
- this._raiInProcess = true;
- let attractivenessData: any;
- try {
- attractivenessData = await this.httpClient
- .post(this.serviceBaseUrl + 'eu/scores/', {
- factors: this.factors.map((f) => {
- return {
- factor: f.name,
- weight: f.weight,
- datasets: f.datasets
- .filter((ds) => ds.included)
- .map((ds) => ds.name),
- };
- }),
- })
- .toPromise();
- } catch (error) {
- console.warn(`Error obtaining data from ${this.serviceBaseUrl}.`);
- console.log(error);
- this._raiInProcess = false;
- this.adjusterEventService.loaded.next({
- success: true,
- type: 'index',
- err: error,
- });
- }
- // Spread the 'aggregate' value between 0 and 1
- const min = attractivenessData.reduce((a, b) =>
- a.aggregate < b.aggregate ? a : b
- ).aggregate;
- const max = attractivenessData.reduce((a, b) =>
- a.aggregate > b.aggregate ? a : b
- ).aggregate;
- const coefficient = 1 / (max - min);
- const constant = -min * coefficient;
- attractivenessData.forEach((a) => {
- a.aggregate *= coefficient;
- a.aggregate += constant;
- });
- // Store relation between region and its data in a hash-table-like structure
- // more memory consuming, but faster then find()
- const codeRecordRelations = {};
- attractivenessData.forEach((a) => {
- codeRecordRelations[a.code.toUpperCase()] = a;
- });
- console.time('forEach-Index');
- this.attractivenessIndexService.processIndex(codeRecordRelations);
- console.timeEnd('forEach-Index');
- this._raiInProcess = false;
- this.adjusterEventService.loaded.next({
- success: true,
- type: 'index',
- });
- }
- async calculateClusters(): Promise<void> {
- this._clusteringInProcess = true;
- let data: any;
- try {
- data = await this.httpClient
- .post(this.serviceBaseUrl + 'eu/clusters/', {
- numberOfClusters: this.numberOfClusters,
- factors: this.factors.map((f) => {
- return {
- factor: f.name,
- weight: f.weight,
- datasets: f.datasets
- .filter((ds) => ds.included)
- .map((ds) => ds.name),
- };
- }),
- })
- .toPromise();
- } catch (error) {
- console.warn(`Error obtaining data from ${this.serviceBaseUrl}.`);
- console.log(error);
- this._clusteringInProcess = false;
- this.adjusterEventService.loaded.next({
- success: false,
- type: 'clusters',
- err: error,
- });
- }
- const clusterData = data.response;
- // Store relation between region and its data in a hash-table-like structure
- // more memory consuming, but much faster then find()
- const codeRecordRelations = {};
- clusterData.forEach((c) => {
- codeRecordRelations[c['nuts_id'].toUpperCase()] = c;
- });
- console.time('forEach-Cluster');
- /*const clusters = [];
- for (const region of clusterData) {
- if (!clusters.includes(region[this.method])) {
- clusters.push(region[this.method]);
- }
- }*/
- //for (const method of this.methods) {
- this.attractivenessClustersService.processClusters(codeRecordRelations);
- //}
- console.timeEnd('forEach-Cluster');
- /*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;
- });*/
- // Fake the legend
- nuts.nuts3ClustersSource.legend_categories = this.adjusterLegendService.createClusterLegend(
- this.numberOfClusters
- );
- this._clustersLoaded = true;
- this._clusteringInProcess = false;
- this.adjusterEventService.loaded.next({
- success: true,
- type: 'clusters',
- });
- }
- async init(): Promise<void> {
- this._loadInProcess = true;
- let data: any;
- try {
- data = await this.httpClient
- .get(this.serviceBaseUrl + 'eu/datasets/')
- .toPromise();
- } catch (error) {
- console.warn(`Web service at ${this.serviceBaseUrl} unavailable!`);
- console.log(error);
- this._loadInProcess = false;
- /*this.adjusterEventService.loaded.next({
- success: false,
- err: error,
- });*/
- }
- this.factors = data.map((dataset) => {
- return {
- name: dataset.Factor,
- weight: this.initialWeights[dataset.Factor] ?? 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._loadInProcess = false;
- this.apply();
- // In HSL 2.5, setting layer greyscale breaks the print() functionality
- //this.hsLayerManagerService.setGreyscale(osmLayer);
- }
- clustersLoaded(): boolean {
- return this._clustersLoaded;
- }
- /**
- * @returns {boolean} true if clustering or index processing is in process or loading data, false otherwise
- */
- isInProcess(): boolean {
- return (
- this._loadInProcess || this._clusteringInProcess || this._raiInProcess
- );
- }
- isLoading(): boolean {
- return this._loadInProcess;
- }
- isClustering(): boolean {
- return this._clusteringInProcess;
- }
- isCalculatingRAI(): boolean {
- return this._raiInProcess;
- }
- }
- type MethodDescription = {
- codename: string;
- layer?: VectorLayer;
- name: string;
- type: string;
- };
|