turf.lineSegment
What is turf.lineSegment?
turf.lineSegment takes a LineString, MultiLineString, Polygon, or MultiPolygon and returns a FeatureCollection<LineString> where each feature is a two-vertex segment from the original geometry.
turf.lineSegment(geojson) → FeatureCollection<LineString>When would you use turf.lineSegment?
Use turf.lineSegment when you need to iterate per-edge — for example, computing bearings per segment, finding the longest or shortest edges, detecting near-colinear edges, or building an r-tree over segments for fast proximity queries.
In Node.js, segment-level operations are the foundation of many custom topology algorithms. Combined with @turf/geojson-rbush, segments give you a performant way to do edge-based spatial queries.
undefinedFAQs
How many segments does a polygon produce?
N − 1 for a closed ring with N coordinates (because the first and last coordinates are duplicates). For polygons with holes, segments from all rings are included.
How do I install just lineSegment?
npm install @turf/line-segment. It depends on @turf/helpers and @turf/meta.
Are the segments ordered?
Yes — they follow the original coordinate sequence, so segment index i connects vertex i to vertex i+1.
Do input properties carry through?
Yes — each segment inherits the parent feature's properties. Overwrite after if you need per-segment attributes.