REST API Automation
The GeoServer REST API allows for remote configuration of GeoServer and is used by GeoCat Bridge.
The most common goal for automation, which is used in the examples below, is to duplicate several layers to a newly created workspace.
- https://docs.geoserver.org/latest/en/api/#1.0.0/coveragestores.yaml
- https://docs.geoserver.org/latest/en/api/#1.0.0/layers.yaml
- Bridge automation
Adapting a workspace example
Reference:
To start we will duplicate a workspace definition, which is defined by two REST resources workspace (for service definition) and namespace (for xml documents).
-
To retrieve a workspace test definition in json:
curl -v -u admin:geoserver -XGET -H "Content-type: text/json" \ http://localhost:8080/geoserver/rest/workspaces/test \ -o workspace.json{ "workspace": { "name": "test", "isolated": false, "dateCreated": "2022-05-27 22:27:15.235 UTC", "dataStores": "http://localhost:8080/geoserver/rest/workspaces/test/datastores.json", "coverageStores": "http://localhost:8080/geoserver/rest/workspaces/test/coveragestores.json", "wmsStores": "http://localhost:8080/geoserver/rest/workspaces/test/wmsstores.json", "wmtsStores": "http://localhost:8080/geoserver/rest/workspaces/test/wmtsstores.json" } }Edit
workspace.jsonas template forexampleworkspace: -
To retrieve a namespace test definition in json:
curl -v -u admin:geoserver -XGET -H "Content-type: text/json" \ http://localhost:8080/geoserver/rest/namespaces/test \ -o namespace.jsonEdit
namespace.jsonas a template forexamplenamespace: -
Create
exampleworkspace:curl -v -u admin:geoserver -XPOST -T workspace.json -H "Content-type: application/json" \ http://localhost:8080/geoserver/rest/workspacesThe
HTTP 201 Createdindicates the workspace was created successfully. Indeed the workspace has been created and a placeholder namespace has already been generated.With this in mind we can edit the already created
examplenamespace usingPUT:curl -v -u admin:geoserver -XPUT -T namespace.json -H "Content-type: application/json" \ http://localhost:8080/geoserver/rest/namespaces/exampleHTTP status code
100 Continueis returned indicating we should continue.
Uploaded resources including data
Managing data files is a challenge on a remote server:
- GeoCat Live includes a WebDAV folder mapped to the
GEOSERVER_DATA_DIR/datalocation, this location can be managed using cURL and http commands, or mounted as a network drive for data management. - The GeoServer
rest/resourcesendpoint is intended for managing config files, icons and fonts - but can also be used to manage.
Reference:
Instructions:
-
Using sample GeoTIFF from https://www.terracolor.net/samples/ :
-
Upload using cURL:
curl -w "\n%{http_code}\n" -u admin:geoserver -XPUT -T NextGen_Perth_AU_15m_Geo.prj \ http://localhost:8080/geoserver/rest/resource/data/terracolor/NextGen_Perth_AU_15m_Geo.prj curl -w "%{http_code}\n" -u admin:geoserver -XPUT -T NextGen_Perth_AU_15m_Geo.tfw \ http://localhost:8080/geoserver/rest/resource/data/terracolor/NextGen_Perth_AU_15m_Geo.tfw curl -w "%{http_code}\n" -u admin:geoserver -XPUT -T NextGen_Perth_AU_15m_Geo.tif \ http://localhost:8080/geoserver/rest/resource/data/terracolor/NextGen_Perth_AU_15m_Geo.tif curl -w "%{http_code}\n" -u admin:geoserver -XPUT -T NextGen_Perth_AU_15m_Geo.txt \ http://localhost:8080/geoserver/rest/resource/data/terracolor/NextGen_Perth_AU_15m_Geo.txt -
The
resourcesendpoint is very relaxed, and will create any folder needed to upload the resource into the requested location. In the example above the folderdata/terracoloris created as needed.
Adapting a coverage store example
Reference:
- https://docs.geoserver.org/latest/en/api/#1.0.0/resource.yaml
Instructions:
-
To retrieve a coverage store test Terracolor definition in json:
curl -v -u admin:geoserver -XGET -H "Content-type: text/json" \ http://localhost:8080/geoserver/rest/workspaces/test/coveragestores/Terracolor.json \ -o coveragestore.jsonThis approach of using an existing coverage is an helpful way to determine all the connection parameters directly from the application.
The GeoServer application is modular with additional coverage stores and data stores added over time. This approach can be used to determine the REST API for new data formats that may not be reflected in the REST API reference.
-
Edit the resulting
coveragestore.jsonwith :[file:`file:data/terracolor/NextGen_Perth_AU_15m_Geo.tif]{.title-ref} connection parameter.coveragestore.json{ "coverageStore": { "name": "Perth", "description": "Perth, Australia 15-meter resolution", "type": "WorldImage", "enabled": true, "workspace": { "name": "example" }, "_default": false, "url": "file:data/terracolor/NextGen_Perth_AU_15m_Geo.tif" } }And create new
Perthcoverage store:
Adapting a coverage example
A coverage is defined by two REST resources coverages (for data definition) and layer (for publication).
Reference:
- https://docs.geoserver.org/latest/en/api/#1.0.0/coverages.yaml
- https://docs.geoserver.org/latest/en/api/#1.0.0/layers.yaml
Instructions:
-
To retrieve a coverage test:sydney definition in json from the workspace:
curl -v -u admin:geoserver -XGET -H "Content-type: text/json" \ http://localhost:8080/geoserver/rest/workspaces/test/coverages/sydney.json \ -o coverage.jsonOr retrieve from test: Terracolor coverage store:
-
A lot of the details will be filled in when the instance is created, edit
coverage.jsondown to the bare essentials. In this case GeoServer was not able to match the providedprjfile so we have included thesrsinformation.`
And create new perth coverage:
curl -v -u admin:geoserver -XPOST -T coverage.json -H "Content-type: application/json" \
http://localhost:8080/geoserver/rest/workspaces/example/coveragestores/Perth/coverages
-
To retrieve
layerpublishing details for the new layer: -
Compare with a working example to learn the json representation of different configuration options.
- Retrieve the existing
layer.jsondefinition - Make the appropriate changes
- Use PUT to update the layer definition in place
If you are more comfortable with an xml toolchain text/xml is available.
- Retrieve the existing