2

I have a function which publishes a layer in GeoServer with the REST API. Now I want to add an existing style in the layer while publishing it.

Anyone has any idea where I write the style name or call the 'style.sld' while publish the layer?

const publish = function (layerName) {
return new Promise((resolve, reject) => {
    axios({
        method: 'POST',
        url: `http://${GEOSERVER_IP}:${GEOSERVER_PORT}/geoserver/rest/workspaces/workspaceName/datastores/datastoreName/featuretypes.xml`,
        auth: {
            username: `${GEOSERVER_ADMIN_USER}`,
            password: `${GEOSERVER_ADMIN_PASS}`
        },
        headers: {
            'Content-Type': 'text/xml'
        },
        data: `<featureType>
                <name>${layerName}</name>
                <srs>EPSG:4326</srs>
            </featureType>`
    })
        .then(response => {
            resolve({
                ok: 1,
                message: `Layer has been published.`
            })
        })
        .catch(err => {
            reject({
                ok: 0,
                message: err.response.data,
            })
        })

})
}
1

As the manual says you need to use POST the details to

/layers/<l>/styles[.<format>]

To determine the format of the file I usually use a request like:

curl -u admin:geoserver http://localhost:8080/geoserver/rest/layers/topp:states/styles.xml

which returns this:

<styles>
  <style>
    <name>pophatch</name>
    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/layers/topp:states/styles/pophatch.xml" type="application/atom+xml"/>
  </style>
  <style>
    <name>polygon</name>
    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/layers/topp:states/styles/polygon.xml" type="application/atom+xml"/>
  </style>
</styles>

So all you need to do is create a payload like:

<styles>
  <style>
    <name>new_style</name>
    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/layers/topp:states/styles/new_style.xml" type="application/atom+xml"/>
  </style>
<styles>

Update

Alternatively, you could fetch and modify the layer.xml to add the new style information. So a request to the layer:

curl -u admin:geoserver http://localhost:8080/geoserver/rest/layers/topp:states.xml

gives:

<layer>
  <name>states</name>
  <path>/</path>
  <type>VECTOR</type>
  <defaultStyle>
    <name>population</name>
    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/styles/population.xml" type="application/xml"/>
  </defaultStyle>
  <styles class="linked-hash-set">
    <style>
      <name>pophatch</name>
      <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/styles/pophatch.xml" type="application/xml"/>
    </style>
    <style>
      <name>polygon</name>
      <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/styles/polygon.xml" type="application/xml"/>
    </style>
  </styles>
  <resource class="featureType">
    <name>topp:states</name>
    <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/workspaces/topp/datastores/states_shapefile/featuretypes/states.xml" type="application/xml"/>
  </resource>
  <attribution>
    <logoWidth>0</logoWidth>
    <logoHeight>0</logoHeight>
  </attribution>
</layer>

So you would need to add a new defaultStyle or style block and then PUT it back to the layer endpoint.

  • 1
    Thank you. I actually can send style in Geoserver but Is it possible to publish a layer with that style(already in existing style list)? Or, we update the style of a newly created layer? – Angels Aug 7 at 8:46
  • also I know the type of my style in .xml which is a line type. – Angels Aug 7 at 8:52
  • I have described how you add an existing style to a layer. – Ian Turton Aug 7 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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