Workspaces¶
The REST API allows you to create and manage workspaces in GeoServer.
Note:
You can find the official example at https://docs.geoserver.org/2.25.x/en/user/rest/workspaces.html
from geoserver import GeoServer
GeoServer Connection¶
Connect to the running GeoServer instance and create a workspace and a store.
# Setup the geoserver instance
geoserver = GeoServer(
service_url="http://localhost:8080/geoserver",
username="admin",
password="geoserver",
)
# If the workspace does not exist, create it
if geoserver.workspace_exists("demo"):
geoserver.delete_workspace("demo", recurse=True)
Adding a new workspace¶
Creates a new workspace named "demo" using the create_workspace method.
# Using JSON format
body = {
"workspace": {
"name": "demo",
}
}
# Using XML format
body = """
<workspace>
<name>demo</name>
</workspace>
"""
# Create the workspace
geoserver.create_workspace(body=body)
'Created'
Because it can be verbose to create the workspace by providing the full XML, we can use the create_workspace_by_name method to create a workspace by providing only the name.
# geoserver.create_workspace_by_name(name="demo")
Retrieving workspace¶
Retrieve information about a specific workspace. You can specify the response format as JSON or XML by setting the format parameter.
geoserver.get_workspace(name="demo")
{'workspace': {'name': 'demo',
'isolated': False,
'dateCreated': '2024-06-12 22:37:19.313 UTC',
'dataStores': 'http://localhost:8080/geoserver/rest/workspaces/demo/datastores.json',
'coverageStores': 'http://localhost:8080/geoserver/rest/workspaces/demo/coveragestores.json',
'wmsStores': 'http://localhost:8080/geoserver/rest/workspaces/demo/wmsstores.json',
'wmtsStores': 'http://localhost:8080/geoserver/rest/workspaces/demo/wmtsstores.json'}}
data = geoserver.get_workspace(name="demo", format="xml")
print(data)
<workspace>
<name>demo</name>
<isolated>false</isolated>
<dateCreated>2024-06-12 22:37:19.313 UTC</dateCreated>
<dataStores>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/workspaces/demo/datastores.xml" type="application/xml"/>
</dataStores>
<coverageStores>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/workspaces/demo/coveragestores.xml" type="application/xml"/>
</coverageStores>
<wmsStores>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/workspaces/demo/wmsstores.xml" type="application/xml"/>
</wmsStores>
<wmtsStores>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/workspaces/demo/wmtsstores.xml" type="application/xml"/>
</wmtsStores>
</workspace>
This shows that the workspace can contain "dataStores" (for vector data), "coverageStores" (for raster data), and "wmsStores" (for cascaded WMS servers).