[GIS] Creating Workspace, Data Store and Layer via GeoServer REST API

geoservergeoserver-rest-api

I'm trying to automate the process of setting up a Workspace, PostGIS Data Store and Layer in GeoServer using the GeoServer REST API.

Using the excellent answer in Create a Layer in GeoServer using REST I'm able to do this, with one sticking point. When I create the workspace using this code…

import requests, json
headers = {'Content-Type': 'application/json'}
auth = ('admin', 'geoserver')
url = "http://<my_geoserver>:8080/geoserver/rest/workspaces"
data = {"workspace": {"name": "blah"}}
r = requests.post(url, headers=headers, auth=auth, data=json.dumps(data))

… the Services are all unchecked on the newly created Workspace:

enter image description here

According to How to set up a Workspace setting in Geoserver using Curl this is a missing feature in the GeoServer REST API.

Given that the Services are unchecked, I'm unable to publish a layer via the GeoServer REST API:

url = 'http://<my_geoserver>:8080/geoserver/rest/workspaces/<workspaceName>/datastores/<dataStoreName>/featuretypes/'
data = {"featureType": {"name": "<layerName>","srs": "EPSG:4326","enabled": "true","store": {"@class": "dataStore","name": "<workspaceName>:<dataStoreName>"}}}
r = requests.post(url, headers=headers, auth=auth, data=json.dumps(data))

as the API responds with :Schema 'http://<workspaceName>:<layerName>' does not exist.

If I manually select the 4 Services checkboxes on the newly created Workspace, then re-run the code to create a layer, the Layer is created without error.

Is it possible to completely automate the process of creating a Workspace and Datastore, then creating Layers within that Datastore and Workspace, via the GeoServer REST API?

Best Answer

I use node/request;

to add a workspace

curl -u admin:geoserver -v -XPOST -H Content-Type:application/xml -d @test.xml http://192.168.1.254:8083/geoserver/rest/workspaces

curl -u admin:geoserver -v -XPOST -H Content-Type:application/json -d @test.json http://192.168.1.254:8083/geoserver/rest/workspaces

curl -u admin:geoserver -v -XPOST -H Content-type:text/xml -d "myworkspace1" http://192.168.1.254:8083/geoserver/rest/workspaces

curl -u admin:geoserver -v -XPOST -H Content-type:text/json -d "{workspace:{name:'string'}}" http://192.168.1.254:8083/geoserver/rest/workspaces

use ndoejs do this:

//create a workspace
request.post('http://192.168.1.254:8083/geoserver/rest/workspaces',{
headers:{
    'Content-Type':"application/json"
},
body:JSON.stringify({
    "workspace":{
        "name":"test"
    }
})
},function(error,response,body){
if(error){
    console.log(error)
};
console.log(response.statusCode);//201 created ok or 401 has alreay one;
console.log(body);
}).auth('admin','geoserver');

to add a datastore:

request.post('http://192.168.1.254:8083/geoserver/rest/workspaces/test/datastores',{
headers:{
    'Content-Type':"application/json"
},
body:JSON.stringify({
    "dataStore":{
        "name":"buildings",
        "connectionParameters": {
            "entry": [
              {"@key":"host","$":"192.168.1.254"},
              {"@key":"port","$":"5432"},
              {"@key":"database","$":"$$$"},
              {"@key":"user","$":"$$$$"},
              {"@key":"passwd","$":"gss7"},
              {"@key":"dbtype","$":"postgis"}
            ]
        }
    }
})
},function(error,response,body){
if(error){
            console.log(error)
        };
        console.log(response.statusCode);//201 created ok or 404 has alreay 
  deleted;
        console.log(body);
 }).auth('admin','geoserver');

add a layer is somehow confused ,it said

To create a new layer, instead POST to one of /workspaces/{workspaceName}/coveragestores/{coveragestoreName}/coverages, /workspaces/{workspaceName}/datastores/{datastoreName}/featuretypes, /workspaces/{workspaceName}/wmsstores/{wmsstoreName}/wmslayers, or /workspaces/{workspaceName}/wmtsstores/{wmststoreName}/wmtslayers

I'll complete this after I success.