ParsePolygon.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  7. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  8. var _ParseGeoPoint = _interopRequireDefault(require("./ParseGeoPoint"));
  9. var ParsePolygon = function () {
  10. function ParsePolygon(coordinates) {
  11. (0, _classCallCheck2.default)(this, ParsePolygon);
  12. this._coordinates = ParsePolygon._validate(coordinates);
  13. }
  14. (0, _createClass2.default)(ParsePolygon, [{
  15. key: "coordinates",
  16. get: function () {
  17. return this._coordinates;
  18. },
  19. set: function (coords) {
  20. this._coordinates = ParsePolygon._validate(coords);
  21. }
  22. }, {
  23. key: "toJSON",
  24. value: function () {
  25. ParsePolygon._validate(this._coordinates);
  26. return {
  27. __type: 'Polygon',
  28. coordinates: this._coordinates
  29. };
  30. }
  31. }, {
  32. key: "equals",
  33. value: function (other) {
  34. if (!(other instanceof ParsePolygon) || this.coordinates.length !== other.coordinates.length) {
  35. return false;
  36. }
  37. var isEqual = true;
  38. for (var i = 1; i < this._coordinates.length; i += 1) {
  39. if (this._coordinates[i][0] != other.coordinates[i][0] || this._coordinates[i][1] != other.coordinates[i][1]) {
  40. isEqual = false;
  41. break;
  42. }
  43. }
  44. return isEqual;
  45. }
  46. }, {
  47. key: "containsPoint",
  48. value: function (point) {
  49. var minX = this._coordinates[0][0];
  50. var maxX = this._coordinates[0][0];
  51. var minY = this._coordinates[0][1];
  52. var maxY = this._coordinates[0][1];
  53. for (var i = 1; i < this._coordinates.length; i += 1) {
  54. var p = this._coordinates[i];
  55. minX = Math.min(p[0], minX);
  56. maxX = Math.max(p[0], maxX);
  57. minY = Math.min(p[1], minY);
  58. maxY = Math.max(p[1], maxY);
  59. }
  60. var outside = point.latitude < minX || point.latitude > maxX || point.longitude < minY || point.longitude > maxY;
  61. if (outside) {
  62. return false;
  63. }
  64. var inside = false;
  65. for (var _i = 0, j = this._coordinates.length - 1; _i < this._coordinates.length; j = _i++) {
  66. var startX = this._coordinates[_i][0];
  67. var startY = this._coordinates[_i][1];
  68. var endX = this._coordinates[j][0];
  69. var endY = this._coordinates[j][1];
  70. var intersect = startY > point.longitude != endY > point.longitude && point.latitude < (endX - startX) * (point.longitude - startY) / (endY - startY) + startX;
  71. if (intersect) {
  72. inside = !inside;
  73. }
  74. }
  75. return inside;
  76. }
  77. }], [{
  78. key: "_validate",
  79. value: function (coords) {
  80. if (!Array.isArray(coords)) {
  81. throw new TypeError('Coordinates must be an Array');
  82. }
  83. if (coords.length < 3) {
  84. throw new TypeError('Polygon must have at least 3 GeoPoints or Points');
  85. }
  86. var points = [];
  87. for (var i = 0; i < coords.length; i += 1) {
  88. var coord = coords[i];
  89. var geoPoint = void 0;
  90. if (coord instanceof _ParseGeoPoint.default) {
  91. geoPoint = coord;
  92. } else if (Array.isArray(coord) && coord.length === 2) {
  93. geoPoint = new _ParseGeoPoint.default(coord[0], coord[1]);
  94. } else {
  95. throw new TypeError('Coordinates must be an Array of GeoPoints or Points');
  96. }
  97. points.push([geoPoint.latitude, geoPoint.longitude]);
  98. }
  99. return points;
  100. }
  101. }]);
  102. return ParsePolygon;
  103. }();
  104. var _default = ParsePolygon;
  105. exports.default = _default;