@turf/nearest-point 7.0.0-alpha.0 → 7.0.0-alpha.110

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,6 +13,9 @@ is geodesic.
13
13
 
14
14
  * `targetPoint` **[Coord][2]** the reference point
15
15
  * `points` **[FeatureCollection][3]<[Point][4]>** against input point set
16
+ * `options` **[Object][5]** Optional parameters (optional, default `{}`)
17
+
18
+ * `options.units` **[string][6]** the units of the numeric result (optional, default `'kilometers'`)
16
19
 
17
20
  ### Examples
18
21
 
@@ -31,7 +34,7 @@ var addToMap = [targetPoint, points, nearest];
31
34
  nearest.properties['marker-color'] = '#F00';
32
35
  ```
33
36
 
34
- Returns **[Feature][5]<[Point][4]>** the closest point in the set to the reference point
37
+ Returns **[Feature][7]<[Point][4]>** the closest point in the set to the reference point
35
38
 
36
39
  [1]: https://tools.ietf.org/html/rfc7946#section-3.1.2
37
40
 
@@ -41,28 +44,27 @@ Returns **[Feature][5]<[Point][4]>** the closest point in the set to the referen
41
44
 
42
45
  [4]: https://tools.ietf.org/html/rfc7946#section-3.1.2
43
46
 
44
- [5]: https://tools.ietf.org/html/rfc7946#section-3.2
47
+ [5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
48
+
49
+ [6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
50
+
51
+ [7]: https://tools.ietf.org/html/rfc7946#section-3.2
45
52
 
46
- <!-- This file is automatically generated. Please don't edit it directly:
47
- if you find an error, edit the source file (likely index.js), and re-run
48
- ./scripts/generate-readmes in the turf project. -->
53
+ <!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
49
54
 
50
55
  ---
51
56
 
52
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
53
- module collection dedicated to geographic algorithms. It is maintained in the
54
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
55
- PRs and issues.
57
+ This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
56
58
 
57
59
  ### Installation
58
60
 
59
- Install this module individually:
61
+ Install this single module individually:
60
62
 
61
63
  ```sh
62
64
  $ npm install @turf/nearest-point
63
65
  ```
64
66
 
65
- Or install the Turf module that includes it as a function:
67
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
66
68
 
67
69
  ```sh
68
70
  $ npm install @turf/turf
