7

I created "by hand" (using GeoServer HTML configuration interface) a layer based on the following SQL view:

SELECT p1.suid, p1.id, p1.geometry, p1.classname, p2.date, COALESCE(p2.percentage, 0) - COALESCE(p1.percentage, 0) AS percentage
FROM get_300km_percentages('%classname%', '%enddate%'::date, '%prevdate%'::date) p1
LEFT OUTER JOIN get_300km_percentages('%classname%', '%startdate%'::date, '%enddate%'::date) p2
ON p1.suid = p2.suid
ORDER BY 
    percentage %order%
LIMIT 
    %limit%

In the picture, we can see that all parameters, including %order% and %limit%" are ok. They were correctly guessed by the API.

enter image description here

How can I create a layer like that using geo.Geoserver or another Python-based GeoServer config library?

ThomasG77
  • 30,725
  • 1
  • 53
  • 93
Mauro Assis
  • 351
  • 2
  • 12

1 Answers1

4

You could do it without a dedicated GeoServer library, only using GeoServer REST in combination with requests library and since you already said you created the layer "by hand", then this approach should work for you.

1. Find the JSON or XML definition of your layer (previously created in GeoServer UI) as instructed in the top answer here:

Creating Layer in GeoServer using REST

The url with layer definition in json may look like this on local GeoServer:

http://localhost:8080/geoserver/rest/workspaces/{workspace_name}/datastores/{datastore_name}/featuretypes/{layer_name}.json

What you get from this is what you should post as a body of the request later in order to create the layer via REST.

2. Make the proper request

Python example:

    import requests
session = requests.Session()
session.auth = ('user', 'pass') #admin - geoserver?
url = '<geoserver_url>/rest/workspaces/<workspace>/datastores/<store>/featuretypes/'
#example 
# 'http://localhost:8080/geoserver/rest/workspaces/leo/datastores/localpai/featuretypes/'

test = session.get(url=url)
#check if the url works - you should get a list of featuretypes for this workspace and datastore
# print(test.text)


headers = {'Content-Type': 'application/json'}
#JSON definition of the layer obtained in step 1.
data = 'JSON_OBTAINED_FROM_PREVIOUS_STEP_AS_STRING'
resp = session.post(url=url, headers=headers, data=data)
print(resp.text)

Leon Powałka
  • 1,653
  • 5
  • 18