[Q&A] How do I parse getStyle request? Get SLD GeoServer
Table of Content
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>< 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>> 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>< 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>> 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>
-
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
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