Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better “label centroid” initializer? (polylabel) #1587

Open
Fil opened this issue May 18, 2023 · 5 comments · May be fixed by #2098
Open

A better “label centroid” initializer? (polylabel) #1587

Fil opened this issue May 18, 2023 · 5 comments · May be fixed by #2098
Labels
enhancement New feature or request question Further information is needed

Comments

@Fil
Copy link
Contributor

Fil commented May 18, 2023

The centroid of a geometry is sometimes outside of it, for example (but not exclusively) with a MultiPolygon such as France+Guyane. This can result in labels or interactive tips that are quite far off from the shape they identify.

A great solution is to use @mapbox/polylabel, which guarantees that the center is inside —and furthermore at an optimal position for a centered label. The dependency is less that 1kB over the wire, including tinyqueue. An alternative approach could be to add an option to Plot.centroid, to make it choose the centroid of the largest polygon when given a multipolygon. Subpar solution, but cheaper.

Prototype https://observablehq.com/@observablehq/plot-pole-of-inaccessibility-1587

@Fil Fil added enhancement New feature or request question Further information is needed labels May 18, 2023
@fgregg
Copy link

fgregg commented Aug 2, 2023

the centroid of the largest polygon could also lead to the point being outside the polygon. this would be the case for a crescent-moon shaped polygon.

polylabel sounds great!

@Fil
Copy link
Contributor Author

Fil commented Aug 2, 2023

We should probably call it labelCentroid, with an ellipseRatio option that defaults to 2 (?). And maybe add an anchor option so that we can align text on the left (with textAnchor: start) or on the right (with textAnchor: end). Since polylabel returns the radius, it's not expensive to add.

@fgregg
Copy link

fgregg commented Aug 2, 2023

this approach could also make it easier to scale labels so they fit inside of polygons, since you'll know the ellipse dimensions anyway.

@Fil
Copy link
Contributor Author

Fil commented Jun 17, 2024

See #2087 (comment) for using this as part of a "geo" pointer option.

@Fil
Copy link
Contributor Author

Fil commented Jun 17, 2024

Related:

@Fil Fil changed the title A better “label centroid” initializer? A better “label centroid” initializer? (polylabel) Aug 25, 2024
@Fil Fil linked a pull request Dec 5, 2024 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants