index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var invariant_1 = require("@turf/invariant");
  4. /**
  5. * Returns true if a point is on a line. Accepts a optional parameter to ignore the
  6. * start and end vertices of the linestring.
  7. *
  8. * @name booleanPointOnLine
  9. * @param {Coord} pt GeoJSON Point
  10. * @param {Feature<LineString>} line GeoJSON LineString
  11. * @param {Object} [options={}] Optional parameters
  12. * @param {boolean} [options.ignoreEndVertices=false] whether to ignore the start and end vertices.
  13. * @param {number} [options.epsilon] Fractional number to compare with the cross product result. Useful for dealing with floating points such as lng/lat points
  14. * @returns {boolean} true/false
  15. * @example
  16. * var pt = turf.point([0, 0]);
  17. * var line = turf.lineString([[-1, -1],[1, 1],[1.5, 2.2]]);
  18. * var isPointOnLine = turf.booleanPointOnLine(pt, line);
  19. * //=true
  20. */
  21. function booleanPointOnLine(pt, line, options) {
  22. if (options === void 0) { options = {}; }
  23. // Normalize inputs
  24. var ptCoords = invariant_1.getCoord(pt);
  25. var lineCoords = invariant_1.getCoords(line);
  26. // Main
  27. for (var i = 0; i < lineCoords.length - 1; i++) {
  28. var ignoreBoundary = false;
  29. if (options.ignoreEndVertices) {
  30. if (i === 0) {
  31. ignoreBoundary = "start";
  32. }
  33. if (i === lineCoords.length - 2) {
  34. ignoreBoundary = "end";
  35. }
  36. if (i === 0 && i + 1 === lineCoords.length - 1) {
  37. ignoreBoundary = "both";
  38. }
  39. }
  40. if (isPointOnLineSegment(lineCoords[i], lineCoords[i + 1], ptCoords, ignoreBoundary, typeof options.epsilon === "undefined" ? null : options.epsilon)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. // See http://stackoverflow.com/a/4833823/1979085
  47. // See https://stackoverflow.com/a/328122/1048847
  48. /**
  49. * @private
  50. * @param {Position} lineSegmentStart coord pair of start of line
  51. * @param {Position} lineSegmentEnd coord pair of end of line
  52. * @param {Position} pt coord pair of point to check
  53. * @param {boolean|string} excludeBoundary whether the point is allowed to fall on the line ends.
  54. * @param {number} epsilon Fractional number to compare with the cross product result. Useful for dealing with floating points such as lng/lat points
  55. * If true which end to ignore.
  56. * @returns {boolean} true/false
  57. */
  58. function isPointOnLineSegment(lineSegmentStart, lineSegmentEnd, pt, excludeBoundary, epsilon) {
  59. var x = pt[0];
  60. var y = pt[1];
  61. var x1 = lineSegmentStart[0];
  62. var y1 = lineSegmentStart[1];
  63. var x2 = lineSegmentEnd[0];
  64. var y2 = lineSegmentEnd[1];
  65. var dxc = pt[0] - x1;
  66. var dyc = pt[1] - y1;
  67. var dxl = x2 - x1;
  68. var dyl = y2 - y1;
  69. var cross = dxc * dyl - dyc * dxl;
  70. if (epsilon !== null) {
  71. if (Math.abs(cross) > epsilon) {
  72. return false;
  73. }
  74. }
  75. else if (cross !== 0) {
  76. return false;
  77. }
  78. if (!excludeBoundary) {
  79. if (Math.abs(dxl) >= Math.abs(dyl)) {
  80. return dxl > 0 ? x1 <= x && x <= x2 : x2 <= x && x <= x1;
  81. }
  82. return dyl > 0 ? y1 <= y && y <= y2 : y2 <= y && y <= y1;
  83. }
  84. else if (excludeBoundary === "start") {
  85. if (Math.abs(dxl) >= Math.abs(dyl)) {
  86. return dxl > 0 ? x1 < x && x <= x2 : x2 <= x && x < x1;
  87. }
  88. return dyl > 0 ? y1 < y && y <= y2 : y2 <= y && y < y1;
  89. }
  90. else if (excludeBoundary === "end") {
  91. if (Math.abs(dxl) >= Math.abs(dyl)) {
  92. return dxl > 0 ? x1 <= x && x < x2 : x2 < x && x <= x1;
  93. }
  94. return dyl > 0 ? y1 <= y && y < y2 : y2 < y && y <= y1;
  95. }
  96. else if (excludeBoundary === "both") {
  97. if (Math.abs(dxl) >= Math.abs(dyl)) {
  98. return dxl > 0 ? x1 < x && x < x2 : x2 < x && x < x1;
  99. }
  100. return dyl > 0 ? y1 < y && y < y2 : y2 < y && y < y1;
  101. }
  102. return false;
  103. }
  104. exports.default = booleanPointOnLine;