helpers.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. ],
  16. POST: [
  17. '/:aoi?/scores',
  18. '/:aoi?/clusters/'
  19. ]
  20. }
  21. }
  22. this.formatResponse(description, req, res)
  23. }
  24. module.exports.formatResponse = function (obj, req, res) {
  25. res.header("Content-Type", 'application/json');
  26. if (req.query && req.query.f && req.query.f == 'pjson')
  27. res.send(JSON.stringify(obj, null, 4));
  28. else
  29. res.send(obj);
  30. }
  31. module.exports.loadJSON = function (filename) {
  32. let rawdata = fs.readFileSync(filename);
  33. let jsondata = JSON.parse(rawdata);
  34. return jsondata;
  35. }
  36. module.exports.getCentroid = function (geometry) {
  37. let firstPoint = geometry.coordinates[0][0]
  38. if (firstPoint.length > 2)
  39. firstPoint = firstPoint[0];
  40. let xmin = firstPoint[0];
  41. let xmax = firstPoint[0];
  42. let ymin = firstPoint[1];
  43. let ymax = firstPoint[1];
  44. geometry.coordinates[0].forEach(pt => {
  45. if (pt[0] < xmin) xmin = pt[0];
  46. if (pt[0] > xmax) xmax = pt[0];
  47. if (pt[1] < ymin) ymin = pt[1];
  48. if (pt[1] > ymax) ymax = pt[1];
  49. });
  50. return [(ymin + ymax) / 2.0, (xmin + xmax) / 2.0];
  51. }
  52. module.exports.getPilotRegion = function (nuts, pilots) {
  53. for (let i = 0; i < pilots.length; i++) {
  54. if (nuts.startsWith(pilots[i].nuts))
  55. return pilots[i];
  56. }
  57. return null;
  58. }