4

I have maps from a PostGIS datastore in Geoserver. The map is styled using SLD created dynamically from my web apps. The problem is that sometimes, the map has float-type attribute data.

The label actually renders correctly using <TextSymbolizer>, but with a trailing zeros. e.g 0.850000000000. What I want is that it should render 0.85 instead of 0.850000000000.

Is there any way I can do that using the SLD?

I'm using Geoserver 2.6.1

3

This is a good question. The answer at Using simple Math in SLD file on Geoserver? may help here.

You could try something like this:

<Label>
  <ogc:Div>
    <ogc:Function name="Round">
      <ogc:Mul>
        <ogc:PropertyName>field_name</ogc:PropertyName>
        <ogc:Literal>100</ogc:Literal>
      </ogc:Mul>
    </ogc:Function>
    <ogc:Literal>100</ogc:Literal>    
  </ogc:Div>
</Label>

This multiplies the value of field_name by 100 (in the Mul section), rounds it to an integer (the "Round" function), then divides the result by 100 (the Div section).

You may need to experiment with the parameters but hopefully this approach will work for you.

  • I'll leave this here, but you should probably use Ian's suggestion instead as it's a lot simpler – Stephen Lead Jul 24 '17 at 23:07
  • 1
    Thank you, this solves my problem. I omit the 'Round' function btw. Just by multiply it by 100 and divide it by 100 solves my problem. Ian's suggestions also works great only if I have a definite number of digits, while in this situation I just want to remove only the trailing zeros. – ariespranata Aug 31 '17 at 2:03
4

You can use the numberFormat function. This allows you to specify how many digits you want after the decimal point.

Here is an example from MASS-GIS:

    <TextSymbolizer>
        <Label>
          <Function name="numberFormat">
            <Literal>#</Literal>
            <PropertyName xmlns="http://www.opengis.net/ogc">CONTOUR_FT</PropertyName>
          </Function>
        </Label>
  • Thanks for answering. The problem I found when using your solution is just that I have no definite number of digits. It can be #.## , #.### , and so on because I just need to remove just the trailing 0. So I prefer Stephen's suggestion. – ariespranata Aug 31 '17 at 2:01
0

You could use strReplace from the string functions which may be better than a divide and multiply. So something like

   <Label>
      <Function name="strReplace">
        <PropertyName xmlns="http://www.opengis.net/ogc">CONTOUR_FT</PropertyName>
        <Literal>0+$</Literal>
        <Literal></Literal>
        <Literal>false</Literal>
      </Function>
    </Label>

This is just a wrapper around the Java regexp methods so it is pretty powerful if you need.

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.