@turf/line-split 7.0.0-alpha.2 → 7.1.0-alpha.7
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 +136 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +15 -0
- package/dist/esm/index.d.ts +15 -0
- package/dist/esm/index.js +136 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +39 -32
- package/dist/es/index.js +0 -220
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -233
- package/index.d.ts +0 -22
package/README.md
CHANGED
|
@@ -31,26 +31,21 @@ Returns **[FeatureCollection][3]<[LineString][2]>** Split LineStrings
|
|
|
31
31
|
|
|
32
32
|
[3]: https://tools.ietf.org/html/rfc7946#section-3.3
|
|
33
33
|
|
|
34
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
35
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
36
|
-
./scripts/generate-readmes in the turf project. -->
|
|
34
|
+
<!-- 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. -->
|
|
37
35
|
|
|
38
36
|
---
|
|
39
37
|
|
|
40
|
-
This module is part of the [Turfjs project](
|
|
41
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
42
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
43
|
-
PRs and issues.
|
|
38
|
+
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.
|
|
44
39
|
|
|
45
40
|
### Installation
|
|
46
41
|
|
|
47
|
-
Install this module individually:
|
|
42
|
+
Install this single module individually:
|
|
48
43
|
|
|
49
44
|
```sh
|
|
50
45
|
$ npm install @turf/line-split
|
|
51
46
|
```
|
|
52
47
|
|
|
53
|
-
Or install the
|
|
48
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
54
49
|
|
|
55
50
|
```sh
|
|
56
51
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.js
|
|
2
|
+
var _geojsonrbush = require('@turf/geojson-rbush');
|
|
3
|
+
var _square = require('@turf/square');
|
|
4
|
+
var _bbox = require('@turf/bbox');
|
|
5
|
+
var _truncate = require('@turf/truncate');
|
|
6
|
+
var _linesegment = require('@turf/line-segment');
|
|
7
|
+
var _lineintersect = require('@turf/line-intersect');
|
|
8
|
+
var _nearestpointonline = require('@turf/nearest-point-on-line');
|
|
9
|
+
var _invariant = require('@turf/invariant');
|
|
10
|
+
var _meta = require('@turf/meta');
|
|
11
|
+
var _helpers = require('@turf/helpers');
|
|
12
|
+
function lineSplit(line, splitter) {
|
|
13
|
+
if (!line)
|
|
14
|
+
throw new Error("line is required");
|
|
15
|
+
if (!splitter)
|
|
16
|
+
throw new Error("splitter is required");
|
|
17
|
+
var lineType = _invariant.getType.call(void 0, line);
|
|
18
|
+
var splitterType = _invariant.getType.call(void 0, splitter);
|
|
19
|
+
if (lineType !== "LineString")
|
|
20
|
+
throw new Error("line must be LineString");
|
|
21
|
+
if (splitterType === "FeatureCollection")
|
|
22
|
+
throw new Error("splitter cannot be a FeatureCollection");
|
|
23
|
+
if (splitterType === "GeometryCollection")
|
|
24
|
+
throw new Error("splitter cannot be a GeometryCollection");
|
|
25
|
+
var truncatedSplitter = _truncate.truncate.call(void 0, splitter, { precision: 7 });
|
|
26
|
+
switch (splitterType) {
|
|
27
|
+
case "Point":
|
|
28
|
+
return splitLineWithPoint(line, truncatedSplitter);
|
|
29
|
+
case "MultiPoint":
|
|
30
|
+
return splitLineWithPoints(line, truncatedSplitter);
|
|
31
|
+
case "LineString":
|
|
32
|
+
case "MultiLineString":
|
|
33
|
+
case "Polygon":
|
|
34
|
+
case "MultiPolygon":
|
|
35
|
+
return splitLineWithPoints(
|
|
36
|
+
line,
|
|
37
|
+
_lineintersect.lineIntersect.call(void 0, line, truncatedSplitter, {
|
|
38
|
+
ignoreSelfIntersections: true
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function splitLineWithPoints(line, splitter) {
|
|
44
|
+
var results = [];
|
|
45
|
+
var tree = _geojsonrbush.geojsonRbush.call(void 0, );
|
|
46
|
+
_meta.flattenEach.call(void 0, splitter, function(point) {
|
|
47
|
+
results.forEach(function(feature, index) {
|
|
48
|
+
feature.id = index;
|
|
49
|
+
});
|
|
50
|
+
if (!results.length) {
|
|
51
|
+
results = splitLineWithPoint(line, point).features;
|
|
52
|
+
results.forEach(function(feature) {
|
|
53
|
+
if (!feature.bbox)
|
|
54
|
+
feature.bbox = _square.square.call(void 0, _bbox.bbox.call(void 0, feature));
|
|
55
|
+
});
|
|
56
|
+
tree.load(_helpers.featureCollection.call(void 0, results));
|
|
57
|
+
} else {
|
|
58
|
+
var search = tree.search(point);
|
|
59
|
+
if (search.features.length) {
|
|
60
|
+
var closestLine = findClosestFeature(point, search);
|
|
61
|
+
results = results.filter(function(feature) {
|
|
62
|
+
return feature.id !== closestLine.id;
|
|
63
|
+
});
|
|
64
|
+
tree.remove(closestLine);
|
|
65
|
+
_meta.featureEach.call(void 0, splitLineWithPoint(closestLine, point), function(line2) {
|
|
66
|
+
results.push(line2);
|
|
67
|
+
tree.insert(line2);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return _helpers.featureCollection.call(void 0, results);
|
|
73
|
+
}
|
|
74
|
+
function splitLineWithPoint(line, splitter) {
|
|
75
|
+
var results = [];
|
|
76
|
+
var startPoint = _invariant.getCoords.call(void 0, line)[0];
|
|
77
|
+
var endPoint = _invariant.getCoords.call(void 0, line)[line.geometry.coordinates.length - 1];
|
|
78
|
+
if (pointsEquals(startPoint, _invariant.getCoord.call(void 0, splitter)) || pointsEquals(endPoint, _invariant.getCoord.call(void 0, splitter)))
|
|
79
|
+
return _helpers.featureCollection.call(void 0, [line]);
|
|
80
|
+
var tree = _geojsonrbush.geojsonRbush.call(void 0, );
|
|
81
|
+
var segments = _linesegment.lineSegment.call(void 0, line);
|
|
82
|
+
tree.load(segments);
|
|
83
|
+
var search = tree.search(splitter);
|
|
84
|
+
if (!search.features.length)
|
|
85
|
+
return _helpers.featureCollection.call(void 0, [line]);
|
|
86
|
+
var closestSegment = findClosestFeature(splitter, search);
|
|
87
|
+
var initialValue = [startPoint];
|
|
88
|
+
var lastCoords = _meta.featureReduce.call(void 0,
|
|
89
|
+
segments,
|
|
90
|
+
function(previous, current, index) {
|
|
91
|
+
var currentCoords = _invariant.getCoords.call(void 0, current)[1];
|
|
92
|
+
var splitterCoords = _invariant.getCoord.call(void 0, splitter);
|
|
93
|
+
if (index === closestSegment.id) {
|
|
94
|
+
previous.push(splitterCoords);
|
|
95
|
+
results.push(_helpers.lineString.call(void 0, previous));
|
|
96
|
+
if (pointsEquals(splitterCoords, currentCoords))
|
|
97
|
+
return [splitterCoords];
|
|
98
|
+
return [splitterCoords, currentCoords];
|
|
99
|
+
} else {
|
|
100
|
+
previous.push(currentCoords);
|
|
101
|
+
return previous;
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
initialValue
|
|
105
|
+
);
|
|
106
|
+
if (lastCoords.length > 1) {
|
|
107
|
+
results.push(_helpers.lineString.call(void 0, lastCoords));
|
|
108
|
+
}
|
|
109
|
+
return _helpers.featureCollection.call(void 0, results);
|
|
110
|
+
}
|
|
111
|
+
function findClosestFeature(point, lines) {
|
|
112
|
+
if (!lines.features.length)
|
|
113
|
+
throw new Error("lines must contain features");
|
|
114
|
+
if (lines.features.length === 1)
|
|
115
|
+
return lines.features[0];
|
|
116
|
+
var closestFeature;
|
|
117
|
+
var closestDistance = Infinity;
|
|
118
|
+
_meta.featureEach.call(void 0, lines, function(segment) {
|
|
119
|
+
var pt = _nearestpointonline.nearestPointOnLine.call(void 0, segment, point);
|
|
120
|
+
var dist = pt.properties.dist;
|
|
121
|
+
if (dist < closestDistance) {
|
|
122
|
+
closestFeature = segment;
|
|
123
|
+
closestDistance = dist;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return closestFeature;
|
|
127
|
+
}
|
|
128
|
+
function pointsEquals(pt1, pt2) {
|
|
129
|
+
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
130
|
+
}
|
|
131
|
+
var turf_line_split_default = lineSplit;
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
exports.default = turf_line_split_default; exports.lineSplit = lineSplit;
|
|
136
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.js"],"names":["line"],"mappings":";AAAA,SAAS,gBAAgB,aAAa;AACtC,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAC5B,SAAS,qBAAqB;AAC9B,SAAS,0BAA0B;AACnC,SAAS,WAAW,UAAU,eAAe;AAC7C,SAAS,aAAa,eAAe,mBAAmB;AACxD,SAAS,YAAY,yBAAyB;AAkB9C,SAAS,UAAU,MAAM,UAAU;AACjC,MAAI,CAAC;AAAM,UAAM,IAAI,MAAM,kBAAkB;AAC7C,MAAI,CAAC;AAAU,UAAM,IAAI,MAAM,sBAAsB;AAErD,MAAI,WAAW,QAAQ,IAAI;AAC3B,MAAI,eAAe,QAAQ,QAAQ;AAEnC,MAAI,aAAa;AAAc,UAAM,IAAI,MAAM,yBAAyB;AACxE,MAAI,iBAAiB;AACnB,UAAM,IAAI,MAAM,wCAAwC;AAC1D,MAAI,iBAAiB;AACnB,UAAM,IAAI,MAAM,yCAAyC;AAI3D,MAAI,oBAAoB,SAAS,UAAU,EAAE,WAAW,EAAE,CAAC;AAE3D,UAAQ,cAAc;AAAA,IACpB,KAAK;AAC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mB,SAAS,KAAK;AAC1C,QAAI,OAAO,GAAG,WAAW;AACzB,QAAI,OAAO,iBAAiB;AAC1B,uBAAiB;AACjB,wBAAkB;AAAA,IACpB;AAAA,EACF,CAAC;AACD,SAAO;AACT;AAUA,SAAS,aAAa,KAAK,KAAK;AAC9B,SAAO,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,IAAI,CAAC;AAC9C;AAGA,IAAO,0BAAQ","sourcesContent":["import { geojsonRbush as rbush } from \"@turf/geojson-rbush\";\nimport { square } from \"@turf/square\";\nimport { bbox } from \"@turf/bbox\";\nimport { truncate } from \"@turf/truncate\";\nimport { lineSegment } from \"@turf/line-segment\";\nimport { lineIntersect } from \"@turf/line-intersect\";\nimport { nearestPointOnLine } from \"@turf/nearest-point-on-line\";\nimport { getCoords, getCoord, getType } from \"@turf/invariant\";\nimport { featureEach, featureReduce, flattenEach } from \"@turf/meta\";\nimport { lineString, featureCollection } from \"@turf/helpers\";\n\n/**\n * Split a LineString by another GeoJSON Feature.\n *\n * @name lineSplit\n * @param {Feature<LineString>} line LineString Feature to split\n * @param {Feature<any>} splitter Feature used to split line\n * @returns {FeatureCollection<LineString>} Split LineStrings\n * @example\n * var line = turf.lineString([[120, -25], [145, -25]]);\n * var splitter = turf.lineString([[130, -15], [130, -35]]);\n *\n * var split = turf.lineSplit(line, splitter);\n *\n * //addToMap\n * var addToMap = [line, splitter]\n */\nfunction lineSplit(line, splitter) {\n if (!line) throw new Error(\"line is required\");\n if (!splitter) throw new Error(\"splitter is required\");\n\n var lineType = getType(line);\n var splitterType = getType(splitter);\n\n if (lineType !== \"LineString\") throw new Error(\"line must be LineString\");\n if (splitterType === \"FeatureCollection\")\n throw new Error(\"splitter cannot be a FeatureCollection\");\n if (splitterType === \"GeometryCollection\")\n throw new Error(\"splitter cannot be a GeometryCollection\");\n\n // remove excessive decimals from splitter\n // to avoid possible approximation issues in rbush\n var truncatedSplitter = truncate(splitter, { precision: 7 });\n\n switch (splitterType) {\n case \"Point\":\n return splitLineWithPoint(line, truncatedSplitter);\n case \"MultiPoint\":\n return splitLineWithPoints(line, truncatedSplitter);\n case \"LineString\":\n case \"MultiLineString\":\n case \"Polygon\":\n case \"MultiPolygon\":\n return splitLineWithPoints(\n line,\n lineIntersect(line, truncatedSplitter, {\n ignoreSelfIntersections: true,\n })\n );\n }\n}\n\n/**\n * Split LineString with MultiPoint\n *\n * @private\n * @param {Feature<LineString>} line LineString\n * @param {FeatureCollection<Point>} splitter Point\n * @returns {FeatureCollection<LineString>} split LineStrings\n */\nfunction splitLineWithPoints(line, splitter) {\n var results = [];\n var tree = rbush();\n\n flattenEach(splitter, function (point) {\n // Add index/id to features (needed for filter)\n results.forEach(function (feature, index) {\n feature.id = index;\n });\n // First Point - doesn't need to handle any previous line results\n if (!results.length) {\n results = splitLineWithPoint(line, point).features;\n\n // Add Square BBox to each feature for GeoJSON-RBush\n results.forEach(function (feature) {\n if (!feature.bbox) feature.bbox = square(bbox(feature));\n });\n tree.load(featureCollection(results));\n // Split with remaining points - lines might needed to be split multiple times\n } else {\n // Find all lines that are within the splitter's bbox\n var search = tree.search(point);\n\n if (search.features.length) {\n // RBush might return multiple lines - only process the closest line to splitter\n var closestLine = findClosestFeature(point, search);\n\n // Remove closest line from results since this will be split into two lines\n // This removes any duplicates inside the results & index\n results = results.filter(function (feature) {\n return feature.id !== closestLine.id;\n });\n tree.remove(closestLine);\n\n // Append the two newly split lines into the results\n featureEach(splitLineWithPoint(closestLine, point), function (line) {\n results.push(line);\n tree.insert(line);\n });\n }\n }\n });\n return featureCollection(results);\n}\n\n/**\n * Split LineString with Point\n *\n * @private\n * @param {Feature<LineString>} line LineString\n * @param {Feature<Point>} splitter Point\n * @returns {FeatureCollection<LineString>} split LineStrings\n */\nfunction splitLineWithPoint(line, splitter) {\n var results = [];\n\n // handle endpoints\n var startPoint = getCoords(line)[0];\n var endPoint = getCoords(line)[line.geometry.coordinates.length - 1];\n if (\n pointsEquals(startPoint, getCoord(splitter)) ||\n pointsEquals(endPoint, getCoord(splitter))\n )\n return featureCollection([line]);\n\n // Create spatial index\n var tree = rbush();\n var segments = lineSegment(line);\n tree.load(segments);\n\n // Find all segments that are within bbox of splitter\n var search = tree.search(splitter);\n\n // Return itself if point is not within spatial index\n if (!search.features.length) return featureCollection([line]);\n\n // RBush might return multiple lines - only process the closest line to splitter\n var closestSegment = findClosestFeature(splitter, search);\n\n // Initial value is the first point of the first segments (beginning of line)\n var initialValue = [startPoint];\n var lastCoords = featureReduce(\n segments,\n function (previous, current, index) {\n var currentCoords = getCoords(current)[1];\n var splitterCoords = getCoord(splitter);\n\n // Location where segment intersects with line\n if (index === closestSegment.id) {\n previous.push(splitterCoords);\n results.push(lineString(previous));\n // Don't duplicate splitter coordinate (Issue #688)\n if (pointsEquals(splitterCoords, currentCoords))\n return [splitterCoords];\n return [splitterCoords, currentCoords];\n\n // Keep iterating over coords until finished or intersection is found\n } else {\n previous.push(currentCoords);\n return previous;\n }\n },\n initialValue\n );\n // Append last line to final split results\n if (lastCoords.length > 1) {\n results.push(lineString(lastCoords));\n }\n return featureCollection(results);\n}\n\n/**\n * Find Closest Feature\n *\n * @private\n * @param {Feature<Point>} point Feature must be closest to this point\n * @param {FeatureCollection<LineString>} lines Collection of Features\n * @returns {Feature<LineString>} closest LineString\n */\nfunction findClosestFeature(point, lines) {\n if (!lines.features.length) throw new Error(\"lines must contain features\");\n // Filter to one segment that is the closest to the line\n if (lines.features.length === 1) return lines.features[0];\n\n var closestFeature;\n var closestDistance = Infinity;\n featureEach(lines, function (segment) {\n var pt = nearestPointOnLine(segment, point);\n var dist = pt.properties.dist;\n if (dist < closestDistance) {\n closestFeature = segment;\n closestDistance = dist;\n }\n });\n return closestFeature;\n}\n\n/**\n * Compares two points and returns if they are equals\n *\n * @private\n * @param {Array<number>} pt1 point\n * @param {Array<number>} pt2 point\n * @returns {boolean} true if they are equals\n */\nfunction pointsEquals(pt1, pt2) {\n return pt1[0] === pt2[0] && pt1[1] === pt2[1];\n}\n\nexport { lineSplit };\nexport default lineSplit;\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Feature, Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, FeatureCollection } from 'geojson';
|
|
2
|
+
|
|
3
|
+
declare type Splitter = Feature<
|
|
4
|
+
Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon
|
|
5
|
+
>;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* http://turfjs.org/docs/#linesplit
|
|
9
|
+
*/
|
|
10
|
+
declare function lineSplit<T extends LineString>(
|
|
11
|
+
line: Feature<T> | T,
|
|
12
|
+
splitter: Splitter
|
|
13
|
+
): FeatureCollection<T>;
|
|
14
|
+
|
|
15
|
+
export { type Splitter, lineSplit as default, lineSplit };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Feature, Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, FeatureCollection } from 'geojson';
|
|
2
|
+
|
|
3
|
+
declare type Splitter = Feature<
|
|
4
|
+
Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon
|
|
5
|
+
>;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* http://turfjs.org/docs/#linesplit
|
|
9
|
+
*/
|
|
10
|
+
declare function lineSplit<T extends LineString>(
|
|
11
|
+
line: Feature<T> | T,
|
|
12
|
+
splitter: Splitter
|
|
13
|
+
): FeatureCollection<T>;
|
|
14
|
+
|
|
15
|
+
export { type Splitter, lineSplit as default, lineSplit };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// index.js
|
|
2
|
+
import { geojsonRbush as rbush } from "@turf/geojson-rbush";
|
|
3
|
+
import { square } from "@turf/square";
|
|
4
|
+
import { bbox } from "@turf/bbox";
|
|
5
|
+
import { truncate } from "@turf/truncate";
|
|
6
|
+
import { lineSegment } from "@turf/line-segment";
|
|
7
|
+
import { lineIntersect } from "@turf/line-intersect";
|
|
8
|
+
import { nearestPointOnLine } from "@turf/nearest-point-on-line";
|
|
9
|
+
import { getCoords, getCoord, getType } from "@turf/invariant";
|
|
10
|
+
import { featureEach, featureReduce, flattenEach } from "@turf/meta";
|
|
11
|
+
import { lineString, featureCollection } from "@turf/helpers";
|
|
12
|
+
function lineSplit(line, splitter) {
|
|
13
|
+
if (!line)
|
|
14
|
+
throw new Error("line is required");
|
|
15
|
+
if (!splitter)
|
|
16
|
+
throw new Error("splitter is required");
|
|
17
|
+
var lineType = getType(line);
|
|
18
|
+
var splitterType = getType(splitter);
|
|
19
|
+
if (lineType !== "LineString")
|
|
20
|
+
throw new Error("line must be LineString");
|
|
21
|
+
if (splitterType === "FeatureCollection")
|
|
22
|
+
throw new Error("splitter cannot be a FeatureCollection");
|
|
23
|
+
if (splitterType === "GeometryCollection")
|
|
24
|
+
throw new Error("splitter cannot be a GeometryCollection");
|
|
25
|
+
var truncatedSplitter = truncate(splitter, { precision: 7 });
|
|
26
|
+
switch (splitterType) {
|
|
27
|
+
case "Point":
|
|
28
|
+
return splitLineWithPoint(line, truncatedSplitter);
|
|
29
|
+
case "MultiPoint":
|
|
30
|
+
return splitLineWithPoints(line, truncatedSplitter);
|
|
31
|
+
case "LineString":
|
|
32
|
+
case "MultiLineString":
|
|
33
|
+
case "Polygon":
|
|
34
|
+
case "MultiPolygon":
|
|
35
|
+
return splitLineWithPoints(
|
|
36
|
+
line,
|
|
37
|
+
lineIntersect(line, truncatedSplitter, {
|
|
38
|
+
ignoreSelfIntersections: true
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function splitLineWithPoints(line, splitter) {
|
|
44
|
+
var results = [];
|
|
45
|
+
var tree = rbush();
|
|
46
|
+
flattenEach(splitter, function(point) {
|
|
47
|
+
results.forEach(function(feature, index) {
|
|
48
|
+
feature.id = index;
|
|
49
|
+
});
|
|
50
|
+
if (!results.length) {
|
|
51
|
+
results = splitLineWithPoint(line, point).features;
|
|
52
|
+
results.forEach(function(feature) {
|
|
53
|
+
if (!feature.bbox)
|
|
54
|
+
feature.bbox = square(bbox(feature));
|
|
55
|
+
});
|
|
56
|
+
tree.load(featureCollection(results));
|
|
57
|
+
} else {
|
|
58
|
+
var search = tree.search(point);
|
|
59
|
+
if (search.features.length) {
|
|
60
|
+
var closestLine = findClosestFeature(point, search);
|
|
61
|
+
results = results.filter(function(feature) {
|
|
62
|
+
return feature.id !== closestLine.id;
|
|
63
|
+
});
|
|
64
|
+
tree.remove(closestLine);
|
|
65
|
+
featureEach(splitLineWithPoint(closestLine, point), function(line2) {
|
|
66
|
+
results.push(line2);
|
|
67
|
+
tree.insert(line2);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return featureCollection(results);
|
|
73
|
+
}
|
|
74
|
+
function splitLineWithPoint(line, splitter) {
|
|
75
|
+
var results = [];
|
|
76
|
+
var startPoint = getCoords(line)[0];
|
|
77
|
+
var endPoint = getCoords(line)[line.geometry.coordinates.length - 1];
|
|
78
|
+
if (pointsEquals(startPoint, getCoord(splitter)) || pointsEquals(endPoint, getCoord(splitter)))
|
|
79
|
+
return featureCollection([line]);
|
|
80
|
+
var tree = rbush();
|
|
81
|
+
var segments = lineSegment(line);
|
|
82
|
+
tree.load(segments);
|
|
83
|
+
var search = tree.search(splitter);
|
|
84
|
+
if (!search.features.length)
|
|
85
|
+
return featureCollection([line]);
|
|
86
|
+
var closestSegment = findClosestFeature(splitter, search);
|
|
87
|
+
var initialValue = [startPoint];
|
|
88
|
+
var lastCoords = featureReduce(
|
|
89
|
+
segments,
|
|
90
|
+
function(previous, current, index) {
|
|
91
|
+
var currentCoords = getCoords(current)[1];
|
|
92
|
+
var splitterCoords = getCoord(splitter);
|
|
93
|
+
if (index === closestSegment.id) {
|
|
94
|
+
previous.push(splitterCoords);
|
|
95
|
+
results.push(lineString(previous));
|
|
96
|
+
if (pointsEquals(splitterCoords, currentCoords))
|
|
97
|
+
return [splitterCoords];
|
|
98
|
+
return [splitterCoords, currentCoords];
|
|
99
|
+
} else {
|
|
100
|
+
previous.push(currentCoords);
|
|
101
|
+
return previous;
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
initialValue
|
|
105
|
+
);
|
|
106
|
+
if (lastCoords.length > 1) {
|
|
107
|
+
results.push(lineString(lastCoords));
|
|
108
|
+
}
|
|
109
|
+
return featureCollection(results);
|
|
110
|
+
}
|
|
111
|
+
function findClosestFeature(point, lines) {
|
|
112
|
+
if (!lines.features.length)
|
|
113
|
+
throw new Error("lines must contain features");
|
|
114
|
+
if (lines.features.length === 1)
|
|
115
|
+
return lines.features[0];
|
|
116
|
+
var closestFeature;
|
|
117
|
+
var closestDistance = Infinity;
|
|
118
|
+
featureEach(lines, function(segment) {
|
|
119
|
+
var pt = nearestPointOnLine(segment, point);
|
|
120
|
+
var dist = pt.properties.dist;
|
|
121
|
+
if (dist < closestDistance) {
|
|
122
|
+
closestFeature = segment;
|
|
123
|
+
closestDistance = dist;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return closestFeature;
|
|
127
|
+
}
|
|
128
|
+
function pointsEquals(pt1, pt2) {
|
|
129
|
+
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
130
|
+
}
|
|
131
|
+
var turf_line_split_default = lineSplit;
|
|
132
|
+
export {
|
|
133
|
+
turf_line_split_default as default,
|
|
134
|
+
lineSplit
|
|
135
|
+
};
|
|
136
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.js"],"sourcesContent":["import { geojsonRbush as rbush } from \"@turf/geojson-rbush\";\nimport { square } from \"@turf/square\";\nimport { bbox } from \"@turf/bbox\";\nimport { truncate } from \"@turf/truncate\";\nimport { lineSegment } from \"@turf/line-segment\";\nimport { lineIntersect } from \"@turf/line-intersect\";\nimport { nearestPointOnLine } from \"@turf/nearest-point-on-line\";\nimport { getCoords, getCoord, getType } from \"@turf/invariant\";\nimport { featureEach, featureReduce, flattenEach } from \"@turf/meta\";\nimport { lineString, featureCollection } from \"@turf/helpers\";\n\n/**\n * Split a LineString by another GeoJSON Feature.\n *\n * @name lineSplit\n * @param {Feature<LineString>} line LineString Feature to split\n * @param {Feature<any>} splitter Feature used to split line\n * @returns {FeatureCollection<LineString>} Split LineStrings\n * @example\n * var line = turf.lineString([[120, -25], [145, -25]]);\n * var splitter = turf.lineString([[130, -15], [130, -35]]);\n *\n * var split = turf.lineSplit(line, splitter);\n *\n * //addToMap\n * var addToMap = [line, splitter]\n */\nfunction lineSplit(line, splitter) {\n if (!line) throw new Error(\"line is required\");\n if (!splitter) throw new Error(\"splitter is required\");\n\n var lineType = getType(line);\n var splitterType = getType(splitter);\n\n if (lineType !== \"LineString\") throw new Error(\"line must be LineString\");\n if (splitterType === \"FeatureCollection\")\n throw new Error(\"splitter cannot be a FeatureCollection\");\n if (splitterType === \"GeometryCollection\")\n throw new Error(\"splitter cannot be a GeometryCollection\");\n\n // remove excessive decimals from splitter\n // to avoid possible approximation issues in rbush\n var truncatedSplitter = truncate(splitter, { precision: 7 });\n\n switch (splitterType) {\n case \"Point\":\n return splitLineWithPoint(line, truncatedSplitter);\n case \"MultiPoint\":\n return splitLineWithPoints(line, truncatedSplitter);\n case \"LineString\":\n case \"MultiLineString\":\n case \"Polygon\":\n case \"MultiPolygon\":\n return splitLineWithPoints(\n line,\n lineIntersect(line, truncatedSplitter, {\n ignoreSelfIntersections: true,\n })\n );\n }\n}\n\n/**\n * Split LineString with MultiPoint\n *\n * @private\n * @param {Feature<LineString>} line LineString\n * @param {FeatureCollection<Point>} splitter Point\n * @returns {FeatureCollection<LineString>} split LineStrings\n */\nfunction splitLineWithPoints(line, splitter) {\n var results = [];\n var tree = rbush();\n\n flattenEach(splitter, function (point) {\n // Add index/id to features (needed for filter)\n results.forEach(function (feature, index) {\n feature.id = index;\n });\n // First Point - doesn't need to handle any previous line results\n if (!results.length) {\n results = splitLineWithPoint(line, point).features;\n\n // Add Square BBox to each feature for GeoJSON-RBush\n results.forEach(function (feature) {\n if (!feature.bbox) feature.bbox = square(bbox(feature));\n });\n tree.load(featureCollection(results));\n // Split with remaining points - lines might needed to be split multiple times\n } else {\n // Find all lines that are within the splitter's bbox\n var search = tree.search(point);\n\n if (search.features.length) {\n // RBush might return multiple lines - only process the closest line to splitter\n var closestLine = findClosestFeature(point, search);\n\n // Remove closest line from results since this will be split into two lines\n // This removes any duplicates inside the results & index\n results = results.filter(function (feature) {\n return feature.id !== closestLine.id;\n });\n tree.remove(closestLine);\n\n // Append the two newly split lines into the results\n featureEach(splitLineWithPoint(closestLine, point), function (line) {\n results.push(line);\n tree.insert(line);\n });\n }\n }\n });\n return featureCollection(results);\n}\n\n/**\n * Split LineString with Point\n *\n * @private\n * @param {Feature<LineString>} line LineString\n * @param {Feature<Point>} splitter Point\n * @returns {FeatureCollection<LineString>} split LineStrings\n */\nfunction splitLineWithPoint(line, splitter) {\n var results = [];\n\n // handle endpoints\n var startPoint = getCoords(line)[0];\n var endPoint = getCoords(line)[line.geometry.coordinates.length - 1];\n if (\n pointsEquals(startPoint, getCoord(splitter)) ||\n pointsEquals(endPoint, getCoord(splitter))\n )\n return featureCollection([line]);\n\n // Create spatial index\n var tree = rbush();\n var segments = lineSegment(line);\n tree.load(segments);\n\n // Find all segments that are within bbox of splitter\n var search = tree.search(splitter);\n\n // Return itself if point is not within spatial index\n if (!search.features.length) return featureCollection([line]);\n\n // RBush might return multiple lines - only process the closest line to splitter\n var closestSegment = findClosestFeature(splitter, search);\n\n // Initial value is the first point of the first segments (beginning of line)\n var initialValue = [startPoint];\n var lastCoords = featureReduce(\n segments,\n function (previous, current, index) {\n var currentCoords = getCoords(current)[1];\n var splitterCoords = getCoord(splitter);\n\n // Location where segment intersects with line\n if (index === closestSegment.id) {\n previous.push(splitterCoords);\n results.push(lineString(previous));\n // Don't duplicate splitter coordinate (Issue #688)\n if (pointsEquals(splitterCoords, currentCoords))\n return [splitterCoords];\n return [splitterCoords, currentCoords];\n\n // Keep iterating over coords until finished or intersection is found\n } else {\n previous.push(currentCoords);\n return previous;\n }\n },\n initialValue\n );\n // Append last line to final split results\n if (lastCoords.length > 1) {\n results.push(lineString(lastCoords));\n }\n return featureCollection(results);\n}\n\n/**\n * Find Closest Feature\n *\n * @private\n * @param {Feature<Point>} point Feature must be closest to this point\n * @param {FeatureCollection<LineString>} lines Collection of Features\n * @returns {Feature<LineString>} closest LineString\n */\nfunction findClosestFeature(point, lines) {\n if (!lines.features.length) throw new Error(\"lines must contain features\");\n // Filter to one segment that is the closest to the line\n if (lines.features.length === 1) return lines.features[0];\n\n var closestFeature;\n var closestDistance = Infinity;\n featureEach(lines, function (segment) {\n var pt = nearestPointOnLine(segment, point);\n var dist = pt.properties.dist;\n if (dist < closestDistance) {\n closestFeature = segment;\n closestDistance = dist;\n }\n });\n return closestFeature;\n}\n\n/**\n * Compares two points and returns if they are equals\n *\n * @private\n * @param {Array<number>} pt1 point\n * @param {Array<number>} pt2 point\n * @returns {boolean} true if they are equals\n */\nfunction pointsEquals(pt1, pt2) {\n return pt1[0] === pt2[0] && pt1[1] === pt2[1];\n}\n\nexport { lineSplit };\nexport default lineSplit;\n"],"mappings":";AAAA,SAAS,gBAAgB,aAAa;AACtC,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAC5B,SAAS,qBAAqB;AAC9B,SAAS,0BAA0B;AACnC,SAAS,WAAW,UAAU,eAAe;AAC7C,SAAS,aAAa,eAAe,mBAAmB;AACxD,SAAS,YAAY,yBAAyB;AAkB9C,SAAS,UAAU,MAAM,UAAU;AACjC,MAAI,CAAC;AAAM,UAAM,IAAI,MAAM,kBAAkB;AAC7C,MAAI,CAAC;AAAU,UAAM,IAAI,MAAM,sBAAsB;AAErD,MAAI,WAAW,QAAQ,IAAI;AAC3B,MAAI,eAAe,QAAQ,QAAQ;AAEnC,MAAI,aAAa;AAAc,UAAM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AmB,OAAO,OAAO;AACxC,MAAI,CAAC,MAAM,SAAS;AAAQ,UAAM,IAAI,MAAM,6BAA6B;AAEzE,MAAI,MAAM,SAAS,WAAW;AAAG,WAAO,MAAM,SAAS,CAAC;AAExD,MAAI;AACJ,MAAI,kBAAkB;AACtB,cAAY,OAAO,SAAU,SAAS;AACpC,QAAI,KAAK,mBAAmB,SAAS,KAAK;AAC1C,QAAI,OAAO,GAAG,WAAW;AACzB,QAAI,OAAO,iBAAiB;AAC1B,uBAAiB;AACjB,wBAAkB;AAAA,IACpB;AAAA,EACF,CAAC;AACD,SAAO;AACT;AAUA,SAAS,aAAa,KAAK,KAAK;AAC9B,SAAO,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,IAAI,CAAC;AAC9C;AAGA,IAAO,0BAAQ;","names":["line"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/line-split",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.0-alpha.7+0ce6ecca0",
|
|
4
4
|
"description": "turf line-split module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -26,49 +26,56 @@
|
|
|
26
26
|
"line",
|
|
27
27
|
"split"
|
|
28
28
|
],
|
|
29
|
-
"
|
|
30
|
-
"
|
|
29
|
+
"type": "module",
|
|
30
|
+
"main": "dist/cjs/index.cjs",
|
|
31
|
+
"module": "dist/esm/index.js",
|
|
32
|
+
"types": "dist/esm/index.d.ts",
|
|
31
33
|
"exports": {
|
|
32
34
|
"./package.json": "./package.json",
|
|
33
35
|
".": {
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/esm/index.d.ts",
|
|
38
|
+
"default": "./dist/esm/index.js"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./dist/cjs/index.d.cts",
|
|
42
|
+
"default": "./dist/cjs/index.cjs"
|
|
43
|
+
}
|
|
37
44
|
}
|
|
38
45
|
},
|
|
39
|
-
"types": "index.d.ts",
|
|
40
46
|
"sideEffects": false,
|
|
41
47
|
"files": [
|
|
42
|
-
"dist"
|
|
43
|
-
"index.d.ts"
|
|
48
|
+
"dist"
|
|
44
49
|
],
|
|
45
50
|
"scripts": {
|
|
46
|
-
"bench": "tsx bench.
|
|
47
|
-
"build": "
|
|
48
|
-
"docs": "tsx ../../scripts/generate-readmes",
|
|
49
|
-
"test": "npm-run-all test:*",
|
|
50
|
-
"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"
|
|
51
56
|
},
|
|
52
57
|
"devDependencies": {
|
|
53
|
-
"benchmark": "
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
58
|
+
"@types/benchmark": "^2.1.5",
|
|
59
|
+
"@types/tape": "^4.2.32",
|
|
60
|
+
"benchmark": "^2.1.4",
|
|
61
|
+
"load-json-file": "^7.0.1",
|
|
62
|
+
"npm-run-all": "^4.1.5",
|
|
63
|
+
"tape": "^5.7.2",
|
|
64
|
+
"tsup": "^8.0.1",
|
|
65
|
+
"tsx": "^4.6.2",
|
|
66
|
+
"write-json-file": "^5.0.0"
|
|
60
67
|
},
|
|
61
68
|
"dependencies": {
|
|
62
|
-
"@turf/bbox": "^7.
|
|
63
|
-
"@turf/geojson-rbush": "^
|
|
64
|
-
"@turf/helpers": "^7.
|
|
65
|
-
"@turf/invariant": "^7.
|
|
66
|
-
"@turf/line-intersect": "^7.
|
|
67
|
-
"@turf/line-segment": "^7.
|
|
68
|
-
"@turf/meta": "^7.
|
|
69
|
-
"@turf/nearest-point-on-line": "^7.
|
|
70
|
-
"@turf/square": "^7.
|
|
71
|
-
"@turf/truncate": "^7.
|
|
69
|
+
"@turf/bbox": "^7.1.0-alpha.7+0ce6ecca0",
|
|
70
|
+
"@turf/geojson-rbush": "^7.1.0-alpha.7+0ce6ecca0",
|
|
71
|
+
"@turf/helpers": "^7.1.0-alpha.7+0ce6ecca0",
|
|
72
|
+
"@turf/invariant": "^7.1.0-alpha.7+0ce6ecca0",
|
|
73
|
+
"@turf/line-intersect": "^7.1.0-alpha.7+0ce6ecca0",
|
|
74
|
+
"@turf/line-segment": "^7.1.0-alpha.7+0ce6ecca0",
|
|
75
|
+
"@turf/meta": "^7.1.0-alpha.7+0ce6ecca0",
|
|
76
|
+
"@turf/nearest-point-on-line": "^7.1.0-alpha.7+0ce6ecca0",
|
|
77
|
+
"@turf/square": "^7.1.0-alpha.7+0ce6ecca0",
|
|
78
|
+
"@turf/truncate": "^7.1.0-alpha.7+0ce6ecca0"
|
|
72
79
|
},
|
|
73
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "0ce6ecca05829690270fec6d6bed2003495fe0ea"
|
|
74
81
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
import rbush from '@turf/geojson-rbush';
|
|
2
|
-
import square from '@turf/square';
|
|
3
|
-
import bbox from '@turf/bbox';
|
|
4
|
-
import truncate from '@turf/truncate';
|
|
5
|
-
import lineSegment from '@turf/line-segment';
|
|
6
|
-
import lineIntersect from '@turf/line-intersect';
|
|
7
|
-
import nearestPointOnLine from '@turf/nearest-point-on-line';
|
|
8
|
-
import { getType, getCoords, getCoord } from '@turf/invariant';
|
|
9
|
-
import { flattenEach, featureEach, featureReduce } from '@turf/meta';
|
|
10
|
-
import { featureCollection, lineString } from '@turf/helpers';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Split a LineString by another GeoJSON Feature.
|
|
14
|
-
*
|
|
15
|
-
* @name lineSplit
|
|
16
|
-
* @param {Feature<LineString>} line LineString Feature to split
|
|
17
|
-
* @param {Feature<any>} splitter Feature used to split line
|
|
18
|
-
* @returns {FeatureCollection<LineString>} Split LineStrings
|
|
19
|
-
* @example
|
|
20
|
-
* var line = turf.lineString([[120, -25], [145, -25]]);
|
|
21
|
-
* var splitter = turf.lineString([[130, -15], [130, -35]]);
|
|
22
|
-
*
|
|
23
|
-
* var split = turf.lineSplit(line, splitter);
|
|
24
|
-
*
|
|
25
|
-
* //addToMap
|
|
26
|
-
* var addToMap = [line, splitter]
|
|
27
|
-
*/
|
|
28
|
-
function lineSplit(line, splitter) {
|
|
29
|
-
if (!line) throw new Error("line is required");
|
|
30
|
-
if (!splitter) throw new Error("splitter is required");
|
|
31
|
-
|
|
32
|
-
var lineType = getType(line);
|
|
33
|
-
var splitterType = getType(splitter);
|
|
34
|
-
|
|
35
|
-
if (lineType !== "LineString") throw new Error("line must be LineString");
|
|
36
|
-
if (splitterType === "FeatureCollection")
|
|
37
|
-
throw new Error("splitter cannot be a FeatureCollection");
|
|
38
|
-
if (splitterType === "GeometryCollection")
|
|
39
|
-
throw new Error("splitter cannot be a GeometryCollection");
|
|
40
|
-
|
|
41
|
-
// remove excessive decimals from splitter
|
|
42
|
-
// to avoid possible approximation issues in rbush
|
|
43
|
-
var truncatedSplitter = truncate(splitter, { precision: 7 });
|
|
44
|
-
|
|
45
|
-
switch (splitterType) {
|
|
46
|
-
case "Point":
|
|
47
|
-
return splitLineWithPoint(line, truncatedSplitter);
|
|
48
|
-
case "MultiPoint":
|
|
49
|
-
return splitLineWithPoints(line, truncatedSplitter);
|
|
50
|
-
case "LineString":
|
|
51
|
-
case "MultiLineString":
|
|
52
|
-
case "Polygon":
|
|
53
|
-
case "MultiPolygon":
|
|
54
|
-
return splitLineWithPoints(
|
|
55
|
-
line,
|
|
56
|
-
lineIntersect(line, truncatedSplitter, {
|
|
57
|
-
ignoreSelfIntersections: true,
|
|
58
|
-
})
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Split LineString with MultiPoint
|
|
65
|
-
*
|
|
66
|
-
* @private
|
|
67
|
-
* @param {Feature<LineString>} line LineString
|
|
68
|
-
* @param {FeatureCollection<Point>} splitter Point
|
|
69
|
-
* @returns {FeatureCollection<LineString>} split LineStrings
|
|
70
|
-
*/
|
|
71
|
-
function splitLineWithPoints(line, splitter) {
|
|
72
|
-
var results = [];
|
|
73
|
-
var tree = rbush();
|
|
74
|
-
|
|
75
|
-
flattenEach(splitter, function (point) {
|
|
76
|
-
// Add index/id to features (needed for filter)
|
|
77
|
-
results.forEach(function (feature, index) {
|
|
78
|
-
feature.id = index;
|
|
79
|
-
});
|
|
80
|
-
// First Point - doesn't need to handle any previous line results
|
|
81
|
-
if (!results.length) {
|
|
82
|
-
results = splitLineWithPoint(line, point).features;
|
|
83
|
-
|
|
84
|
-
// Add Square BBox to each feature for GeoJSON-RBush
|
|
85
|
-
results.forEach(function (feature) {
|
|
86
|
-
if (!feature.bbox) feature.bbox = square(bbox(feature));
|
|
87
|
-
});
|
|
88
|
-
tree.load(featureCollection(results));
|
|
89
|
-
// Split with remaining points - lines might needed to be split multiple times
|
|
90
|
-
} else {
|
|
91
|
-
// Find all lines that are within the splitter's bbox
|
|
92
|
-
var search = tree.search(point);
|
|
93
|
-
|
|
94
|
-
if (search.features.length) {
|
|
95
|
-
// RBush might return multiple lines - only process the closest line to splitter
|
|
96
|
-
var closestLine = findClosestFeature(point, search);
|
|
97
|
-
|
|
98
|
-
// Remove closest line from results since this will be split into two lines
|
|
99
|
-
// This removes any duplicates inside the results & index
|
|
100
|
-
results = results.filter(function (feature) {
|
|
101
|
-
return feature.id !== closestLine.id;
|
|
102
|
-
});
|
|
103
|
-
tree.remove(closestLine);
|
|
104
|
-
|
|
105
|
-
// Append the two newly split lines into the results
|
|
106
|
-
featureEach(splitLineWithPoint(closestLine, point), function (line) {
|
|
107
|
-
results.push(line);
|
|
108
|
-
tree.insert(line);
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
return featureCollection(results);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Split LineString with Point
|
|
118
|
-
*
|
|
119
|
-
* @private
|
|
120
|
-
* @param {Feature<LineString>} line LineString
|
|
121
|
-
* @param {Feature<Point>} splitter Point
|
|
122
|
-
* @returns {FeatureCollection<LineString>} split LineStrings
|
|
123
|
-
*/
|
|
124
|
-
function splitLineWithPoint(line, splitter) {
|
|
125
|
-
var results = [];
|
|
126
|
-
|
|
127
|
-
// handle endpoints
|
|
128
|
-
var startPoint = getCoords(line)[0];
|
|
129
|
-
var endPoint = getCoords(line)[line.geometry.coordinates.length - 1];
|
|
130
|
-
if (
|
|
131
|
-
pointsEquals(startPoint, getCoord(splitter)) ||
|
|
132
|
-
pointsEquals(endPoint, getCoord(splitter))
|
|
133
|
-
)
|
|
134
|
-
return featureCollection([line]);
|
|
135
|
-
|
|
136
|
-
// Create spatial index
|
|
137
|
-
var tree = rbush();
|
|
138
|
-
var segments = lineSegment(line);
|
|
139
|
-
tree.load(segments);
|
|
140
|
-
|
|
141
|
-
// Find all segments that are within bbox of splitter
|
|
142
|
-
var search = tree.search(splitter);
|
|
143
|
-
|
|
144
|
-
// Return itself if point is not within spatial index
|
|
145
|
-
if (!search.features.length) return featureCollection([line]);
|
|
146
|
-
|
|
147
|
-
// RBush might return multiple lines - only process the closest line to splitter
|
|
148
|
-
var closestSegment = findClosestFeature(splitter, search);
|
|
149
|
-
|
|
150
|
-
// Initial value is the first point of the first segments (beginning of line)
|
|
151
|
-
var initialValue = [startPoint];
|
|
152
|
-
var lastCoords = featureReduce(
|
|
153
|
-
segments,
|
|
154
|
-
function (previous, current, index) {
|
|
155
|
-
var currentCoords = getCoords(current)[1];
|
|
156
|
-
var splitterCoords = getCoord(splitter);
|
|
157
|
-
|
|
158
|
-
// Location where segment intersects with line
|
|
159
|
-
if (index === closestSegment.id) {
|
|
160
|
-
previous.push(splitterCoords);
|
|
161
|
-
results.push(lineString(previous));
|
|
162
|
-
// Don't duplicate splitter coordinate (Issue #688)
|
|
163
|
-
if (pointsEquals(splitterCoords, currentCoords))
|
|
164
|
-
return [splitterCoords];
|
|
165
|
-
return [splitterCoords, currentCoords];
|
|
166
|
-
|
|
167
|
-
// Keep iterating over coords until finished or intersection is found
|
|
168
|
-
} else {
|
|
169
|
-
previous.push(currentCoords);
|
|
170
|
-
return previous;
|
|
171
|
-
}
|
|
172
|
-
},
|
|
173
|
-
initialValue
|
|
174
|
-
);
|
|
175
|
-
// Append last line to final split results
|
|
176
|
-
if (lastCoords.length > 1) {
|
|
177
|
-
results.push(lineString(lastCoords));
|
|
178
|
-
}
|
|
179
|
-
return featureCollection(results);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Find Closest Feature
|
|
184
|
-
*
|
|
185
|
-
* @private
|
|
186
|
-
* @param {Feature<Point>} point Feature must be closest to this point
|
|
187
|
-
* @param {FeatureCollection<LineString>} lines Collection of Features
|
|
188
|
-
* @returns {Feature<LineString>} closest LineString
|
|
189
|
-
*/
|
|
190
|
-
function findClosestFeature(point, lines) {
|
|
191
|
-
if (!lines.features.length) throw new Error("lines must contain features");
|
|
192
|
-
// Filter to one segment that is the closest to the line
|
|
193
|
-
if (lines.features.length === 1) return lines.features[0];
|
|
194
|
-
|
|
195
|
-
var closestFeature;
|
|
196
|
-
var closestDistance = Infinity;
|
|
197
|
-
featureEach(lines, function (segment) {
|
|
198
|
-
var pt = nearestPointOnLine(segment, point);
|
|
199
|
-
var dist = pt.properties.dist;
|
|
200
|
-
if (dist < closestDistance) {
|
|
201
|
-
closestFeature = segment;
|
|
202
|
-
closestDistance = dist;
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
return closestFeature;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Compares two points and returns if they are equals
|
|
210
|
-
*
|
|
211
|
-
* @private
|
|
212
|
-
* @param {Array<number>} pt1 point
|
|
213
|
-
* @param {Array<number>} pt2 point
|
|
214
|
-
* @returns {boolean} true if they are equals
|
|
215
|
-
*/
|
|
216
|
-
function pointsEquals(pt1, pt2) {
|
|
217
|
-
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export default lineSplit;
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var rbush = require('@turf/geojson-rbush');
|
|
4
|
-
var square = require('@turf/square');
|
|
5
|
-
var bbox = require('@turf/bbox');
|
|
6
|
-
var truncate = require('@turf/truncate');
|
|
7
|
-
var lineSegment = require('@turf/line-segment');
|
|
8
|
-
var lineIntersect = require('@turf/line-intersect');
|
|
9
|
-
var nearestPointOnLine = require('@turf/nearest-point-on-line');
|
|
10
|
-
var invariant = require('@turf/invariant');
|
|
11
|
-
var meta = require('@turf/meta');
|
|
12
|
-
var helpers = require('@turf/helpers');
|
|
13
|
-
|
|
14
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
15
|
-
|
|
16
|
-
var rbush__default = /*#__PURE__*/_interopDefaultLegacy(rbush);
|
|
17
|
-
var square__default = /*#__PURE__*/_interopDefaultLegacy(square);
|
|
18
|
-
var bbox__default = /*#__PURE__*/_interopDefaultLegacy(bbox);
|
|
19
|
-
var truncate__default = /*#__PURE__*/_interopDefaultLegacy(truncate);
|
|
20
|
-
var lineSegment__default = /*#__PURE__*/_interopDefaultLegacy(lineSegment);
|
|
21
|
-
var lineIntersect__default = /*#__PURE__*/_interopDefaultLegacy(lineIntersect);
|
|
22
|
-
var nearestPointOnLine__default = /*#__PURE__*/_interopDefaultLegacy(nearestPointOnLine);
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Split a LineString by another GeoJSON Feature.
|
|
26
|
-
*
|
|
27
|
-
* @name lineSplit
|
|
28
|
-
* @param {Feature<LineString>} line LineString Feature to split
|
|
29
|
-
* @param {Feature<any>} splitter Feature used to split line
|
|
30
|
-
* @returns {FeatureCollection<LineString>} Split LineStrings
|
|
31
|
-
* @example
|
|
32
|
-
* var line = turf.lineString([[120, -25], [145, -25]]);
|
|
33
|
-
* var splitter = turf.lineString([[130, -15], [130, -35]]);
|
|
34
|
-
*
|
|
35
|
-
* var split = turf.lineSplit(line, splitter);
|
|
36
|
-
*
|
|
37
|
-
* //addToMap
|
|
38
|
-
* var addToMap = [line, splitter]
|
|
39
|
-
*/
|
|
40
|
-
function lineSplit(line, splitter) {
|
|
41
|
-
if (!line) throw new Error("line is required");
|
|
42
|
-
if (!splitter) throw new Error("splitter is required");
|
|
43
|
-
|
|
44
|
-
var lineType = invariant.getType(line);
|
|
45
|
-
var splitterType = invariant.getType(splitter);
|
|
46
|
-
|
|
47
|
-
if (lineType !== "LineString") throw new Error("line must be LineString");
|
|
48
|
-
if (splitterType === "FeatureCollection")
|
|
49
|
-
throw new Error("splitter cannot be a FeatureCollection");
|
|
50
|
-
if (splitterType === "GeometryCollection")
|
|
51
|
-
throw new Error("splitter cannot be a GeometryCollection");
|
|
52
|
-
|
|
53
|
-
// remove excessive decimals from splitter
|
|
54
|
-
// to avoid possible approximation issues in rbush
|
|
55
|
-
var truncatedSplitter = truncate__default['default'](splitter, { precision: 7 });
|
|
56
|
-
|
|
57
|
-
switch (splitterType) {
|
|
58
|
-
case "Point":
|
|
59
|
-
return splitLineWithPoint(line, truncatedSplitter);
|
|
60
|
-
case "MultiPoint":
|
|
61
|
-
return splitLineWithPoints(line, truncatedSplitter);
|
|
62
|
-
case "LineString":
|
|
63
|
-
case "MultiLineString":
|
|
64
|
-
case "Polygon":
|
|
65
|
-
case "MultiPolygon":
|
|
66
|
-
return splitLineWithPoints(
|
|
67
|
-
line,
|
|
68
|
-
lineIntersect__default['default'](line, truncatedSplitter, {
|
|
69
|
-
ignoreSelfIntersections: true,
|
|
70
|
-
})
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Split LineString with MultiPoint
|
|
77
|
-
*
|
|
78
|
-
* @private
|
|
79
|
-
* @param {Feature<LineString>} line LineString
|
|
80
|
-
* @param {FeatureCollection<Point>} splitter Point
|
|
81
|
-
* @returns {FeatureCollection<LineString>} split LineStrings
|
|
82
|
-
*/
|
|
83
|
-
function splitLineWithPoints(line, splitter) {
|
|
84
|
-
var results = [];
|
|
85
|
-
var tree = rbush__default['default']();
|
|
86
|
-
|
|
87
|
-
meta.flattenEach(splitter, function (point) {
|
|
88
|
-
// Add index/id to features (needed for filter)
|
|
89
|
-
results.forEach(function (feature, index) {
|
|
90
|
-
feature.id = index;
|
|
91
|
-
});
|
|
92
|
-
// First Point - doesn't need to handle any previous line results
|
|
93
|
-
if (!results.length) {
|
|
94
|
-
results = splitLineWithPoint(line, point).features;
|
|
95
|
-
|
|
96
|
-
// Add Square BBox to each feature for GeoJSON-RBush
|
|
97
|
-
results.forEach(function (feature) {
|
|
98
|
-
if (!feature.bbox) feature.bbox = square__default['default'](bbox__default['default'](feature));
|
|
99
|
-
});
|
|
100
|
-
tree.load(helpers.featureCollection(results));
|
|
101
|
-
// Split with remaining points - lines might needed to be split multiple times
|
|
102
|
-
} else {
|
|
103
|
-
// Find all lines that are within the splitter's bbox
|
|
104
|
-
var search = tree.search(point);
|
|
105
|
-
|
|
106
|
-
if (search.features.length) {
|
|
107
|
-
// RBush might return multiple lines - only process the closest line to splitter
|
|
108
|
-
var closestLine = findClosestFeature(point, search);
|
|
109
|
-
|
|
110
|
-
// Remove closest line from results since this will be split into two lines
|
|
111
|
-
// This removes any duplicates inside the results & index
|
|
112
|
-
results = results.filter(function (feature) {
|
|
113
|
-
return feature.id !== closestLine.id;
|
|
114
|
-
});
|
|
115
|
-
tree.remove(closestLine);
|
|
116
|
-
|
|
117
|
-
// Append the two newly split lines into the results
|
|
118
|
-
meta.featureEach(splitLineWithPoint(closestLine, point), function (line) {
|
|
119
|
-
results.push(line);
|
|
120
|
-
tree.insert(line);
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
return helpers.featureCollection(results);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Split LineString with Point
|
|
130
|
-
*
|
|
131
|
-
* @private
|
|
132
|
-
* @param {Feature<LineString>} line LineString
|
|
133
|
-
* @param {Feature<Point>} splitter Point
|
|
134
|
-
* @returns {FeatureCollection<LineString>} split LineStrings
|
|
135
|
-
*/
|
|
136
|
-
function splitLineWithPoint(line, splitter) {
|
|
137
|
-
var results = [];
|
|
138
|
-
|
|
139
|
-
// handle endpoints
|
|
140
|
-
var startPoint = invariant.getCoords(line)[0];
|
|
141
|
-
var endPoint = invariant.getCoords(line)[line.geometry.coordinates.length - 1];
|
|
142
|
-
if (
|
|
143
|
-
pointsEquals(startPoint, invariant.getCoord(splitter)) ||
|
|
144
|
-
pointsEquals(endPoint, invariant.getCoord(splitter))
|
|
145
|
-
)
|
|
146
|
-
return helpers.featureCollection([line]);
|
|
147
|
-
|
|
148
|
-
// Create spatial index
|
|
149
|
-
var tree = rbush__default['default']();
|
|
150
|
-
var segments = lineSegment__default['default'](line);
|
|
151
|
-
tree.load(segments);
|
|
152
|
-
|
|
153
|
-
// Find all segments that are within bbox of splitter
|
|
154
|
-
var search = tree.search(splitter);
|
|
155
|
-
|
|
156
|
-
// Return itself if point is not within spatial index
|
|
157
|
-
if (!search.features.length) return helpers.featureCollection([line]);
|
|
158
|
-
|
|
159
|
-
// RBush might return multiple lines - only process the closest line to splitter
|
|
160
|
-
var closestSegment = findClosestFeature(splitter, search);
|
|
161
|
-
|
|
162
|
-
// Initial value is the first point of the first segments (beginning of line)
|
|
163
|
-
var initialValue = [startPoint];
|
|
164
|
-
var lastCoords = meta.featureReduce(
|
|
165
|
-
segments,
|
|
166
|
-
function (previous, current, index) {
|
|
167
|
-
var currentCoords = invariant.getCoords(current)[1];
|
|
168
|
-
var splitterCoords = invariant.getCoord(splitter);
|
|
169
|
-
|
|
170
|
-
// Location where segment intersects with line
|
|
171
|
-
if (index === closestSegment.id) {
|
|
172
|
-
previous.push(splitterCoords);
|
|
173
|
-
results.push(helpers.lineString(previous));
|
|
174
|
-
// Don't duplicate splitter coordinate (Issue #688)
|
|
175
|
-
if (pointsEquals(splitterCoords, currentCoords))
|
|
176
|
-
return [splitterCoords];
|
|
177
|
-
return [splitterCoords, currentCoords];
|
|
178
|
-
|
|
179
|
-
// Keep iterating over coords until finished or intersection is found
|
|
180
|
-
} else {
|
|
181
|
-
previous.push(currentCoords);
|
|
182
|
-
return previous;
|
|
183
|
-
}
|
|
184
|
-
},
|
|
185
|
-
initialValue
|
|
186
|
-
);
|
|
187
|
-
// Append last line to final split results
|
|
188
|
-
if (lastCoords.length > 1) {
|
|
189
|
-
results.push(helpers.lineString(lastCoords));
|
|
190
|
-
}
|
|
191
|
-
return helpers.featureCollection(results);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Find Closest Feature
|
|
196
|
-
*
|
|
197
|
-
* @private
|
|
198
|
-
* @param {Feature<Point>} point Feature must be closest to this point
|
|
199
|
-
* @param {FeatureCollection<LineString>} lines Collection of Features
|
|
200
|
-
* @returns {Feature<LineString>} closest LineString
|
|
201
|
-
*/
|
|
202
|
-
function findClosestFeature(point, lines) {
|
|
203
|
-
if (!lines.features.length) throw new Error("lines must contain features");
|
|
204
|
-
// Filter to one segment that is the closest to the line
|
|
205
|
-
if (lines.features.length === 1) return lines.features[0];
|
|
206
|
-
|
|
207
|
-
var closestFeature;
|
|
208
|
-
var closestDistance = Infinity;
|
|
209
|
-
meta.featureEach(lines, function (segment) {
|
|
210
|
-
var pt = nearestPointOnLine__default['default'](segment, point);
|
|
211
|
-
var dist = pt.properties.dist;
|
|
212
|
-
if (dist < closestDistance) {
|
|
213
|
-
closestFeature = segment;
|
|
214
|
-
closestDistance = dist;
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
return closestFeature;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Compares two points and returns if they are equals
|
|
222
|
-
*
|
|
223
|
-
* @private
|
|
224
|
-
* @param {Array<number>} pt1 point
|
|
225
|
-
* @param {Array<number>} pt2 point
|
|
226
|
-
* @returns {boolean} true if they are equals
|
|
227
|
-
*/
|
|
228
|
-
function pointsEquals(pt1, pt2) {
|
|
229
|
-
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
module.exports = lineSplit;
|
|
233
|
-
module.exports.default = lineSplit;
|
package/index.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Feature,
|
|
3
|
-
FeatureCollection,
|
|
4
|
-
Point,
|
|
5
|
-
MultiPoint,
|
|
6
|
-
LineString,
|
|
7
|
-
MultiLineString,
|
|
8
|
-
Polygon,
|
|
9
|
-
MultiPolygon,
|
|
10
|
-
} from "geojson";
|
|
11
|
-
|
|
12
|
-
export type Splitter = Feature<
|
|
13
|
-
Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon
|
|
14
|
-
>;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* http://turfjs.org/docs/#linesplit
|
|
18
|
-
*/
|
|
19
|
-
export default function lineSplit<T extends LineString>(
|
|
20
|
-
line: Feature<T> | T,
|
|
21
|
-
splitter: Splitter
|
|
22
|
-
): FeatureCollection<T>;
|