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.

I am having problem parsing

http://localhost:8080/geoserver/wms?request=GetStyles&layers=topp%3Astates&service=wms&version=1.1.1

getStyle response from geoserver of layer topp:states. I want to get the SLD information of a layer with openlayers. I get blank response for the following request. The response parsed SLD file has no value. For now I am doing :

var json = new OpenLayers.Format.JSON();
    OpenLayers.Request.GET({
            url: "http://localhost:8080/geoserver/wms",
             params: {request: "GetStyles",layers:"topp:states",service:"wms",version:"1.1.1"},
            success: complete
        });

        function complete(req) {
        //console.log(req);
            sld = format.read(req.responseXML || req.responseText);
            //console.log("The sld");
            //console.log(sld.namedLayers);
            //styles = sld.namedLayers.interpreted.userStyles[0];
            //console.log(styles);
           // building_vec.styleMap.styles.default = styles;
           console.log(json.write(sld, true));

    }

The response:

{
    "namedLayers": {
    },
    "version": "1.0.0"
}

But when I hit direct requested url I get right sld.

http://localhost:8080/geoserver/wms?request=GetStyles&layers=topp%3Astates&service=wms&version=1.1.1

The SLD with url directly on address bar:

<?xml version="1.0" encoding="UTF-8"?>
<sld:StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:sld="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml" version="1.0.0">
    <sld:NamedLayer>
        <sld:Name>topp:states</sld:Name>
        <sld:UserStyle>
            <sld:Name>population</sld:Name>
            <sld:Title>Population in the United States</sld:Title>
            <sld:IsDefault>1</sld:IsDefault>
            <sld:Abstract>A sample filter that filters the United States into three
        categories of population, drawn in different colors</sld:Abstract>
            <sld:FeatureTypeStyle>
                <sld:Name>name</sld:Name>
                <sld:Rule>
                    <sld:Title>&lt; 2M</sld:Title>
                    <ogc:Filter>
                        <ogc:PropertyIsLessThan>
                            <ogc:PropertyName>PERSONS</ogc:PropertyName>
                            <ogc:Literal>2000000</ogc:Literal>
                        </ogc:PropertyIsLessThan>
                    </ogc:Filter>
                    <sld:PolygonSymbolizer>
                        <sld:Fill>
                            <sld:CssParameter name="fill">#4DFF4D</sld:CssParameter>
                            <sld:CssParameter name="fill-opacity">0.7</sld:CssParameter>
                        </sld:Fill>
                    </sld:PolygonSymbolizer>
                </sld:Rule>
                <sld:Rule>
                    <sld:Title>2M - 4M</sld:Title>
                    <ogc:Filter>
                        <ogc:PropertyIsBetween>
                            <ogc:PropertyName>PERSONS</ogc:PropertyName>
                            <ogc:LowerBoundary>
                                <ogc:Literal>2000000</ogc:Literal>
                            </ogc:LowerBoundary>
                            <ogc:UpperBoundary>
                                <ogc:Literal>4000000</ogc:Literal>
                            </ogc:UpperBoundary>
                        </ogc:PropertyIsBetween>
                    </ogc:Filter>
                    <sld:PolygonSymbolizer>
                        <sld:Fill>
                            <sld:CssParameter name="fill">#FF4D4D</sld:CssParameter>
                            <sld:CssParameter name="fill-opacity">0.7</sld:CssParameter>
                        </sld:Fill>
                    </sld:PolygonSymbolizer>
                </sld:Rule>
                <sld:Rule>
                    <sld:Title>&gt; 4M</sld:Title>
                    <ogc:Filter>
                        <ogc:PropertyIsGreaterThan>
                            <ogc:PropertyName>PERSONS</ogc:PropertyName>
                            <ogc:Literal>4000000</ogc:Literal>
                        </ogc:PropertyIsGreaterThan>
                    </ogc:Filter>
                    <sld:PolygonSymbolizer>
                        <sld:Fill>
                            <sld:CssParameter name="fill">#4D4DFF</sld:CssParameter>
                            <sld:CssParameter name="fill-opacity">0.7</sld:CssParameter>
                        </sld:Fill>
                    </sld:PolygonSymbolizer>
                </sld:Rule>
                <sld:Rule>
                    <sld:Title>Boundary</sld:Title>
                    <sld:LineSymbolizer>
                        <sld:Stroke>
                            <sld:CssParameter name="stroke-width">0.2</sld:CssParameter>
                        </sld:Stroke>
                    </sld:LineSymbolizer>
                    <sld:TextSymbolizer>
                        <sld:Label>
                            <ogc:PropertyName>STATE_ABBR</ogc:PropertyName>
                        </sld:Label>
                        <sld:Font>
                            <sld:CssParameter name="font-family">Times New Roman</sld:CssParameter>
                            <sld:CssParameter name="font-size">14</sld:CssParameter>
                            <sld:CssParameter name="font-style">Normal</sld:CssParameter>
                            <sld:CssParameter name="font-weight">normal</sld:CssParameter>
                        </sld:Font>
                        <sld:LabelPlacement>
                            <sld:PointPlacement>
                                <sld:AnchorPoint>
                                    <sld:AnchorPointX>
                                        <ogc:Literal>0.5</ogc:Literal>
                                    </sld:AnchorPointX>
                                    <sld:AnchorPointY>
                                        <ogc:Literal>0.5</ogc:Literal>
                                    </sld:AnchorPointY>
                                </sld:AnchorPoint>
                                <sld:Rotation>
                                    <ogc:Literal>0.0</ogc:Literal>
                                </sld:Rotation>
                            </sld:PointPlacement>
                        </sld:LabelPlacement>
                    </sld:TextSymbolizer>
                </sld:Rule>
            </sld:FeatureTypeStyle>
        </sld:UserStyle>
        <sld:UserStyle>
            <sld:Name>pophatch</sld:Name>
            <sld:Title>Population in the United States</sld:Title>
            <sld:Abstract>A sample filter that filters the United States into three
        categories of population, drawn in different colors</sld:Abstract>
            <sld:FeatureTypeStyle>
                <sld:Name>name</sld:Name>
                <sld:Rule>
                    <sld:Title>&lt; 2M</sld:Title>
                    <ogc:Filter>
                        <ogc:PropertyIsLessThan>
                            <ogc:PropertyName>PERSONS</ogc:PropertyName>
                            <ogc:Literal>2000000</ogc:Literal>
                        </ogc:PropertyIsLessThan>
                    </ogc:Filter>
                    <sld:PolygonSymbolizer>
                        <sld:Fill>
                            <sld:GraphicFill>
                                <sld:Graphic>
                                    <sld:Mark>
                                        <sld:WellKnownName>shape://slash</sld:WellKnownName>
                                        <sld:Stroke>
                                            <sld:CssParameter name="stroke">0xAAAAAA</sld:CssParameter>
                                        </sld:Stroke>
                                    </sld:Mark>
                                    <sld:Size>
                                        <ogc:Literal>16</ogc:Literal>
                                    </sld:Size>
                                </sld:Graphic>
                            </sld:GraphicFill>
                        </sld:Fill>
                    </sld:PolygonSymbolizer>
                </sld:Rule>
                <sld:Rule>
                    <sld:Title>2M - 4M</sld:Title>
                    <ogc:Filter>
                        <ogc:PropertyIsBetween>
                            <ogc:PropertyName>PERSONS</ogc:PropertyName>
                            <ogc:LowerBoundary>
                                <ogc:Literal>2000000</ogc:Literal>
                            </ogc:LowerBoundary>
                            <ogc:UpperBoundary>
                                <ogc:Literal>4000000</ogc:Literal>
                            </ogc:UpperBoundary>
                        </ogc:PropertyIsBetween>
                    </ogc:Filter>
                    <sld:PolygonSymbolizer>
                        <sld:Fill>
                            <sld:GraphicFill>
                                <sld:Graphic>
                                    <sld:Mark>
                                        <sld:WellKnownName>shape://slash</sld:WellKnownName>
                                        <sld:Stroke>
                                            <sld:CssParameter name="stroke">0xAAAAAA</sld:CssParameter>
                                        </sld:Stroke>
                                    </sld:Mark>
                                    <sld:Size>
                                        <ogc:Literal>8</ogc:Literal>
                                    </sld:Size>
                                </sld:Graphic>
                            </sld:GraphicFill>
                        </sld:Fill>
                    </sld:PolygonSymbolizer>
                </sld:Rule>
                <sld:Rule>
                    <sld:Title>&gt; 4M</sld:Title>
                    <ogc:Filter>
                        <ogc:PropertyIsGreaterThan>
                            <ogc:PropertyName>PERSONS</ogc:PropertyName>
                            <ogc:Literal>4000000</ogc:Literal>
                        </ogc:PropertyIsGreaterThan>
                    </ogc:Filter>
                    <sld:PolygonSymbolizer>
                        <sld:Fill>
                            <sld:GraphicFill>
                                <sld:Graphic>
                                    <sld:Mark>
                                        <sld:WellKnownName>shape://slash</sld:WellKnownName>
                                        <sld:Stroke>
                                            <sld:CssParameter name="stroke">0xAAAAAA</sld:CssParameter>
                                        </sld:Stroke>
                                    </sld:Mark>
                                    <sld:Size>
                                        <ogc:Literal>4</ogc:Literal>
                                    </sld:Size>
                                </sld:Graphic>
                            </sld:GraphicFill>
                        </sld:Fill>
                    </sld:PolygonSymbolizer>
                </sld:Rule>
                <sld:Rule>
                    <sld:Title>Boundary</sld:Title>
                    <sld:LineSymbolizer>
                        <sld:Stroke/>
                    </sld:LineSymbolizer>
                    <sld:TextSymbolizer>
                        <sld:Label>
                            <ogc:PropertyName>STATE_ABBR</ogc:PropertyName>
                        </sld:Label>
                        <sld:Font>
                            <sld:CssParameter name="font-family">Times New Roman</sld:CssParameter>
                            <sld:CssParameter name="font-size">14</sld:CssParameter>
                            <sld:CssParameter name="font-style">Normal</sld:CssParameter>
                            <sld:CssParameter name="font-weight">normal</sld:CssParameter>
                        </sld:Font>
                        <sld:LabelPlacement>
                            <sld:PointPlacement>
                                <sld:AnchorPoint>
                                    <sld:AnchorPointX>
                                        <ogc:Literal>0.5</ogc:Literal>
                                    </sld:AnchorPointX>
                                    <sld:AnchorPointY>
                                        <ogc:Literal>0.5</ogc:Literal>
                                    </sld:AnchorPointY>
                                </sld:AnchorPoint>
                                <sld:Rotation>
                                    <ogc:Literal>0.0</ogc:Literal>
                                </sld:Rotation>
                            </sld:PointPlacement>
                        </sld:LabelPlacement>
                        <sld:Halo>
                            <sld:Radius>
                                <ogc:Literal>2</ogc:Literal>
                            </sld:Radius>
                            <sld:Fill>
                                <sld:CssParameter name="fill">0xFFFFFF</sld:CssParameter>
                            </sld:Fill>
                        </sld:Halo>
                    </sld:TextSymbolizer>
                </sld:Rule>
            </sld:FeatureTypeStyle>
        </sld:UserStyle>
        <sld:UserStyle>
            <sld:Name>polygon</sld:Name>
            <sld:Title>Default Polygon</sld:Title>
            <sld:Abstract>A sample style that draws a polygon</sld:Abstract>
            <sld:FeatureTypeStyle>
                <sld:Name>name</sld:Name>
                <sld:Rule>
                    <sld:Name>rule1</sld:Name>
                    <sld:Title>Gray Polygon with Black Outline</sld:Title>
                    <sld:Abstract>A polygon with a gray fill and a 1 pixel black outline</sld:Abstract>
                    <sld:PolygonSymbolizer>
                        <sld:Fill>
                            <sld:CssParameter name="fill">#AAAAAA</sld:CssParameter>
                        </sld:Fill>
                        <sld:Stroke/>
                    </sld:PolygonSymbolizer>
                </sld:Rule>
            </sld:FeatureTypeStyle>
        </sld:UserStyle>
    </sld:NamedLayer>
