Hi Benjamin.
It is very much possible. We are loading about 800 layers into geoserver
using the REST api directly here.
We have added complexity, however, since our source "template" is a set of
QGIS projects, so we transfer layer names, styles, filters...

In your case, you could use a small python script.
1. Connect to your database using psycopg2 and get the table names (search
for "How do I get tables in postgres using psycopg2?") OR load a text file
with the table names.
2. Log in to geoserver (see attached snippets)
3. Make the calls to the REST api to configure the layers using the
"requests" library.

I have attached python code snippets that I wish I had access to a year
ago. The snippets do not constitute a whole functional script, but will
give a good idea of where to start.

Daniel Miranda
Forensic Expert
Geomatics Area, Brazilian Federal Police


Em seg, 19 de nov de 2018 às 17:35, Benjamin Krepp <[email protected]>
escreveu:

> Hi all,
>
>
>
> I’d like to ask the community if it is possible to automate the following,
> and if so how it is best done:
>
>
>
> Given:   A PostGIS database containing some number of spatial data tables
> not yet available as layers in  GeoServer
>
> To do:   Add a layer for each of these tables to GeoServer
>
>
>
> Looking through the documentation on the GeoServer REST API, it looks like
> this should be possible. But so far, I’ve not been successful in
> “connecting all the dots” and figuring out exactly what the necessary
> sequence of calls to the REST API should look like.
>
>
>
> When adding a small number of new layers to GeoServer, using the web
> interface to do this isn’t a problem. However, this approach becomes
> tedious and error-prone when there’s a need to add more than a handful of
> layers.
>
>
>
> I’d appreciate any advice/guidance the community has on automating this
> process.
>
> Thanks for any help you can offer with this.
>
>
>
> All best,
>
> Ben
>
>
>
> *Benjamin Krepp*  |  Chief GIS Programmer/Analyst
>
> CENTRAL TRANSPORTATION PLANNING STAFF
>
> 857.702.3670  | *[email protected] <[email protected]>*
>
> www.ctps.org/bostonmpo
>
>
>
> [image: email_logos_2015]
>
>
>
>
> _______________________________________________
> Geoserver-users mailing list
>
> Please make sure you read the following two resources before posting to
> this list:
> - Earning your support instead of buying it, but Ian Turton:
> http://www.ianturton.com/talks/foss4g.html#/
> - The GeoServer user list posting guidelines:
> http://geoserver.org/comm/userlist-guidelines.html
>
> If you want to request a feature or an improvement, also see this:
> https://github.com/geoserver/geoserver/wiki/Successfully-requesting-and-integrating-new-features-and-improvements-in-GeoServer
>
>
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geoserver-users
>
import json
import requests

s = requests.session()

# Skip ssl certificate validation and proxy configuration on the test server:
s.trust_env = False
s.verify = False

url = "https://www.blablabla.com/geoserver/";
req = 'web/'
res = s.get(url + req)

# Logging in to geoserver. The session "s" holds the authentication token for 
the next requests.
req = "j_spring_security_check"                         # Pedido de acesso ao 
login
res = s.post(url + req, data={'username': 'admin', 'password': 'XXXXXXXXXX'})


# Example workspace creation
req = "rest/workspaces.json"
header = {'content-type': 'application/json'}       # Header que identifica o 
formato do payload, passado no request
res = s.post(url + req, data=json.dumps({'workspace': {'name': 
workspacename}}), headers=header)

# QGIS boilerplate
iface.newProject()
project = QgsProject.instance()
root = project.layerTreeRoot()
# WARNING -- this part of the code is not included here. you need to write your 
own.
# search recursively beginning in root.children().
# the layer variable is a node from the root tree where isinstance(layer, 
QgsLayerTreeLayer) is True.
layer  # is a qgis layer


# Example datastore creation
source = parse_source_Qgis(layer.layer().source())  # parses qgis layer to our 
format
dataStore = {
    'dataStore': {
        'name': source['dbname'] + '.' + source['schema'],
        'connectionParameters': {
            'entry': [
                {'@key': 'host', '$': source['host']},
                {'@key': 'port', '$': source['port']},
                {'@key': 'dbtype', '$': 'postgis'},
                {'@key': 'database', '$': source['dbname']},
                {'@key': 'schema', '$': source['schema']},
                {'@key': 'user', '$': source['user']},
                {'@key': 'passwd', '$': source['password']}
            ]
        }
    }
}
req = 'datastores.json'
res = s.post(url + req, data=json.dumps(dataStore), headers=header)


# Example featureType creation (adding individual layers from the datastore)
featureType = {
    'featureType': {
        'name': source['tbname'] + '.' + clean(layer.name()),
        'nativeName': source['tbname'],
        'title': layer.name(),
        'cqlFilter': source['sql']
    }
}
req = "datastores/" + dataStore['dataStore']['name'] + "/featuretypes.json"
res = s.post(url + req, data=json.dumps(featureType), headers=header)

# Updating the featureType to add the style (you need an actual sld file in 
your filesystem for that)
assert layer.layer().saveSldStyle(pasta_destino + clean(layer.name()) + 
'.sld')[1]  # generate the sld file
styleData = {
    'style': {
        'name': clean(layer.name()),
        'filename': clean(layer.name()) + '.sld',
        'default': 'True'
    }
}


header = {'content-type': 'application/vnd.ogc.sld+xml'}
req = 'styles/' + styleData['style']['name'] + '.sld'
res = s.put(url + req, headers=header, data=file, params={'raw': 'True'})  # 
load the style into geoserver

header = {'content-type': 'application/json'}
req = featureType['featureType']['name'] + '.json'
res = s.put(url.replace('workspaces/' + proj + '/', 'layers/' + req), 
headers=header,
            data=json.dumps({'layer': {'defaultStyle': 
styleData['style']['name']}}))  # assign style to layer
_______________________________________________
Geoserver-users mailing list

Please make sure you read the following two resources before posting to this 
list:
- Earning your support instead of buying it, but Ian Turton: 
http://www.ianturton.com/talks/foss4g.html#/
- The GeoServer user list posting guidelines: 
http://geoserver.org/comm/userlist-guidelines.html

If you want to request a feature or an improvement, also see this: 
https://github.com/geoserver/geoserver/wiki/Successfully-requesting-and-integrating-new-features-and-improvements-in-GeoServer


[email protected]
https://lists.sourceforge.net/lists/listinfo/geoserver-users

Reply via email to