helpers.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Helper methods for use in the other modules.
  3. */
  4. const fs = require('fs');
  5. module.exports.formatResponse = function (obj, req, res) {
  6. res.header("Content-Type", 'application/json');
  7. if (req.query && req.query.f && req.query.f == 'pjson')
  8. res.send(JSON.stringify(obj, null, 4));
  9. else
  10. res.send(obj);
  11. }
  12. module.exports.loadJSON = function (filename) {
  13. let rawdata = fs.readFileSync(filename);
  14. let jsondata = JSON.parse(rawdata);
  15. return jsondata;
  16. }
  17. module.exports.getCentroid = function (geometry) {
  18. let firstPoint = geometry.coordinates[0][0]
  19. if (firstPoint.length > 2)
  20. firstPoint = firstPoint[0];
  21. let xmin = firstPoint[0];
  22. let xmax = firstPoint[0];
  23. let ymin = firstPoint[1];
  24. let ymax = firstPoint[1];
  25. geometry.coordinates[0].forEach(pt => {
  26. if (pt[0] < xmin) xmin = pt[0];
  27. if (pt[0] > xmax) xmax = pt[0];
  28. if (pt[1] < ymin) ymin = pt[1];
  29. if (pt[1] > ymax) ymax = pt[1];
  30. });
  31. return [(ymin + ymax) / 2.0, (xmin + xmax) / 2.0];
  32. }
  33. module.exports.getPilotRegion = function (nuts, pilots) {
  34. for (let i = 0; i < pilots.length; i++) {
  35. if (nuts.startsWith(pilots[i].nuts))
  36. return pilots[i];
  37. }
  38. return null;
  39. }