</sld:StyledLayerDescriptor>
share|improve this question
    
Can you post your expected request response? What should the request have returned? –  R.K. Apr 30 '13 at 12:26
    
@R.K. There is the requred SLD. –  kinkajou Apr 30 '13 at 12:28
    
Why do you want to do that btw? –  R.K. Apr 30 '13 at 12:38
    
@R.K. Actually I want to know how the layer is styled using what column name of table. So that I can find feature hits using wfs query.Feature Count –  kinkajou Apr 30 '13 at 12:52
    
First thing to check is whether the two requests being sent are exactly the same. Could you confirm that the OpenLayers request string is the same as the one that produces the correct response. –  pecoanddeco May 3 '13 at 6:10

2 Answers 2

up vote 2 down vote accepted
+50

It seems the problem is in the line breaks from Geoserver XML indent.

It works when I do something like below

OpenLayers.Request.GET({
    url: "http://localhost:8080/geoserver/wms",
    params: {
        request: "GetStyles",
        layers: "topp:states",
        service: "wms",
        version: "1.1.1"
    },
    success: function (data, statut, xhr) {
        var format = new OpenLayers.Format.SLD();
        //Trick is to clean line break
        var obj = format.read(data.responseText.replace(/(\r\n|\n|\r)/gm, ""));
        //Js object
        console.log(obj);
        var json = new OpenLayers.Format.JSON();
        // Object converted to json for readability but not really useful at js level
        console.log(json.write(obj, true));
    }
});

