Developer’s Corner: GeoServer GetLegendGraphic Enhancements

Dear All,
recently some fixes and enhancements were made  by GeoSolutions to GeoServer, in particular to the WMS GetLegendGraphic  request.

Layer groups support

The first improvement was done to produce a legend for layer groups, a very much needed functionality, to simplify and support wide usage of layer groups in GeoServer.

The support for layer groups is quite flexible. You can:

  • specify a layergroup name in the LAYER parameter to have a legend with default options
  • specify a custom style for some or all of the layer group sublayers with the STYLE parameter: just use a list of style names (one for each layer, separated by commas), leaving empty those that you don’t want to override
  • specify a custom rule  for some or all of the layer group sublayers with the RULE parameter, using it in the same way STYLE is used
The layer title (or name if title is not defined) is printed on top of each layer of the group, unless you specify forceTitles=off in the LEGEND_OPTIONS parameter.
Some examples, using default GeoServer layers:
Default styles
http://localhost:8080/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&
LAYER=spearfish


Overridden styles
http://localhost:8080/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&
LAYER=spearfish&STYLE=,line,tiger_roads,giant_polygon,burg,poi


Mixed geometry support

Until now, asking a legend for a layer with a generic GEOMETRY type, tipically used for mixed type (puncutal, linear and polygonal) geometries, would produce an exception in GeoServer, because it was not clear what kind of sample icon should be produced.
From now on, the sample icon is produced looking at the requested SLD style, choosing the best matching geometry type based on the symbolizers:
  • A polygon is chosen if at least a PolygonSymbolizer is present
  • A polyline is chosen if no PolygonSymbolizer is present, and at least a LineSymbolizer is present
  • A point is chosen in any other case

Dynamic symbol size handling

Until now asking for a legend for a punctual layer, where symbols or images with very big or very small size are used, compared to the icon size, the legend will be unreadable, as the symbol would be rendered with the requested size.
From now on, the symbol sizes are rescaled to always fall inside the icon requested box, and to respect a minimum size (defaulted to 3 pixels). The default minimum size can be overridden with the minSymbolSize: LEGEND_OPTIONS parameter.
More, for a style with multiple rules, the sizes of each rule are rendered proportional to tthe respective style sizes.
Look at the following image for a before – after effect example:
It is eventually worth to point out that all this fixes and enhancements are already available in the latest GeoServer nightlies, for the 2.2.x, 2.3.x, and 2.4.x series.
Regards,
the GeoSolutions team
 
  • Mark

    I don’t see any difference between the listed url for legend1 and legend2, though there is an obvious diffrence between the images

  • Mark

    ahh wait it’s a layout problem on the blog…

  • Ouch, let’s see if I can fix that.

  • This is awesome, thanks guys! I’ve actually been thinking about this a lot recently, it’d be useful for some of the stuff I’m working on.

  • Anonymous

    Could you also add to GetCapabilities to layergroup? Thanks in advance!

  • Anonymous

    Could you also add LegendURL to GetCapabilities to layergroup? Thanks in advance!

  • Ciao Anonymous,
    I would open a JIRA enhancement request for these items.

    Although, unless you have funding backing your requests that you are willing to use, I would not hold my breath for having them implemented :)

    Notice that we do equally appreciate code donations 😛