Uploading and updating styles with REST

In Chapter 3, Advanced Styling, you learned a lot about styles and SLD. You used a very powerful extension, the CSS module, to create styles avoiding the complexity of SLD syntax. There are many options to create SLD, and some users prefer using external tools to manage them.

Whatever you prefer to use to create and edit your styles in order to configure a proper visualization, you should upload them on GeoServer and publish them.

REST offers you two resources to manage styles:

  • /styles
  • /workspaces/<ws>/styles

The former points to styles not associated to a workspace while the latter contains the workspaces with associated styles.

Adding a new style is a routine task if you are going to publish data with REST. We will retrieve an existing style from GeoServer, update it, and then upload it to GeoServer as a new one.

How to do it…

  1. We will use the Capitals style as the template for our new style. Send a request to GeoServer to retrieve it and save to the CapitalsBlue.xml file:
    $ curl -u admin:geoserver -XGET -H 'Accept: application/vnd.ogc.sld+xml' http://localhost:8080/geoserver/rest/styles/Capitals -o CapitalsBlue.xml
    
  2. You can also use the following commands in Python:
    >>> myUrl = 'http://localhost:8080/geoserver/rest/styles/Capitals'
    >>> headers = {'Accept':'application/vnd.ogc.sld+xml'}
    >>> resp = requests.get(myUrl, auth=('admin','geoserver'), headers=headers)
    >>> file = open('CapitalsBlue.xml','w')
    >>> file.write(resp.text)
    >>> file.close()
    
  3. Now open the CapitalsBlue.xml file. Unless you use an editor that can format XML in a more readable way, you will see just one line containing all the code. Locate this code fragment:
    </sld:LabelPlacement>
    <sld:Fill>
      <sld:CssParameter name="fill">#ffffff</sld:CssParameter>
    </sld:Fill>
    </sld:TextSymbolizer>
  4. Then, edit the font and fill parameters to change the font color from black to blue:
    <sld:Fill>
      <sld:CssParameter name="fill">#0000ff</sld:CssParameter>
    </sld:Fill>
  5. Go to the beginning of the code and replace the old name with this new one:
      <sld:Name>CapitalsBlue</sld:Name>
  6. Save the file and close it. Now, we will create a new style with this file. Send a POST request to create CapitalsBlue style:
    $ curl -u admin:geoserver -XPOST -H 'Content-type: application/vnd.ogc.sld+xml' -d @CapitalsBlue.xml http://localhost:8080/geoserver/rest/styles
    
  7. Alternatively, do the same operation in Python:
    >>> myUrl = 'http://localhost:8080/geoserver/rest/styles'
    >>> file = open('CapitalsBlue.xml','r')
    >>> payload = file.read()
    >>> headers = {'Content-type': 'application/vnd.ogc.sld+xml','Accept': 'text/xml'}
    >>> resp = requests.post(myUrl, auth=('admin','geoserver'), data=payload, headers=headers)
    >>> resp.status_code
    201
    

How it works…

You are now quite used to the REST syntax, so nothing seems too strange in the execution steps. You are probably wondering why we used a different header:

$ curl -u admin:geoserver -XGET -H 'Accept: application/vnd.ogc.sld+xml'

This format is to tell GeoServer we actually want the SLD format. If you specify text/xml, as in the previous recipes, you will get only a description of what the SLD is and not the rendering rules. Send the request modified as shown here:

$ curl -u admin:geoserver -XGET -H 'Accept: text/xml'http://localhost:8080/geoserver/rest/styles/Capitals -o CapitalsBlue.xml

Then, open the CapitalsBlue.xml file:

<style>
  <name>Capitals</name>
  <sldVersion>
    <version>1.0.0</version>
  </sldVersion>
  <filename>Capitals.sld</filename>
</style>

This is not really what you need to inspect drawing rules and then edit them!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.62