4

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?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Stephen Lead
  • 21,119
  • 17
  • 113
  • 240

2 Answers2

6

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.

GIS Grade 1
  • 81
  • 1
  • 2
3

Here's a workaround using Selenium, which opens a web browser and programatically clicks the 4 checkboxes:

from selenium import webdriver

serverUrl = "http://<my_geoserver>:8080/geoserver/web/"
workspaceName = "<workspaceName>"
login = "admin"
pwd = "geoserver"

# Open a browser and log in to the GeoServer admin page
browser = webdriver.Chrome()
browser.get(serverUrl)
browser.find_element_by_id("username").send_keys(login)
browser.find_element_by_id("password").send_keys(pwd)
browser.find_element_by_class_name("positive").click()

# Navigate to the new Workspace, check the Services checkboxes, and Save
browser.get(serverUrl + "bookmarkable/org.geoserver.web.data.workspace.WorkspaceEditPage?name=" + workspaceName)
for idx in range (0,4):
  chkBox =  browser.find_element_by_name("services:services:" + str(idx) + ":enabled")
  if not (chkBox.is_selected()):
    chkBox.click()
browser.find_element_by_id("id9").click()

It's a little hacky, and may require editing if the GeoServer UI elements change (eg login = .positive, save = #id9) but is good enough to allow me to automate this process.

Stephen Lead
  • 21,119
  • 17
  • 113
  • 240