Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How to add SLD style which is written in String format and stored in a 'var' variable of javascript and asign it to a WMS layer while creating it.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

All you need to do is replace the styles parameter with an SLD parameter that contains your style. The following code from this example demonstrates the concept.

            theSLD_BODY= '<?xml version="1.0" encoding="utf-8"?>' + '<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:gml="http://www.opengis.net/gml" 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 http://schemas.opengeospatial.net/sld/1.0.0/StyledLayerDescriptor.xsd"><NamedLayer><Name>COUNTRY</Name><UserStyle><FeatureTypeStyle><Rule><ogc:Filter><ogc:PropertyIsLike wildCard="*" singleChar="#" escape="!"><ogc:PropertyName>Continent</ogc:PropertyName><ogc:Literal>Africa</ogc:Literal></ogc:PropertyIsLike></ogc:Filter><PolygonSymbolizer><Fill><CssParameter name="fill">#5599DD</CssParameter></Fill><Stroke><CssParameter name="stroke">#000000</CssParameter></Stroke></PolygonSymbolizer></Rule></FeatureTypeStyle></UserStyle></NamedLayer></StyledLayerDescriptor>';
        layer2 = new OpenLayers.Layer.WMS( "Africa (SLD_BODY)",
                "http://gis.ibbeck.de/include/mapserver/mapserv.exe?map=/daten/mapfiles/world/World.map",
                {   "layers": "COUNTRY",
                    "format": "image/png",
                    "version": "1.1.1",
                    "transparent": "TRUE",
                    "SLD_BODY": theSLD_BODY
                });

You may encounter problems if the SLD becomes too long as it may exceed the maximum URL length of your browser. In which case you will need to switch to POST instead of GET requests.

share|improve this answer
    
thanks iant, it worked. –  DileepKumar Jan 8 '13 at 20:33

Your Answer

 
discard

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.