Repository navigation
Description
Sometimes, the label of a smaller country happens to be close enough to the label of a bigger country that we have to prioritize the bigger country’s label (#985) to avoid littering the map with obscure features. But countries usually aren’t perfect circles, so there may be enough room to one side that the smaller country’s label can be shifted over without affecting accuracy. We should allow the renderer to offset the lower-priority label before hiding it as a last resort.
Automatic placement will save mappers the trouble of deconflicting the labels using a manually mapped label point. That workaround is inherently limited anyways, because the font size does not vary linearly with the map’s style, and the user can also rotate and tilt the map.
The MapLibre Style Specification includes a text-variable-anchor-offset property that lets MapLibre attempt to place the label at any number of relative offsets. We can vary these offsets in magnitude by zoom level. We could also vary the offsets by the country’s size (based on area_km2), to offset more aggressively without sending the label clear into another country. However, only MapLibre GL JS supports data-driven styling for this property so far: maplibre/maplibre-native#2358.
text-variable-anchor-offset is specific to MapLibre and not included in the Mapbox Style Specification, so we would be breaking compatibility with Mapbox for the first time (see also #963). This is not necessarily a problem for the Web, since GL JS merely warns about unrecognized properties, but we would be precluding support for the Mapbox Maps SDK for Android and iOS, which would raise an error. text-variable-anchor is common to the two specifications, but it doesn’t support data-driven styling.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
Testing text-variable-anchor-offset
Testing text variable anchor, radial offset, justify auto
Revert to anchor offset
tsinn commentedon Mar 31, 2025
I've tested the basic approach shown here: https://maplibre.org/maplibre-gl-js/docs/examples/variable-offset-label-placement/
While reviewing, it seems like this offset would work best when there are label points associated with each label, which I don't think we want for country points. When zoomed far out, there are some country labels that end up being offset into other countries' boundaries, such as Luxembourg.
1ec5 commentedon Mar 31, 2025
The example provided by MapLibre is for a label attached below an icon, similar to our city icons. Our country labels appear by themselves, so the offsets need to be more conservative, and I’m not sure but (0, 0) might need to be one of the entries too. That could reduce the risk of misplacement, though it’s imperfect: osm-americana/openstreetmap-americana#533. Hence the idea about conditionalizing the offset on the
area_km2.Incidentally, city labels would be interesting to experiment with as well, especially since they always have manually placed label points.
Smaller offset values
1ec5 commentedon Apr 10, 2025
The experiments so far landed in OpenHistoricalMap/ohm-deploy#509. Unfortunately it isn’t ready for production. At low zoom levels, every label is either over the wrong country or inexplicably offset far from a vacant centroid.
The embed site with collision boxes enabled (
map.showCollisionBoxes = true) shows that the offset labels wouldn’t have collided with anything anyways:The current expression specifies anchors
top,bottom,left, andrightbut notcenter. We actually prefer labels to be centered, but this expression prevents any label from being centered on the centroid anymore.Comparing the label for Cuba at vector zoom levels 3 and 4, another problem seems to be that the offset doesn’t vary by zoom level.
text-variable-anchor-offsetis measured in ems – that is, based on the label’s line height. At a minimum, we need to stick this expression in aninterpolateexpression based on the zoom level, and the values need to account for any change intext-sizefrom one zoom level to another.Can we make these changes and see how it improves low-zoom labeling? If it doesn’t work, I think we should back out the changes and experiment further on a copy of the stylesheet that isn’t in the production pipeline.
Localities and offsets