Thursday, 30 July 2015

openlayers 2 - How do I parse getStyle request?



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:





topp:states

population

Population in the United States
1
A sample filter that filters the United States into three
categories of population, drawn in different colors


name

< 2M



PERSONS
2000000




#4DFF4D
0.7





2M - 4M


PERSONS

2000000



4000000





#FF4D4D
0.7





> 4M


PERSONS
4000000





#4D4DFF
0.7




Boundary



0.2




STATE_ABBR


Times New Roman
14

Normal
normal





0.5



0.5



0.0









pophatch
Population in the United States
A sample filter that filters the United States into three
categories of population, drawn in different colors


name


< 2M


PERSONS
2000000








shape://slash

0xAAAAAA



16








2M - 4M


PERSONS


2000000


4000000









shape://slash

0xAAAAAA



8








> 4M



PERSONS
4000000







shape://slash


0xAAAAAA



4








Boundary





STATE_ABBR



Times New Roman
14
Normal
normal






0.5


0.5



0.0






2


0xFFFFFF








polygon
Default Polygon
A sample style that draws a polygon

name


rule1
Gray Polygon with Black Outline
A polygon with a gray fill and a 1 pixel black outline


#AAAAAA










Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...