Parcourir la source

Fix georeport response

jmacura il y a 3 ans
Parent
commit
d0177a04bc
3 fichiers modifiés avec 47 ajouts et 33 suppressions
  1. 22 29
      index.js
  2. 22 1
      nuts-data.js
  3. 3 3
      views/nuts.pug

+ 22 - 29
index.js

@@ -24,6 +24,7 @@ const _ontologyFilePath = 'data/rural_attractiveness.owl.json';
 var _datasetsEU = undefined;
 var _datasetsCZ = undefined;
 var _datasetsAfrica = undefined;
+var _ontology = undefined;
 var _ruralDataEU = undefined;
 var _ruralDataCZ = undefined;
 var _ruralDataAfrica = undefined;
@@ -48,10 +49,10 @@ app.get('/refresh', async (req, res, next) => {
   try {
     _datasetsEU = await nutsData.loadDatasets(_dataEuFilePath)
     _datasetsCZ = await nutsData.loadDatasets(_dataCzFilePath)
-    //console.log('Datasets loaded succesfully');
+    //console.log('Datasets loaded successfully');
     _ruralDataEU = await nutsData.loadRuralData(_dataEuFilePath)
     _ruralDataCZ = await nutsData.loadRuralData(_dataCzFilePath)
-    //console.log('Rural data loaded succesfully');
+    //console.log('Rural data loaded successfully');
   } catch (e) {
     res.send(e.toString() || e)
   }
@@ -93,7 +94,7 @@ app.get('/:aoi?/scores', async (req, res, next) => {
 });
 
 /* Computes and returns attractivity data for all the NUTS regions based on the
-   incomming datasets and factor weights. */
+   incoming datasets and factor weights. */
 app.post('/:aoi?/scores', async (req, res, next) => {
   //console.log("query: " + JSON.stringify(req.body.factors, null, 4));
   const aoi = req.params.aoi || 'eu'
@@ -193,27 +194,22 @@ app.post('/:aoi?/clusters/', async (req, res, next) => {
  * This ensures that both backend and frontend are using the same version of the ontology
 */
 app.get('/ontology/', async (req, res, next) => {
-  const onto = await nutsData.loadOntology(_ontologyFilePath)
-  helpers.formatResponse(onto, req, res)
+  if (!_ontology) {
+    _ontology = await nutsData.loadOntology(_ontologyFilePath)
+  }
+  helpers.formatResponse(_ontology, req, res)
 });
 
-//FIXME: use ontology to read datasets metadata // CURRENTLY BROKEN
+/*
+ * Render georeport for a given region. Currently only available for EU/EEA
+*/
 app.get('/georeport/:nuts', async (req, res, next) => {
-  if (!_datasetsEU) { // datasets must be loaded prior to data loading
-    try {
-      _datasetsEU = await nutsData.loadDatasets(_dataEuFilePath)
-    } catch (e) {
-      res.send(e.toString() || e)
-    }
+  await loadDatasetsIfNeeded(req, res, 'eu')
+  await loadRuralDataIfNeeded(req, res, 'eu')
+  if (!_ontology) {
+    _ontology = await nutsData.loadOntology(_ontologyFilePath)
   }
-  if (!_ruralDataEU) {
-    try {
-      _ruralDataEU = await nutsData.loadRuralData(_dataEuFilePath)
-    } catch (e) {
-      res.send(e.toString() || e)
-    }
-  }
-  renderPugReport(req.params.nuts, req, res);
+  renderPugReport(req.params.nuts, req, res)
 });
 
 // start the service on the port xxxx
@@ -221,24 +217,21 @@ app.listen(3000, () => console.log('Rural attractivity WS listening on port 3000
 
 
 function renderPugReport(nuts, req, res) {
-  let found = false;
+  const dsMetadata = nutsData.parseDatasetsMetadata(_ontology)
 
+  let found = false;
   _ruralDataEU.forEach(region => {
     if (region.nuts == nuts) {
-
       let nutsGeoJson = helpers.loadJSON('static/NUTS3_4326.json');
-      let filterednutsGeoJson = where(nutsGeoJson.features, { "properties": { "NUTS_ID": nuts } });
+      let filteredNutsGeoJson = where(nutsGeoJson.features, { "properties": { "NUTS_ID": nuts } });
       let pilots = helpers.loadJSON('static/pilots.json');
-
       region.metadata = {
-        name: filterednutsGeoJson[0].properties.NUTS_NAME,
+        name: filteredNutsGeoJson[0].properties.NUTS_NAME,
         code: nuts,
-        centroid: helpers.getCentroid(filterednutsGeoJson[0].geometry),
+        centroid: helpers.getCentroid(filteredNutsGeoJson[0].geometry),
         pilot: helpers.getPilotRegion(nuts, pilots)
       };
-
-      //FIXME: don't imput _datasets here but already pre-processed metadata object
-      res.render('nuts', { region: region, datasets: _datasetsEU });
+      res.render('nuts', { region: region, datasets: dsMetadata });
       found = true;
     }
   });

+ 22 - 1
nuts-data.js

@@ -24,7 +24,7 @@ module.exports.loadDatasets = async function(filePath) {
     })
 }
 
-/* Load all the attractivness data from CSV into a server object.
+/* Load all the attractiveness data from CSV into a server object.
    The data don't need to be loaded for each request then. */
 module.exports.loadRuralData = async function (filePath) {
     //console.log('Reading rural data file processing started.')
@@ -80,6 +80,27 @@ module.exports.loadOntology = async function(filePath) {
     })
 }
 
+module.exports.parseDatasetsMetadata = function(ontology) {
+    return ontology
+        .filter((entity) => entity['@type']?.includes('http://www.semanticweb.org/attractiveness/Dataset'))
+        .filter(
+            (entity) => entity['http://www.semanticweb.org/attractiveness/hasCoverage']
+                .some((coverage) => coverage['@id'] == 'http://www.semanticweb.org/attractiveness/Europe')
+        )
+        .filter(
+            (entity) => entity['http://www.semanticweb.org/attractiveness/hasLoD']
+                .some((lod) => lod['@id'] == 'http://www.semanticweb.org/attractiveness/NUTS3')
+        )
+        .map((entity) => {
+            return {
+                name: entity['@id'].split('/').splice(-1).pop(),
+                //FIXME: instead of [0] search for "@language"="en"
+                description: entity['http://www.semanticweb.org/attractiveness#description']?.[0]['@value'],
+                factor: entity['http://www.semanticweb.org/attractiveness/isDatasetOf']?.[0]['@id'].split('/').splice(-1).pop()
+            }
+        })
+}
+
 /**
  * Resolves with an array representing rows of CSV file
  * @param {string} inputFileName path to the CSV file with input data for clustering calculation

+ 3 - 3
views/nuts.pug

@@ -41,9 +41,9 @@ html
       tbody
         each ds in datasets
           tr
-            td= ds.Description
-            td= ds.Factor
-            td= region[ds.Factor][ds.Name] !== undefined ? +(region[ds.Factor][ds.Name] * 100).toFixed(2) + ' %' : '--'
+            td= ds.description
+            td= ds.factor
+            td= region.values[ds.name] !== undefined ? +(region.values[ds.name] * 100).toFixed(2) + ' %' : '--'
     
     script.
       let c = !{JSON.stringify(region.metadata.centroid)};