1

With MapServer, is it possible to use an expression to round a number for a label?

For example, my dataset has the float value 174.000000000 which I want to show in a label as 174.

I have tried a few mathematical expressions for a label, but, now think that MapServer may not accomodate mathematical expressions in labelling.

3

Certainly yes but is difficult to guess how to do it even it is somehow documented in https://www.mapserver.org/mapfile/expressions.html

The function to use is "tostring"

tostring ( n1, “Format1” )

uses “Format1” to format the number n1 (C style formatting - sprintf).

Example with field SQKM that contains areas in square kilometers with decimals. Notice also how you can add the unit into the label.

CLASS
      NAME "world"
      STYLE
        COLOR 58 132 59
        OUTLINECOLOR 30 30 30
      END # STYLE
      TEXT (tostring(([SQKM]),"%.0f")+" km2")
      LABEL
        COLOR  150 200 200
        FONT "sans"
        TYPE truetype
        SIZE 16
        POSITION AUTO
        PARTIALS FALSE
      END #LABEL
    END # CLASS

The result looks like this

enter image description here

It is possible to combine with math and show thousands of square kilometers with

TEXT (tostring(([SQKM]/1000),"%.0f"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.