Turf.jsFeature Conversion

turf.lineToPolygon

What is turf.lineToPolygon?

turf.lineToPolygon converts a LineString or MultiLineString into a Polygon or MultiPolygon Feature. If the input is not closed (first coordinate equals last), it closes the ring automatically.

JavaScript
turf.lineToPolygon(line, options?) → Feature<Polygon | MultiPolygon>

Options include:

  • properties — output feature properties (default: input properties)
  • autoCompleteboolean (default true); closes open rings automatically
  • orderCoordsboolean (default true); orients coordinates per GeoJSON right-hand rule

When would you use turf.lineToPolygon?

Use turf.lineToPolygon when a user draws a closed shape as a LineString (e.g. some MapLibre draw tools emit lines for closed shapes) and you want to treat it as a Polygon for area or intersect queries. It is also useful when importing a closed polyline from a KML or CSV export that stores rings as open lines.

In Node.js ETL, turf.lineToPolygon normalises boundary datasets where administrative edges are stored as lines — you can rebuild polygons by pairing it with turf.polygonize for network-level input or turf.lineToPolygon directly for already-closed rings.

Code
undefined

FAQs

What if the line is not closed?

With autoComplete: true (default) Turf appends the first coordinate to the end to close the ring. Disable autoComplete to throw instead when the input is open.

How do I install just lineToPolygon?

npm install @turf/line-to-polygon. It depends on @turf/helpers and @turf/invariant.

Does the ring get proper right-hand-rule orientation?

Yes when orderCoords: true (default). Turf reorders the ring per RFC 7946 so exterior rings are counter-clockwise.

What about self-intersecting lines?

turf.lineToPolygon does not validate topology — a self-crossing line becomes an invalid polygon. Validate with turf.kinks or turf.booleanValid if the input source is untrusted.