Problem when using external SLD in WebMapServiceProvider

Hi !

I’m having trouble when trying to specify a custom SLD style to Cesium WebmapService provider.

For a bit of context, here is the code for the provider I’m currently using :

const provider = new Cesium.WebMapServiceImageryProvider({
    url: wms_ncdf,
    layers: wms_variable,
    parameters: {
        styles: wms_style + "/" + wms_palette,
        format: 'image/png',
        transparent: 'true',
        colorscalerange: wms_min+","+wms_max,
        numcolorbands: wms_numcolorbands,
        opacity: wms_opacity,
        sld_body: sld_custom
    }
});

The “sld_body: sld_custom” line is a new addition. Without that, the provider work and return the requested tiles after sending this type of requests to our Thredds server where are stored our NCDF files :

https://[URL]/[FILE].nc?styles=default-scalar%2Fdefault&format=image%2Fpng&transparent=true&colorscalerange=6.656%2C43.22&numcolorbands=250&opacity=100&service=WMS&version=1.1.1&request=GetMap&layers=SSS&bbox=-90%2C-90%2C0%2C0&width=256&height=256&srs=EPSG%3A4326

When adding the “sld_body” line to the provider, the requests sent to the Thredds server now look like :

https://[URL]/[FILE].nc?styles=default-scalar%2Fdefault&format=image%2Fpng&transparent=true&colorscalerange=6.656%2C43.22&numcolorbands=250&opacity=100&sld_body=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22ISO-8859-1%22%3F%3E%3CStyledLayerDescriptor%20version%3D%221.0.0%22%20xsi%3AschemaLocation%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%20StyledLayerDescriptor.xsd%22%20xmlns%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xmlns%3Aogc%3D%22http%3A%2F%2Fwww.opengis.net%2Fogc%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%3E%3CNamedLayer%3E%3CName%3Econtours%3C%2FName%3E%3CUserStyle%3E%3CTitle%3Econtours%3C%2FTitle%3E%3CFeatureTypeStyle%3E%3CRule%3E%3CLineSymbolizer%3E%3CStroke%3E%3CCssParameter%20name%3D%22stroke%22%3E%23000000%3C%2FCssParameter%3E%3CCssParameter%20name%3D%22stroke-width%22%3E3%3C%2FCssParameter%3E%3C%2FStroke%3E%3C%2FLineSymbolizer%3E%3C%2FRule%3E%3C%2FFeatureTypeStyle%3E%3C%2FUserStyle%3E%3C%2FNamedLayer%3E%3C%2FStyledLayerDescriptor%3E&service=WMS&version=1.1.1&request=GetMap&layers=SSS&bbox=-22.5%2C-67.5%2C0%2C-45&width=256&height=256&srs=EPSG%3A4326

The SLD used is a simple one, only for test purpose. I tested it on GeoServer and the SLD worked. For better readability, here is the “sld_custom” variable used in the provider (the line breaks are removed in my code to avoid encoding problems) :

<?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>Simple Line</Name>
    <UserStyle>
      <Title>SLD Cook Book: Simple Line</Title>
      <FeatureTypeStyle>
        <Rule>
          <LineSymbolizer>
            <Stroke>
              <CssParameter name="stroke">#000000</CssParameter>
              <CssParameter name="stroke-width">3</CssParameter>    
            </Stroke>
          </LineSymbolizer>
       	</Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

But the request don’t work, returning this error :


<ServiceExceptionReport version="1.1.1" xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wms/1.1.1/exceptions_1_1_1.xsd">
  <ServiceException>
    Problem with SLD document Cause: A named layer must be named.
  </ServiceException>
</ServiceExceptionReport>

