helpers.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Helper methods for use in the other modules.
  3. */
  4. const fs = require('fs');
  5. module.exports.describeSelf = function (req, res) {
  6. const description = {
  7. description: 'Rural attractiveness web service',
  8. methods: {
  9. GET: [
  10. '/refresh',
  11. '/:aoi?/datasets/',
  12. '/:aoi?/scores/:nuts',
  13. '/:aoi?/scores',
  14. '/georeport/:nuts',
  15. '/ontology'
  16. ],
  17. POST: [
  18. '/:aoi?/scores',
  19. '/:aoi?/clusters/'
  20. ]
  21. }
  22. }
  23. this.formatResponse(description, req, res)
  24. }
  25. module.exports.formatResponse = function (obj, req, res) {
  26. res.header("Content-Type", 'application/json');
  27. if (req.query && req.query.f && req.query.f == 'pjson')
  28. res.send(JSON.stringify(obj, null, 4));
  29. else
  30. res.send(obj);
  31. }
  32. module.exports.loadJSON = function (filename) {
  33. let rawdata = fs.readFileSync(filename);
  34. let jsondata = JSON.parse(rawdata);
  35. return jsondata;
  36. }
  37. module.exports.getCentroid = function (geometry) {
  38. let firstPoint = geometry.coordinates[0][0]
  39. if (firstPoint.length > 2)
  40. firstPoint = firstPoint[0];
  41. let xmin = firstPoint[0];
  42. let xmax = firstPoint[0];
  43. let ymin = firstPoint[1];
  44. let ymax = firstPoint[1];
  45. geometry.coordinates[0].forEach(pt => {
  46. if (pt[0] < xmin) xmin = pt[0];
  47. if (pt[0] > xmax) xmax = pt[0];
  48. if (pt[1] < ymin) ymin = pt[1];
  49. if (pt[1] > ymax) ymax = pt[1];
  50. });
  51. return [(ymin + ymax) / 2.0, (xmin + xmax) / 2.0];
  52. }
  53. module.exports.getPilotRegion = function (nuts, pilots) {
  54. for (let i = 0; i < pilots.length; i++) {
  55. if (nuts.startsWith(pilots[i].nuts))
  56. return pilots[i];
  57. }
  58. return null;
  59. }