0
# Line Processing
1
2
Specialized functions for working with LineString geometries including splitting, slicing, offsetting, and intersection operations. These functions provide comprehensive tools for linear feature analysis and manipulation.
3
4
## Capabilities
5
6
### Line Slicing and Segmentation
7
8
Extract portions of lines based on points or distances.
9
10
```typescript { .api }
11
/**
12
* Takes a line, a start Point, and a stop point and returns a subsection of the line in-between those points.
13
*/
14
function lineSlice(
15
startPt: Coord,
16
stopPt: Coord,
17
line: Feature<LineString> | LineString
18
): Feature<LineString>;
19
20
/**
21
* Takes a line and returns a subsection of the line between startDist and stopDist using distance along the line.
22
*/
23
function lineSliceAlong(
24
line: Feature<LineString> | LineString,
25
startDist: number,
26
stopDist: number,
27
options?: { units?: Units }
28
): Feature<LineString>;
29
30
/**
31
* Creates a FeatureCollection of 2-vertex LineString segments from a LineString.
32
*/
33
function lineSegment<P>(
34
geojson: FeatureCollection<LineString, P> | Feature<LineString, P> | LineString
35
): FeatureCollection<LineString, P>;
36
37
/**
38
* Divides a LineString into chunks of a specified length.
39
*/
40
function lineChunk<P>(
41
geojson: FeatureCollection<LineString, P> | Feature<LineString, P> | LineString,
42
segmentLength: number,
43
options?: { units?: Units; reverse?: boolean }
44
): FeatureCollection<LineString, P>;
45
```
46
47
**Usage Examples:**
48
49
```typescript
50
import { lineSlice, lineSliceAlong, lineSegment, lineChunk, lineString, point } from "@turf/turf";
51
52
const line = lineString([
53
[-77.031669, 38.878605],
54
[-77.029609, 38.881946],
55
[-77.020339, 38.884084],
56
[-77.025661, 38.885821],
57
[-77.021884, 38.889563],
58
[-77.019824, 38.892368]
59
]);
60
61
// Slice between two points
62
const start = point([-77.029609, 38.881946]);
63
const stop = point([-77.021884, 38.889563]);
64
const sliced = lineSlice(start, stop, line);
65
66
// Slice by distance (1km to 3km along line)
67
const slicedByDistance = lineSliceAlong(line, 1, 3, { units: 'kilometers' });
68
69
// Break into segments
70
const segments = lineSegment(line);
71
72
// Break into chunks of specific length
73
const chunks = lineChunk(line, 0.5, { units: 'kilometers' });
74
75
console.log('Sliced line:', sliced);
76
console.log(`Line broken into ${segments.features.length} segments`);
77
console.log(`Line broken into ${chunks.features.length} chunks`);
78
```
79
80
### Line Splitting
81
82
Split lines using points or other geometric features.
83
84
```typescript { .api }
85
/**
86
* Split a LineString by another GeoJSON Feature.
87
*/
88
function lineSplit(
89
line: FeatureCollection<LineString> | Feature<LineString> | LineString,
90
splitter: FeatureCollection | Feature<any> | Geometry
91
): FeatureCollection<LineString>;
92
```
93
94
**Usage Examples:**
95
96
```typescript
97
import { lineSplit, lineString, point, multiPoint } from "@turf/turf";
98
99
const line = lineString([
100
[-95, 25], [-85, 25], [-75, 35], [-65, 35]
101
]);
102
103
// Split by single point
104
const splitPoint = point([-85, 25]);
105
const splitBySinglePoint = lineSplit(line, splitPoint);
106
107
// Split by multiple points
108
const splitPoints = multiPoint([
109
[-85, 25], [-75, 35]
110
]);
111
const splitByMultiplePoints = lineSplit(line, splitPoints);
112
113
console.log(`Line split into ${splitBySinglePoint.features.length} segments by single point`);
114
console.log(`Line split into ${splitByMultiplePoints.features.length} segments by multiple points`);
115
```
116
117
### Line Offsetting
118
119
Create parallel lines at specified distances.
120
121
```typescript { .api }
122
/**
123
* Takes a LineString or MultiLineString and returns a line offset by the specified distance.
124
*/
125
function lineOffset(
126
geojson: Feature<LineString | MultiLineString> | LineString | MultiLineString,
127
distance: number,
128
options?: { units?: Units }
129
): FeatureCollection<LineString>;
130
```
131
132
**Usage Examples:**
133
134
```typescript
135
import { lineOffset, lineString } from "@turf/turf";
136
137
const line = lineString([
138
[-83, 30], [-84, 36], [-78, 41]
139
]);
140
141
// Create parallel lines
142
const offsetLeft = lineOffset(line, 2, { units: 'kilometers' });
143
const offsetRight = lineOffset(line, -2, { units: 'kilometers' });
144
145
console.log('Left offset:', offsetLeft);
146
console.log('Right offset:', offsetRight);
147
```
148
149
### Line Intersections
150
151
Find intersection points between linear features.
152
153
```typescript { .api }
154
/**
155
* Takes any LineString or Polygon GeoJSON and returns the intersecting point(s).
156
*/
157
function lineIntersect(
158
line1: FeatureCollection | Feature<LineString | MultiLineString | Polygon | MultiPolygon> | LineString | MultiLineString | Polygon | MultiPolygon,
159
line2: FeatureCollection | Feature<LineString | MultiLineString | Polygon | MultiPolygon> | LineString | MultiLineString | Polygon | MultiPolygon,
160
options?: { removeDuplicates?: boolean }
161
): FeatureCollection<Point>;
162
```
163
164
**Usage Examples:**
165
166
```typescript
167
import { lineIntersect, lineString } from "@turf/turf";
168
169
const line1 = lineString([
170
[-126, -11], [-116, -10], [-102, -11], [-100, -17]
171
]);
172
173
const line2 = lineString([
174
[-123, -17], [-119, -12], [-115, -16], [-105, -9]
175
]);
176
177
// Find intersection points
178
const intersections = lineIntersect(line1, line2);
179
180
console.log(`Found ${intersections.features.length} intersection points`);
181
intersections.features.forEach((point, i) => {
182
console.log(`Intersection ${i + 1}:`, point.geometry.coordinates);
183
});
184
```
185
186
### Line Overlap Analysis
187
188
Find overlapping portions between lines.
189
190
```typescript { .api }
191
/**
192
* Takes any LineString or Polygon and returns the overlapping lines between both features.
193
*/
194
function lineOverlap(
195
line1: Feature<LineString | MultiLineString | Polygon | MultiPolygon> | LineString | MultiLineString | Polygon | MultiPolygon,
196
line2: Feature<LineString | MultiLineString | Polygon | MultiPolygon> | LineString | MultiLineString | Polygon | MultiPolygon,
197
options?: { tolerance?: number }
198
): FeatureCollection<LineString>;
199
```
200
201
**Usage Examples:**
202
203
```typescript
204
import { lineOverlap, lineString } from "@turf/turf";
205
206
const line1 = lineString([
207
[0, 0], [1, 1], [2, 2], [3, 3]
208
]);
209
210
const line2 = lineString([
211
[1, 1], [2, 2], [3, 3], [4, 4]
212
]);
213
214
// Find overlapping segments
215
const overlaps = lineOverlap(line1, line2);
216
217
console.log(`Found ${overlaps.features.length} overlapping segments`);
218
overlaps.features.forEach(overlap => {
219
console.log('Overlap:', overlap.geometry.coordinates);
220
});
221
```
222
223
### Line-Polygon Conversion
224
225
Convert between line and polygon geometries.
226
227
```typescript { .api }
228
/**
229
* Converts a LineString to a Polygon.
230
*/
231
function lineToPolygon<P>(
232
geojson: FeatureCollection<LineString, P> | Feature<LineString, P> | LineString,
233
options?: { properties?: P; autoComplete?: boolean; orderCoords?: boolean }
234
): FeatureCollection<Polygon, P> | Feature<Polygon, P>;
235
236
/**
237
* Converts a Polygon to a LineString or MultiLineString.
238
*/
239
function polygonToLine<P>(
240
geojson: FeatureCollection<Polygon | MultiPolygon, P> | Feature<Polygon | MultiPolygon, P> | Polygon | MultiPolygon,
241
options?: { properties?: P }
242
): FeatureCollection<LineString, P> | Feature<LineString | MultiLineString, P>;
243
```
244
245
**Usage Examples:**
246
247
```typescript
248
import { lineToPolygon, polygonToLine, lineString, polygon } from "@turf/turf";
249
250
// Convert line to polygon (must be closed)
251
const closedLine = lineString([
252
[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]
253
]);
254
255
const poly = lineToPolygon(closedLine, {
256
properties: { converted: true },
257
autoComplete: true
258
});
259
260
// Convert polygon back to line
261
const originalPoly = polygon([
262
[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]],
263
[[1, 1], [1, 4], [4, 4], [4, 1], [1, 1]] // hole
264
]);
265
266
const lines = polygonToLine(originalPoly, {
267
properties: { type: 'boundary' }
268
});
269
270
console.log('Line to polygon:', poly);
271
console.log('Polygon to lines:', lines);
272
```
273
274
### Geometric Line Operations
275
276
Perform geometric operations on linear features.
277
278
```typescript { .api }
279
/**
280
* Returns kinks (self-intersections) in a LineString.
281
*/
282
function kinks<P>(
283
geojson: FeatureCollection<LineString | MultiLineString, P> | Feature<LineString | MultiLineString, P> | LineString | MultiLineString
284
): FeatureCollection<Point, P>;
285
286
/**
287
* Removes kinks from a Polygon.
288
*/
289
function unkinkPolygon<P>(
290
geojson: FeatureCollection<Polygon | MultiPolygon, P> | Feature<Polygon | MultiPolygon, P> | Polygon | MultiPolygon
291
): FeatureCollection<Polygon, P>;
292
```
293
294
**Usage Examples:**
295
296
```typescript
297
import { kinks, unkinkPolygon, lineString, polygon } from "@turf/turf";
298
299
// Find kinks in self-intersecting line
300
const selfIntersectingLine = lineString([
301
[0, 0], [2, 2], [2, 0], [0, 2]
302
]);
303
304
const kinkPoints = kinks(selfIntersectingLine);
305
306
// Remove kinks from polygon
307
const kinkedPolygon = polygon([[
308
[0, 0], [2, 2], [2, 0], [0, 2], [0, 0]
309
]]);
310
311
const unkinked = unkinkPolygon(kinkedPolygon);
312
313
console.log(`Found ${kinkPoints.features.length} kinks`);
314
console.log('Unkinked polygons:', unkinked.features.length);
315
```