I want to change the default style of the layer in Geo server using the REST API.
For this i have this code:
curl -v -u admin:geoserver -XPUT -H "Content-type: text/xml"
-d <layer><defaultStyle><name>polygon</name></defaultStyle></layer>"
http://mindcrewgis.com/geoserver/rest/layers/pymble:pymble_water
when run this command it is working fine form me. But when i convert this command in PHP then it is not working for me My PHP code for above curl command is
<?php
$url="http://mindcrewgis.com/geoserver/rest/layers/pymble:pymble_water";
$contentType = 'text/xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$data="<layer>
<defaultStyle>
<name>polygon</name>
</defaultStyle>
</layer>";
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: $contentType",
'Content-Length: '.strlen($data))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rslt = curl_exec($ch);
$info = curl_getinfo($ch);
?>
But this code is not working for me . There is something wrong in execute curl using PHP.
-
1When you say "is not working", do you get an error back from GeoServer? Have you compared the two queries at the network (e.g. using Wireshark or similar)? Since you are doing HTTP PUT, have you tried using CURLOPT_INFILE and CURLOPT_INFILESIZE instead of CURLOPT_POSTFIELDS (which is documented to be for POST)?BradHards– BradHards2014年04月28日 05:34:45 +00:00Commented Apr 28, 2014 at 5:34
3 Answers 3
The following is a modified version of what has been working for us. You'll need to supply your own logic for setting the specific style. In our situation, we base the style on the layer name using a series of if/else statements.
import os
from geoserver.catalog import Catalog
"""
geoserver_username - the username of the server
geoserver_password - the password
geoserver_server - the url for the server (e.g.: http://127.0.0.1:8080). Note that there is no trailing slash!
"""
def SetGeoServerDefaultStyles(self, geoserver_username, geoserver_password, geoserver_server):
geoserver_url = geoserver_server + "/geoserver"
geoserver_catalog_url = geoserver_url + "/gwc/rest"
cat = Catalog(geoserver_catalog_url, geoserver_username, geoserver_password)
#Get all layers
all_layers = cat.get_layers()
totalLayerCount = len(all_layers)
print "Total layers: ", totalLayerCount
for layer in all_layers:
# Insert your style here. Originally, we used some logic here to set the style based on the layer name. Feel free to replace with whatever works best for you.
style = "<layer><defaultStyle><name>" + styleToUse + "</name></defaultStyle></layer>"
# Actually make the call to set the default style. For debugging purposes, you may wish to add
# the -v (verbose) flag.
curlString = 'curl -u ' + geoserver_username + ':' + geoserver_password + ' -XPUT -H "Content-type: text/xml" -d "' + style + '" ' + geoserver_url + '/rest/layers/' + layer.name
os.system(curlString)
currentLayer = currentLayer + 1
print "Processed layer %i of %i" % (currentLayer, totalLayerCount)
I solved it by using this way
Change curl_setopt($ch, CURLOPT_PUT, true);
to curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
optional. this is function php for change exist layer style in geoserver v2.3.0.
I solved it by folowing function notice that $params it have to put "<enabled>true</enabled>
" for enable layer after chang style in geoserver.
function change_layer_style($url_layer,$style_name) {
$params = '<layer><defaultStyle><name>'.$style_name.'</name></defaultStyle><enabled>true</enabled></layer>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url_layer);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_USERPWD,"user:password"); //geoserver.
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Receive server response ...
$response = curl_exec($ch);
curl_close ($ch);
return $response;
}
//--> how to use.
//--> 1. config your geoserver url.
$your_workspace = "workspace_name";
$your_layer_name = "layer_name";
$url_layer = "http://xxxx.co.uk:8080/geoserver/rest/layers/".$your_workspace.":".$your_layer_name;
$style_name ="your_exist_style_name";
//--> call above function.
change_layer_style($url_layer,$style_name);