Create a Geoserver layer with data from a Postgis SQL view with Python/gsconfig

1.4k views Asked by At

I'm trying to get a Python program, using gsconfig api, to create dynamically a Geoserver layer with data from a native SQL (like you do on the Geoserver web interface).

I can create the workspace and the datastore, but I can't figure out how to create a layer with a sql view of the Postgis database (example: select * from table where _filter_clause_).

This is the current Python code that I have.

from geoserver.catalog import Catalog

if __name__ == '__main__':

    cat = Catalog("http://localhost:8080/geoserver/rest", username = "admin", password = "geoserver")

    ws = cat.get_workspace ("wsProva")
    if ws is None:
        ws = cat.create_workspace('wsProva','wsProva')

    ds = cat.get_store ("dsProva", "wsProva")
    if ds is None:
        ds = cat.create_datastore('dsProva','wsProva')

    ds.connection_parameters.update (host='localhost', port='5432', database='dbtest', user='userdb', passwd='pwddb', dbtype='postgis', schema='postgis')
    cat.save(ds)

I've googled a lot about this issue, but I haven't found anything useful for me. Thanks a lot for your replies.

1

There are 1 answers

2
Tek Kshetri On

2020 update,

You can simply use geoserver-rest library (pip install geoserver-rest). The example case is below,

#import and initialize library
from geo.Geoserver import Geoserver
geo = Geoserver('http://localhost:8080/geoserver', username='admin', password='geoserver')


# create feature store and publish layer
geo.create_featurestore(store_name='geo_data', workspace='demo', db='postgres', host='localhost', pg_user='postgres', pg_password='admin')
geo.publish_featurestore(workspace='demo', store_name='geo_data', pg_table='geodata_table_name')