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

Andreas Hocevar ahocevar at opengeo.org
Tue Jul 19 06:54:34 EDT 2011


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
>



-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.


More information about the Users mailing list