GeoServer – Creating Gradient Legends for Polygons with Gradient Fill

geoserver

I have a GeoPackage layer of polygons that is colored by a numeric attribute xy with this SLD:

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0" 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"
  xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
  <NamedLayer>
    <Name>Style McStyleface</Name>
    <UserStyle>
      <Name>Test</Name>
      <Title>Test style</Title>
      <FeatureTypeStyle>
        <Rule>
          <PolygonSymbolizer>
             <Fill>
               <CssParameter name="fill">
                 <ogc:Function name="Interpolate">
                   <!-- Property to transform -->
                   <ogc:PropertyName>xy</ogc:PropertyName>

                   <!-- Mapping curve definition pairs (input, output) -->
                   <ogc:Literal>0</ogc:Literal>
                   <ogc:Literal>#ff0000</ogc:Literal>

                   <ogc:Literal>100</ogc:Literal>
                   <ogc:Literal>#000000</ogc:Literal>

                   <!-- Interpolation method - defaults to linear -->
                   <ogc:Literal>color</ogc:Literal>

                 </ogc:Function>
               </CssParameter>
               <CssParameter name="fill-opacity">0.7</CssParameter>
             </Fill>
          </PolygonSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

So far, so good–the map is rendered accordingly. Now, I was looking for a legend that represents the gradient nature of the data, but could only find so for raster data. In the examples that are shipped with the software all gradient legends are in combination with raster data, while vector data are mapped to discrete factors. So, my question: how is it possible to generate a gradient legend for vector data with GeoServer?

In figures, I am aiming to achieve this… …rather than this:
gradient discrete

Would be great if anyone could give me a hint 🙂

Best Answer

I don't think there is (currently) any way to do this as the legend generator doesn't have access to all the features to get the required range of values in an interpolate function. Looking at the code:

final boolean buildRasterLegend =
                (!strict && layer == null && LegendUtils.checkRasterSymbolizer(gt2Style))
                        || (LegendUtils.checkGridLayer(layer) && !hasVectorTransformation)
                        || hasRasterTransformation;

Looks like it explicitly excludes all vector transformations from the legend.

I suspect that adding code to do that would be difficult, but if you want to take a look then BufferedImageLegendGraphicBuilder would be the place to start.

Related Question