I don’t understand where this error come from (The “NamedLayer” tag in my SLD already have a “Name” tag and work in the GeoServer preview) anddI can’t find any occurrence of it in forums. Our Thredds server is in 5.4 version (since it seems that the external SLD functionality was added in 5.0 : Update TDS WMS configuration to support SLD parameter by ethanrd · Pull Request #73 · Unidata/tds · GitHub).

Hi there,

I tested it on GeoServer and the SLD worked.

Do you mind providing more info on how you tested this? Did you pass sld_body as a query parameter as you are doing in the example above?

My first guess here would be a url encoding or decoding issue.

Thanks,
Gabby

Hi Gabby !

When I tested it, it was on my local Geoserver (I just downloaded it to run some tests so it’s the default configuration). I tried to create a new Style named “contours” with this SLD, and I checked the “Layer Preview” tab to check if my SLD was correct. It seems to work for this simple SLD.

Geoserver Preview

After that, I tried to insert this SLD in the “SLD_BODY” parameter of the GetMap url, but even after formatting the SLD (removing brackets, slashes…), I couldn’t make it work.

I tried searching for a working example and found this post : GeoServer WMS inline SLD_BODY syntax problem - Geographic Information Systems Stack Exchange

Error with contours' SLD_BODY
http://127.0.0.1:8080/geoserver/topp/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&LAYERS=topp%3Astates&exceptions=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&WIDTH=768&HEIGHT=370&BBOX=-270%2C-133.59375%2C270%2C126.5625&SLD_BODY=%3CStyledLayerDescriptor%20version%3D%221.0.0%22%20xmlns%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xmlns%3Aogc%3D%22http%3A%2F%2Fwww.opengis.net%2Fogc%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%20xmlns%3Agml%3D%22http%3A%2F%2Fwww.opengis.net%2Fgml%22%20xsi%3AschemaLocation%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%20http%3A%2F%2Fschemas.opengis.net%2Fsld%2F1.0.0%2FStyledLayerDescriptor.xsd%22%3E%3CNamedLayer%3E%3CName%3Etopp%3Astates%3C%2FName%3E%3CUserStyle%3E%3CFeatureTypeStyle%3E%3CRule%3E%3CLineSymbolizer%3E%3CStroke%3E%3CCssParameter+name=%22stroke%22%3E#000000%3C%2FCssParameter%3E%3C%2FStroke%3E%3C%2FLineSymbolizer%3E%3C%2FRule%3E%3C%2FFeatureTypeStyle%3E%3C%2FUserStyle%3E%3C%2FNamedLayer%3E%3C%2FStyledLayerDescriptor%3E

I tried to adapt it to apply it on Geoserver’s “topp:states” layer and it kinda worked. I got a result but it seems to be the default display of this layer. But at least, this wasn’t an error

Result with new SLD_BODY
http://127.0.0.1:8080/geoserver/topp/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&LAYERS=topp%3Astates&exceptions=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&WIDTH=768&HEIGHT=370&BBOX=-150%2C20%2C-50%2C55&SLD_BODY=%3CStyledLayerDescriptor%20version%3D%221.0.0%22%20xmlns%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xmlns%3Aogc%3D%22http%3A%2F%2Fwww.opengis.net%2Fogc%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%20xmlns%3Agml%3D%22http%3A%2F%2Fwww.opengis.net%2Fgml%22%20xsi%3AschemaLocation%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%20http%3A%2F%2Fschemas.opengis.net%2Fsld%2F1.0.0%2FStyledLayerDescriptor.xsd%22%3E%3CNamedLayer%3E%3CName%3Etopp%3Astates%3C%2FName%3E%3CUserStyle%3E%3CFeatureTypeStyle%3E%3CRule%3E%3CPolygonSymbolizer%3E%3CFill%3E%3CCssParameter+name%3D%22workers%22%3E%23FF0000%3C%2FCssParameter%3E%3C%2FFill%3E%3C%2FPolygonSymbolizer%3E%3C%2FRule%3E%3C%2FFeatureTypeStyle%3E%3C%2FUserStyle%3E%3C%2FNamedLayer%3E%3C%2FStyledLayerDescriptor%3E

But when I tried to include this SLD_BODY in my Thredds request (after changing “NamedLayer > Name” by “SSS” since it’s my layer’s name), I got the same error again.

Error after requesting my Thredds' layer with SLD_BODY
[URL]/[NCDF_FILE].nc?service=WMS&version=1.1.1&request=GetMap&layers=SSS&bbox=-124.73142200000001%2C24.955967%2C-66.969849%2C49.371735&width=768&height=330&srs=EPSG%3A4326&styles=&format=image%2Fpng&SLD_BODY=%3CStyledLayerDescriptor%20version%3D%221.0.0%22%20xmlns%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xmlns%3Aogc%3D%22http%3A%2F%2Fwww.opengis.net%2Fogc%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%20xmlns%3Agml%3D%22http%3A%2F%2Fwww.opengis.net%2Fgml%22%20xsi%3AschemaLocation%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%20http%3A%2F%2Fschemas.opengis.net%2Fsld%2F1.0.0%2FStyledLayerDescriptor.xsd%22%3E%3CNamedLayer%3E%3CName%3ESSS%3C%2FName%3E%3CUserStyle%3E%3CFeatureTypeStyle%3E%3CRule%3E%3CPolygonSymbolizer%3E%3CFill%3E%3CCssParameter+name%3D%22fill%22%3E%23FF0000%3C%2FCssParameter%3E%3C%2FFill%3E%3C%2FPolygonSymbolizer%3E%3C%2FRule%3E%3C%2FFeatureTypeStyle%3E%3C%2FUserStyle%3E%3C%2FNamedLayer%3E%3C%2FStyledLayerDescriptor%3E

And once again, the base URL was generated by Cesium and return a correct layer if I remove the SLD_BODY parameter, so I don’t thing I’m doing a mistake while naming my NamedLayer.

Result after requesting my Thredds' layer without SLD_BODY
[URL]/[NCDF_FILE].nc?service=WMS&version=1.1.1&request=GetMap&layers=SSS&bbox=-124.73142200000001%2C24.955967%2C-66.969849%2C49.371735&width=768&height=330&srs=EPSG%3A4326&styles=&format=image%2Fpng

Now, I don’t really have any clues left to solve this problem so I hope someone will be able to help.