ParsePolygon.js 5.9 KB

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