turf.centerOfMass
What is turf.centerOfMass?
turf.centerOfMass returns the true centroid of a polygon — the point where the shape would balance if cut from uniform material. It implements the signed-area centroid formula, which is more accurate for irregular polygons than averaging vertices.
turf.centerOfMass(geojson, options?) → Feature<Point>Options include:
properties— object assigned to the output point'sproperties
When would you use turf.centerOfMass?
Use turf.centerOfMass when placing labels on irregular polygons such as country shapes, administrative boundaries, or drawn parcels. It gives a visually centred result because it accounts for area distribution — a much better label anchor than a vertex mean, especially for elongated or L-shaped polygons.
In a Node.js ETL job, call it during feature ingestion to precompute a labelPoint property on each polygon so that your client-side map can render labels without additional geometry work at runtime.
undefinedFAQs
Is the result always inside the polygon?
No. For strongly concave shapes (crescents, U-shapes) the center of mass can fall outside the polygon. If you need a guaranteed interior point — especially for labels — prefer turf.pointOnFeature.
How does it differ from turf.centroid?
turf.centroid is the arithmetic mean of all vertices, which skews toward areas with more vertices. turf.centerOfMass uses the signed-area formula so the result depends on geometry, not digitisation density. For labelling, centerOfMass is almost always the better choice.
Does it handle MultiPolygon inputs?
Yes. For MultiPolygon features it computes a single weighted centroid across all polygons, with each contributing by its area. If you want per-ring centers, split with turf.flatten first.
How do I install just centerOfMass?
npm install @turf/center-of-mass. It depends on @turf/convex, @turf/centroid, and a few small helpers, so it is still small but slightly heavier than turf.center.