Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.
share|improve this question

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
I need a little bit more explanation. Can you provide me an example! – thelastray Mar 19 '12 at 8:54

Finally I am able to fetch LegendGraphic. from this post.

All I had to do is loop through all my layers, and then set the legendURL.

 var layers; 
 for(var i = 1; i < map.layers.length; i++) 
 { 
    layers; = mapPanel.layers.getAt(i);
    layers.set("legendURL","http://my-ip-address/geoserver/wms?TRANSPARENT=true&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&EXCEPTIONS=application%2Fvnd.ogc.se_xml&FORMAT=image%2Fpng&LAYER=" + map.layers[i].params["LAYERS"]); 
  } 

May be this will help someone else!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.