[GIS] Getting WMS LegendGraphic using GeoServer and GeoWebCache

geoextgeoservergeowebcachegetlegendgraphicopenlayers-2

I am using GeoServer alongwith the default, build in GeoWebCache. I am using Extjs and GeoExt, as well. Everything is running well, except for the legend graphics request. My code for layers is as follows:

var MyLayer = new OpenLayers.Layer.WMS( "MyLayer",
        "http://my-ip-address/geoserver/gwc/service/wms",
        {layers: 'MyLayer',transparent: "true",format: "image/png",
        tileSize: new OpenLayers.Size(256,256),
                tilesOrigin : map.maxExtent.left + ',' + map.maxExtent.bottom },
                { isBaseLayer: false, visibility:false} );

And for legend panel:

var legend = new GeoExt.LegendPanel({
    title: "Map Legend",
    iconCls: 'legend',
                autoScroll: true,
                defaults: {
                    cls: 'legend-item',
        baseParams: {FORMAT: 'image/png'}
                },
                items: []
            });

Whenever I am trying to open this map, I am getting the layer name in my legend panel, but there is no image for layer legend and firebug is giving this error:

"NetworkError: 400 Bad Request –
http://my-ip-address/geoserver/gwc/service/wms?TRANSPARENT=true&TILESIZE=256,256&TILESORIGIN=72.45,24.45&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&EXCEPTIONS=application.ogc.se_xml&LAYER=MyLayer&FORMAT=image/gif&SCALE=2500000.0000000005&FORMAT=image/png"

I understand I will have to replace part of the URL request for GetLegendGraphic. But I am not able to do the same.Could anybody tell me how to accomplish this?

  • I am using GeoServer 2.1.1; GeoWebCache 1.2.6, OpenLayers 2.11, Ext
    3.2.1 and GeoExt 1.1.

Best Answer

The correct url for the legend graphic for each layer and style is embedded in the GetCapabilities response as the LegendURL element. To get it:

  1. fetch the capabilities xml document
  2. parse it (OpenLayers has a parser class)
  3. retrieve the LegendURL for each layer you're interested in

Alternatively you can build a GetLegendGraphic request by following the documentation here.

Related Question