0

I would like to change the layer's style via REST. My problem is very similar to Layer sets not enabled after change style via REST. and How to set default style of layer using rest api in Geoserver.

Running the following via curl works:

curl.exe -v -u user:password -XPUT -H "Content-type: text/xml" 
-d "<layer><defaultStyle><name>poi</name></defaultStyle></layer>" 
http://localhost:8080/geoserver/rest/layers/workspace:layer

Though, I'm building on top of GeoServerWrapper in PHP provided by IBM to interact with the GeoServer REST API so my code is as follows:

private function runApi($apiPath, $method = 'GET', $data = '', $contentType = 'text/xml') {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->serverUrl.'rest/'.$apiPath);
    curl_setopt($ch, CURLOPT_USERPWD, $this->username.":".$this->password); 
    if ($method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    } else if ($method == 'DELETE' || $method == 'PUT') {
        // return $method;
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    }

    if ($data != '') {
        curl_setopt($ch, CURLOPT_HTTPHEADER, 
            array("Content-Type: $contentType",
            'Content-Length: '.strlen($data))
        );
    }

    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $rslt = curl_exec($ch);
    $info = curl_getinfo($ch);

    if ($info['http_code'] == 401) {
        return 'Access denied. Check login credentials.';
    } else {
        return $rslt;
    }
}

public function addStyleToLayer($layerName, $workspaceName, $styleName) {
    return $this->runApi('layers/'.urlencode($workspaceName).':'.urlencode($layerName).'', 'PUT', '<layer><defaultStyle><name>'.htmlentities($styleName, ENT_COMPAT).'</name></defaultStyle><enabled>true</enabled></layer>');
}

Specifying 'PUT' didn't work so I'm back to the drawing boards.


I'm sending these parameters to the GeoServer REST:

$.ajax({
               url: 'geoserverRestFinal.php',
               type: 'PUT',
               data: {
                'action': 'assignstyle',
                'username': $('#username').val(),
                'password': $('#password').val(),
                'workspace': $('#assignstyle_workspace').val(),
                'layer': $('#assignstyle_layer').val(),
                'stylename': $('#assignstyle_stylename').val()
               },
               success: function(ret) {
                $('#assignstyle_results').html(escape(ret).replace(/%(..)/g,"&#x$1;")); 
               }
            });

From the 'action' parameter:

if (isset($_REQUEST['action'])) {
include "GeoserverWrapper.php";
$geoserver = new GeoserverWrapper('http://server:8080/geoserver/', $_REQUEST['username'], $_REQUEST['password']);

switch ($_REQUEST['action']) {
    case 'assignstyle':
        print_r($geoserver->addStyleToLayer($_REQUEST['layer'], $_REQUEST['workspace'], $_REQUEST['stylename']));
        break;
}

return;}

My jQuery Ajax call is sending my data as Form Data, not as Query Strs.

Current:

http://server/php/geoserverRestFinal.php

Form Data action:assignstyle username:user password:password workspace:geoportal layer:glocalidad stylename:point

Should be:

http://server/php/geoserverRestFinal.php?action=assignstyle&username=user&password=password&workspace=geoportal&layer=glocalidad&stylename=point

Looking into jQuery.params

2
  • can you show us what is actually sent to the server by your code so we can compare it to the curl example?
    – Ian Turton
    Commented Dec 4, 2014 at 9:14
  • edit 2 has what is being sent to the server
    – Zach
    Commented Dec 4, 2014 at 17:38

1 Answer 1

-1

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 "true" 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 = "xxx";
    $your_layer_name = = "bbb";

    $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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.