index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 invariant_1 = require("@turf/invariant");
  7. var meta_1 = require("@turf/meta");
  8. var point_to_line_distance_1 = __importDefault(require("@turf/point-to-line-distance"));
  9. var object_assign_1 = __importDefault(require("object-assign"));
  10. /**
  11. * Returns the closest {@link Point|point}, of a {@link FeatureCollection|collection} of points,
  12. * to a {@link LineString|line}. The returned point has a `dist` property indicating its distance to the line.
  13. *
  14. * @name nearestPointToLine
  15. * @param {FeatureCollection|GeometryCollection<Point>} points Point Collection
  16. * @param {Feature|Geometry<LineString>} line Line Feature
  17. * @param {Object} [options] Optional parameters
  18. * @param {string} [options.units='kilometers'] unit of the output distance property
  19. * (eg: degrees, radians, miles, or kilometers)
  20. * @param {Object} [options.properties={}] Translate Properties to Point
  21. * @returns {Feature<Point>} the closest point
  22. * @example
  23. * var pt1 = turf.point([0, 0]);
  24. * var pt2 = turf.point([0.5, 0.5]);
  25. * var points = turf.featureCollection([pt1, pt2]);
  26. * var line = turf.lineString([[1,1], [-1,1]]);
  27. *
  28. * var nearest = turf.nearestPointToLine(points, line);
  29. *
  30. * //addToMap
  31. * var addToMap = [nearest, line];
  32. */
  33. function nearestPointToLine(points, line, options) {
  34. if (options === void 0) { options = {}; }
  35. var units = options.units;
  36. var properties = options.properties || {};
  37. // validation
  38. var pts = normalize(points);
  39. if (!pts.features.length) {
  40. throw new Error("points must contain features");
  41. }
  42. if (!line) {
  43. throw new Error("line is required");
  44. }
  45. if (invariant_1.getType(line) !== "LineString") {
  46. throw new Error("line must be a LineString");
  47. }
  48. var dist = Infinity;
  49. var pt = null;
  50. meta_1.featureEach(pts, function (point) {
  51. var d = point_to_line_distance_1.default(point, line, { units: units });
  52. if (d < dist) {
  53. dist = d;
  54. pt = point;
  55. }
  56. });
  57. /**
  58. * Translate Properties to final Point, priorities:
  59. * 1. options.properties
  60. * 2. inherent Point properties
  61. * 3. dist custom properties created by NearestPointToLine
  62. */
  63. if (pt) {
  64. pt.properties = object_assign_1.default({ dist: dist }, pt.properties, properties);
  65. }
  66. // if (pt) { pt.properties = objectAssign({dist}, pt.properties, properties); }
  67. return pt;
  68. }
  69. /**
  70. * Convert Collection to FeatureCollection
  71. *
  72. * @private
  73. * @param {FeatureCollection|GeometryCollection} points Points
  74. * @returns {FeatureCollection<Point>} points
  75. */
  76. function normalize(points) {
  77. var features = [];
  78. var type = points.geometry ? points.geometry.type : points.type;
  79. switch (type) {
  80. case "GeometryCollection":
  81. meta_1.geomEach(points, function (geom) {
  82. if (geom.type === "Point") {
  83. features.push({ type: "Feature", properties: {}, geometry: geom });
  84. }
  85. });
  86. return { type: "FeatureCollection", features: features };
  87. case "FeatureCollection":
  88. points.features = points.features.filter(function (feature) {
  89. return feature.geometry.type === "Point";
  90. });
  91. return points;
  92. default:
  93. throw new Error("points must be a Point Collection");
  94. }
  95. }
  96. exports.default = nearestPointToLine;