Earlier versions of OpenLayers allowed for the submission of the text of an SLD in a call to the server side WMS.

This describes the procedure, and cautions that, if your SLD is too long, you might have to use POST rather than GET.

Documentation on how to do this with OpenLayers 3 seems to be hard to find. Is it still possible to do this?

I did find that you can specify an SLD defined & stored within GeoServer by way of the STYLES property of the params argument to, for example, the ol.source.TileWMS.

I need to dynamically create the text of an SLD on the client side (within JavaScript) and submit this in my calls to get tiled maps from GeoServer.

Prior to OL 3, you could submit your SLD text with the SLD_BODY: parameter, but this does not seem to work, at least in the ol.source.TileWMS constructor. I guess the crux of my problem is that the params object is not very well documented.

  • 1
    You can also specify an SLD on an external (public) web server, there's no need for the referenced style to be sitting on the GeoServer instance you are requesting the map from. – nmtoken Mar 24 '17 at 8:08
  • @nmtoken how do you do that? – user1919 Apr 25 '18 at 11:00
  • @user1919 take a look at gis.stackexchange.com/questions/148256/… – nmtoken Apr 25 '18 at 15:24

The documentation for params is correct when it says: "At least a LAYERS param is required. STYLES is '' by default. VERSION is 1.3.0 by default. WIDTH, HEIGHT, BBOX and CRS (SRS for WMS version < 1.3.0) will be set dynamically.". So when you want to set SLD_BODY, you also have to unset STYLES:

source.updateParams({STYLES: undefined, SLD_BODY: 'your_sld_body'});

This will work fine as long as your URL does not get too long or your WMS server and browser do not choke on long urls. If they do, you will have to use a custom tileLoadFunction on the source to request the image with XHR and a POST request. You will then have to set the img src to a data URI that you calculate from the WMS response.

  • Thanks for this reply! Having to unset STYLES was the missing piece of the puzzle, I think. – JimP Dec 3 '14 at 18:29
  • Unfortunately, the documentation is incorrect, a STYLES parameter is optional for WMS+SLD GetMap requests. a STYLES parameter is only required for WMS GetMap requests. – nmtoken Mar 24 '17 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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