GeoServer WMS Service – Creating a Legend

geoserverlegendwms

I am working with Geoserver for some weeks now and were able to deploy some services already. For my thematic map (e.g. temperature) I created a custom style displaying all tiles in specific colors (blue, yellow, red). Therefore I want to create a legend, but I really dont know how to do this. This legend should show 3 polygons in 3 different colors and some text right of it. Something like "[red polygon] > 30".

custom style sld:

<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
    xsi:schemaLocation="http://www.opengis.net/sld 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>Temperature</Name>
    <UserStyle>
      <Title>My Temperature Style</Title>
      <FeatureTypeStyle>
        <Rule>
            <PolygonSymbolizer>
               <Fill>
                 <CssParameter name="fill">
                   <ogc:Function name="Interpolate">
                     <ogc:PropertyName>dTA</ogc:PropertyName>

                     <ogc:Literal>0</ogc:Literal>
                     <ogc:Literal>#0000ff</ogc:Literal>

                     <ogc:Literal>15</ogc:Literal>
                     <ogc:Literal>#ffff00</ogc:Literal>

                     <ogc:Literal>30</ogc:Literal>
                     <ogc:Literal>#ff0000</ogc:Literal>

                     <ogc:Literal>color</ogc:Literal>
                   </ogc:Function>
                 </CssParameter>
               </Fill>
            </PolygonSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
   </NamedLayer>
</StyledLayerDescriptor>

any idea?

Best Answer

You need to call the getLegendGraphic endpoint of your WMS service. So if your temperature layer is called topp:temperature you would add a request like:

http://localhost:8080/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&LAYER=topp:temperature

The documentation gives you details on all the possible options you can add to control fonts and sizes etc.

Related Question