4

I have worked with geoserver rest api and it is great, The only problem is that I have no idea how to post content of my sld using api, I have this code

public void AddNewStyle(String styledata, string workspace)
        {
            String requestUrl = RestServiceUrl + "/workspaces/" + workspace + "/styles";

            String status = String.Empty;

            object payload = styledata;

            try
            {
                status = SendRestRequest(requestUrl, RequestMethod.Post, typeof(WorkspaceRequest), ref payload, ContentType.Html, AcceptType.Json);
            }



            catch (Exception ex)
            {
                throw new Exception("Adding datastores failed.", ex);
            }
        }

this code works fine with other requests. the only problem is I dont know what to send as styledata. I have set styledata as

<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml"
  xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
  <NamedLayer>
    <Name>Baft Farsode</Name>
    <UserStyle>
      <Name>BaftFarsoode</Name>
      <Title>بافت فرسوده</Title>
      <Abstract>BaftFarsoode area</Abstract>
      <FeatureTypeStyle>
      <FeatureTypeName>Feature</FeatureTypeName>
        <Rule>
          <Title>بافت فرسوده</Title>
          <ogc:Filter>
            <ogc:PropertyIsEqualTo>
             <ogc:PropertyName>Farsoode</ogc:PropertyName>
             <ogc:Literal>1</ogc:Literal>
            </ogc:PropertyIsEqualTo>
          </ogc:Filter>
          <PolygonSymbolizer>
             <Fill>
                <!-- CssParameters allowed are fill (the color) and fill-opacity -->
                <CssParameter name="fill">#4DFF4D</CssParameter>
                <CssParameter name="fill-opacity">0.7</CssParameter>
             </Fill>     
          </PolygonSymbolizer>
        </Rule>
         <Rule>
          <Title>بدون داده</Title>
          <ogc:Filter>

            <ogc:PropertyIsNull>
             <ogc:PropertyName>Farsoode</ogc:PropertyName>

            </ogc:PropertyIsNull>
          </ogc:Filter>
          <PolygonSymbolizer>
             <Fill>
                <!-- CssParameters allowed are fill (the color) and fill-opacity -->
                <CssParameter name="fill">#ffffff</CssParameter>
                <CssParameter name="fill-opacity">0.7</CssParameter>
             </Fill>     
          </PolygonSymbolizer>
        </Rule>

        <Rule>
          <Title>غیر فرسوده</Title>
          <ogc:Filter>
            <ogc:PropertyIsEqualTo>
                    <ogc:PropertyName>Farsoode</ogc:PropertyName>
                    <ogc:Literal>0</ogc:Literal>
            </ogc:PropertyIsEqualTo>
          </ogc:Filter>
          <PolygonSymbolizer>
             <Fill>
                <!-- CssParameters allowed are fill (the color) and fill-opacity -->
                <CssParameter name="fill">#FF4D4D</CssParameter>
                <CssParameter name="fill-opacity">0.7</CssParameter>
             </Fill>     
          </PolygonSymbolizer>
        </Rule>

        <Rule>
          <Title>مرز املاک</Title>
          <LineSymbolizer>
            <Stroke>
              <CssParameter name="stroke-width">0.2</CssParameter>
            </Stroke>
          </LineSymbolizer>
        </Rule>
     </FeatureTypeStyle>
    </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>

but it gives error, I think there must be another way to add style data as raw xml using rest api.

Can someone help me with this?

here is an example but it uploads sld file into geoserver. I want to send data as a raw string not by uploading. http://docs.geoserver.org/latest/en/user/rest/examples/curl.html#creating-a-layer-style-sld-package

10
  • Did you ever get it solved?
    – nathanvda
    Jan 2, 2018 at 16:37
  • @nathanvda yes, Content-type: text/xml should make it work Jan 2, 2018 at 17:41
  • @MajidHojati Did you have any problem with unicode SLD expression ? Aug 11, 2020 at 19:23
  • 1
    @MostafaFallah Geoserver is based on jetty(or tomcat) server which listens to port number 8080, if my memory serves me well, and both are java based. I guess in your configuration you are using windows server as proxy server for geoservices. so just configure your java version which is installed on the windows to add Unicode support. Aug 13, 2020 at 1:16
  • 1
    @MostafaFallah nice, Good luck! Aug 14, 2020 at 19:09

2 Answers 2

3

It's not clear what libraries you are using but as the example you link to shows you need to send the sld file (zipped and encoded) to http://localhost:8080/geoserver/rest/styles with the Content-type set.

I've never tried but I expect you could send just the XML data with a Content-type: text/xml but it will take longer than sending a compressed file.

1
  • Hi, I am using C#, I found the way, I am posting the answer Dec 6, 2016 at 9:20
3

If you're sure that .xml is valid (you can check its validation via this online tool) you need to add the following line to your request's header as mentioned in geoserver official documentation (here)

{content-type:'application/vnd.ogc.sld+xml'}

