3

I am trying to set style to my WMS layer in my script like this :

http://opendem.info/ol3/wms_sld.html

but I got nothing.

This is my code :

var sldBody = '<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/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>opengeo_arbousiers2</Name>
    <UserStyle>
      <Title>A azure polygon style</Title>
      <FeatureTypeStyle>
        <Rule>
          <Title>azure polygon</Title>
          <PolygonSymbolizer>
            <Fill>
              <CssParameter name="fill">#0033cc</CssParameter> 
            </Fill>
          </PolygonSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>';

var map = new ol.Map({
  layers: [
    new ol.layer.Image({
      source: new ol.source.ImageWMS({
        url: 'http://localhost:8080/geoserver/opengeo/wms',
        params: {'LAYERS': 'opengeo:regions1','SLD_BODY' :sldBody},
        serverType: 'geoserver'
      })
    })
  ],
  target: document.getElementById('map'),
  view: new ol.View({
    center: [-1095791.453557, 3422374.879112],
    maxZoom: 19,
    zoom: 5
  })
});
2
  • Does the layer name match what is in the sld.?
    – Ian Turton
    Commented Jul 2, 2017 at 18:34
  • I did copy directly the same sld in geoserver who attached to the layer. it's Ok like that? Commented Jul 2, 2017 at 19:03

2 Answers 2

4

You are attempting to use SLD in library mode which imposes some additional rules on the operation from normal.

So assuming that your SLD is valid and works when used normally, you must ensure that your request contains the name of the layer and the name of the style and that this matches the names in the SLD file you provide or isDefault is set to true.

In your request you don't specify a Style name nor do you set isDefault in the SLD, you also don't have matching layer names (opengeo:regions1 vs opengeo_arbousiers2) so GeoServer will ignore the SLD you have provided.

2
  • I did change the <NamedLayer> to opengeo_regions1 but i still got nothing Commented Jul 3, 2017 at 19:46
  • 1
    it needs to be exactly the same so opengeo:regions1
    – Ian Turton
    Commented Jul 4, 2017 at 7:50
3

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>
2
  • thank you for your answer; the SLD i want to add to my layer is the default SLD in Geoserver i want just to test if 'SLD_Body' work before attach style to a dropdown list. Commented Jul 3, 2017 at 19:27
  • I'm going to edit my answer with a working example from an app I'm working on.
    – Alex Leith
    Commented Jul 3, 2017 at 23:16

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.