ParsePolygon.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
  9. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
  10. var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
  11. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  12. var _ParseGeoPoint = _interopRequireDefault(require("./ParseGeoPoint"));
  13. /**
  14. * @flow
  15. */
  16. /**
  17. * Creates a new Polygon with any of the following forms:<br>
  18. * <pre>
  19. * new Polygon([[0,0],[0,1],[1,1],[1,0]])
  20. * new Polygon([GeoPoint, GeoPoint, GeoPoint])
  21. * </pre>
  22. *
  23. * <p>Represents a coordinates that may be associated
  24. * with a key in a ParseObject or used as a reference point for geo queries.
  25. * This allows proximity-based queries on the key.</p>
  26. *
  27. * <p>Example:<pre>
  28. * var polygon = new Parse.Polygon([[0,0],[0,1],[1,1],[1,0]]);
  29. * var object = new Parse.Object("PlaceObject");
  30. * object.set("area", polygon);
  31. * object.save();</pre></p>
  32. *
  33. * @alias Parse.Polygon
  34. */
  35. var ParsePolygon = /*#__PURE__*/function () {
  36. /**
  37. * @param {(number[][] | Parse.GeoPoint[])} coordinates An Array of coordinate pairs
  38. */
  39. function ParsePolygon(coordinates /*: Array<Array<number>> | Array<ParseGeoPoint>*/) {
  40. (0, _classCallCheck2.default)(this, ParsePolygon);
  41. (0, _defineProperty2.default)(this, "_coordinates", void 0);
  42. this._coordinates = ParsePolygon._validate(coordinates);
  43. }
  44. /**
  45. * Coordinates value for this Polygon.
  46. * Throws an exception if not valid type.
  47. *
  48. * @property {(number[][] | Parse.GeoPoint[])} coordinates list of coordinates
  49. * @returns {number[][]}
  50. */
  51. (0, _createClass2.default)(ParsePolygon, [{
  52. key: "coordinates",
  53. get: function () /*: Array<Array<number>>*/{
  54. return this._coordinates;
  55. },
  56. set: function (coords /*: Array<Array<number>> | Array<ParseGeoPoint>*/) {
  57. this._coordinates = ParsePolygon._validate(coords);
  58. }
  59. /**
  60. * Returns a JSON representation of the Polygon, suitable for Parse.
  61. *
  62. * @returns {object}
  63. */
  64. }, {
  65. key: "toJSON",
  66. value: function () /*: { __type: string, coordinates: Array<Array<number>> }*/{
  67. ParsePolygon._validate(this._coordinates);
  68. return {
  69. __type: 'Polygon',
  70. coordinates: this._coordinates
  71. };
  72. }
  73. /**
  74. * Checks if two polygons are equal
  75. *
  76. * @param {(Parse.Polygon | object)} other
  77. * @returns {boolean}
  78. */
  79. }, {
  80. key: "equals",
  81. value: function (other /*: mixed*/) /*: boolean*/{
  82. if (!(other instanceof ParsePolygon) || this.coordinates.length !== other.coordinates.length) {
  83. return false;
  84. }
  85. var isEqual = true;
  86. for (var i = 1; i < this._coordinates.length; i += 1) {
  87. if (this._coordinates[i][0] != other.coordinates[i][0] || this._coordinates[i][1] != other.coordinates[i][1]) {
  88. isEqual = false;
  89. break;
  90. }
  91. }
  92. return isEqual;
  93. }
  94. /**
  95. *
  96. * @param {Parse.GeoPoint} point
  97. * @returns {boolean} Returns if the point is contained in the polygon
  98. */
  99. }, {
  100. key: "containsPoint",
  101. value: function (point /*: ParseGeoPoint*/) /*: boolean*/{
  102. var minX = this._coordinates[0][0];
  103. var maxX = this._coordinates[0][0];
  104. var minY = this._coordinates[0][1];
  105. var maxY = this._coordinates[0][1];
  106. for (var i = 1; i < this._coordinates.length; i += 1) {
  107. var p = this._coordinates[i];
  108. minX = Math.min(p[0], minX);
  109. maxX = Math.max(p[0], maxX);
  110. minY = Math.min(p[1], minY);
  111. maxY = Math.max(p[1], maxY);
  112. }
  113. var outside = point.latitude < minX || point.latitude > maxX || point.longitude < minY || point.longitude > maxY;
  114. if (outside) {
  115. return false;
  116. }
  117. var inside = false;
  118. for (var _i = 0, j = this._coordinates.length - 1; _i < this._coordinates.length; j = _i++) {
  119. var startX = this._coordinates[_i][0];
  120. var startY = this._coordinates[_i][1];
  121. var endX = this._coordinates[j][0];
  122. var endY = this._coordinates[j][1];
  123. var intersect = startY > point.longitude != endY > point.longitude && point.latitude < (endX - startX) * (point.longitude - startY) / (endY - startY) + startX;
  124. if (intersect) {
  125. inside = !inside;
  126. }
  127. }
  128. return inside;
  129. }
  130. /**
  131. * Validates that the list of coordinates can form a valid polygon
  132. *
  133. * @param {Array} coords the list of coordinates to validate as a polygon
  134. * @throws {TypeError}
  135. * @returns {number[][]} Array of coordinates if validated.
  136. */
  137. }], [{
  138. key: "_validate",
  139. value: function (coords /*: Array<Array<number>> | Array<ParseGeoPoint>*/) /*: Array<Array<number>>*/{
  140. if (!(0, _isArray.default)(coords)) {
  141. throw new TypeError('Coordinates must be an Array');
  142. }
  143. if (coords.length < 3) {
  144. throw new TypeError('Polygon must have at least 3 GeoPoints or Points');
  145. }
  146. var points = [];
  147. for (var i = 0; i < coords.length; i += 1) {
  148. var coord = coords[i];
  149. var geoPoint = void 0;
  150. if (coord instanceof _ParseGeoPoint.default) {
  151. geoPoint = coord;
  152. } else if ((0, _isArray.default)(coord) && coord.length === 2) {
  153. geoPoint = new _ParseGeoPoint.default(coord[0], coord[1]);
  154. } else {
  155. throw new TypeError('Coordinates must be an Array of GeoPoints or Points');
  156. }
  157. points.push([geoPoint.latitude, geoPoint.longitude]);
  158. }
  159. return points;
  160. }
  161. }]);
  162. return ParsePolygon;
  163. }();
  164. var _default = ParsePolygon;
  165. exports.default = _default;