@@ -0,0 +1,54 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});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
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
21
+
22
+ // index.ts
23
+ var _clone = require('@turf/clone');
24
+ var _distance = require('@turf/distance');
25
+ var _meta = require('@turf/meta');
26
+ function nearestPoint(targetPoint, points, options = {}) {
27
+ if (!targetPoint)
28
+ throw new Error("targetPoint is required");
29
+ if (!points)
30
+ throw new Error("points is required");
31
+ let minDist = Infinity;
32
+ let bestFeatureIndex = 0;
33
+ _meta.featureEach.call(void 0, points, (pt, featureIndex) => {
34
+ const distanceToPoint = _distance.distance.call(void 0, targetPoint, pt, options);
35
+ if (distanceToPoint < minDist) {
36
+ bestFeatureIndex = featureIndex;
37
+ minDist = distanceToPoint;
38
+ }
39
+ });
40
+ const nearestPoint2 = _clone.clone.call(void 0, points.features[bestFeatureIndex]);
41
+ return __spreadProps(__spreadValues({}, nearestPoint2), {
42
+ properties: __spreadProps(__spreadValues({}, nearestPoint2.properties), {
43
+ featureIndex: bestFeatureIndex,
44
+ distanceToPoint: minDist
45
+ })
46
+ });
47
+ }
48
+ __name(nearestPoint, "nearestPoint");
49
+ var turf_nearest_point_default = nearestPoint;
50
+
51
+
52
+
53
+ exports.default = turf_nearest_point_default; exports.nearestPoint = nearestPoint;
54
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"names":["nearestPoint"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,aAAa;AACtB,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAoC5B,SAAS,aACP,aACA,QACA,UAEI,CAAC,GACS;AAEd,MAAI,CAAC;AAAa,UAAM,IAAI,MAAM,yBAAyB;AAC3D,MAAI,CAAC;AAAQ,UAAM,IAAI,MAAM,oBAAoB;AAEjD,MAAI,UAAU;AACd,MAAI,mBAAmB;AACvB,cAAY,QAAQ,CAAC,IAAI,iBAAiB;AACxC,UAAM,kBAAkB,SAAS,aAAa,IAAI,OAAO;AACzD,QAAI,kBAAkB,SAAS;AAC7B,yBAAmB;AACnB,gBAAU;AAAA,IACZ;AAAA,EACF,CAAC;AACD,QAAMA,gBAAe,MAAM,OAAO,SAAS,gBAAgB,CAAC;AAE5D,SAAO,iCACFA,gBADE;AAAA,IAEL,YAAY,iCACPA,cAAa,aADN;AAAA,MAEV,cAAc;AAAA,MACd,iBAAiB;AAAA,IACnB;AAAA,EACF;AACF;AA9BS;AAiCT,IAAO,6BAAQ","sourcesContent":["import { Feature, FeatureCollection, Point } from \"geojson\";\nimport { Coord, Units } from \"@turf/helpers\";\nimport { clone } from \"@turf/clone\";\nimport { distance } from \"@turf/distance\";\nimport { featureEach } from \"@turf/meta\";\n\ninterface NearestPoint extends Feature<Point> {\n properties: {\n featureIndex: number;\n distanceToPoint: number;\n [key: string]: any;\n };\n}\n\n/**\n * Takes a reference {@link Point|point} and a FeatureCollection of Features\n * with Point geometries and returns the\n * point from the FeatureCollection closest to the reference. This calculation\n * is geodesic.\n *\n * @name nearestPoint\n * @param {Coord} targetPoint the reference point\n * @param {FeatureCollection<Point>} points against input point set\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] the units of the numeric result\n * @returns {Feature<Point>} the closest point in the set to the reference point\n * @example\n * var targetPoint = turf.point([28.965797, 41.010086], {\"marker-color\": \"#0F0\"});\n * var points = turf.featureCollection([\n * turf.point([28.973865, 41.011122]),\n * turf.point([28.948459, 41.024204]),\n * turf.point([28.938674, 41.013324])\n * ]);\n *\n * var nearest = turf.nearestPoint(targetPoint, points);\n *\n * //addToMap\n * var addToMap = [targetPoint, points, nearest];\n * nearest.properties['marker-color'] = '#F00';\n */\nfunction nearestPoint(\n targetPoint: Coord,\n points: FeatureCollection<Point>,\n options: {\n units?: Units;\n } = {}\n): NearestPoint {\n // Input validation\n if (!targetPoint) throw new Error(\"targetPoint is required\");\n if (!points) throw new Error(\"points is required\");\n\n let minDist = Infinity;\n let bestFeatureIndex = 0;\n featureEach(points, (pt, featureIndex) => {\n const distanceToPoint = distance(targetPoint, pt, options);\n if (distanceToPoint < minDist) {\n bestFeatureIndex = featureIndex;\n minDist = distanceToPoint;\n }\n });\n const nearestPoint = clone(points.features[bestFeatureIndex]);\n\n return {\n ...nearestPoint,\n properties: {\n ...nearestPoint.properties,\n featureIndex: bestFeatureIndex,\n distanceToPoint: minDist,\n },\n };\n}\n\nexport { nearestPoint, NearestPoint };\nexport default nearestPoint;\n"]}
@@ -1,6 +1,7 @@
1
- import { Feature, FeatureCollection, Point } from "geojson";
2
- import { Coord } from "@turf/helpers";
3
- export interface NearestPoint extends Feature<Point> {
1
+ import { Feature, Point, FeatureCollection } from 'geojson';
2
+ import { Coord, Units } from '@turf/helpers';
3
+
4
+ interface NearestPoint extends Feature<Point> {
4
5
  properties: {
5
6
  featureIndex: number;
6
7
  distanceToPoint: number;
@@ -16,6 +17,8 @@ export interface NearestPoint extends Feature<Point> {
16
17
  * @name nearestPoint
17
18
  * @param {Coord} targetPoint the reference point
18
19
  * @param {FeatureCollection<Point>} points against input point set
20
+ * @param {Object} [options={}] Optional parameters
21
+ * @param {string} [options.units='kilometers'] the units of the numeric result
19
22
  * @returns {Feature<Point>} the closest point in the set to the reference point
20
23
  * @example
21
24
  * var targetPoint = turf.point([28.965797, 41.010086], {"marker-color": "#0F0"});
@@ -31,5 +34,8 @@ export interface NearestPoint extends Feature<Point> {
31
34
  * var addToMap = [targetPoint, points, nearest];
32
35
  * nearest.properties['marker-color'] = '#F00';
33
36
  */
34
- declare function nearestPoint(targetPoint: Coord, points: FeatureCollection<Point>): NearestPoint;
35
- export default nearestPoint;
37
+ declare function nearestPoint(targetPoint: Coord, points: FeatureCollection<Point>, options?: {
38
+ units?: Units;
39
+ }): NearestPoint;
40
+
41
+ export { type NearestPoint, nearestPoint as default, nearestPoint };
@@ -0,0 +1,41 @@
1
+ import { Feature, Point, FeatureCollection } from 'geojson';
2
+ import { Coord, Units } from '@turf/helpers';
3
+
4
+ interface NearestPoint extends Feature<Point> {
5
+ properties: {
6
+ featureIndex: number;
7
+ distanceToPoint: number;
8
+ [key: string]: any;
9
+ };
10
+ }
11
+ /**
12
+ * Takes a reference {@link Point|point} and a FeatureCollection of Features
13
+ * with Point geometries and returns the
14
+ * point from the FeatureCollection closest to the reference. This calculation
15
+ * is geodesic.
16
+ *
17
+ * @name nearestPoint
18
+ * @param {Coord} targetPoint the reference point
19
+ * @param {FeatureCollection<Point>} points against input point set
20
+ * @param {Object} [options={}] Optional parameters
21
+ * @param {string} [options.units='kilometers'] the units of the numeric result
22
+ * @returns {Feature<Point>} the closest point in the set to the reference point
23
+ * @example
24
+ * var targetPoint = turf.point([28.965797, 41.010086], {"marker-color": "#0F0"});
25
+ * var points = turf.featureCollection([
26
+ * turf.point([28.973865, 41.011122]),
27
+ * turf.point([28.948459, 41.024204]),
28
+ * turf.point([28.938674, 41.013324])
29
+ * ]);
30
+ *
31
+ * var nearest = turf.nearestPoint(targetPoint, points);
32
+ *
33
+ * //addToMap
34
+ * var addToMap = [targetPoint, points, nearest];
35
+ * nearest.properties['marker-color'] = '#F00';
36
+ */
37
+ declare function nearestPoint(targetPoint: Coord, points: FeatureCollection<Point>, options?: {
38
+ units?: Units;
39
+ }): NearestPoint;
40
+
41
+ export { type NearestPoint, nearestPoint as default, nearestPoint };
@@ -0,0 +1,54 @@
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
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
21
+
22
+ // index.ts
23
+ import { clone } from "@turf/clone";
24
+ import { distance } from "@turf/distance";
25
+ import { featureEach } from "@turf/meta";
26
+ function nearestPoint(targetPoint, points, options = {}) {
27
+ if (!targetPoint)
28
+ throw new Error("targetPoint is required");
29
+ if (!points)
30
+ throw new Error("points is required");
31
+ let minDist = Infinity;
32
+ let bestFeatureIndex = 0;
33
+ featureEach(points, (pt, featureIndex) => {
34
+ const distanceToPoint = distance(targetPoint, pt, options);
35
+ if (distanceToPoint < minDist) {
36
+ bestFeatureIndex = featureIndex;
37
+ minDist = distanceToPoint;
38
+ }
39
+ });
40
+ const nearestPoint2 = clone(points.features[bestFeatureIndex]);
41
+ return __spreadProps(__spreadValues({}, nearestPoint2), {
42
+ properties: __spreadProps(__spreadValues({}, nearestPoint2.properties), {
43
+ featureIndex: bestFeatureIndex,
44
+ distanceToPoint: minDist
45
+ })
46
+ });
47
+ }
48
+ __name(nearestPoint, "nearestPoint");
49
+ var turf_nearest_point_default = nearestPoint;
50
+ export {
51
+ turf_nearest_point_default as default,
52
+ nearestPoint
53
+ };
54
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts"],"sourcesContent":["import { Feature, FeatureCollection, Point } from \"geojson\";\nimport { Coord, Units } from \"@turf/helpers\";\nimport { clone } from \"@turf/clone\";\nimport { distance } from \"@turf/distance\";\nimport { featureEach } from \"@turf/meta\";\n\ninterface NearestPoint extends Feature<Point> {\n properties: {\n featureIndex: number;\n distanceToPoint: number;\n [key: string]: any;\n };\n}\n\n/**\n * Takes a reference {@link Point|point} and a FeatureCollection of Features\n * with Point geometries and returns the\n * point from the FeatureCollection closest to the reference. This calculation\n * is geodesic.\n *\n * @name nearestPoint\n * @param {Coord} targetPoint the reference point\n * @param {FeatureCollection<Point>} points against input point set\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] the units of the numeric result\n * @returns {Feature<Point>} the closest point in the set to the reference point\n * @example\n * var targetPoint = turf.point([28.965797, 41.010086], {\"marker-color\": \"#0F0\"});\n * var points = turf.featureCollection([\n * turf.point([28.973865, 41.011122]),\n * turf.point([28.948459, 41.024204]),\n * turf.point([28.938674, 41.013324])\n * ]);\n *\n * var nearest = turf.nearestPoint(targetPoint, points);\n *\n * //addToMap\n * var addToMap = [targetPoint, points, nearest];\n * nearest.properties['marker-color'] = '#F00';\n */\nfunction nearestPoint(\n targetPoint: Coord,\n points: FeatureCollection<Point>,\n options: {\n units?: Units;\n } = {}\n): NearestPoint {\n // Input validation\n if (!targetPoint) throw new Error(\"targetPoint is required\");\n if (!points) throw new Error(\"points is required\");\n\n let minDist = Infinity;\n let bestFeatureIndex = 0;\n featureEach(points, (pt, featureIndex) => {\n const distanceToPoint = distance(targetPoint, pt, options);\n if (distanceToPoint < minDist) {\n bestFeatureIndex = featureIndex;\n minDist = distanceToPoint;\n }\n });\n const nearestPoint = clone(points.features[bestFeatureIndex]);\n\n return {\n ...nearestPoint,\n properties: {\n ...nearestPoint.properties,\n featureIndex: bestFeatureIndex,\n distanceToPoint: minDist,\n },\n };\n}\n\nexport { nearestPoint, NearestPoint };\nexport default nearestPoint;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,aAAa;AACtB,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAoC5B,SAAS,aACP,aACA,QACA,UAEI,CAAC,GACS;AAEd,MAAI,CAAC;AAAa,UAAM,IAAI,MAAM,yBAAyB;AAC3D,MAAI,CAAC;AAAQ,UAAM,IAAI,MAAM,oBAAoB;AAEjD,MAAI,UAAU;AACd,MAAI,mBAAmB;AACvB,cAAY,QAAQ,CAAC,IAAI,iBAAiB;AACxC,UAAM,kBAAkB,SAAS,aAAa,IAAI,OAAO;AACzD,QAAI,kBAAkB,SAAS;AAC7B,yBAAmB;AACnB,gBAAU;AAAA,IACZ;AAAA,EACF,CAAC;AACD,QAAMA,gBAAe,MAAM,OAAO,SAAS,gBAAgB,CAAC;AAE5D,SAAO,iCACFA,gBADE;AAAA,IAEL,YAAY,iCACPA,cAAa,aADN;AAAA,MAEV,cAAc;AAAA,MACd,iBAAiB;AAAA,IACnB;AAAA,EACF;AACF;AA9BS;AAiCT,IAAO,6BAAQ;","names":["nearestPoint"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/nearest-point",
3
- "version": "7.0.0-alpha.0",
3
+ "version": "7.0.0-alpha.110+1411d63a7",
4
4
  "description": "turf nearest-point module",
5
5
  "author": "Turf Authors",
6
6
  "license": "MIT",
@@ -24,45 +24,51 @@
24
24
  "nearest",
25
25
  "point"
26
26
  ],
27
- "main": "dist/js/index.js",
28
- "module": "dist/es/index.js",
27
+ "type": "commonjs",
28
+ "main": "dist/cjs/index.cjs",
29
+ "module": "dist/esm/index.mjs",
30
+ "types": "dist/cjs/index.d.ts",
29
31
  "exports": {
30
32
  "./package.json": "./package.json",
31
33
  ".": {
32
- "import": "./dist/es/index.js",
33
- "require": "./dist/js/index.js"
34
+ "import": {
35
+ "types": "./dist/esm/index.d.mts",
36
+ "default": "./dist/esm/index.mjs"
37
+ },
38
+ "require": {
39
+ "types": "./dist/cjs/index.d.ts",
40
+ "default": "./dist/cjs/index.cjs"
41
+ }
34
42
  }
35
43
  },
36
- "types": "dist/js/index.d.ts",
37
44
  "sideEffects": false,
38
45
  "files": [
39
46
  "dist"
40
47
  ],
41
48
  "scripts": {
42
- "bench": "ts-node bench.js",
43
- "build": "npm-run-all build:*",
44
- "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
45
- "build:js": "tsc",
46
- "docs": "node ../../scripts/generate-readmes",
47
- "test": "npm-run-all test:*",
48
- "test:tape": "ts-node -r esm test.js",
49
+ "bench": "tsx bench.ts",
50
+ "build": "tsup --config ../../tsup.config.ts",
51
+ "docs": "tsx ../../scripts/generate-readmes.ts",
52
+ "test": "npm-run-all --npm-path npm test:*",
53
+ "test:tape": "tsx test.ts",
49
54
  "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
50
55
  },
51
56
  "devDependencies": {
52
- "@types/tape": "*",
53
- "benchmark": "*",
54
- "npm-run-all": "*",
55
- "tape": "*",
56
- "ts-node": "*",
57
- "tslint": "*",
58
- "typescript": "*"
57
+ "@types/benchmark": "^2.1.5",
58
+ "@types/tape": "^4.2.32",
59
+ "benchmark": "^2.1.4",
60
+ "npm-run-all": "^4.1.5",
61
+ "tape": "^5.7.2",
62
+ "tsup": "^8.0.1",
63
+ "tsx": "^4.6.2",
64
+ "typescript": "^5.2.2"
59
65
  },
60
66
  "dependencies": {
61
- "@turf/clone": "^7.0.0-alpha.0",
62
- "@turf/distance": "^7.0.0-alpha.0",
63
- "@turf/helpers": "^7.0.0-alpha.0",
64
- "@turf/meta": "^7.0.0-alpha.0",
65
- "tslib": "^2.3.0"
67
+ "@turf/clone": "^7.0.0-alpha.110+1411d63a7",
68
+ "@turf/distance": "^7.0.0-alpha.110+1411d63a7",
69
+ "@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
70
+ "@turf/meta": "^7.0.0-alpha.110+1411d63a7",
71
+ "tslib": "^2.6.2"
66
72
  },
67
- "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
73
+ "gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
68
74
  }
package/dist/es/index.js DELETED
@@ -1,46 +0,0 @@
1
- import clone from "@turf/clone";
2
- import distance from "@turf/distance";
3
- import { featureEach } from "@turf/meta";
4
- /**
5
- * Takes a reference {@link Point|point} and a FeatureCollection of Features
6
- * with Point geometries and returns the
7
- * point from the FeatureCollection closest to the reference. This calculation
8
- * is geodesic.
9
- *
10
- * @name nearestPoint
11
- * @param {Coord} targetPoint the reference point
12
- * @param {FeatureCollection<Point>} points against input point set
13
- * @returns {Feature<Point>} the closest point in the set to the reference point
14
- * @example
15
- * var targetPoint = turf.point([28.965797, 41.010086], {"marker-color": "#0F0"});
16
- * var points = turf.featureCollection([
17
- * turf.point([28.973865, 41.011122]),
18
- * turf.point([28.948459, 41.024204]),
19
- * turf.point([28.938674, 41.013324])
20
- * ]);
21
- *
22
- * var nearest = turf.nearestPoint(targetPoint, points);
23
- *
24
- * //addToMap
25
- * var addToMap = [targetPoint, points, nearest];
26
- * nearest.properties['marker-color'] = '#F00';
27
- */
28
- function nearestPoint(targetPoint, points) {
29
- // Input validation
30
- if (!targetPoint)
31
- throw new Error("targetPoint is required");
32
- if (!points)
33
- throw new Error("points is required");
34
- let minDist = Infinity;
35
- let bestFeatureIndex = 0;
36
- featureEach(points, (pt, featureIndex) => {
37
- const distanceToPoint = distance(targetPoint, pt);
38
- if (distanceToPoint < minDist) {
39
- bestFeatureIndex = featureIndex;
40
- minDist = distanceToPoint;
41
- }
42
- });
43
- const nearestPoint = clone(points.features[bestFeatureIndex]);
44
- return Object.assign(Object.assign({}, nearestPoint), { properties: Object.assign(Object.assign({}, nearestPoint.properties), { featureIndex: bestFeatureIndex, distanceToPoint: minDist }) });
45
- }
46
- export default nearestPoint;
@@ -1 +0,0 @@
1
- {"type":"module"}
package/dist/js/index.js DELETED
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const clone_1 = tslib_1.__importDefault(require("@turf/clone"));
5
- const distance_1 = tslib_1.__importDefault(require("@turf/distance"));
6
- const meta_1 = require("@turf/meta");
7
- /**
8
- * Takes a reference {@link Point|point} and a FeatureCollection of Features
9
- * with Point geometries and returns the
10
- * point from the FeatureCollection closest to the reference. This calculation
11
- * is geodesic.
12
- *
13
- * @name nearestPoint
14
- * @param {Coord} targetPoint the reference point
15
- * @param {FeatureCollection<Point>} points against input point set
16
- * @returns {Feature<Point>} the closest point in the set to the reference point
17
- * @example
18
- * var targetPoint = turf.point([28.965797, 41.010086], {"marker-color": "#0F0"});
19
- * var points = turf.featureCollection([
20
- * turf.point([28.973865, 41.011122]),
21
- * turf.point([28.948459, 41.024204]),
22
- * turf.point([28.938674, 41.013324])
23
- * ]);
24
- *
25
- * var nearest = turf.nearestPoint(targetPoint, points);
26
- *
27
- * //addToMap
28
- * var addToMap = [targetPoint, points, nearest];
29
- * nearest.properties['marker-color'] = '#F00';
30
- */
31
- function nearestPoint(targetPoint, points) {
32
- // Input validation
33
- if (!targetPoint)
34
- throw new Error("targetPoint is required");
35
- if (!points)
36
- throw new Error("points is required");
37
- let minDist = Infinity;
38
- let bestFeatureIndex = 0;
39
- meta_1.featureEach(points, (pt, featureIndex) => {
40
- const distanceToPoint = distance_1.default(targetPoint, pt);
41
- if (distanceToPoint < minDist) {
42
- bestFeatureIndex = featureIndex;
43
- minDist = distanceToPoint;
44
- }
45
- });
46
- const nearestPoint = clone_1.default(points.features[bestFeatureIndex]);
47
- return Object.assign(Object.assign({}, nearestPoint), { properties: Object.assign(Object.assign({}, nearestPoint.properties), { featureIndex: bestFeatureIndex, distanceToPoint: minDist }) });
48
- }
49
- exports.default = nearestPoint;