|
|
@@ -8,53 +8,135 @@ import {obce} from '../app.config';
|
|
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
export class AdjusterService {
|
|
|
+ serviceBaseUrl: string;
|
|
|
nutsCodeRecordRelations = {};
|
|
|
- attractivity;
|
|
|
- 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,
|
|
|
- },
|
|
|
- ];
|
|
|
+ //attractivity;
|
|
|
+ factors: any = [];
|
|
|
|
|
|
- constructor(public HsUtilsService: HsUtilsService, public $http: HttpClient) {
|
|
|
+ constructor(public hsUtilsService: HsUtilsService, public $http: HttpClient) {
|
|
|
+ this.serviceBaseUrl =
|
|
|
+ window.location.hostname === 'localhost'
|
|
|
+ ? 'http://localhost:3000/' // 'https://jmacura.ml/ws/'
|
|
|
+ : 'https://publish.lesprojekt.cz/nodejs/';
|
|
|
+ this.init();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sends a request to polirural-attractiveness-service
|
|
|
+ * and applies the returned values
|
|
|
+ */
|
|
|
+ apply(): void {
|
|
|
+ const f = () => {
|
|
|
+ this.$http
|
|
|
+ .post(this.serviceBaseUrl + 'scores/cz', {
|
|
|
+ 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((attractivenessData: any) => {
|
|
|
+ console.log(attractivenessData);
|
|
|
+ //this.attractivity = attractivenessData;
|
|
|
+ /*let max = 0;
|
|
|
+ attractivenessData.forEach((a) => {
|
|
|
+ if (a.aggregate > max) {
|
|
|
+ max = a.aggregate;
|
|
|
+ }
|
|
|
+ });*/
|
|
|
+ const max = Math.max(
|
|
|
+ ...attractivenessData.map((a) => a.aggregate),
|
|
|
+ 0
|
|
|
+ );
|
|
|
+ const normalizer = 1 / max;
|
|
|
+ attractivenessData.forEach((a) => {
|
|
|
+ a.aggregate *= normalizer;
|
|
|
+ });
|
|
|
+ const codeRecordRelations = {};
|
|
|
+ attractivenessData.forEach((a) => {
|
|
|
+ codeRecordRelations[a.code] = a;
|
|
|
+ });
|
|
|
+ let errs = 0;
|
|
|
+ let logs = 0;
|
|
|
+ console.time('forEachObce');
|
|
|
+ obce.forEachFeature((feature) => {
|
|
|
+ // Pair each feature with its attractivity data
|
|
|
+ const featureData =
|
|
|
+ codeRecordRelations[feature.get('nationalCode')];
|
|
|
+ /*const featureData = attractivenessData.find(
|
|
|
+ // NOTE: Do NOT add triple equal sign!
|
|
|
+ (item) => item['code'] == feature.get('nationalCode')
|
|
|
+ );*/
|
|
|
+ if (!featureData && errs < 20) {
|
|
|
+ errs++;
|
|
|
+ console.warn(
|
|
|
+ `No data for feature ${feature.get('nationalCode')}`
|
|
|
+ );
|
|
|
+ console.log(feature);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ logs++;
|
|
|
+ if (logs % 100 == 0) {
|
|
|
+ console.log(`processed ${logs} items`);
|
|
|
+ }
|
|
|
+ Object.keys(featureData).forEach((key, index) => {
|
|
|
+ if (key !== 'lau2') {
|
|
|
+ feature.set(key, featureData[key]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ /*feature.set(
|
|
|
+ 'total',
|
|
|
+ this.nutsCodeRecordRelations[feature.get('nationalCode')]
|
|
|
+ .aggregate
|
|
|
+ );*/
|
|
|
+ /*feature.set(
|
|
|
+ 'totalForHumans',
|
|
|
+ (
|
|
|
+ this.nutsCodeRecordRelations[feature.get('nationalCode')]
|
|
|
+ .aggregate * 100
|
|
|
+ ).toFixed(2)
|
|
|
+ );*/
|
|
|
+ /*this.factors.forEach((factor) => {
|
|
|
+ feature.set(
|
|
|
+ factor.name,
|
|
|
+ (
|
|
|
+ this.nutsCodeRecordRelations[feature.get('nationalCode')][
|
|
|
+ factor.name
|
|
|
+ ] * 100
|
|
|
+ ).toFixed(2)
|
|
|
+ );
|
|
|
+ });*/
|
|
|
+ });
|
|
|
+ console.timeEnd('forEachObce');
|
|
|
+ })
|
|
|
+ .catch((error) => {
|
|
|
+ console.warn(`Error obtaining data from ${this.serviceBaseUrl}.`);
|
|
|
+ console.log(error);
|
|
|
+ });
|
|
|
+ };
|
|
|
+ this.hsUtilsService.debounce(f, 300, false, this)();
|
|
|
+ }
|
|
|
+
|
|
|
+ init(): void {
|
|
|
this.$http
|
|
|
- .get('https://publish.lesprojekt.cz/nodejs/datasets')
|
|
|
+ .get(this.serviceBaseUrl + 'datasets/cz')
|
|
|
.toPromise()
|
|
|
.then((data: any) => {
|
|
|
this.factors = data.map((dataset) => {
|
|
|
- return {factor: dataset.Factor, weight: 1, datasets: []};
|
|
|
+ return {name: dataset.Factor, weight: 1, datasets: []};
|
|
|
});
|
|
|
- this.factors = HsUtilsService.removeDuplicates(this.factors, 'factor');
|
|
|
+ this.factors = this.hsUtilsService.removeDuplicates(
|
|
|
+ this.factors,
|
|
|
+ 'name'
|
|
|
+ );
|
|
|
this.factors.forEach((factor) => {
|
|
|
factor.datasets = data
|
|
|
- .filter((ds) => ds.Factor === factor.factor)
|
|
|
+ .filter((ds) => ds.Factor === factor.name)
|
|
|
.map((ds) => {
|
|
|
return {
|
|
|
name: ds.Name,
|
|
|
@@ -63,68 +145,10 @@ export class AdjusterService {
|
|
|
});
|
|
|
});
|
|
|
this.apply();
|
|
|
+ })
|
|
|
+ .catch((error) => {
|
|
|
+ console.warn(`Web service at ${this.serviceBaseUrl} unavailable!`);
|
|
|
+ console.log(error);
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
- 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((data: any) => {
|
|
|
- this.attractivity = 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;
|
|
|
- });
|
|
|
- obce.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
|
|
|
- )();
|
|
|
- }
|
|
|
}
|