util.js 2.7 KB

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