turf.polygon
What is turf.polygon?
turf.polygon wraps an array of linear rings into a valid Feature<Polygon>. The first ring is the outer boundary; any subsequent rings are holes. Each ring must have at least four positions and must close (first and last position identical).
turf.polygon(coordinates, properties?, options?) → Feature<Polygon>Options include:
bbox— bounding box[minX, minY, maxX, maxY]id— identifier for the feature
When would you use turf.polygon?
Use turf.polygon when you need to construct a polygon feature from raw coordinates — a draw control's output ring, a bounding box converted via turf.bboxPolygon, or a parcel footprint parsed from a non-GeoJSON source — before feeding it to turf.area, turf.booleanPointInPolygon, or another spatial operation.
It is also the way to add holes. Provide the outer ring first and each hole as an additional ring in the same outer array.
1const parcelWithCourtyard = turf.polygon(
2 [
3 [[-122.42, 37.77], [-122.41, 37.77], [-122.41, 37.78], [-122.42, 37.78], [-122.42, 37.77]],
4 [[-122.418, 37.772], [-122.415, 37.772], [-122.415, 37.775], [-122.418, 37.775], [-122.418, 37.772]]
5 ],
6 { parcelId: 'P-42' }
7);FAQs
How do I install Turf.js to use this function?
Install npm install @turf/helpers and import with import { polygon } from '@turf/helpers', or access via the @turf/turf umbrella bundle.
What errors can turf.polygon throw?
It throws if any ring has fewer than four positions, or if the first and last position of a ring do not match. Close your rings explicitly by repeating the first coordinate at the end.
Does turf.polygon enforce ring winding order?
No. GeoJSON recommends counter-clockwise outer rings and clockwise holes, but Turf does not rewrite the winding. Use turf.rewind if you need spec-compliant winding, or turf.booleanClockwise to check.
How do I build a MultiPolygon or add multiple disconnected parts?
Use turf.multiPolygon instead — its coordinate array has one extra level of nesting to accommodate multiple polygons, each with its own outer ring and holes.