SLD Legend Preview – Failed to Build SLD Legend Preview

datelabelingsld

I write an SLD style on GeoServer. The date in label is showing with the letter 'Z' at the end (2021-07-28Z). I want to display the date parameter in the label but without the 'Z' at the end.

The 'date' parameter in the table in the database has the date format, the standard interpretation of the timestamp on the GeoServer is the format yyyy-MM-ddThh: mm: ss.SSSZ. So I find that solution is to use the dateformat style function.

Here is my code:

<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
                   xsi:schemaLocation="http://www.opengis.net/sld 
http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd"
                   xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
                   xmlns:xlink="http://www.w3.org/1999/xlink" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<NamedLayer>
<Name></Name>
<UserStyle>
  <Title></Title>
  <FeatureTypeStyle>

    <Rule>
      <Name>test</Name>
      <Title>test</Title>
      <Abstract>test</Abstract>
      <PolygonSymbolizer>
        <Fill>
          <CssParameter name="fill">#ffffff</CssParameter>
          <CssParameter name="fill-opacity">
            <ogc:Literal>0.01</ogc:Literal>
          </CssParameter>
        </Fill>
      </PolygonSymbolizer>
    </Rule>

    <Rule>
      <TextSymbolizer>
        <Label>
          <ogc:Function name="dateFormat">
            <ogc:Literal>"yyyy-MM-dd"</ogc:Literal>
            <ogc:PropertyName>date</ogc:PropertyName>
          </ogc:Function>
        </Label>
        <Font>
          <CssParameter name="font-family">Dialog.bold</CssParameter>
          <CssParameter name="font-weight">Bold</CssParameter>
          <CssParameter name="font-size">10</CssParameter>
        </Font>
        <LabelPlacement>
          <PointPlacement>
            <AnchorPoint>
              <AnchorPointX>0.5</AnchorPointX>
              <AnchorPointY>0.5</AnchorPointY>
            </AnchorPoint>
            <Displacement>
              <DisplacementX>0</DisplacementX>
              <DisplacementY>65</DisplacementY>
            </Displacement>
          </PointPlacement>

        </LabelPlacement>

        <Halo>
          <Radius>
            <ogc:Literal>1.2</ogc:Literal>
          </Radius>
          <Fill>
            <CssParameter name="fill">#666666</CssParameter>
          </Fill>
        </Halo>
        <Fill>
          <CssParameter name="fill">#ffffff</CssParameter>
        </Fill>

      </TextSymbolizer>
    </Rule>

  </FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>

When I check validation of SLD I got – No validation errors.
But when I try to check 'Preview Legend' I got – Failed to build legend preview. Check to see if the style is valid.

Can someone help me what I am doing wrong?

This is from log file:

WARN [web.data] – Failed to build legend preview
java.lang.NullPointerException
at java.util.Calendar.setTime(Calendar.java:1770)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:943)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936)
at java.text.DateFormat.format(DateFormat.java:345)
at org.geotools.filter.function.FilterFunction_dateFormat.evaluate(FilterFunction_dateFormat.java:72)
at org.geotools.filter.expression.ExpressionAbstract.evaluate(ExpressionAbstract.java:65)

Best Answer

If someone else has the same problem, here is the solution:

The problem was in double quotes when setting the date format:

<Label>
    <ogc:Function name="dateFormat">
         <ogc:Literal>yyyy-MM-dd</ogc:Literal>
         <ogc:PropertyName>datum</ogc:PropertyName>
    </ogc:Function>
</Label>
Related Question