index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Taken from http://geomalgorithms.com/a02-_lines.html
  7. var distance_1 = __importDefault(require("@turf/distance"));
  8. var helpers_1 = require("@turf/helpers");
  9. var invariant_1 = require("@turf/invariant");
  10. var meta_1 = require("@turf/meta");
  11. var rhumb_distance_1 = __importDefault(require("@turf/rhumb-distance"));
  12. /**
  13. * Returns the minimum distance between a {@link Point} and a {@link LineString}, being the distance from a line the
  14. * minimum distance between the point and any segment of the `LineString`.
  15. *
  16. * @name pointToLineDistance
  17. * @param {Feature<Point>|Array<number>} pt Feature or Geometry
  18. * @param {Feature<LineString>} line GeoJSON Feature or Geometry
  19. * @param {Object} [options={}] Optional parameters
  20. * @param {string} [options.units="kilometers"] can be anything supported by turf/convertLength
  21. * (ex: degrees, radians, miles, or kilometers)
  22. * @param {string} [options.method="geodesic"] wether to calculate the distance based on geodesic (spheroid) or
  23. * planar (flat) method. Valid options are 'geodesic' or 'planar'.
  24. * @returns {number} distance between point and line
  25. * @example
  26. * var pt = turf.point([0, 0]);
  27. * var line = turf.lineString([[1, 1],[-1, 1]]);
  28. *
  29. * var distance = turf.pointToLineDistance(pt, line, {units: 'miles'});
  30. * //=69.11854715938406
  31. */
  32. function pointToLineDistance(pt, line, options) {
  33. if (options === void 0) { options = {}; }
  34. // Optional parameters
  35. if (!options.method) {
  36. options.method = "geodesic";
  37. }
  38. if (!options.units) {
  39. options.units = "kilometers";
  40. }
  41. // validation
  42. if (!pt) {
  43. throw new Error("pt is required");
  44. }
  45. if (Array.isArray(pt)) {
  46. pt = helpers_1.point(pt);
  47. }
  48. else if (pt.type === "Point") {
  49. pt = helpers_1.feature(pt);
  50. }
  51. else {
  52. invariant_1.featureOf(pt, "Point", "point");
  53. }
  54. if (!line) {
  55. throw new Error("line is required");
  56. }
  57. if (Array.isArray(line)) {
  58. line = helpers_1.lineString(line);
  59. }
  60. else if (line.type === "LineString") {
  61. line = helpers_1.feature(line);
  62. }
  63. else {
  64. invariant_1.featureOf(line, "LineString", "line");
  65. }
  66. var distance = Infinity;
  67. var p = pt.geometry.coordinates;
  68. meta_1.segmentEach(line, function (segment) {
  69. var a = segment.geometry.coordinates[0];
  70. var b = segment.geometry.coordinates[1];
  71. var d = distanceToSegment(p, a, b, options);
  72. if (d < distance) {
  73. distance = d;
  74. }
  75. });
  76. return helpers_1.convertLength(distance, "degrees", options.units);
  77. }
  78. /**
  79. * Returns the distance between a point P on a segment AB.
  80. *
  81. * @private
  82. * @param {Array<number>} p external point
  83. * @param {Array<number>} a first segment point
  84. * @param {Array<number>} b second segment point
  85. * @param {Object} [options={}] Optional parameters
  86. * @returns {number} distance
  87. */
  88. function distanceToSegment(p, a, b, options) {
  89. var v = [b[0] - a[0], b[1] - a[1]];
  90. var w = [p[0] - a[0], p[1] - a[1]];
  91. var c1 = dot(w, v);
  92. if (c1 <= 0) {
  93. return calcDistance(p, a, { method: options.method, units: "degrees" });
  94. }
  95. var c2 = dot(v, v);
  96. if (c2 <= c1) {
  97. return calcDistance(p, b, { method: options.method, units: "degrees" });
  98. }
  99. var b2 = c1 / c2;
  100. var Pb = [a[0] + b2 * v[0], a[1] + b2 * v[1]];
  101. return calcDistance(p, Pb, { method: options.method, units: "degrees" });
  102. }
  103. function dot(u, v) {
  104. return u[0] * v[0] + u[1] * v[1];
  105. }
  106. function calcDistance(a, b, options) {
  107. return options.method === "planar"
  108. ? rhumb_distance_1.default(a, b, options)
  109. : distance_1.default(a, b, options);
  110. }
  111. exports.default = pointToLineDistance;