attractiveness-index.service.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import {Geometry} from 'ol/geom';
  2. import {Injectable} from '@angular/core';
  3. import {Vector as VectorSource} from 'ol/source';
  4. @Injectable({providedIn: 'root'})
  5. export class AttractivenessIndexService {
  6. constructor() {}
  7. processIndex(
  8. indexSource: VectorSource<Geometry>,
  9. codeRecordRelations: Record<string, unknown>
  10. ): void {
  11. /*if (obce.getFeatures()?.length < 1) {
  12. obce.once('changefeature', () => this.processIndex(codeRecordRelations));
  13. return;
  14. }*/
  15. let errs = 0;
  16. //let logs = 0;
  17. indexSource.forEachFeature((feature) => {
  18. // Pair each feature with its attractivity data
  19. const featureData =
  20. codeRecordRelations[feature.get('NUTS_ID').toUpperCase()];
  21. if (!featureData) {
  22. if (errs < 20) {
  23. errs++;
  24. console.warn(
  25. `No data for feature ${feature.get('NUTS_ID').toUpperCase()}`
  26. );
  27. console.log(feature);
  28. }
  29. return;
  30. }
  31. /*logs++;
  32. if (logs % 100 == 0) {
  33. console.log(`processed ${logs} items`);
  34. }*/
  35. Object.keys(featureData).forEach((key, index) => {
  36. if (key !== 'nuts_id') {
  37. feature.set(key, featureData[key], true); //true stands for "silent" - important for performance!
  38. }
  39. });
  40. });
  41. // Since we are updating the features silently, we now have to refresh manually
  42. indexSource.getFeatures()[0].dispatchEvent('change');
  43. }
  44. }