[GIS] Applying SLD to WMS layer with OpenLayers

openlayers-2sldwms

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(
    'nlcd',
    '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"
    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>nlcd</Name>
    <UserStyle>
      <Title>ncld test</Title>
      <FeatureTypeStyle>
        <Rule>
          <RasterSymbolizer>
            <ColorMap>
              <ColorMapEntry opacity="0" color="#000000" quantity="11" />
            </ColorMap>
          </RasterSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

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:   
OpenLayers.Request.GET({
    url: "sldcode.sld",
    success: complete
});  

//function that attempts to apply SLD
function complete(req) {
    mySLD = format.read(req.responseXML || req.responseText);
    map.layers[3].mergeNewParams({sld_body:mySLD});
}   

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

Best Answer

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.

Related Question