@turf/line-slice 7.0.0-alpha.1 → 7.0.0-alpha.111
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 +4 -9
- package/dist/cjs/index.cjs +33 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +13 -0
- package/dist/esm/index.d.mts +13 -0
- package/dist/esm/index.mjs +33 -0
- package/dist/esm/index.mjs.map +1 -0
- package/index.d.ts +4 -1
- package/package.json +30 -24
- package/dist/es/index.js +0 -60
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -67
package/README.md
CHANGED
|
@@ -48,26 +48,21 @@ Returns **[Feature][4]<[LineString][5]>** sliced line
|
|
|
48
48
|
|
|
49
49
|
[5]: https://tools.ietf.org/html/rfc7946#section-3.1.4
|
|
50
50
|
|
|
51
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
52
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
53
|
-
./scripts/generate-readmes in the turf project. -->
|
|
51
|
+
<!-- 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. -->
|
|
54
52
|
|
|
55
53
|
---
|
|
56
54
|
|
|
57
|
-
This module is part of the [Turfjs project](
|
|
58
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
59
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
60
|
-
PRs and issues.
|
|
55
|
+
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.
|
|
61
56
|
|
|
62
57
|
### Installation
|
|
63
58
|
|
|
64
|
-
Install this module individually:
|
|
59
|
+
Install this single module individually:
|
|
65
60
|
|
|
66
61
|
```sh
|
|
67
62
|
$ npm install @turf/line-slice
|
|
68
63
|
```
|
|
69
64
|
|
|
70
|
-
Or install the
|
|
65
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
71
66
|
|
|
72
67
|
```sh
|
|
73
68
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.js
|
|
5
|
+
var _invariant = require('@turf/invariant');
|
|
6
|
+
var _helpers = require('@turf/helpers');
|
|
7
|
+
var _nearestpointonline = require('@turf/nearest-point-on-line');
|
|
8
|
+
function lineSlice(startPt, stopPt, line) {
|
|
9
|
+
var coords = _invariant.getCoords.call(void 0, line);
|
|
10
|
+
if (_invariant.getType.call(void 0, line) !== "LineString")
|
|
11
|
+
throw new Error("line must be a LineString");
|
|
12
|
+
var startVertex = _nearestpointonline.nearestPointOnLine.call(void 0, line, startPt);
|
|
13
|
+
var stopVertex = _nearestpointonline.nearestPointOnLine.call(void 0, line, stopPt);
|
|
14
|
+
var ends;
|
|
15
|
+
if (startVertex.properties.index <= stopVertex.properties.index) {
|
|
16
|
+
ends = [startVertex, stopVertex];
|
|
17
|
+
} else {
|
|
18
|
+
ends = [stopVertex, startVertex];
|
|
19
|
+
}
|
|
20
|
+
var clipCoords = [ends[0].geometry.coordinates];
|
|
21
|
+
for (var i = ends[0].properties.index + 1; i < ends[1].properties.index + 1; i++) {
|
|
22
|
+
clipCoords.push(coords[i]);
|
|
23
|
+
}
|
|
24
|
+
clipCoords.push(ends[1].geometry.coordinates);
|
|
25
|
+
return _helpers.lineString.call(void 0, clipCoords, line.properties);
|
|
26
|
+
}
|
|
27
|
+
__name(lineSlice, "lineSlice");
|
|
28
|
+
var turf_line_slice_default = lineSlice;
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
exports.default = turf_line_slice_default; exports.lineSlice = lineSlice;
|
|
33
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.js"],"names":[],"mappings":";;;;AAAA,SAAS,WAAW,eAAe;AACnC,SAAS,cAAc,kBAAkB;AACzC,SAAS,0BAA0B;AA+BnC,SAAS,UAAU,SAAS,QAAQ,MAAM;AAExC,MAAI,SAAS,UAAU,IAAI;AAC3B,MAAI,QAAQ,IAAI,MAAM;AACpB,UAAM,IAAI,MAAM,2BAA2B;AAE7C,MAAI,cAAc,mBAAmB,MAAM,OAAO;AAClD,MAAI,aAAa,mBAAmB,MAAM,MAAM;AAChD,MAAI;AACJ,MAAI,YAAY,WAAW,SAAS,WAAW,WAAW,OAAO;AAC/D,WAAO,CAAC,aAAa,UAAU;AAAA,EACjC,OAAO;AACL,WAAO,CAAC,YAAY,WAAW;AAAA,EACjC;AACA,MAAI,aAAa,CAAC,KAAK,CAAC,EAAE,SAAS,WAAW;AAC9C,WACM,IAAI,KAAK,CAAC,EAAE,WAAW,QAAQ,GACnC,IAAI,KAAK,CAAC,EAAE,WAAW,QAAQ,GAC/B,KACA;AACA,eAAW,KAAK,OAAO,CAAC,CAAC;AAAA,EAC3B;AACA,aAAW,KAAK,KAAK,CAAC,EAAE,SAAS,WAAW;AAC5C,SAAO,WAAW,YAAY,KAAK,UAAU;AAC/C;AAxBS;AA2BT,IAAO,0BAAQ","sourcesContent":["import { getCoords, getType } from \"@turf/invariant\";\nimport { lineString as linestring } from \"@turf/helpers\";\nimport { nearestPointOnLine } from \"@turf/nearest-point-on-line\";\n\n/**\n * Takes a {@link LineString|line}, a start {@link Point}, and a stop point\n * and returns a subsection of the line in-between those points.\n * The start & stop points don't need to fall exactly on the line.\n *\n * This can be useful for extracting only the part of a route between waypoints.\n *\n * @name lineSlice\n * @param {Coord} startPt starting point\n * @param {Coord} stopPt stopping point\n * @param {Feature<LineString>|LineString} line line to slice\n * @returns {Feature<LineString>} sliced line\n * @example\n * var line = turf.lineString([\n * [-77.031669, 38.878605],\n * [-77.029609, 38.881946],\n * [-77.020339, 38.884084],\n * [-77.025661, 38.885821],\n * [-77.021884, 38.889563],\n * [-77.019824, 38.892368]\n * ]);\n * var start = turf.point([-77.029609, 38.881946]);\n * var stop = turf.point([-77.021884, 38.889563]);\n *\n * var sliced = turf.lineSlice(start, stop, line);\n *\n * //addToMap\n * var addToMap = [start, stop, line]\n */\nfunction lineSlice(startPt, stopPt, line) {\n // Validation\n var coords = getCoords(line);\n if (getType(line) !== \"LineString\")\n throw new Error(\"line must be a LineString\");\n\n var startVertex = nearestPointOnLine(line, startPt);\n var stopVertex = nearestPointOnLine(line, stopPt);\n var ends;\n if (startVertex.properties.index <= stopVertex.properties.index) {\n ends = [startVertex, stopVertex];\n } else {\n ends = [stopVertex, startVertex];\n }\n var clipCoords = [ends[0].geometry.coordinates];\n for (\n var i = ends[0].properties.index + 1;\n i < ends[1].properties.index + 1;\n i++\n ) {\n clipCoords.push(coords[i]);\n }\n clipCoords.push(ends[1].geometry.coordinates);\n return linestring(clipCoords, line.properties);\n}\n\nexport { lineSlice };\nexport default lineSlice;\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Feature, LineString } from 'geojson';
|
|
2
|
+
import { Coord } from '@turf/helpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* http://turfjs.org/docs/#lineslice
|
|
6
|
+
*/
|
|
7
|
+
declare function lineSlice(
|
|
8
|
+
startPt: Coord,
|
|
9
|
+
stopPt: Coord,
|
|
10
|
+
line: Feature<LineString> | LineString
|
|
11
|
+
): Feature<LineString>;
|
|
12
|
+
|
|
13
|
+
export { lineSlice as default, lineSlice };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Feature, LineString } from 'geojson';
|
|
2
|
+
import { Coord } from '@turf/helpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* http://turfjs.org/docs/#lineslice
|
|
6
|
+
*/
|
|
7
|
+
declare function lineSlice(
|
|
8
|
+
startPt: Coord,
|
|
9
|
+
stopPt: Coord,
|
|
10
|
+
line: Feature<LineString> | LineString
|
|
11
|
+
): Feature<LineString>;
|
|
12
|
+
|
|
13
|
+
export { lineSlice as default, lineSlice };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.js
|
|
5
|
+
import { getCoords, getType } from "@turf/invariant";
|
|
6
|
+
import { lineString as linestring } from "@turf/helpers";
|
|
7
|
+
import { nearestPointOnLine } from "@turf/nearest-point-on-line";
|
|
8
|
+
function lineSlice(startPt, stopPt, line) {
|
|
9
|
+
var coords = getCoords(line);
|
|
10
|
+
if (getType(line) !== "LineString")
|
|
11
|
+
throw new Error("line must be a LineString");
|
|
12
|
+
var startVertex = nearestPointOnLine(line, startPt);
|
|
13
|
+
var stopVertex = nearestPointOnLine(line, stopPt);
|
|
14
|
+
var ends;
|
|
15
|
+
if (startVertex.properties.index <= stopVertex.properties.index) {
|
|
16
|
+
ends = [startVertex, stopVertex];
|
|
17
|
+
} else {
|
|
18
|
+
ends = [stopVertex, startVertex];
|
|
19
|
+
}
|
|
20
|
+
var clipCoords = [ends[0].geometry.coordinates];
|
|
21
|
+
for (var i = ends[0].properties.index + 1; i < ends[1].properties.index + 1; i++) {
|
|
22
|
+
clipCoords.push(coords[i]);
|
|
23
|
+
}
|
|
24
|
+
clipCoords.push(ends[1].geometry.coordinates);
|
|
25
|
+
return linestring(clipCoords, line.properties);
|
|
26
|
+
}
|
|
27
|
+
__name(lineSlice, "lineSlice");
|
|
28
|
+
var turf_line_slice_default = lineSlice;
|
|
29
|
+
export {
|
|
30
|
+
turf_line_slice_default as default,
|
|
31
|
+
lineSlice
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.js"],"sourcesContent":["import { getCoords, getType } from \"@turf/invariant\";\nimport { lineString as linestring } from \"@turf/helpers\";\nimport { nearestPointOnLine } from \"@turf/nearest-point-on-line\";\n\n/**\n * Takes a {@link LineString|line}, a start {@link Point}, and a stop point\n * and returns a subsection of the line in-between those points.\n * The start & stop points don't need to fall exactly on the line.\n *\n * This can be useful for extracting only the part of a route between waypoints.\n *\n * @name lineSlice\n * @param {Coord} startPt starting point\n * @param {Coord} stopPt stopping point\n * @param {Feature<LineString>|LineString} line line to slice\n * @returns {Feature<LineString>} sliced line\n * @example\n * var line = turf.lineString([\n * [-77.031669, 38.878605],\n * [-77.029609, 38.881946],\n * [-77.020339, 38.884084],\n * [-77.025661, 38.885821],\n * [-77.021884, 38.889563],\n * [-77.019824, 38.892368]\n * ]);\n * var start = turf.point([-77.029609, 38.881946]);\n * var stop = turf.point([-77.021884, 38.889563]);\n *\n * var sliced = turf.lineSlice(start, stop, line);\n *\n * //addToMap\n * var addToMap = [start, stop, line]\n */\nfunction lineSlice(startPt, stopPt, line) {\n // Validation\n var coords = getCoords(line);\n if (getType(line) !== \"LineString\")\n throw new Error(\"line must be a LineString\");\n\n var startVertex = nearestPointOnLine(line, startPt);\n var stopVertex = nearestPointOnLine(line, stopPt);\n var ends;\n if (startVertex.properties.index <= stopVertex.properties.index) {\n ends = [startVertex, stopVertex];\n } else {\n ends = [stopVertex, startVertex];\n }\n var clipCoords = [ends[0].geometry.coordinates];\n for (\n var i = ends[0].properties.index + 1;\n i < ends[1].properties.index + 1;\n i++\n ) {\n clipCoords.push(coords[i]);\n }\n clipCoords.push(ends[1].geometry.coordinates);\n return linestring(clipCoords, line.properties);\n}\n\nexport { lineSlice };\nexport default lineSlice;\n"],"mappings":";;;;AAAA,SAAS,WAAW,eAAe;AACnC,SAAS,cAAc,kBAAkB;AACzC,SAAS,0BAA0B;AA+BnC,SAAS,UAAU,SAAS,QAAQ,MAAM;AAExC,MAAI,SAAS,UAAU,IAAI;AAC3B,MAAI,QAAQ,IAAI,MAAM;AACpB,UAAM,IAAI,MAAM,2BAA2B;AAE7C,MAAI,cAAc,mBAAmB,MAAM,OAAO;AAClD,MAAI,aAAa,mBAAmB,MAAM,MAAM;AAChD,MAAI;AACJ,MAAI,YAAY,WAAW,SAAS,WAAW,WAAW,OAAO;AAC/D,WAAO,CAAC,aAAa,UAAU;AAAA,EACjC,OAAO;AACL,WAAO,CAAC,YAAY,WAAW;AAAA,EACjC;AACA,MAAI,aAAa,CAAC,KAAK,CAAC,EAAE,SAAS,WAAW;AAC9C,WACM,IAAI,KAAK,CAAC,EAAE,WAAW,QAAQ,GACnC,IAAI,KAAK,CAAC,EAAE,WAAW,QAAQ,GAC/B,KACA;AACA,eAAW,KAAK,OAAO,CAAC,CAAC;AAAA,EAC3B;AACA,aAAW,KAAK,KAAK,CAAC,EAAE,SAAS,WAAW;AAC5C,SAAO,WAAW,YAAY,KAAK,UAAU;AAC/C;AAxBS;AA2BT,IAAO,0BAAQ;","names":[]}
|
package/index.d.ts
CHANGED
|
@@ -4,8 +4,11 @@ import { Coord } from "@turf/helpers";
|
|
|
4
4
|
/**
|
|
5
5
|
* http://turfjs.org/docs/#lineslice
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
declare function lineSlice(
|
|
8
8
|
startPt: Coord,
|
|
9
9
|
stopPt: Coord,
|
|
10
10
|
line: Feature<LineString> | LineString
|
|
11
11
|
): Feature<LineString>;
|
|
12
|
+
|
|
13
|
+
export { lineSlice };
|
|
14
|
+
export default lineSlice;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/line-slice",
|
|
3
|
-
"version": "7.0.0-alpha.
|
|
3
|
+
"version": "7.0.0-alpha.111+08576cb50",
|
|
4
4
|
"description": "turf line-slice module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,43 +25,49 @@
|
|
|
25
25
|
"line",
|
|
26
26
|
"distance"
|
|
27
27
|
],
|
|
28
|
-
"
|
|
29
|
-
"
|
|
28
|
+
"type": "commonjs",
|
|
29
|
+
"main": "dist/cjs/index.cjs",
|
|
30
|
+
"module": "dist/esm/index.mjs",
|
|
31
|
+
"types": "dist/cjs/index.d.ts",
|
|
30
32
|
"exports": {
|
|
31
33
|
"./package.json": "./package.json",
|
|
32
34
|
".": {
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
"import": {
|
|
36
|
+
"types": "./dist/esm/index.d.mts",
|
|
37
|
+
"default": "./dist/esm/index.mjs"
|
|
38
|
+
},
|
|
39
|
+
"require": {
|
|
40
|
+
"types": "./dist/cjs/index.d.ts",
|
|
41
|
+
"default": "./dist/cjs/index.cjs"
|
|
42
|
+
}
|
|
36
43
|
}
|
|
37
44
|
},
|
|
38
|
-
"types": "index.d.ts",
|
|
39
45
|
"sideEffects": false,
|
|
40
46
|
"files": [
|
|
41
47
|
"dist",
|
|
42
48
|
"index.d.ts"
|
|
43
49
|
],
|
|
44
50
|
"scripts": {
|
|
45
|
-
"bench": "tsx bench.
|
|
46
|
-
"build": "
|
|
47
|
-
"docs": "tsx ../../scripts/generate-readmes",
|
|
48
|
-
"test": "npm-run-all test:*",
|
|
49
|
-
"test:tape": "tsx test.
|
|
51
|
+
"bench": "tsx bench.ts",
|
|
52
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
53
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
54
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
55
|
+
"test:tape": "tsx test.ts"
|
|
50
56
|
},
|
|
51
57
|
"devDependencies": {
|
|
52
|
-
"@turf/truncate": "^7.0.0-alpha.
|
|
53
|
-
"benchmark": "
|
|
54
|
-
"load-json-file": "
|
|
55
|
-
"npm-run-all": "
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"tsx": "
|
|
59
|
-
"write-json-file": "
|
|
58
|
+
"@turf/truncate": "^7.0.0-alpha.111+08576cb50",
|
|
59
|
+
"benchmark": "^2.1.4",
|
|
60
|
+
"load-json-file": "^7.0.1",
|
|
61
|
+
"npm-run-all": "^4.1.5",
|
|
62
|
+
"tape": "^5.7.2",
|
|
63
|
+
"tsup": "^8.0.1",
|
|
64
|
+
"tsx": "^4.6.2",
|
|
65
|
+
"write-json-file": "^5.0.0"
|
|
60
66
|
},
|
|
61
67
|
"dependencies": {
|
|
62
|
-
"@turf/helpers": "^7.0.0-alpha.
|
|
63
|
-
"@turf/invariant": "^7.0.0-alpha.
|
|
64
|
-
"@turf/nearest-point-on-line": "^7.0.0-alpha.
|
|
68
|
+
"@turf/helpers": "^7.0.0-alpha.111+08576cb50",
|
|
69
|
+
"@turf/invariant": "^7.0.0-alpha.111+08576cb50",
|
|
70
|
+
"@turf/nearest-point-on-line": "^7.0.0-alpha.111+08576cb50"
|
|
65
71
|
},
|
|
66
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "08576cb50376e0199aea02dbd887e3af83672246"
|
|
67
73
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { getCoords, getType } from '@turf/invariant';
|
|
2
|
-
import { lineString } from '@turf/helpers';
|
|
3
|
-
import nearestPointOnLine from '@turf/nearest-point-on-line';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Takes a {@link LineString|line}, a start {@link Point}, and a stop point
|
|
7
|
-
* and returns a subsection of the line in-between those points.
|
|
8
|
-
* The start & stop points don't need to fall exactly on the line.
|
|
9
|
-
*
|
|
10
|
-
* This can be useful for extracting only the part of a route between waypoints.
|
|
11
|
-
*
|
|
12
|
-
* @name lineSlice
|
|
13
|
-
* @param {Coord} startPt starting point
|
|
14
|
-
* @param {Coord} stopPt stopping point
|
|
15
|
-
* @param {Feature<LineString>|LineString} line line to slice
|
|
16
|
-
* @returns {Feature<LineString>} sliced line
|
|
17
|
-
* @example
|
|
18
|
-
* var line = turf.lineString([
|
|
19
|
-
* [-77.031669, 38.878605],
|
|
20
|
-
* [-77.029609, 38.881946],
|
|
21
|
-
* [-77.020339, 38.884084],
|
|
22
|
-
* [-77.025661, 38.885821],
|
|
23
|
-
* [-77.021884, 38.889563],
|
|
24
|
-
* [-77.019824, 38.892368]
|
|
25
|
-
* ]);
|
|
26
|
-
* var start = turf.point([-77.029609, 38.881946]);
|
|
27
|
-
* var stop = turf.point([-77.021884, 38.889563]);
|
|
28
|
-
*
|
|
29
|
-
* var sliced = turf.lineSlice(start, stop, line);
|
|
30
|
-
*
|
|
31
|
-
* //addToMap
|
|
32
|
-
* var addToMap = [start, stop, line]
|
|
33
|
-
*/
|
|
34
|
-
function lineSlice(startPt, stopPt, line) {
|
|
35
|
-
// Validation
|
|
36
|
-
var coords = getCoords(line);
|
|
37
|
-
if (getType(line) !== "LineString")
|
|
38
|
-
throw new Error("line must be a LineString");
|
|
39
|
-
|
|
40
|
-
var startVertex = nearestPointOnLine(line, startPt);
|
|
41
|
-
var stopVertex = nearestPointOnLine(line, stopPt);
|
|
42
|
-
var ends;
|
|
43
|
-
if (startVertex.properties.index <= stopVertex.properties.index) {
|
|
44
|
-
ends = [startVertex, stopVertex];
|
|
45
|
-
} else {
|
|
46
|
-
ends = [stopVertex, startVertex];
|
|
47
|
-
}
|
|
48
|
-
var clipCoords = [ends[0].geometry.coordinates];
|
|
49
|
-
for (
|
|
50
|
-
var i = ends[0].properties.index + 1;
|
|
51
|
-
i < ends[1].properties.index + 1;
|
|
52
|
-
i++
|
|
53
|
-
) {
|
|
54
|
-
clipCoords.push(coords[i]);
|
|
55
|
-
}
|
|
56
|
-
clipCoords.push(ends[1].geometry.coordinates);
|
|
57
|
-
return lineString(clipCoords, line.properties);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export default lineSlice;
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var invariant = require('@turf/invariant');
|
|
4
|
-
var helpers = require('@turf/helpers');
|
|
5
|
-
var nearestPointOnLine = require('@turf/nearest-point-on-line');
|
|
6
|
-
|
|
7
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
-
|
|
9
|
-
var nearestPointOnLine__default = /*#__PURE__*/_interopDefaultLegacy(nearestPointOnLine);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Takes a {@link LineString|line}, a start {@link Point}, and a stop point
|
|
13
|
-
* and returns a subsection of the line in-between those points.
|
|
14
|
-
* The start & stop points don't need to fall exactly on the line.
|
|
15
|
-
*
|
|
16
|
-
* This can be useful for extracting only the part of a route between waypoints.
|
|
17
|
-
*
|
|
18
|
-
* @name lineSlice
|
|
19
|
-
* @param {Coord} startPt starting point
|
|
20
|
-
* @param {Coord} stopPt stopping point
|
|
21
|
-
* @param {Feature<LineString>|LineString} line line to slice
|
|
22
|
-
* @returns {Feature<LineString>} sliced line
|
|
23
|
-
* @example
|
|
24
|
-
* var line = turf.lineString([
|
|
25
|
-
* [-77.031669, 38.878605],
|
|
26
|
-
* [-77.029609, 38.881946],
|
|
27
|
-
* [-77.020339, 38.884084],
|
|
28
|
-
* [-77.025661, 38.885821],
|
|
29
|
-
* [-77.021884, 38.889563],
|
|
30
|
-
* [-77.019824, 38.892368]
|
|
31
|
-
* ]);
|
|
32
|
-
* var start = turf.point([-77.029609, 38.881946]);
|
|
33
|
-
* var stop = turf.point([-77.021884, 38.889563]);
|
|
34
|
-
*
|
|
35
|
-
* var sliced = turf.lineSlice(start, stop, line);
|
|
36
|
-
*
|
|
37
|
-
* //addToMap
|
|
38
|
-
* var addToMap = [start, stop, line]
|
|
39
|
-
*/
|
|
40
|
-
function lineSlice(startPt, stopPt, line) {
|
|
41
|
-
// Validation
|
|
42
|
-
var coords = invariant.getCoords(line);
|
|
43
|
-
if (invariant.getType(line) !== "LineString")
|
|
44
|
-
throw new Error("line must be a LineString");
|
|
45
|
-
|
|
46
|
-
var startVertex = nearestPointOnLine__default['default'](line, startPt);
|
|
47
|
-
var stopVertex = nearestPointOnLine__default['default'](line, stopPt);
|
|
48
|
-
var ends;
|
|
49
|
-
if (startVertex.properties.index <= stopVertex.properties.index) {
|
|
50
|
-
ends = [startVertex, stopVertex];
|
|
51
|
-
} else {
|
|
52
|
-
ends = [stopVertex, startVertex];
|
|
53
|
-
}
|
|
54
|
-
var clipCoords = [ends[0].geometry.coordinates];
|
|
55
|
-
for (
|
|
56
|
-
var i = ends[0].properties.index + 1;
|
|
57
|
-
i < ends[1].properties.index + 1;
|
|
58
|
-
i++
|
|
59
|
-
) {
|
|
60
|
-
clipCoords.push(coords[i]);
|
|
61
|
-
}
|
|
62
|
-
clipCoords.push(ends[1].geometry.coordinates);
|
|
63
|
-
return helpers.lineString(clipCoords, line.properties);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
module.exports = lineSlice;
|
|
67
|
-
module.exports.default = lineSlice;
|