@turf/nearest-point-on-line 6.4.0 → 7.0.0-alpha.0

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
@@ -6,14 +6,15 @@
6
6
 
7
7
  Takes a [Point][1] and a [LineString][2] and calculates the closest Point on the (Multi)LineString.
8
8
 
9
- **Parameters**
9
+ ### Parameters
10
10
 
11
- - `lines` **([Geometry][3] \| [Feature][4]<([LineString][5] \| [MultiLineString][6])>)** lines to snap to
12
- - `pt` **([Geometry][3] \| [Feature][4]<[Point][7]> | [Array][8]<[number][9]>)** point to snap from
13
- - `options` **[Object][10]** Optional parameters (optional, default `{}`)
14
- - `options.units` **[string][11]** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
11
+ * `lines` **([Geometry][3] | [Feature][4]<([LineString][5] | [MultiLineString][6])>)** lines to snap to
12
+ * `pt` **([Geometry][3] | [Feature][4]<[Point][7]> | [Array][8]<[number][9]>)** point to snap from
13
+ * `options` **[Object][10]** Optional parameters (optional, default `{}`)
15
14
 
16
- **Examples**
15
+ * `options.units` **[string][11]** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
16
+
17
+ ### Examples
17
18
 
18
19
  ```javascript
19
20
  var line = turf.lineString([
@@ -33,7 +34,7 @@ var addToMap = [line, pt, snapped];
33
34
  snapped.properties['marker-color'] = '#00f';
34
35
  ```
35
36
 
36
- Returns **[Feature][4]&lt;[Point][7]>** closest point on the `line` to `point`. The properties object will contain three values: `index`: closest point was found on nth line part, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
37
+ Returns **[Feature][4]<[Point][7]>** closest point on the `line` to `point`. The properties object will contain three values: `index`: closest point was found on nth line part, `dist`: distance between pt and the closest point, `location`: distance along the line between start and the closest point.
37
38
 
38
39
  [1]: https://tools.ietf.org/html/rfc7946#section-3.1.2
39
40
 
package/dist/es/index.js CHANGED
@@ -3,7 +3,7 @@ import distance from "@turf/distance";
3
3
  import destination from "@turf/destination";
4
4
  import lineIntersects from "@turf/line-intersect";
5
5
  import { flattenEach } from "@turf/meta";
6
- import { point, lineString, } from "@turf/helpers";
6
+ import { point, lineString } from "@turf/helpers";
7
7
  import { getCoords } from "@turf/invariant";
8
8
  /**
9
9
  * Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.
@@ -31,53 +31,52 @@ import { getCoords } from "@turf/invariant";
31
31
  * var addToMap = [line, pt, snapped];
32
32
  * snapped.properties['marker-color'] = '#00f';
33
33
  */
