util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in-polygon"));
  7. var helpers_1 = require("@turf/helpers");
  8. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign#Polyfill
  9. function mathSign(x) {
  10. return ((x > 0) - (x < 0) || +x);
  11. }
  12. /**
  13. * Returns the direction of the point q relative to the vector p1 -> p2.
  14. *
  15. * Implementation of geos::algorithm::CGAlgorithm::orientationIndex()
  16. * (same as geos::algorithm::CGAlgorithm::computeOrientation())
  17. *
  18. * @param {number[]} p1 - the origin point of the vector
  19. * @param {number[]} p2 - the final point of the vector
  20. * @param {number[]} q - the point to compute the direction to
  21. *
  22. * @returns {number} - 1 if q is ccw (left) from p1->p2,
  23. * -1 if q is cw (right) from p1->p2,
  24. * 0 if q is colinear with p1->p2
  25. */
  26. function orientationIndex(p1, p2, q) {
  27. var dx1 = p2[0] - p1[0], dy1 = p2[1] - p1[1], dx2 = q[0] - p2[0], dy2 = q[1] - p2[1];
  28. return mathSign(dx1 * dy2 - dx2 * dy1);
  29. }
  30. exports.orientationIndex = orientationIndex;
  31. /**
  32. * Checks if two envelopes are equal.
  33. *
  34. * The function assumes that the arguments are envelopes, i.e.: Rectangular polygon
  35. *
  36. * @param {Feature<Polygon>} env1 - Envelope
  37. * @param {Feature<Polygon>} env2 - Envelope
  38. * @returns {boolean} - True if the envelopes are equal
  39. */
  40. function envelopeIsEqual(env1, env2) {
  41. var envX1 = env1.geometry.coordinates[0].map(function (c) { return c[0]; }), envY1 = env1.geometry.coordinates[0].map(function (c) { return c[1]; }), envX2 = env2.geometry.coordinates[0].map(function (c) { return c[0]; }), envY2 = env2.geometry.coordinates[0].map(function (c) { return c[1]; });
  42. return (Math.max.apply(null, envX1) === Math.max.apply(null, envX2) &&
  43. Math.max.apply(null, envY1) === Math.max.apply(null, envY2) &&
  44. Math.min.apply(null, envX1) === Math.min.apply(null, envX2) &&
  45. Math.min.apply(null, envY1) === Math.min.apply(null, envY2));
  46. }
  47. exports.envelopeIsEqual = envelopeIsEqual;
  48. /**
  49. * Check if a envelope is contained in other one.
  50. *
  51. * The function assumes that the arguments are envelopes, i.e.: Convex polygon
  52. * XXX: Envelopes are rectangular, checking if a point is inside a rectangule is something easy,
  53. * this could be further improved.
  54. *
  55. * @param {Feature<Polygon>} self - Envelope
  56. * @param {Feature<Polygon>} env - Envelope
  57. * @returns {boolean} - True if env is contained in self
  58. */
  59. function envelopeContains(self, env) {
  60. return env.geometry.coordinates[0].every(function (c) {
  61. return boolean_point_in_polygon_1.default(helpers_1.point(c), self);
  62. });
  63. }
  64. exports.envelopeContains = envelopeContains;
  65. /**
  66. * Checks if two coordinates are equal.
  67. *
  68. * @param {number[]} coord1 - First coordinate
  69. * @param {number[]} coord2 - Second coordinate
  70. * @returns {boolean} - True if coordinates are equal
  71. */
  72. function coordinatesEqual(coord1, coord2) {
  73. return coord1[0] === coord2[0] && coord1[1] === coord2[1];
  74. }
  75. exports.coordinatesEqual = coordinatesEqual;