AW: [OpenLayers-Users] Call a SLD file for a Vector Layer

gbrun gbrun at myopera.com
Wed Jul 20 03:00:42 EDT 2011


Thanks for the update Andreas!

So now, it works... almost! Now, there is a problem when I apply the  
selected style: when I add the layer without a style, it works, but when I  
try to apply a style to a layer with a SLD file, it doesn't work. What is  
quite strange is that, when I load a layer, I can see the layer while 1  
second, until the style is effectively applied! Here is the code for my  
"add layer" button, which allows to load a selected layer from my tree. I  
apply the layer style at this moment because it avoids to load all the  
style when I build my tree. The style is loaded only when the layer is  
added.

bbar: ['->',{
	text: "Add to the map",
	handler: function() {
		var node = layerCatalog.getSelectionModel().getSelectedNode();
		if(node) {
			var sld;
			var layername = node.attributes.layer.name;
			var url_sld = "sld/"+layername+".xml";
			var format_sld = new OpenLayers.Format.SLD();
			
			function setLayerStyles() {
				// set the default style for each layer from sld
				for (var l in sld.namedLayers) {
					var styles = sld.namedLayers[l].userStyles, style;
					for (var i=0,ii=styles.length; i<ii; ++i) {
						style = styles[i];
						node.attributes.layer.styleMap.styles["default"] = style;
						break;
					}
				}
			}
			
			function complete(req) {
				sld = format_sld.read(req.responseXML || req.responseText);
				setLayerStyles();
			}

			OpenLayers.loadURL(url_sld, null, null, complete);
			mapPanel.map.addLayer(node.attributes.layer);
		}
	}
}]

Any idea?

Geoffrey



On Tue, 19 Jul 2011 12:54:34 +0200, Andreas Hocevar <ahocevar at opengeo.org>  
wrote:

> Hi,
>
> you may want to have a look at the updated sld example
> (http://openlayers.org/dev/examples/sld.html). Especially at the
> setLayerStyles function in http://openlayers.org/dev/examples/sld.js,
> which shows you how to link the styles to an already existing StyleMap
> of a layer.
>
> Andreas.
>
> On Tue, Jul 19, 2011 at 8:26 AM, gbrun <gbrun at myopera.com> wrote:
>> Thanks for your reply!
>>
>> So I tried to so something with the LoadURL. The SLD file is well  
>> called (I
>> can see it in firebug), but the problem is that I now don't know how to  
>> link
>> it with its layer. I'm still a newbie in webmapping. I think, what I  
>> want to
>> do is quite easy, but I'm still not used to! Here is my current code:
>>
>>                createWFSLayer: function(data, featureType) {
>>
>>                        //Style definition
>>                        var sld;
>>                        var layername = featureType.name;
>>                        var url_sld = "sld/"+layername+".xml";
>>
>>                        var format_sld = new OpenLayers.Format.SLD();
>>
>>                        OpenLayers.loadURL(url_sld, null, null, complete)
>>
>>                        function getDefaultStyle(sld, layerName) {
>>                                var styles =
>> sld.namedLayers[layerName].userStyles;
>>                                var style;
>>                                for(var i=0; i<styles.length; ++i) {
>>                                        style = styles[i];
>>                                        if(style.isDefault) {
>>                                                break;
>>                                        }
>>                                }
>>                                return style;
>>                        };
>>
>>                        function complete(req) {
>>                                sld = format_sld.read(req.responseXML ||
>> req.responseText);
>>
>>                                var styleMap = new OpenLayers.StyleMap({
>>                                                "default":
>> getDefaultStyle(sld, layername)
>>                                });
>>                                return styleMap;
>>                        }
>>
>>                        var styleWFS = {styleMap: styleMap}
>>
>>                        //Layer options
>>                        var layerOptions, protocolOptions;
>>                        //Layer options definition
>>                        protocolOptions = {
>>                                url:
>> data.capability.request.getfeature.href.post,
>>                                featureType: featureType.name,
>>                                featureNS: featureType.featureNS,
>>                                outputFormat: "json",
>>                                readFormat: new  
>> OpenLayers.Format.GeoJSON()
>>                        };
>>                        //Layer protocol definition
>>                        layerOptions = {
>>                                protocol: new
>> OpenLayers.Protocol.WFS(protocolOptions),
>>                                strategies: [new  
>> OpenLayers.Strategy.Fixed()]
>>                        };
>>                        return  new OpenLayers.Layer.Vector(
>>                                featureType.title,
>>                                layerOptions,
>>                                styleWFS
>>                        );
>>                },
>>
>> My problem comes from the "complete" function. I can't get its result  
>> for
>> the styleMap property of my layer!
>>
>> Geoffrey
>>
>>
>> On Mon, 18 Jul 2011 18:17:07 +0200, Arnd Wippermann  
>> <arnd.wippermann at web.de>
>> wrote:
>>
>>> Hi,
>>>
>>> As far as I know you can't load data with OpenLayers.Format.SLD(url).  
>>> You
>>> have to do it with an ajax call, i.e LoadUrl.
>>>
>>> Arnd
>>>
>>> -----Ursprüngliche Nachricht-----
>>> Von: openlayers-users-bounces at lists.osgeo.org
>>> [mailto:openlayers-users-bounces at lists.osgeo.org] Im Auftrag von gbrun
>>> Gesendet: Montag, 18. Juli 2011 12:32
>>> An: openlayers-users at lists.osgeo.org
>>> Betreff: [OpenLayers-Users] Call a SLD file for a Vector Layer
>>>
>>> Hi list!
>>>
>>> I would like to apply SLD styles to my vector layers. It should work  
>>> like
>>> in
>>> this example : http://www.openlayers.org/dev/examples/sld.html
>>>
>>> But what I want to do is a little bit different: each layer has its own
>>> style. So I want that each vector layer calls its default style from a
>>> folder in my web application where styles are stored with the same  
>>> name as
>>> their specific layer. For example, my application has a vector layer  
>>> named
>>> "vector". In this case, this layer must called a SLD file named
>>> "vector.xml".
>>>
>>> The OpenLayers example uses a "OpenLayers.LoadURL()" method. I'm  
>>> looking
>>> for
>>> a different way: I would like to read the SLD response without this
>>> method.
>>> It should look like this:
>>>
>>> var url_sld = "../sld/"+featureType.name+".xml";
>>>                        var format_sld = new
>>> OpenLayers.Format.SLD(url_sld);
>>>
>>>                        processResponse : function (response){
>>>                                var sld =
>>> format_sld.read(response.responseXML ||
>>> response.responseText);
>>>                        };
>>>
>>> But it doesn't work. I think, there is something that I don't  
>>> understand
>>> when I read the SLD response.
>>>
>>> Can someone clarify me this?
>>>
>>> Geoffrey
>>>
>>>
>>> --
>>> Using Opera's revolutionary email client: http://www.opera.com/mail/
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>>>
>>
>>
>> --
>> Using Opera's revolutionary email client: http://www.opera.com/mail/
>> _______________________________________________
>> Users mailing list
>> Users at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>>
>
>
>


-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Users mailing list