mapboxmapbox-gl-jsmapbox-studio

Make contour labels point uphill: reverse direction of text label on line feature


I am designing a map in Mapbox Studio, using mapbox.mapbox-terrain-v2’s contour layer to determine the contour lines. This is based on the outdoors style.

In the outdoors style, the contour labels are oriented so that the text is displayed between -90° and 90° of horizontal, because Keep Text Upright is set. If I disable this, then the text becomes oriented at any angle. However, this text is oriented at exactly the opposite angle to what I would like: it always points downhill, not uphill. See first image below: note that the numbers are upright where the elevation increases from north to south.

What I am aiming for is to use the Ordnance Survey style of orienting numbers uphill (upright where the elevation increases from south to north), as in the second image below.

My current style can be seen here. I have tried using the text-rotate field, but this rotates each character individually, and there does not appear to be a function to reverse the string to counteract this. Additionally, this rotates about the top of each letter, meaning that the result is offset from the actual contour line, and text-translate + text-translate-anchor seem unable to resolve this.

current state

desired


Solution

  • As you noticed, using text-rotate on a symbol layer with placement: line rotates each character individually. So you would need to then reverse the string, which isn't possible within the expressions available.

    However. There is one hacky workaround which may solve the problem for you. You can simply hardcode all the possible elevations with their reversals:

    ["match", ["get", "ele"],
    750, "m057",
    700, "m007",
    650, "m056",
    600, "m006",
    550, "m055",
    500, "m005",
     ""
    ]
    

    Combined with a text-offset of 0,1.5, this actually looks alright:

    enter image description here

    I've published the style here