Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a vector layer by reading a geojson file and I have a SLD file created for this layer. I am wondering how do I link the SLD file to the layer. I had a read of this example on Openlayers website:

I am having a bit of trouble understanding the code for this example- which are using radiobuttons to change the style to the selected button. I believe the linking in my situation will be much simple than what is showing in the example- just linking one SLD file to one single vector layer.

Any guidance will be greatly appreciated.

share|improve this question
are you using GeoServer or just OpenLayers? – iant Jul 10 '13 at 7:41
I am using geoserver, iant – user1829700 Jul 10 '13 at 14:07
up vote 1 down vote accepted

The ideal way to apply a style to a vector layer, is by using a Style Object. You will find more examples on this page: Styling.

But given that you have a SLD, you need to do what is done in the Lined Example. You need to use OpenLayers.Format.SLD to read the SLD, and once it is parsed, you need to apply the style that it contains to your vector Layer.

share|improve this answer
Thanks, The sld file is linked to my vector layer now. Thanks. – user1829700 Jul 11 '13 at 6:40
I'm glad to help you out. – Devdatta Tengshe Jul 11 '13 at 6:58

Well, to apply the style through GeoServer, you would add the style to GeoServer's Style catalogue, then select that style in the layer's configuration. GeoServer would then provide rendered map tiles via WMS, which you could use in OpenLayers.

I'm afraid I'm not as familiar with applying the style inside OpenLayers though.

share|improve this answer
Thanks smithkm, I already did this but it doesnt work for me. i think because it is a vector layer. – user1829700 Jul 10 '13 at 23:20
Yes the WMS method would be rendering your vector layer into raster tiles on the server. If you need the layer to be vector inside OL, then it wouldn't work. If you just mean that the layer is based on a vector dataset, that shouldn't be an issue though although it would require telling OL to use a raster tile layer instead of a vector one. – smithkm Jul 16 '13 at 17:36

Here is the simple version

var aVectorLayer = new OpenLayers.Layer.Vector('someLayer', someOptions);
//apply the stylemap with ajax request
            url: "some_sld_url",
            success: function(req) {
                var format = new OpenLayers.Format.SLD();
                var sld = || req.responseText);
                aVectorLayer.styleMap.styles["default"] = sld.namedLayers["some_name_layer_in_sld_file"].userStyles[0];//use 0 if there's only one user styles

But I'm afraid you don't get what you want. Yes vector layer can be styled with SLD in openlayers, but it applied with limited capabilities. i.e. you cannot apply multiple "FeatureTypeStyle" in a feature.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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