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've the function below which creates or replaces an existing layer group:

public bool AddToLayerGroup(string layerGroupName, string layerName)
        {
            try
            {
                // Retrive the layer group
                string cLayerGroup = GetLayerGroup(layerGroupName);
                // Add the new layer as a new layer node among the retrieved layer nodes
                XmlDocument newLayerGrpXml = AddLayerNodeToLayerGroupXml(layerName, cLayerGroup);
                // Convert the newly created layer group xml to string
                string newLayerGrpString = ConvertXmlToString(newLayerGrpXml);
                newLayerGrpString = newLayerGrpString.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");

                // Delete the existing layer group
                //DeleteLayerGroup(layerGroupName);

                // Now, do ur thing here
                string gUrl = GEOSERVER_HOST + "/rest/layergroups/" + layerGroupName + ".xml";
                WebRequest request = WebRequest.Create(gUrl);

                request.ContentType = "application/xml";
                request.Method = "PUT";
                request.Credentials = new NetworkCredential(GEOSERVER_USER, GEOSERVER_PASSWD);

                byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(newLayerGrpString);
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(buffer, 0, buffer.Length);
                requestStream.Close();

                WebResponse response = request.GetResponse();

                return true;
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                return false;
            }
        }

This executes successfully. Tomcat logs also indicates successful execution of the PUT method. However, I can't see the created layer group using GeoServer web UI. Is there anything else I need to do after executing the above code? Trying to create another layer group with the same name fails with the error layer group already exists; a further proof that the layer group was created. But, why can't I see it using the GeoServer web UI. Any assistance will be highly appreciated.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

After reviewing every step, I found that there was a slight problem with my XML. When inserting a new layer, <layer name="layer_name" /> is not acceptable to GeoServer, but it doesn't complain about it. It has to be <layer><name>layer_name</name></layer>. A C# method for constructing this XML is shown below. This now works fine:

private XmlDocument AddLayerNodeToLayerGroupXml(string layerName, string layerGroupString)
        {

            try
            {
                XmlDocument layerGrpXml = new XmlDocument();
                layerGrpXml.Load(new StringReader(layerGroupString));

                XmlNode layerNode = layerGrpXml.SelectSingleNode("layerGroup/layers");
                XmlNode newLayerNode = layerGrpXml.CreateNode(XmlNodeType.Element, "layer", null);

                XmlNode newLayerNameNode = layerGrpXml.CreateNode(XmlNodeType.Element, "name", null);
                newLayerNameNode.InnerText = layerName;
                newLayerNode.AppendChild(newLayerNameNode);

                layerNode.AppendChild(newLayerNode);

                XmlNode styleNode = layerGrpXml.SelectSingleNode("layerGroup/styles");
                XmlNode newLayerStyleNode = layerGrpXml.CreateNode(XmlNodeType.Element, "style", null);
                styleNode.AppendChild(newLayerStyleNode);

                layerGrpXml.Save("layerGroup.xml");

                Console.WriteLine(layerGrpXml);
                return layerGrpXml;
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                return null;
            }
        }

In the question above, this method is called at this line XmlDocument newLayerGrpXml = AddLayerNodeToLayerGroupXml(layerName, cLayerGroup);.

share|improve this answer
add comment

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.