First, test that you layer works:
- Go to GeoServer, set up a default style (make it simple
- Go to Layer Preview
If your data and a plain style work, try a more complex style:
- Set up your SLD (like the one you have above) in GeoServer as a style,
- Make this new style the default for that layer
- Go to layer preview.
If this works, then test your apps code:
- You've got it added and you're seeing nothing?
- Go to the debug tools and find the call to your WMS server (should be the URL like above with the parameters inline)
- Copy that URL and put it in a new browser tab on its own
- You should see any errors pop up here.
EDIT: Here's a working example.
Layer Parameters:
var layer = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: WMS_URL,
params: {
LAYERS: LAYER_NAME,
STYLES: undefined,
SLD_BODY: SLD_TEXT,
TILED: true
},
serverType: 'geoserver',
})
});
SLD:
<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0" 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" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd">
<NamedLayer>
<Name>WORKSPACE_CHANGEME:LAYER_NAME_CHANGEME</Name>
<UserStyle>
<Title>This can be anything</Title>
<FeatureTypeStyle>
<Rule>
<Name>0.00 - 0.20</Name>
<ogc:Filter>
<ogc:And>
<ogc:PropertyIsGreaterThanOrEqualTo>
<ogc:PropertyName>mce_score</ogc:PropertyName>
<ogc:Literal>0.00</ogc:Literal>
</ogc:PropertyIsGreaterThanOrEqualTo>
<ogc:PropertyIsLessThanOrEqualTo>
<ogc:PropertyName>mce_score</ogc:PropertyName>
<ogc:Literal>0.20</ogc:Literal>
</ogc:PropertyIsLessThanOrEqualTo>
</ogc:And>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#ffffcc</CssParameter>
<CssParameter name="fill-opacity">0.8</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#ffffcc</CssParameter>
<CssParameter name="stroke-width">0.1</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
<Rule>
<Name>0.20 - 0.40</Name>
<ogc:Filter>
<ogc:And>
<ogc:PropertyIsGreaterThan>
<ogc:PropertyName>mce_score</ogc:PropertyName>
<ogc:Literal>0.20</ogc:Literal>
</ogc:PropertyIsGreaterThan>
<ogc:PropertyIsLessThanOrEqualTo>
<ogc:PropertyName>mce_score</ogc:PropertyName>
<ogc:Literal>0.40</ogc:Literal>
</ogc:PropertyIsLessThanOrEqualTo>
</ogc:And>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#a1dab4</CssParameter>
<CssParameter name="fill-opacity">0.8</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#a1dab4</CssParameter>
<CssParameter name="stroke-width">0.1</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
<Rule>
<Name>0.40 - 0.60</Name>
<ogc:Filter>
<ogc:And>
<ogc:PropertyIsGreaterThan>
<ogc:PropertyName>mce_score</ogc:PropertyName>
<ogc:Literal>0.40</ogc:Literal>
</ogc:PropertyIsGreaterThan>
<ogc:PropertyIsLessThanOrEqualTo>
<ogc:PropertyName>mce_score</ogc:PropertyName>
<ogc:Literal>0.60</ogc:Literal>
</ogc:PropertyIsLessThanOrEqualTo>
</ogc:And>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#41b6c4</CssParameter>
<CssParameter name="fill-opacity">0.8</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#41b6c4</CssParameter>
<CssParameter name="stroke-width">0.1</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
<Rule>
<Name>0.60 - 0.80</Name>
<ogc:Filter>
<ogc:And>
<ogc:PropertyIsGreaterThan>
<ogc:PropertyName>mce_score</ogc:PropertyName>
<ogc:Literal>0.60</ogc:Literal>
</ogc:PropertyIsGreaterThan>
<ogc:PropertyIsLessThanOrEqualTo>
<ogc:PropertyName>mce_score</ogc:PropertyName>
<ogc:Literal>0.80</ogc:Literal>
</ogc:PropertyIsLessThanOrEqualTo>
</ogc:And>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#2c7fb8</CssParameter>
<CssParameter name="fill-opacity">0.8</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#2c7fb8</CssParameter>
<CssParameter name="stroke-width">0.1</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>