ParsePolygon.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * Copyright (c) 2015-present, Parse, LLC.
  15. * All rights reserved.
  16. *
  17. * This source code is licensed under the BSD-style license found in the
  18. * LICENSE file in the root directory of this source tree. An additional grant
  19. * of patent rights can be found in the PATENTS file in the same directory.
  20. *
  21. * @flow
  22. */
  23. /**
  24. * Creates a new Polygon with any of the following forms:<br>
  25. * <pre>
  26. * new Polygon([[0,0],[0,1],[1,1],[1,0]])
  27. * new Polygon([GeoPoint, GeoPoint, GeoPoint])
  28. * </pre>
  29. *
  30. * <p>Represents a coordinates that may be associated
  31. * with a key in a ParseObject or used as a reference point for geo queries.
  32. * This allows proximity-based queries on the key.</p>
  33. *
  34. * <p>Example:<pre>
  35. * var polygon = new Parse.Polygon([[0,0],[0,1],[1,1],[1,0]]);
  36. * var object = new Parse.Object("PlaceObject");
  37. * object.set("area", polygon);
  38. * object.save();</pre></p>
  39. *
  40. * @alias Parse.Polygon
  41. */
  42. var ParsePolygon = /*#__PURE__*/function () {
  43. /**
  44. * @param {(number[][] | Parse.GeoPoint[])} coordinates An Array of coordinate pairs
  45. */
  46. function ParsePolygon(coordinates
  47. /*: Array<Array<number>> | Array<ParseGeoPoint>*/
  48. ) {
  49. (0, _classCallCheck2.default)(this, ParsePolygon);
  50. (0, _defineProperty2.default)(this, "_coordinates", void 0);
  51. this._coordinates = ParsePolygon._validate(coordinates);
  52. }
  53. /**
  54. * Coordinates value for this Polygon.
  55. * Throws an exception if not valid type.
  56. *
  57. * @property {(number[][] | Parse.GeoPoint[])} coordinates list of coordinates
  58. * @returns {number[][]}
  59. */
  60. (0, _createClass2.default)(ParsePolygon, [{
  61. key: "coordinates",
  62. get: function ()
  63. /*: Array<Array<number>>*/
  64. {
  65. return this._coordinates;
  66. },
  67. set: function (coords
  68. /*: Array<Array<number>> | Array<ParseGeoPoint>*/
  69. ) {
  70. this._coordinates = ParsePolygon._validate(coords);
  71. }
  72. /**
  73. * Returns a JSON representation of the Polygon, suitable for Parse.
  74. *
  75. * @returns {object}
  76. */
  77. }, {
  78. key: "toJSON",
  79. value: function ()
  80. /*: { __type: string, coordinates: Array<Array<number>> }*/
  81. {
  82. ParsePolygon._validate(this._coordinates);
  83. return {
  84. __type: 'Polygon',
  85. coordinates: this._coordinates
  86. };
  87. }
  88. /**
  89. * Checks if two polygons are equal
  90. *
  91. * @param {(Parse.Polygon | object)} other
  92. * @returns {boolean}
  93. */
  94. }, {
  95. key: "equals",
  96. value: function (other
  97. /*: mixed*/
  98. )
  99. /*: boolean*/
  100. {
  101. if (!(other instanceof ParsePolygon) || this.coordinates.length !== other.coordinates.length) {
  102. return false;
  103. }
  104. var isEqual = true;
  105. for (var i = 1; i < this._coordinates.length; i += 1) {
  106. if (this._coordinates[i][0] !== other.coordinates[i][0] || this._coordinates[i][1] !== other.coordinates[i][1]) {
  107. isEqual = false;
  108. break;
  109. }
  110. }
  111. return isEqual;
  112. }
  113. /**
  114. *
  115. * @param {Parse.GeoPoint} point
  116. * @returns {boolean} Returns if the point is contained in the polygon
  117. */
  118. }, {
  119. key: "containsPoint",
  120. value: function (point
  121. /*: ParseGeoPoint*/
  122. )
  123. /*: boolean*/
  124. {
  125. var minX = this._coordinates[0][0];
  126. var maxX = this._coordinates[0][0];
  127. var minY = this._coordinates[0][1];
  128. var maxY = this._coordinates[0][1];
  129. for (var i = 1; i < this._coordinates.length; i += 1) {
  130. var p = this._coordinates[i];
  131. minX = Math.min(p[0], minX);
  132. maxX = Math.max(p[0], maxX);
  133. minY = Math.min(p[1], minY);
  134. maxY = Math.max(p[1], maxY);
  135. }
  136. var outside = point.latitude < minX || point.latitude > maxX || point.longitude < minY || point.longitude > maxY;
  137. if (outside) {
  138. return false;
  139. }
  140. var inside = false;
  141. for (var _i = 0, j = this._coordinates.length - 1; _i < this._coordinates.length; j = _i++) {
  142. var startX = this._coordinates[_i][0];
  143. var startY = this._coordinates[_i][1];
  144. var endX = this._coordinates[j][0];
  145. var endY = this._coordinates[j][1];
  146. var intersect = startY > point.longitude !== endY > point.longitude && point.latitude < (endX - startX) * (point.longitude - startY) / (endY - startY) + startX;
  147. if (intersect) {
  148. inside = !inside;
  149. }
  150. }
  151. return inside;
  152. }
  153. /**
  154. * Validates that the list of coordinates can form a valid polygon
  155. *
  156. * @param {Array} coords the list of coordinates to validate as a polygon
  157. * @throws {TypeError}
  158. * @returns {number[][]} Array of coordinates if validated.
  159. */
  160. }], [{
  161. key: "_validate",
  162. value: function (coords
  163. /*: Array<Array<number>> | Array<ParseGeoPoint>*/
  164. )
  165. /*: Array<Array<number>>*/
  166. {
  167. if (!(0, _isArray.default)(coords)) {
  168. throw new TypeError('Coordinates must be an Array');
  169. }
  170. if (coords.length < 3) {
  171. throw new TypeError('Polygon must have at least 3 GeoPoints or Points');
  172. }
  173. var points = [];
  174. for (var i = 0; i < coords.length; i += 1) {
  175. var coord = coords[i];
  176. var geoPoint = void 0;
  177. if (coord instanceof _ParseGeoPoint.default) {
  178. geoPoint = coord;
  179. } else if ((0, _isArray.default)(coord) && coord.length === 2) {
  180. geoPoint = new _ParseGeoPoint.default(coord[0], coord[1]);
  181. } else {
  182. throw new TypeError('Coordinates must be an Array of GeoPoints or Points');
  183. }
  184. points.push([geoPoint.latitude, geoPoint.longitude]);
  185. }
  186. return points;
  187. }
  188. }]);
  189. return ParsePolygon;
  190. }();
  191. var _default = ParsePolygon;
  192. exports.default = _default;