Improve the polygon label function for rectangular geometries, and automatically compute a sane tolerance
Description
None
Environment
The polylabel function uses the calculation of the maximum inscribed circle. That’s generally good, but it has drawbacks:
On a rectangular shape, there are infinite maximum inscribed circles. It should be picking a sane position
The “tolerance” needs to be “rough” to get good results, otherwise it’s too sensitive to small local differences. But a layer has many polygons of different sizes, each one would need a different value. Allow an automatic computation
The position is sometimes odd when the centroid or the center of the bounding rectangle could be visually more obvious. Compare with those positions to see if they are inside the polygon, and have a comparable distance with the optimal result, and choose one of them in that case.
For reference this is the default result for topp:states, with a tolerance of 0.1 (some labels are pretty bad, on the side of the polygon when there is a lot of space in the middle):
The polylabel function uses the calculation of the maximum inscribed circle. That’s generally good, but it has drawbacks:
On a rectangular shape, there are infinite maximum inscribed circles. It should be picking a sane position
The “tolerance” needs to be “rough” to get good results, otherwise it’s too sensitive to small local differences. But a layer has many polygons of different sizes, each one would need a different value. Allow an automatic computation
The position is sometimes odd when the centroid or the center of the bounding rectangle could be visually more obvious. Compare with those positions to see if they are inside the polygon, and have a comparable distance with the optimal result, and choose one of them in that case.
For reference this is the default result for topp:states, with a tolerance of 0.1 (some labels are pretty bad, on the side of the polygon when there is a lot of space in the middle):
This is the output with the improved algorithm: