index.cjs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _helpers = require('@turf/helpers');
  3. var _nearestpointonline = require('@turf/nearest-point-on-line');
  4. var _invariant = require('@turf/invariant');
  5. var _meta = require('@turf/meta');
  6. var _rhumbdistance = require('@turf/rhumb-distance');
  7. function pointToLineDistance(pt, line, options = {}) {
  8. var _a, _b;
  9. const method = (_a = options.method) != null ? _a : "geodesic";
  10. const units = (_b = options.units) != null ? _b : "kilometers";
  11. if (!pt) {
  12. throw new Error("pt is required");
  13. }
  14. if (Array.isArray(pt)) {
  15. pt = _helpers.point.call(void 0, pt);
  16. } else if (pt.type === "Point") {
  17. pt = _helpers.feature.call(void 0, pt);
  18. } else {
  19. _invariant.featureOf.call(void 0, pt, "Point", "point");
  20. }
  21. if (!line) {
  22. throw new Error("line is required");
  23. }
  24. if (Array.isArray(line)) {
  25. line = _helpers.lineString.call(void 0, line);
  26. } else if (line.type === "LineString") {
  27. line = _helpers.feature.call(void 0, line);
  28. } else {
  29. _invariant.featureOf.call(void 0, line, "LineString", "line");
  30. }
  31. let distance = Infinity;
  32. const p = pt.geometry.coordinates;
  33. _meta.segmentEach.call(void 0, line, (segment) => {
  34. if (segment) {
  35. const a = segment.geometry.coordinates[0];
  36. const b = segment.geometry.coordinates[1];
  37. const d = distanceToSegment(p, a, b, { method });
  38. if (d < distance) {
  39. distance = d;
  40. }
  41. }
  42. });
  43. return _helpers.convertLength.call(void 0, distance, "degrees", units);
  44. }
  45. function distanceToSegment(p, a, b, options) {
  46. if (options.method === "geodesic") {
  47. const nearest = _nearestpointonline.nearestPointOnLine.call(void 0, _helpers.lineString.call(void 0, [a, b]).geometry, p, {
  48. units: "degrees"
  49. });
  50. return nearest.properties.dist;
  51. }
  52. const v = [b[0] - a[0], b[1] - a[1]];
  53. const w = [p[0] - a[0], p[1] - a[1]];
  54. const c1 = dot(w, v);
  55. if (c1 <= 0) {
  56. return _rhumbdistance.rhumbDistance.call(void 0, p, a, { units: "degrees" });
  57. }
  58. const c2 = dot(v, v);
  59. if (c2 <= c1) {
  60. return _rhumbdistance.rhumbDistance.call(void 0, p, b, { units: "degrees" });
  61. }
  62. const b2 = c1 / c2;
  63. const Pb = [a[0] + b2 * v[0], a[1] + b2 * v[1]];
  64. return _rhumbdistance.rhumbDistance.call(void 0, p, Pb, { units: "degrees" });
  65. }
  66. function dot(u, v) {
  67. return u[0] * v[0] + u[1] * v[1];
  68. }
  69. var turf_point_to_line_distance_default = pointToLineDistance;
  70. exports.default = turf_point_to_line_distance_default; exports.pointToLineDistance = pointToLineDistance;
  71. //# sourceMappingURL=index.cjs.map