index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  3. var __hasOwnProp = Object.prototype.hasOwnProperty;
  4. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  5. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  6. var __spreadValues = (a, b) => {
  7. for (var prop in b || (b = {}))
  8. if (__hasOwnProp.call(b, prop))
  9. __defNormalProp(a, prop, b[prop]);
  10. if (__getOwnPropSymbols)
  11. for (var prop of __getOwnPropSymbols(b)) {
  12. if (__propIsEnum.call(b, prop))
  13. __defNormalProp(a, prop, b[prop]);
  14. }
  15. return a;
  16. };
  17. // index.ts
  18. import { getType } from "@turf/invariant";
  19. import { featureEach, geomEach } from "@turf/meta";
  20. import { pointToLineDistance } from "@turf/point-to-line-distance";
  21. function nearestPointToLine(points, line, options = {}) {
  22. const units = options.units;
  23. const properties = options.properties || {};
  24. const pts = normalize(points);
  25. if (!pts.features.length) {
  26. throw new Error("points must contain features");
  27. }
  28. if (!line) {
  29. throw new Error("line is required");
  30. }
  31. if (getType(line) !== "LineString") {
  32. throw new Error("line must be a LineString");
  33. }
  34. let dist = Infinity;
  35. let pt = null;
  36. featureEach(pts, (point) => {
  37. const d = pointToLineDistance(point, line, { units });
  38. if (d < dist) {
  39. dist = d;
  40. pt = point;
  41. }
  42. });
  43. if (pt) {
  44. pt.properties = __spreadValues(__spreadValues(__spreadValues({}, { dist }), pt.properties), properties);
  45. }
  46. return pt;
  47. }
  48. function normalize(points) {
  49. const features = [];
  50. const type = points.geometry ? points.geometry.type : points.type;
  51. switch (type) {
  52. case "GeometryCollection":
  53. geomEach(points, (geom) => {
  54. if (geom.type === "Point") {
  55. features.push({ type: "Feature", properties: {}, geometry: geom });
  56. }
  57. });
  58. return { type: "FeatureCollection", features };
  59. case "FeatureCollection":
  60. points.features = points.features.filter((feature) => {
  61. return feature.geometry.type === "Point";
  62. });
  63. return points;
  64. default:
  65. throw new Error("points must be a Point Collection");
  66. }
  67. }
  68. var turf_nearest_point_to_line_default = nearestPointToLine;
  69. export {
  70. turf_nearest_point_to_line_default as default,
  71. nearestPointToLine
  72. };
  73. //# sourceMappingURL=index.js.map