Here I give an example. First of all I created a sld string on my Node server using javascript:

    <?xml version="1.0" encoding="UTF-8"?>
    <StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
    xmlns="http://www.opengis.net/sld"
    xmlns:ogc="http://www.opengis.net/ogc"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <NamedLayer>
        <UserStyle>
        <Name>sld_tit_12</Name>
            <Title>No Title Existed.</Title>
            <Abstract>No Abstract Existed.</Abstract>
            <FeatureTypeStyle>
                <Rule>
                    <Title>class_0</Title>
                    <ogc:Filter>
                        <ogc:PropertyIsBetween>
                            <ogc:PropertyName>tit_12</ogc:PropertyName>
                            <ogc:LowerBoundary>
                                <ogc:Literal>0</ogc:Literal>
                            </ogc:LowerBoundary>
                            <ogc:UpperBoundary>
                                <ogc:Literal>281</ogc:Literal>
                            </ogc:UpperBoundary>
                        </ogc:PropertyIsBetween>
                    </ogc:Filter>
                    <PolygonSymbolizer>
                        <Fill>
                            <CssParameter name="fill">#fcd1a4</CssParameter>
                            <CssParameter name="fill">0.7</CssParameter>
                        </Fill>
                        <Stroke>
                            <CssParameter name="stroke">#000</CssParameter>
                            <CssParameter name="stroke-width">0.5</CssParameter>
                        </Stroke>
                    </PolygonSymbolizer>
                </Rule>
                <Rule>
                    <Title>class_1</Title>
                    <ogc:Filter>
                        <ogc:PropertyIsBetween>
                            <ogc:PropertyName>tit_12</ogc:PropertyName>
                            <ogc:LowerBoundary>
                                <ogc:Literal>281</ogc:Literal>
                            </ogc:LowerBoundary>
                            <ogc:UpperBoundary>
                                <ogc:Literal>547</ogc:Literal>
                            </ogc:UpperBoundary>
                        </ogc:PropertyIsBetween>
                    </ogc:Filter>
                    <PolygonSymbolizer>
                        <Fill>
                            <CssParameter name="fill">#fab072</CssParameter>
                            <CssParameter name="fill">0.7</CssParameter>
                        </Fill>
                        <Stroke>
                            <CssParameter name="stroke">#000</CssParameter>
                            <CssParameter name="stroke-width">0.5</CssParameter>
                        </Stroke>
                    </PolygonSymbolizer>
                </Rule>
                <Rule>
                    <Title>class_2</Title>
                    <ogc:Filter>
                        <ogc:PropertyIsBetween>
                            <ogc:PropertyName>tit_12</ogc:PropertyName>
                            <ogc:LowerBoundary>
                                <ogc:Literal>547</ogc:Literal>
                            </ogc:LowerBoundary>
                            <ogc:UpperBoundary>
                                <ogc:Literal>905</ogc:Literal>
                            </ogc:UpperBoundary>
                        </ogc:PropertyIsBetween>
                    </ogc:Filter>
                    <PolygonSymbolizer>
                        <Fill>
                            <CssParameter name="fill">#f6904c</CssParameter>
                            <CssParameter name="fill">0.7</CssParameter>
                        </Fill>
                        <Stroke>
                            <CssParameter name="stroke">#000</CssParameter>
                            <CssParameter name="stroke-width">0.5</CssParameter>
                        </Stroke>
                    </PolygonSymbolizer>
                </Rule>
                <Rule>
                    <Title>class_3</Title>
                    <ogc:Filter>
                        <ogc:PropertyIsBetween>
                            <ogc:PropertyName>tit_12</ogc:PropertyName>
                            <ogc:LowerBoundary>
                                <ogc:Literal>905</ogc:Literal>
                            </ogc:LowerBoundary>
                            <ogc:UpperBoundary>
                                <ogc:Literal>2075</ogc:Literal>
                            </ogc:UpperBoundary>
                        </ogc:PropertyIsBetween>
                    </ogc:Filter>
                    <PolygonSymbolizer>
                        <Fill>
                            <CssParameter name="fill">#d75538</CssParameter>
                            <CssParameter name="fill">0.7</CssParameter>
                        </Fill>
                        <Stroke>
                            <CssParameter name="stroke">#000</CssParameter>
                            <CssParameter name="stroke-width">0.5</CssParameter>
                        </Stroke>
                    </PolygonSymbolizer>
                </Rule>
                <Rule>
                    <Title>class_4</Title>
                    <ogc:Filter>
                        <ogc:PropertyIsBetween>
                            <ogc:PropertyName>tit_12</ogc:PropertyName>
                            <ogc:LowerBoundary>
                                <ogc:Literal>2075</ogc:Literal>
                            </ogc:LowerBoundary>
                            <ogc:UpperBoundary>
                                <ogc:Literal>2889820</ogc:Literal>
                            </ogc:UpperBoundary>
                        </ogc:PropertyIsBetween>
                    </ogc:Filter>
                    <PolygonSymbolizer>
                        <Fill>
                            <CssParameter name="fill">#914032</CssParameter>
                            <CssParameter name="fill">0.7</CssParameter>
                        </Fill>
                        <Stroke>
                            <CssParameter name="stroke">#000</CssParameter>
                            <CssParameter name="stroke-width">0.5</CssParameter>
                        </Stroke>
                    </PolygonSymbolizer>
                </Rule>
            </FeatureTypeStyle>
        </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>

The above string is used to classify features on map. Then I made a request like this:

const geoserverRESTAPI = 'http://localhost:8080/geoserver/rest/';
const options = {
   url: `${geoserverRESTAPI}styles`,
   method: 'POST',
   headers: { 'Content-type': 'application/vnd.ogc.sld+xml' },
   body: sldString, //in string format
   auth: {
      user: '<username>',
      pass: '<password>'
   }
}

Don't forget to put your own username and password in auth.

If everything is OK, you will be received 201 - created response.

4

Your Answer

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

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