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 using C# and the WebRequest class to create a worksapce and then another WebRequest to create a datastore and upload a folder full of shapefiles to a geoserver. However, I would also like to change the style of the uploaded shapefiles to another style that has already been installed on the geoserver. Any ideas?

Lazaros.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Ok, this is what I did....

Assumptions: I have a List<string> named layers that contains my layer names, I have a string called style that contains the style name, I have a class SessionConfig defined as follows and being populated with appropriate values (url, username, password etc):

static class SessionConfig
{
    public static string url = null;
    public static string username = null;
    public static string password = null;
    // public static string locationOfCurl = null;
    public static string workspaceName = null;
    public static string storeName = null;
    public static string locationOfShapeFiles = null;
};

Then the code to change the style of each layer in my workspace/datastore is as follows:

    string responseFromServer = null;

            for (int i = 0; i < layers.Count; i++)
            {
    WebRequest request = WebRequest.Create(SessionConfig.url + "/rest/layers/" +
                                               SessionConfig.workspaceName + ":" + layers[i]);
                    request.ContentType = "text/xml";
                    request.Method = "PUT";
                    request.Credentials = new NetworkCredential(SessionConfig.username, SessionConfig.password);

                    byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("<layer><defaultStyle><name>"+style+"</name></defaultStyle><enabled>true</enabled></layer>");
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(buffer, 0, buffer.Length);
                    requestStream.Close();
                    WebResponse response = null;

                    bool done = false;

                    try
                    {
                        while (!done)
                        {
                            response = request.GetResponse();
                            response.Close();
                            done = true;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
}

I'm not sure if this is 100% good code but it works for me.

share|improve this answer
    
If this is your solution please make this answer as accepted. –  Chad Cooper May 19 '13 at 14:18
    
I'm not sure how to do this. –  Lazaros Filippidis May 19 '13 at 18:19
    
By the vote count and up/down vote button, there is a greyed out check Mark. Check it to make it green. That marks it as answered. –  Chad Cooper May 20 '13 at 1:39
    
Thank you for the tip. –  Lazaros Filippidis May 20 '13 at 8:27

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.