[GIS] Accessing Rest Api of Geoserver using python

geoserverpython

is there anyway to access Rest Api of Geoserver using python other than gsconfig.
since it doesn't support for me.i'm using python3.4
Like creating workspace,adding layers programmatically to Geoserver

Best Answer

As already mentioned slightly by Slslam, you can use Python's requests module to send the same POST requests to GeoServer's REST API as you could do from the command line with cUrl.

This is an example that I recently used to activate a layer not yet known to GeoServer but already available in the connected DataStore of the given workspace (a PostGIS database in my case), including activation of the TIME dimension.

In fact, the only thing that is different to the examples given on the cUrl page above is how to fire the POST request. Apart from that, the XML you send to the REST API can contain whatever you want as long as it is valid (otherwise, you'll get an error message back from the server in your requests variable).

To use the module, either install it via pip install requests or, preferably on Windows systems, by downloading this binary and installing it via pip install path\to\the\file.whl.

import requests

# define your parameters
url = 'http://localhost:8080/geoserver/rest/workspaces/my_workspace/datastores/my_datastore/featuretypes'
headers = {'Content-Type': 'text/xml'}
auth = ('admin', 'geoserver')

# define your XML string that you want to send to the server
data = """
    <featureType>
    <name>my_layer_name</name>
    <srs>EPSG:4326</srs>
    <enabled>true</enabled>
    <metadata>
    <entry key="time">
    <dimensionInfo>
    <enabled>true</enabled>
    <attribute>datetime</attribute>
    <presentation>CONTINUOUS_INTERVAL</presentation>
    <units>ISO8601</units>
    <defaultValue><strategy>MAXIMUM</strategy></defaultValue>
    </dimensionInfo>
    </entry>
    </metadata>
    <store class="dataStore">
        <name>my_datastore</name>
    </store>
</featureType>
"""

# fire the request
r = requests.post(url, headers=headers, auth=auth, data=data)

# inspect the response
print(r.text)