Saturday, 7 April 2018

Creating Workspace, Data Store and Layer via GeoServer 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://: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://:8080/geoserver/rest/workspaces//datastores//featuretypes/'
data = {"featureType": {"name": "","srs": "EPSG:4326","enabled": "true","store": {"@class": "dataStore","name": ":"}}}

r = requests.post(url, headers=headers, auth=auth, data=json.dumps(data))

as the API responds with :Schema 'http://:' 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?



Answer



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


from selenium import webdriver

serverUrl = "http://:8080/geoserver/web/"

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.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...