I am attempting to adjust the symbology of a loaded WMS raster layer (img file through MapServer). It is the NLCD dataset. The WMS layer loads find with

nlcd = new OpenLayers.Layer.WMS(
    'http://server /fswms/html/rlayers',
        layers: 'nlcd-2006', transparent: true,
        projection: new OpenLayers.Projection("EPSG:4326")
        isBaseLayer: false

And I have a SLD file that I have created in which I am attempting to make all of the pixels with the value of 11 opacity of 0.

<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
    xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
      <Title>ncld test</Title>
              <ColorMapEntry opacity="0" color="#000000" quantity="11" />

The WMS layer ends up being map.layers[3] in my test application so I attempting to apply the SLD by the following code but it is not working.

//this is in the init:   
    url: "sldcode.sld",
    success: complete

//function that attempts to apply SLD
function complete(req) {
    mySLD = format.read(req.responseXML || req.responseText);

Here a link to the dev site: http://rain.nemac.org/~derek/ol/sldTest.html Any help would be greatly appreciated.

1 Answer 1


I spent an hour picking at this thing before I found this. You need to have the Style file before you submit the WMS Request, as the rendering is applied on the server side.

It looks like you borrowed a lot from this example, correct? You're loading up your SLD code with AJAX, which is probably unnecessary--The SLD file is small and straightforward. If you don't need to specify the contents of the file programatically, then you can cut the AJAX exchange out of the picture and just pass the XML in as a string--it looks like you're already trying to parse it in that fashion anyway.

So, load the SLD file and then pass the request for the WMS layer. That should fix the problem.

  • Thanks Tony and yes I was looking at the example you mentioned. One more question though if I load the SLD ahead of time how to I pass it into the request for the WMS layer? Is it as a text string, or a "OpenLayers.Format.SLD" object?
    – geo derek
    Commented Oct 20, 2012 at 13:28
  • I'd try something like this: //Create the OL.Format.SLD var format = new OpenLayers.Format.SLD(); //Read the SLD Data var mySLD = format.read('yourSLDstring'); var myWMSLayer = new OpenLayers.Layer.WMS("yourparams", ..., {SLD : mySLD}); Commented Oct 20, 2012 at 15:55
  • I really appreciate your feedback. I have tried exactly has you suggested. I don't get an error, but my style does not see to be applied.
    – geo derek
    Commented Oct 20, 2012 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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