Generate centroids from polygons for GL JS labeling with Turf.js
I have been excited to try Turf.js since it came out, but I haven’t had a reason to until now. It is as awesome as I thought it would be.
One problem with GL JS styling is placing labels in the middle of polygons. As in, you can’t.
Fair enough. I imagine this is due to centroid calculation slagging client-side rendering performance. I added a turf.js build step that automates the generation of a centroid point file. It chews through ~470 complex polygons in 1-2 seconds, and I don’t have to worry about maintaining another data layer. Problem solved.
1 | var fs = require('fs'); |
Edit: If you have concave/ugly polys, Turf’s point-on-surface is a bit slower but you won’t get a centroid outside of a polygon. Switch in var turfCentroid = require('turf-point-on-surface');
and everything else stays the same.