turf.pointOnFeature
What is turf.pointOnFeature?
turf.pointOnFeature returns a Point that is guaranteed to lie on the input feature — unlike turf.center or turf.centroid, which can fall outside concave polygons. For polygons it first tries the centroid, and if that point is outside the shape it falls back to a vertex on the boundary.
turf.pointOnFeature(geojson) → Feature<Point>When would you use turf.pointOnFeature?
Use turf.pointOnFeature as a label anchor for irregular or multipart polygons — country outlines, river polygons, U-shaped parks, or any shape where the centroid might land in a hole or outside the feature. The output is safe to pass to a MapLibre or Mapbox symbol layer without visual "label in the sea" artefacts.
It is also useful for click-through testing — generating a representative point for each feature that your backend can use to validate hit-tests, cluster inputs, or anchor popups.
undefinedFAQs
How is this different from turf.centerOfMass?
turf.centerOfMass returns the area-weighted centroid, which can still fall outside concave shapes. turf.pointOnFeature explicitly validates containment and returns a point guaranteed to intersect the feature, which is what you want for labels.
How do I install just pointOnFeature?
npm install @turf/point-on-feature. It depends on @turf/boolean-point-in-polygon, @turf/center, and @turf/meta.
Is the returned point the centroid, a vertex, or something else?
It prefers the centroid when the centroid is inside. Otherwise it returns a coordinate on the feature's boundary. For LineStrings and Points the behaviour is simpler — it returns an existing vertex.
Does it handle FeatureCollections?
Yes. For a FeatureCollection it picks a single representative point from the collection. If you want a point per feature, iterate with features.map(f => pointOnFeature(f)).