Edit

When I do my test going to http://localhost:8080/geoserver/topp/wms?service=WMS&version=1.1.0&request=GetMap&layers=topp:states&styles=&bbox=-124.73142200000001,24.955967,-66.969849,49.371735&width=780&height=330&srs=EPSG:4326&format=application/openlayers, it failed.

I solved the problem by replacing the file OpenLayers.js into the geoserver/WEB-INF/lib/wms-2.3.1.jar with the http://openlayers.org/dev/OpenLayers.js (because the default built-in Openlayers library in Geoserver don't have OpenLayers.Format.SLD() and OpenLayers.Format.JSON())

If you don't want to change anything but only confirm it works just try with this other ajax request

OpenLayers.Request.GET({
    url: "http://localhost:8080/geoserver/wms",
    params: {
        request: "GetStyles",
        layers: "topp:states",
        service: "wms",
        version: "1.1.1"
    },
    success: function (data, statut, xhr) {
        var format = new OpenLayers.Format.XML();
        //Trick is to clean line break
        var obj = format.read(data.responseText.replace(/(\r\n|\n|\r)/gm, ""));
        //Dom object to manipulate yourself
        console.log(obj);
        //Try that you can convert the XML object back to text
        if (window.ActiveXObject){
        //For Internet Explorer:
            var string = obj.xml;
        } else {
            var string = (new XMLSerializer()).serializeToString(obj);
        }
        console.log(string)
    }
});

