Point.js 894 B

1234567891011121314151617181920212223242526272829303132333435
  1. var mgrs = require('mgrs');
  2. function Point(x, y, z) {
  3. if (!(this instanceof Point)) {
  4. return new Point(x, y, z);
  5. }
  6. if (Array.isArray(x)) {
  7. this.x = x[0];
  8. this.y = x[1];
  9. this.z = x[2] || 0.0;
  10. }else if(typeof x === 'object'){
  11. this.x = x.x;
  12. this.y = x.y;
  13. this.z = x.z || 0.0;
  14. } else if (typeof x === 'string' && typeof y === 'undefined') {
  15. var coords = x.split(',');
  16. this.x = parseFloat(coords[0], 10);
  17. this.y = parseFloat(coords[1], 10);
  18. this.z = parseFloat(coords[2], 10) || 0.0;
  19. }
  20. else {
  21. this.x = x;
  22. this.y = y;
  23. this.z = z || 0.0;
  24. }
  25. console.warn('proj4.Point will be removed in version 3, use proj4.toPoint');
  26. }
  27. Point.fromMGRS = function(mgrsStr) {
  28. return new Point(mgrs.toPoint(mgrsStr));
  29. };
  30. Point.prototype.toMGRS = function(accuracy) {
  31. return mgrs.forward([this.x, this.y], accuracy);
  32. };
  33. module.exports = Point;