34
- function nearestPointOnLine(lines, pt, options) {
35
- if (options === void 0) { options = {}; }
36
- var closestPt = point([Infinity, Infinity], {
34
+ function nearestPointOnLine(lines, pt, options = {}) {
35
+ if (!lines || !pt) {
36
+ throw new Error("lines and pt are required arguments");
37
+ }
38
+ let closestPt = point([Infinity, Infinity], {
37
39
  dist: Infinity,
40
+ index: -1,
41
+ location: -1,
38
42
  });
39
- var length = 0.0;
43
+ let length = 0.0;
40
44
  flattenEach(lines, function (line) {
41
- var coords = getCoords(line);
42
- for (var i = 0; i < coords.length - 1; i++) {
45
+ const coords = getCoords(line);
46
+ for (let i = 0; i < coords.length - 1; i++) {
43
47
  //start
44
- var start = point(coords[i]);
48
+ const start = point(coords[i]);
45
49
  start.properties.dist = distance(pt, start, options);
46
50
  //stop
47
- var stop_1 = point(coords[i + 1]);
48
- stop_1.properties.dist = distance(pt, stop_1, options);
51
+ const stop = point(coords[i + 1]);
52
+ stop.properties.dist = distance(pt, stop, options);
49
53
  // sectionLength
50
- var sectionLength = distance(start, stop_1, options);
54
+ const sectionLength = distance(start, stop, options);
51
55
  //perpendicular
52
- var heightDistance = Math.max(start.properties.dist, stop_1.properties.dist);
53
- var direction = bearing(start, stop_1);
54
- var perpendicularPt1 = destination(pt, heightDistance, direction + 90, options);
55
- var perpendicularPt2 = destination(pt, heightDistance, direction - 90, options);
56
- var intersect = lineIntersects(lineString([
56
+ const heightDistance = Math.max(start.properties.dist, stop.properties.dist);
57
+ const direction = bearing(start, stop);
58
+ const perpendicularPt1 = destination(pt, heightDistance, direction + 90, options);
59
+ const perpendicularPt2 = destination(pt, heightDistance, direction - 90, options);
60
+ const intersect = lineIntersects(lineString([
57
61
  perpendicularPt1.geometry.coordinates,
58
62
  perpendicularPt2.geometry.coordinates,
59
- ]), lineString([start.geometry.coordinates, stop_1.geometry.coordinates]));
60
- var intersectPt = null;
61
- if (intersect.features.length > 0) {
62
- intersectPt = intersect.features[0];
63
- intersectPt.properties.dist = distance(pt, intersectPt, options);
64
- intersectPt.properties.location =
65
- length + distance(start, intersectPt, options);
63
+ ]), lineString([start.geometry.coordinates, stop.geometry.coordinates]));
64
+ let intersectPt;
65
+ if (intersect.features.length > 0 && intersect.features[0]) {
66
+ intersectPt = Object.assign(Object.assign({}, intersect.features[0]), { properties: {
67
+ dist: distance(pt, intersect.features[0], options),
68
+ location: length + distance(start, intersect.features[0], options),
69
+ } });
66
70
  }
67
71
  if (start.properties.dist < closestPt.properties.dist) {
68
- closestPt = start;
69
- closestPt.properties.index = i;
70
- closestPt.properties.location = length;
72
+ closestPt = Object.assign(Object.assign({}, start), { properties: Object.assign(Object.assign({}, start.properties), { index: i, location: length }) });
71
73
  }
72
- if (stop_1.properties.dist < closestPt.properties.dist) {
73
- closestPt = stop_1;
74
- closestPt.properties.index = i + 1;
75
- closestPt.properties.location = length + sectionLength;
74
+ if (stop.properties.dist < closestPt.properties.dist) {
75
+ closestPt = Object.assign(Object.assign({}, stop), { properties: Object.assign(Object.assign({}, stop.properties), { index: i + 1, location: length + sectionLength }) });
76
76
  }
77
77
  if (intersectPt &&
78
78
  intersectPt.properties.dist < closestPt.properties.dist) {
79
- closestPt = intersectPt;
80
- closestPt.properties.index = i;
79
+ closestPt = Object.assign(Object.assign({}, intersectPt), { properties: Object.assign(Object.assign({}, intersectPt.properties), { index: i }) });
81
80
  }
82
81
  // update length
83
82
  length += sectionLength;
@@ -1,12 +1,5 @@
1
- import { Feature, Point, LineString, MultiLineString, Coord, Units } from "@turf/helpers";
2
- export interface NearestPointOnLine extends Feature<Point> {
3
- properties: {
4
- index?: number;
5
- dist?: number;
6
- location?: number;
7
- [key: string]: any;
8
- };
9
- }
1
+ import { Feature, Point, LineString, MultiLineString } from "geojson";
2
+ import { Coord, Units } from "@turf/helpers";
10
3
  /**
11
4
  * Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.
12
5
  *
@@ -35,5 +28,10 @@ export interface NearestPointOnLine extends Feature<Point> {
35
28
  */
36
29
  declare function nearestPointOnLine<G extends LineString | MultiLineString>(lines: Feature<G> | G, pt: Coord, options?: {
37
30
  units?: Units;
38
- }): NearestPointOnLine;
31
+ }): Feature<Point, {
32
+ dist: number;
33
+ index: number;
34
+ location: number;
35
+ [key: string]: any;
36
+ }>;
39
37
  export default nearestPointOnLine;
package/dist/js/index.js CHANGED
@@ -1,15 +1,13 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- var bearing_1 = __importDefault(require("@turf/bearing"));
7
- var distance_1 = __importDefault(require("@turf/distance"));
8
- var destination_1 = __importDefault(require("@turf/destination"));
9
- var line_intersect_1 = __importDefault(require("@turf/line-intersect"));
10
- var meta_1 = require("@turf/meta");
11
- var helpers_1 = require("@turf/helpers");
12
- var invariant_1 = require("@turf/invariant");
3
+ const tslib_1 = require("tslib");
4
+ const bearing_1 = tslib_1.__importDefault(require("@turf/bearing"));
5
+ const distance_1 = tslib_1.__importDefault(require("@turf/distance"));
6
+ const destination_1 = tslib_1.__importDefault(require("@turf/destination"));
7
+ const line_intersect_1 = tslib_1.__importDefault(require("@turf/line-intersect"));
8
+ const meta_1 = require("@turf/meta");
9
+ const helpers_1 = require("@turf/helpers");
10
+ const invariant_1 = require("@turf/invariant");
13
11
  /**
14
12
  * Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString.
15
13
  *
@@ -36,53 +34,52 @@ var invariant_1 = require("@turf/invariant");
36
34
  * var addToMap = [line, pt, snapped];
37
35
  * snapped.properties['marker-color'] = '#00f';
38
36
  */
39
- function nearestPointOnLine(lines, pt, options) {
40
- if (options === void 0) { options = {}; }
41
- var closestPt = helpers_1.point([Infinity, Infinity], {
37
+ function nearestPointOnLine(lines, pt, options = {}) {
38
+ if (!lines || !pt) {
39
+ throw new Error("lines and pt are required arguments");
40
+ }
41
+ let closestPt = helpers_1.point([Infinity, Infinity], {
42
42
  dist: Infinity,
43
+ index: -1,
44
+ location: -1,
43
45
  });
44
- var length = 0.0;
46
+ let length = 0.0;
45
47
  meta_1.flattenEach(lines, function (line) {
46
- var coords = invariant_1.getCoords(line);
47
- for (var i = 0; i < coords.length - 1; i++) {
48
+ const coords = invariant_1.getCoords(line);
49
+ for (let i = 0; i < coords.length - 1; i++) {
48
50
  //start
49
- var start = helpers_1.point(coords[i]);
51
+ const start = helpers_1.point(coords[i]);
50
52
  start.properties.dist = distance_1.default(pt, start, options);
51
53
  //stop
52
- var stop_1 = helpers_1.point(coords[i + 1]);
53
- stop_1.properties.dist = distance_1.default(pt, stop_1, options);
54
+ const stop = helpers_1.point(coords[i + 1]);
55
+ stop.properties.dist = distance_1.default(pt, stop, options);
54
56
  // sectionLength
55
- var sectionLength = distance_1.default(start, stop_1, options);
57
+ const sectionLength = distance_1.default(start, stop, options);
56
58
  //perpendicular
57
- var heightDistance = Math.max(start.properties.dist, stop_1.properties.dist);
58
- var direction = bearing_1.default(start, stop_1);
59
- var perpendicularPt1 = destination_1.default(pt, heightDistance, direction + 90, options);
60
- var perpendicularPt2 = destination_1.default(pt, heightDistance, direction - 90, options);
61
- var intersect = line_intersect_1.default(helpers_1.lineString([
59
+ const heightDistance = Math.max(start.properties.dist, stop.properties.dist);
60
+ const direction = bearing_1.default(start, stop);
61
+ const perpendicularPt1 = destination_1.default(pt, heightDistance, direction + 90, options);
62
+ const perpendicularPt2 = destination_1.default(pt, heightDistance, direction - 90, options);
63
+ const intersect = line_intersect_1.default(helpers_1.lineString([
62
64
  perpendicularPt1.geometry.coordinates,
63
65
  perpendicularPt2.geometry.coordinates,
64
- ]), helpers_1.lineString([start.geometry.coordinates, stop_1.geometry.coordinates]));
65
- var intersectPt = null;
66
- if (intersect.features.length > 0) {
67
- intersectPt = intersect.features[0];
68
- intersectPt.properties.dist = distance_1.default(pt, intersectPt, options);
69
- intersectPt.properties.location =
70
- length + distance_1.default(start, intersectPt, options);
66
+ ]), helpers_1.lineString([start.geometry.coordinates, stop.geometry.coordinates]));
67
+ let intersectPt;
68
+ if (intersect.features.length > 0 && intersect.features[0]) {
69
+ intersectPt = Object.assign(Object.assign({}, intersect.features[0]), { properties: {
70
+ dist: distance_1.default(pt, intersect.features[0], options),
71
+ location: length + distance_1.default(start, intersect.features[0], options),
72
+ } });
71
73
  }
72
74
  if (start.properties.dist < closestPt.properties.dist) {
73
- closestPt = start;
74
- closestPt.properties.index = i;
75
- closestPt.properties.location = length;
75
+ closestPt = Object.assign(Object.assign({}, start), { properties: Object.assign(Object.assign({}, start.properties), { index: i, location: length }) });
76
76
  }
77
- if (stop_1.properties.dist < closestPt.properties.dist) {
78
- closestPt = stop_1;
79
- closestPt.properties.index = i + 1;
80
- closestPt.properties.location = length + sectionLength;
77
+ if (stop.properties.dist < closestPt.properties.dist) {
78
+ closestPt = Object.assign(Object.assign({}, stop), { properties: Object.assign(Object.assign({}, stop.properties), { index: i + 1, location: length + sectionLength }) });
81
79
  }
82
80
  if (intersectPt &&
83
81
  intersectPt.properties.dist < closestPt.properties.dist) {
84
- closestPt = intersectPt;
85
- closestPt.properties.index = i;
82
+ closestPt = Object.assign(Object.assign({}, intersectPt), { properties: Object.assign(Object.assign({}, intersectPt.properties), { index: i }) });
86
83
  }
87
84
  // update length
88
85
  length += sectionLength;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/nearest-point-on-line",
3
- "version": "6.4.0",
3
+ "version": "7.0.0-alpha.0",
4
4
  "description": "turf nearest-point-on-line module",
5
5
  "author": "Turf Authors",
6
6
  "license": "MIT",
@@ -12,6 +12,7 @@
12
12
  "type": "git",
13
13
  "url": "git://github.com/Turfjs/turf.git"
14
14
  },
15
+ "funding": "https://opencollective.com/turf",
15
16
  "publishConfig": {
16
17
  "access": "public"
17
18
  },
@@ -37,12 +38,12 @@
37
38
  "docs": "node ../../scripts/generate-readmes",
38
39
  "test": "npm-run-all test:*",
39
40
  "test:tape": "ts-node -r esm test.js",
40
- "test:types": "tsc --esModuleInterop --noEmit types.ts"
41
+ "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
41
42
  },
42
43
  "devDependencies": {
43
- "@turf/along": "^6.4.0",
44
- "@turf/length": "^6.4.0",
45
- "@turf/truncate": "^6.4.0",
44
+ "@turf/along": "^7.0.0-alpha.0",
45
+ "@turf/length": "^7.0.0-alpha.0",
46
+ "@turf/truncate": "^7.0.0-alpha.0",
46
47
  "@types/tape": "*",
47
48
  "benchmark": "*",
48
49
  "load-json-file": "*",
@@ -54,13 +55,14 @@
54
55
  "write-json-file": "*"
55
56
  },
56
57
  "dependencies": {
57
- "@turf/bearing": "^6.4.0",
58
- "@turf/destination": "^6.4.0",
59
- "@turf/distance": "^6.4.0",
60
- "@turf/helpers": "^6.4.0",
61
- "@turf/invariant": "^6.4.0",
62
- "@turf/line-intersect": "^6.4.0",
63
- "@turf/meta": "^6.4.0"
58
+ "@turf/bearing": "^7.0.0-alpha.0",
59
+ "@turf/destination": "^7.0.0-alpha.0",
60
+ "@turf/distance": "^7.0.0-alpha.0",
61
+ "@turf/helpers": "^7.0.0-alpha.0",
62
+ "@turf/invariant": "^7.0.0-alpha.0",
63
+ "@turf/line-intersect": "^7.0.0-alpha.0",
64
+ "@turf/meta": "^7.0.0-alpha.0",
65
+ "tslib": "^2.3.0"
64
66
  },
65
- "gitHead": "1e62773cfc88c627cca8effcb5c14cfb65a905ac"
67
+ "gitHead": "0edc4c491b999e5ace770a61e1cf549f7c004189"
66
68
  }