Skip to content

Offset conflicting country labels #1002

Open

Description

@1ec5

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.

Activity

  1. added theissue type on Mar 27, 2025
  2. tsinn commented on Mar 31, 2025

    @tsinn

    I've tested the basic approach shown here: https://maplibre.org/maplibre-gl-js/docs/examples/variable-offset-label-placement/

    "text-variable-anchor-offset": ["top", [0, 1], "bottom", [0, -2], "left", [1, 0], "right", [-2, 0]],
    "text-justify": "auto",
    

    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.

    Image

    Image

  3. 1ec5 commented on Mar 31, 2025

    @1ec5
    MemberAuthor

    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.

  4. added a commit that references this issue on Mar 31, 2025
  5. 1ec5 commented on Apr 10, 2025

    @1ec5
    MemberAuthor

    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 United States over Canada, Denmark over Ireland, Ethiopia over Sudan, Vietnam over India, South Korea over Japan, the People’s Republic of China over Mongolia, Pakistan over Nepal, Romania over Georgia, etc.

    The embed site with collision boxes enabled (map.showCollisionBoxes = true) shows that the offset labels wouldn’t have collided with anything anyways:

    The same with collision boxes enabled.

    The current expression specifies anchors top, bottom, left, and right but not center. 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-offset is measured in ems – that is, based on the label’s line height. At a minimum, we need to stick this expression in an interpolate expression based on the zoom level, and the values need to account for any change in text-size from one zoom level to another.

    z3 z4
    Cuba z3 Cuba z4

    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.

  6. moved this to In Progress in OHM Cartoon Apr 10, 2025
  7. added a commit that references this issue on Apr 10, 2025
  8. moved this from In Progress to TODO Soon in OHM Cartoon Oct 8, 2025
  9. moved this from TODO Soon to No status in OHM Cartoon Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions