Turf.jsMeasurement

turf.centroid

What is turf.centroid?

turf.centroid computes the mean longitude and latitude of every vertex in the input geometry and returns a Point Feature at that location. It is the simplest and fastest centroid algorithm in Turf, but because it treats every vertex equally it can be pulled toward areas with denser digitisation.

JavaScript
turf.centroid(geojson, options?) → Feature<Point>

Options include:

  • properties — object assigned to the output point's properties

When would you use turf.centroid?

Use turf.centroid when you need a very quick representative point for any GeoJSON input — lines, polygons, or mixed FeatureCollections — and exact centering is less important than speed. It is especially handy for clustering hover-interactions on a MapLibre map, or as a fallback when you want a single point to index a feature in a lookup table.

For labelling irregular polygons, prefer turf.centerOfMass or turf.pointOnFeature. For zoom/fit anchors, prefer turf.center. turf.centroid shines in low-stakes cases like quick summary statistics or anchor points over many features.

Code
undefined

FAQs

Why does the centroid shift when I densify my polygon?

turf.centroid is the mean of vertices, so adding more vertices on one side shifts the result toward that side. If this matters, switch to turf.centerOfMass, which is geometry-dependent rather than vertex-dependent.

Does it work on LineStrings and Points too?

Yes. turf.centroid accepts any GeoJSON input and walks all coordinates with @turf/meta. For a Point Feature it simply returns that point; for a FeatureCollection it averages across all features.

How do I install just centroid?

npm install @turf/centroid. It has a tiny footprint — just @turf/helpers and @turf/meta — and is one of the lightest Turf functions to include in a client-side bundle.

Is the result guaranteed to be inside the polygon?

No. For concave or multipart polygons the centroid can sit outside the feature. Use turf.pointOnFeature when you need a point guaranteed to lie on the geometry.