ParsePolygon.js 5.1 KB

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