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.
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
>>> 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()
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>
font
and fill
parameters to change the font color from black to blue:<sld:Fill> <sld:CssParameter name="fill">#0000ff</sld:CssParameter> </sld:Fill>
<sld:Name>CapitalsBlue</sld:Name>
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
>>> 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
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!
18.191.235.62