helpers.js 1.7 KB

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