index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var __defProp = Object.defineProperty;
  2. var __defProps = Object.defineProperties;
  3. var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
  4. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  7. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  8. var __spreadValues = (a, b) => {
  9. for (var prop in b || (b = {}))
  10. if (__hasOwnProp.call(b, prop))
  11. __defNormalProp(a, prop, b[prop]);
  12. if (__getOwnPropSymbols)
  13. for (var prop of __getOwnPropSymbols(b)) {
  14. if (__propIsEnum.call(b, prop))
  15. __defNormalProp(a, prop, b[prop]);
  16. }
  17. return a;
  18. };
  19. var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
  20. // index.ts
  21. import { clone } from "@turf/clone";
  22. import { distance } from "@turf/distance";
  23. import { featureEach } from "@turf/meta";
  24. function nearestPoint(targetPoint, points, options = {}) {
  25. if (!targetPoint) throw new Error("targetPoint is required");
  26. if (!points) throw new Error("points is required");
  27. let minDist = Infinity;
  28. let bestFeatureIndex = 0;
  29. featureEach(points, (pt, featureIndex) => {
  30. const distanceToPoint = distance(targetPoint, pt, options);
  31. if (distanceToPoint < minDist) {
  32. bestFeatureIndex = featureIndex;
  33. minDist = distanceToPoint;
  34. }
  35. });
  36. const nearestPoint2 = clone(points.features[bestFeatureIndex]);
  37. return __spreadProps(__spreadValues({}, nearestPoint2), {
  38. properties: __spreadProps(__spreadValues({}, nearestPoint2.properties), {
  39. featureIndex: bestFeatureIndex,
  40. distanceToPoint: minDist
  41. })
  42. });
  43. }
  44. var turf_nearest_point_default = nearestPoint;
  45. export {
  46. turf_nearest_point_default as default,
  47. nearestPoint
  48. };
  49. //# sourceMappingURL=index.js.map