If you always have a blank answer with code below, look on the Cross Domain Policy matter. It's a common problem with ajax calls. See http://www.gistutor.com/geoserver/21-intermediate-geoserver-tutorials/38-configuring-geoserver-proxy-for-public-and-remote-data-access.html

share|improve this answer
    
The response is still blank. Both xml and json. Did you get the response? –  kinkajou May 9 '13 at 4:12
    
See my updated answer –  ThomasG77 May 9 '13 at 11:05
    
It was all beacause of Cross Domain Policy. –  kinkajou May 10 '13 at 3:55

If the request is the same then there must be an issue with the code that's dealing with the response.

Comparing your code to that on the OpenLayers documentation there is some difference. The documentation uses the 'callback' parameter rather than 'success', not sure if that would make a difference or not, or whether it's just a difference in versions.

The other thing to check is what the value of req.responseXML is before you convert it into JSON.

Try something like the following and see what you get:

function handler(req) {
    alert(req.responseXML);
}

OpenLayers.Request.GET({
    url: "http://localhost:8080/geoserver/wms",
    params: {request: "GetStyles",layers:"topp:states",service:"wms",version:"1.1.1"},
    callback: handler
});
share|improve this answer
    
I have changed success to handler. It doesn't matter. xml response is null. –  kinkajou May 3 '13 at 12:42
    
How are you checking the request string that your code is creating? Seems like there must be an issue there if you're getting different responses back. –  pecoanddeco May 8 '13 at 15:21
    
I am checking it using firebug. It's same. –  kinkajou May 9 '13 at 4:04

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.